Ich habe gerade eine erweiterbare Recycler View mit 2 verschiedenen Child View Layout erstellt Ich habe viel gesucht, wie ich ein Projekt so etwas wie ich finde Ich habe versucht, diese Schritte zu folgen, aber es tut nicht ‚t arbeitet, wie ich 1 Recycler Ansicht mit einer parentlist Artikel habe aber mit zwei Differenz Kind Artikeln Layout hier ist meine Haupt-XML-Datei, die die Recycler Ansicht enthältSo erstellen Sie eine erweiterbare RecyclerView mit verschiedenen Childs Layouts
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#dedede"
tools:context="abtech.waiteriano.com.waitrer.MainPaymentActivity">
<RelativeLayout
android:id="@+id/relativeLayout2"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentStart="true"
android:layout_alignParentTop="true"
android:layout_marginTop="19dp"
android:background="#ffffff">
<EditText
android:id="@+id/checkNoET"
android:layout_width="140dp"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_toEndOf="@+id/imageView7"
android:editable="false"
android:ems="10"
android:gravity="right"
android:text="505050505050"
android:textSize="12dp">
<requestFocus />
</EditText>
<TextView
android:id="@+id/checkNo"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="@+id/checkNoET"
android:layout_alignBottom="@+id/checkNoET"
android:layout_marginStart="10dp"
android:layout_toEndOf="@+id/imageView7"
android:text="Check#"
android:textColor="@color/colorPrimary"
android:textSize="12dp"
android:textStyle="bold" />
<ImageView
android:id="@+id/imageView7"
android:layout_width="25dp"
android:layout_height="wrap_content"
android:layout_alignBottom="@+id/checkNoET"
android:layout_alignParentStart="true"
android:layout_alignTop="@+id/checkNoET"
app:srcCompat="@drawable/check" />
<EditText
android:id="@+id/noOfCoversEt"
android:layout_width="130dp"
android:layout_height="wrap_content"
android:layout_alignBaseline="@+id/checkNoET"
android:layout_alignBottom="@+id/checkNoET"
android:layout_alignParentEnd="true"
android:editable="false"
android:ems="10"
android:gravity="end"
android:text="5"
android:textSize="12dp">
<requestFocus />
</EditText>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="@+id/noOfCoversEt"
android:layout_alignBottom="@+id/noOfCoversEt"
android:layout_alignStart="@+id/noOfCoversEt"
android:layout_marginStart="13dp"
android:text="Coves"
android:textColor="@color/colorPrimary"
android:textSize="12dp"
android:textStyle="bold" />
<ImageView
android:id="@+id/imageView8"
android:layout_width="25dp"
android:layout_height="wrap_content"
android:layout_alignBottom="@+id/noOfCoversEt"
android:layout_toStartOf="@+id/noOfCoversEt"
app:srcCompat="@drawable/covers" />
<EditText
android:id="@+id/tableNo"
android:layout_width="130dp"
android:layout_height="wrap_content"
android:layout_below="@+id/checkNoET"
android:layout_toEndOf="@+id/imageView7"
android:editable="false"
android:ems="10"
android:gravity="end"
android:text="25"
android:textSize="12dp">
<requestFocus />
</EditText>
<TextView
android:id="@+id/textView13"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="@+id/tableNo"
android:layout_alignBottom="@+id/tableNo"
android:layout_alignStart="@+id/checkNo"
android:text="Table"
android:textColor="@color/colorPrimary"
android:textSize="12dp"
android:textStyle="bold" />
<ImageView
android:id="@+id/imageView9"
android:layout_width="25dp"
android:layout_height="wrap_content"
android:layout_alignParentStart="true"
android:layout_below="@+id/checkNoET"
app:srcCompat="@drawable/tablepay" />
<EditText
android:id="@+id/dateTimeEt"
android:layout_width="130dp"
android:layout_height="wrap_content"
android:layout_alignBaseline="@+id/tableNo"
android:layout_alignBottom="@+id/tableNo"
android:layout_alignParentEnd="true"
android:editable="false"
android:text="15/10/2017 03:15PM"
android:textColor="#000000"
android:textSize="10dp"
android:textStyle="bold" />
<ImageView
android:id="@+id/imageView10"
android:layout_width="25dp"
android:layout_height="wrap_content"
android:layout_alignBottom="@+id/dateTimeEt"
android:layout_toStartOf="@+id/dateTimeEt"
app:srcCompat="@drawable/calendar" />
</RelativeLayout>
<RelativeLayout
android:id="@+id/relativeLayout3"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentStart="true"
android:layout_below="@+id/relativeLayout2"
android:layout_marginTop="14dp"
android:background="#ffffff">
<android.support.v7.widget.RecyclerView
android:id="@+id/parentList"
android:layout_width="match_parent"
android:scaleType="fitXY"
android:adjustViewBounds="true"
android:layout_height="wrap_content" />
</RelativeLayout>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="220dp"
android:layout_below="@+id/relativeLayout3"
android:layout_marginTop="10dp"
android:background="#ffffff">
<GridView
android:id="@+id/paymentsTypeGV"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:columnWidth="80dp"
android:gravity="center"
android:horizontalSpacing="5dp"
android:numColumns="4"
android:stretchMode="columnWidth"
android:verticalSpacing="5dp"></GridView>
</RelativeLayout>
</RelativeLayout>
und dies ist meine Liste übergeordnetes Element
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:orientation="horizontal"
android:paddingLeft="13dp"
android:paddingStart="13dp"
android:paddingRight="10dp"
android:paddingEnd="10dp"
android:paddingTop="5dp"
android:paddingBottom="5dp"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<de.hdodenhof.circleimageview.CircleImageView
android:id="@+id/itemImage"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/cart"
app:srcCompat="@drawable/cart"
android:layout_alignParentTop="true"
android:layout_alignParentStart="true" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Items:"
android:textStyle="bold"
android:layout_marginStart="61dp"
android:id="@+id/textView9"
android:layout_centerVertical="true"
android:layout_toEndOf="@+id/itemImage" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textStyle="bold"
android:text="20"
android:id="@+id/textView15"
android:layout_centerVertical="true"
android:layout_toEndOf="@+id/textView9" />
<TextView
android:id="@+id/textView11"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textStyle="bold"
android:text="$SubTotal"
android:layout_marginEnd="32dp"
android:layout_centerVertical="true"
android:layout_toStartOf="@+id/btn_expand_toggle" />
<ImageView
android:id="@+id/btn_expand_toggle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="@+id/parentItemRL"
android:layout_alignParentEnd="true"
app:srcCompat="@drawable/circle_plus" />
</RelativeLayout>
und thi s ist das erste Kind item.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="wrap_content">
<RelativeLayout
android:id="@+id/childItemRL"
android:layout_width="fill_parent"
android:layout_height="40dp"
android:layout_alignParentTop="true"
android:layout_alignParentStart="true">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="12"
android:id="@+id/qtyId"
android:textStyle="bold"
android:layout_marginLeft="5dp"
android:layout_centerVertical="true"
android:layout_alignParentStart="true" />
<View
android:background="#b3b3b3"
android:layout_height="30dp"
android:layout_centerVertical="true"
android:id="@+id/line1"
android:layout_width="1dp"
android:layout_toEndOf="@+id/qtyId"
android:layout_marginLeft="10dp"
android:layout_centerHorizontal="true" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/modName"
android:textStyle="bold"
android:text="Item Name"
android:layout_centerVertical="true"
android:background="@color/colorPrimary"
android:textColor="#ffffff"
android:layout_marginBottom="2dp"
android:gravity="center"
android:textSize="8dp"
android:layout_alignBottom="@+id/line1"
android:layout_alignStart="@+id/itemName2" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#000"
android:id="@+id/itemName2"
android:textStyle="bold"
android:text="Item Name"
android:layout_centerVertical="true"
android:layout_above="@+id/modName"
android:layout_toEndOf="@+id/line1"
android:layout_marginStart="11dp" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/itemPrice"
android:text="$50.00"
android:textColor="#000"
android:textStyle="bold"
android:layout_centerVertical="true"
android:layout_alignParentEnd="true"
android:layout_marginEnd="21dp" />
</RelativeLayout>
</RelativeLayout>
und dies ist das zweite Kind item.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:orientation="horizontal"
android:paddingLeft="13dp"
android:paddingStart="13dp"
android:paddingRight="10dp"
android:paddingEnd="10dp"
android:paddingTop="5dp"
android:paddingBottom="5dp"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#000"
android:id="@+id/adjTextView"
android:textStyle="bold"
android:text="Adj"
android:layout_alignBaseline="@+id/tPrice"
android:layout_alignBottom="@+id/tPrice"
android:layout_toStartOf="@+id/tPrice"
android:layout_marginEnd="100dp" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/tPrice"
android:text="$50.00"
android:textColor="#000"
android:textStyle="bold"
android:layout_marginEnd="109dp"
android:layout_centerVertical="true"
android:layout_alignParentEnd="true" />
</RelativeLayout>
und dies ist mein Adapter Class
package abtech.waiteriano.com.waitrer.adapters;
import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
import java.util.ArrayList;
import abtech.waiteriano.com.waitrer.R;
/**
* Created by anandbose on 09/06/15.
*/
public class ExpandableListPaymentAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
public static final int HEADER = 0;
public static final int CHILD_ITEMS = 1;
public static final int CHILD_ADJ = 2;
public static final int MODIFIER_CHILD_ITEMS = 3;
public static final int CHILD_ITEMS_QTY = 4;
public static final int CHILD_ITEMS_PRICE = 5;
public static final int CHILD_ADJ_TOTAL = 6;
private ArrayList<Item> itemsData;
private ArrayList<itemAdj> adjItemsData;
public ExpandableListPaymentAdapter(ArrayList<Item> itemsData, ArrayList<itemAdj> adjItemsData) {
this.itemsData = itemsData;
this.adjItemsData = adjItemsData;
}
@Override
public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int type) {
View view = null;
Context context = parent.getContext();
float dp = context.getResources().getDisplayMetrics().density;
int subItemPaddingLeft = (int) (18 * dp);
int subItemPaddingTopAndBottom = (int) (5 * dp);
switch (type) {
case HEADER:
LayoutInflater inflater = (LayoutInflater) parent.getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
view = inflater.inflate(R.layout.list_item_parent_payment, parent, false);
ListHeaderViewHolder header = new ListHeaderViewHolder(view);
return header;
case CHILD_ITEMS:
LayoutInflater inflater2 = (LayoutInflater) parent.getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
view = inflater2.inflate(R.layout.list_item_child_payment, parent, false);
TextView itemName = (TextView) view.findViewById(R.id.itemName2);
return new RecyclerView.ViewHolder(itemName) {
};
case MODIFIER_CHILD_ITEMS:
LayoutInflater inflater3 = (LayoutInflater) parent.getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
view = inflater3.inflate(R.layout.list_item_child_payment, parent, false);
TextView itemNameModifeir = (TextView) view.findViewById(R.id.modName);
return new RecyclerView.ViewHolder(itemNameModifeir) {
};
case CHILD_ITEMS_QTY:
LayoutInflater inflater5 = (LayoutInflater) parent.getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
view = inflater5.inflate(R.layout.list_item_child_payment, parent, false);
TextView itemsQTY = (TextView) view.findViewById(R.id.qtyId);
return new RecyclerView.ViewHolder(itemsQTY) {
};
case CHILD_ITEMS_PRICE:
LayoutInflater inflater6 = (LayoutInflater) parent.getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
view = inflater6.inflate(R.layout.list_item_child_payment, parent, false);
TextView itemsPrice = (TextView) view.findViewById(R.id.itemPrice);
return new RecyclerView.ViewHolder(itemsPrice) {
};
case CHILD_ADJ:
LayoutInflater inflater4 = (LayoutInflater) parent.getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
view = inflater4.inflate(R.layout.tax_adj_payments_child, parent, false);
TextView adjName = (TextView) view.findViewById(R.id.taxandAdjId);
return new RecyclerView.ViewHolder(adjName) {
};
case CHILD_ADJ_TOTAL:
LayoutInflater inflater7 = (LayoutInflater) parent.getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
view = inflater7.inflate(R.layout.tax_adj_payments_child, parent, false);
TextView adjPrice = (TextView) view.findViewById(R.id.tPrice);
return new RecyclerView.ViewHolder(adjPrice) {
};
}
return null;
}
@Override
public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
final Item item = itemsData.get(position);
final itemAdj itemAdj = adjItemsData.get(position);
switch (item.type) {
case HEADER:
final ListHeaderViewHolder itemContoller1 = (ListHeaderViewHolder) holder;
itemContoller1.refferalItem1 = item;
itemContoller1.header_title.setText(item.itemName);
if (item.invisibleChildren == null) {
itemContoller1.btn_expand_toggle.setImageResource(R.drawable.circle_minus);
} else {
itemContoller1.btn_expand_toggle.setImageResource(R.drawable.circle_plus);
}
itemContoller1.btn_expand_toggle.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (item.invisibleChildren == null) {
item.invisibleChildren = new ArrayList<Item>();
int count = 0;
int pos = itemsData.indexOf(itemContoller1.refferalItem1);
while (itemsData.size() > pos + 1 && itemsData.get(pos + 1).type == CHILD_ITEMS) {
item.invisibleChildren.add(itemsData.remove(pos + 1));
count ++;
}
while (itemsData.size() > pos + 1 && itemsData.get(pos + 1).type == MODIFIER_CHILD_ITEMS) {
item.invisibleChildren.add(itemsData.remove(pos + 1));
count ++;
}
while (itemsData.size() > pos + 1 && itemsData.get(pos + 1).type == CHILD_ITEMS_QTY) {
item.invisibleChildren.add(itemsData.remove(pos + 1));
count ++;
}
while (itemsData.size() > pos + 1 && itemsData.get(pos + 1).type == CHILD_ITEMS_PRICE) {
item.invisibleChildren.add(itemsData.remove(pos + 1));
count ++;
}
notifyItemRangeRemoved(pos + 1, count);
itemContoller1.btn_expand_toggle.setImageResource(R.drawable.circle_plus);
}else {
int pos = itemsData.indexOf(itemContoller1.refferalItem1);
int index = pos + 1;
for (Item i : item.invisibleChildren){
itemsData.add(index, i);
index ++;
}
notifyItemRangeInserted(pos + 1, index - pos - 1);
itemContoller1.btn_expand_toggle.setImageResource(R.drawable.circle_minus);
item.invisibleChildren = null;
}
}
});
case CHILD_ITEMS:
TextView itemTv = (TextView) holder.itemView.findViewById(R.id.itemName2);
itemTv.setText(itemsData.get(position).itemName);
break;
case MODIFIER_CHILD_ITEMS:
TextView itemModTv = (TextView) holder.itemView.findViewById(R.id.modName);
itemModTv.setText(itemsData.get(position).itemModName);
break;
case CHILD_ITEMS_QTY:
TextView itemQtyTv = (TextView) holder.itemView.findViewById(R.id.qtyId);
itemQtyTv.setText((int) itemsData.get(position).itemsQty);
break;
case CHILD_ITEMS_PRICE:
TextView itemPriceTv = (TextView) holder.itemView.findViewById(R.id.itemPrice);
itemPriceTv.setText((int) itemsData.get(position).itemsSubTotal);
break;
}
switch (itemAdj.type) {
}
}
@Override
public int getItemViewType(int position) {
return itemsData.get(position).type;
}
@Override
public int getItemCount() {
return itemsData.size();
}
private static class ListHeaderViewHolder extends RecyclerView.ViewHolder {
public TextView header_title;
public ImageView btn_expand_toggle;
public Item refferalItem1;
public itemAdj refferalItem2;
public ListHeaderViewHolder(View itemView) {
super(itemView);
header_title = (TextView) itemView.findViewById(R.id.textView9);
btn_expand_toggle = (ImageView) itemView.findViewById(R.id.btn_expand_toggle);
}
}
public static class Item {
public int type;
public String itemName;
public String itemModName;
public double itemsQty;
public double itemsSubTotal;
public ArrayList<Item> invisibleChildren;
public Item(int header, String items, double v, double parseDouble) {
}
public Item(int type, String itemName,String itemModName, double itemsQty, double itemsSubTotal) {
this.type = type;
this.itemName = itemName;
this.itemModName = itemModName;
this.itemsQty = itemsQty;
this.itemsSubTotal = itemsSubTotal;
}
}
public static class itemAdj {
public int type;
public String adjName;
public double subTotal;
public itemAdj() {
}
public itemAdj(int type, String adjName, double subTotal) {
this.type = type;
this.adjName = adjName;
this.subTotal = subTotal;
}
}
}
und das ist mein MainActivity
package abtech.waiteriano.com.waitrer;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.RecyclerView;
import android.widget.EditText;
import java.util.ArrayList;
import abtech.waiteriano.com.waitrer.adapters.ExpandableListPaymentAdapter;
public class MainPaymentActivity extends AppCompatActivity {
private RecyclerView recyclerview;
private EditText checkNo, noOfCovers, tableNo, dateTime;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main_payment);
recyclerview = (RecyclerView) findViewById(R.id.parentList);
checkNo = (EditText) findViewById(R.id.checkNoET);
noOfCovers = (EditText) findViewById(R.id.noOfCoversEt);
tableNo = (EditText) findViewById(R.id.tableNo);
dateTime = (EditText) findViewById(R.id.dateTimeEt);
ArrayList<ExpandableListPaymentAdapter.Item> itemsdData = new ArrayList<>();
ArrayList<ExpandableListPaymentAdapter.itemAdj> adjdData = new ArrayList<>();
itemsdData.add(new ExpandableListPaymentAdapter.Item(ExpandableListPaymentAdapter.HEADER,"Items",Double.parseDouble("10"),Double.parseDouble("150")));
itemsdData.add(new ExpandableListPaymentAdapter.Item(ExpandableListPaymentAdapter.CHILD_ITEMS,"Items",Double.parseDouble("10"),Double.parseDouble("150")));
recyclerview.setAdapter(new ExpandableListPaymentAdapter(itemsdData,adjdData));
}
}
Ich laufe es aber ich fo und dieser Fehler
java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.Object java.util.ArrayList.get(int)' on a null object reference
at abtech.waiteriano.com.waitrer.adapters.ExpandableListPaymentAdapter.onBindViewHolder(ExpandableListPaymentAdapter.java:99)
Dies ist die Zeile Fehler
final itemAdj itemAdj = adjItemsData.get(position);
Ich brauche nur irgendeine meinen Fall zu lösen oder mir ein Github Beispiel senden, wie die folgenden Schritte ausführen leid, wenn irgend etwas nicht klar I hoffe das wird
vielen Dank, aber ich habe einen Fehler „java.lang.NullPointerException: Der Versuch, virtuelle Methode‚java.lang.Object java.util.ArrayList.get (int)‘auf ein Null-Objekt Verweis aufrufen“ in Zeile 99 bitte können Sie meine Adapterklasse überprüfen – Nativony
Bitte zeigen Sie die Linie 99 –
Bitte überprüfen Sie meine Bearbeitungen – Nativony