を働いていないビュータイプこれは私の必要なプロトタイプです: 私は
リサイクラー複数のアダプタで見ると
を取得していますどのような結果私は、単一のRecylerViewで複数のビュータイプでこれをしようとしていますが、私は必要な結果を得ていないのです。 getItemViewType()
を使用する場合は、ロジックを書く必要がありません。これは
mrecyclerview = (RecyclerView) findViewById(R.id.recyclerView);
try {
JSONObject jsonObj = new JSONObject(dataStr.getJsonString());
JSONArray jsondata = jsonObj.getJSONArray("rows");
for(int i = 0;i<jsondata.length();i++){
JSONObject rowsdata = jsondata.getJSONObject(i);
Integer sequence_id = rowsdata.getInt("sequence_id");
String sequence_description = rowsdata.getString("sequence_description");
String sequence_image = rowsdata.getString("sequence_image");
String sequence_title = rowsdata.getString("sequence_title");
List<ScreenOneInventory> minventorylist = new ArrayList<ScreenOneInventory>();
ScreenOneRows soRows = new ScreenOneRows();
JSONArray childres = rowsdata.getJSONArray("child_rows");
for(int j =0;j<childres.length();j++){
JSONObject inventorydata = childres.getJSONObject(j);
Log.i("Fragment","I am here in loop j:"+j);
String id = inventorydata.getString("id");
String name = inventorydata.getString("name");
String description = inventorydata.getString("description");
String image = inventorydata.getString("image");
ScreenOneInventory soinventoryObj = new ScreenOneInventory();
soinventoryObj.setId(id);
soinventoryObj.setname(name);
soinventoryObj.setDescription(description);
soinventoryObj.setImage(image);
minventorylist.add(soinventoryObj);
}
soRows.setSequence_id(sequence_id);
soRows.setSequence_Description(sequence_description);
soRows.setSequence_image(sequence_image);
soRows.setSop_title(sequence_title);
soRows.setInventoryList(minventorylist);
RowsData.add(soRows);
}
} catch (JSONException e) {
e.printStackTrace();
}
mrecyclerview.setLayoutManager(new LinearLayoutManager(getActivity()));
ScreenOneAdapter adapter = new ScreenOneAdapter(getActivity(),RowsData,mrecyclerview);
mrecyclerview.setAdapter(adapter);
}
これは私がRecylerView
{
"rows": [{
"sequence_id":1,
"sequence_description":"animal description goes here",
"sequence_image":"thumbnail.png",
"sequence_title": "Animal",
"child_rows":[
{
"id": "1",
"name":"Lion",
"description" : "lion description goes here",
"image": "Lion.png"
},
{
"id": "2",
"name":"Tiger",
"description" : "Tiger description goes here",
"image": "Tiger.png"
},
{
"id": "3",
"name":"Elephant",
"description" : "Elephant description goes here",
"image": "Elephant.png"
}
]
},
{
"sequence_id":2,
"sequence_description":"animal description goes here",
"sequence_image":"thumbnail.png",
"sequence_title": "Birds",
"child_rows":[
{
"id": "1",
"name":"Parrot",
"description" : "Parrot description goes here",
"image": "parrot.png"
},
{
"id": "2",
"name":"Pigeon",
"description" : "Pigeon description goes here",
"image": "Pigeon.png"
},
{
"id": "3",
"name":"Crow",
"description" : "Crow description goes here",
"image": "crow.png"
}
]
}
]
}
Activity.javaに実装したいJSONデータである
を私を助けてください。
public class ScreenOneAdapter extends RecyclerView.Adapter<ScreenOneAdapter.BaseViewHolder> {
private LayoutInflater mInflater;
private List<ScreenOneRows> mRowsList;
public static final int PARENT_VIEW = 0;
public static final int CHILD_VIEW = 1;
private RecyclerView recyclerView;
private Context mcontext;
public ScreenOneAdapter(Context context, List<ScreenOneRows> mRowsList, RecyclerView mrecyclerview) {
this.mInflater = LayoutInflater.from(context);
this.mRowsList = mRowsList;
this.recyclerView = mrecyclerview;
this.mcontext = context;
}
@Override
public BaseViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view;
view = mInflater.inflate(R.layout.list_item_rows,parent,false);
RowsListHolder rowHolder = new RowsListHolder(view);
return rowHolder;
}
@Override
public void onBindViewHolder(BaseViewHolder holder, int position) {
ScreenOneRows current = mRowsList.get(position);
RowsListHolder rHolder = (RowsListHolder) holder;
rHolder.setData(current);
List mInventoryList = current.getInventoryList();
InventoryListAdapter iadapter = new InventoryListAdapter(mcontext,mInventoryList);
recyclerView.setAdapter(iadapter);
}
@Override
public int getItemCount() {
return (null != mRowsList ? mRowsList.size() : 0);
}
public class BaseViewHolder extends RecyclerView.ViewHolder{
public BaseViewHolder(View itemView) {
super(itemView);
}
}
public class RowsListHolder extends BaseViewHolder{
private ImageView icon;
private TextView title;
private TextView description;
public RowsListHolder(View itemView) {
super(itemView);
icon = (ImageView) itemView.findViewById(R.id.img_icon);
title = (TextView) itemView.findViewById(R.id.tvTitle);
description = (TextView) itemView.findViewById(R.id.tvDescription);
}
public void setData(ScreenOneRows current){
this.title.setText(current.getSequence_title());
this.description.setText(current.getSequence_description());
}
}
}
これは、あなたが行と子の場合、2つの異なるアイテムのレイアウトを使用できる2つの異なるアダプタを作成する必要はありません私の子行アダプタInventoryListAdapter.java
public class InventoryListAdapter extends RecyclerView.Adapter<InventoryListAdapter.InventoryListHolder> {
private List<ScreenOneInventory> mInventoryList;
private LayoutInflater mInflater;
public InventoryListAdapter(Context context, List<ScreenOneInventory> mInventoryList) {
this.mInventoryList = mInventoryList;
this.mInflater = LayoutInflater.from(context);
}
@Override
public InventoryListAdapter.InventoryListHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View rootView;
rootView = mInflater.inflate(R.layout.list_item_inventory,parent,false);
InventoryListHolder invHolder = new InventoryListHolder(rootView);
return invHolder;
}
@Override
public void onBindViewHolder(InventoryListAdapter.InventoryListHolder holder, int position) {
ScreenOneInventory currentInv = mInventoryList.get(position);
InventoryListHolder invenHolder = (InventoryListHolder) holder;
invenHolder.setData(currentInv);
}
@Override
public int getItemCount() {
return (null != mInventoryList ? mInventoryList.size() : 0);
}
public class InventoryListHolder extends RecyclerView.ViewHolder {
private ImageView imgicon;
private TextView title;
private TextView description;
public InventoryListHolder(View itemView) {
super(itemView);
imgicon = (ImageView) itemView.findViewById(R.id.img_inv_icon);
title = (TextView) itemView.findViewById(R.id.titletxt);
description = (TextView) itemView.findViewById(R.id.descriptiontxt);
}
public void setData(ScreenOneInventory data) {
this.title.setText(data.getName());
this.status.setText(data.getDescription());
}
}
}
です。それを受け入れられた回答としてマークしてください。そうでない場合は、コメントセクションで懸念事項をコメントすることができます。私はそれを解決しようとします。 – MKY
ありがとう@MKY :) – Siraj