Description: In this post I'm gonna show you how we can have multiple row layout in Android's Material Design RecyclerView using getItemViewType.
GitHub Project HERE
So lets get started :-)
Step 1: Create Recycler Adapter by overriding getItemViewType method and giving the type for different layout as shown below.
compile 'com.android.support:cardview-v7:23.0.0' compile 'com.android.support:recyclerview-v7:23.0.0'
GitHub Project HERE
So lets get started :-)
Step 1: Create Recycler Adapter by overriding getItemViewType method and giving the type for different layout as shown below.
@Override public int getItemViewType(int position) { MultipleRowModel multipleRowModel = multipleRowModelList.get(position); if (multipleRowModel != null) return multipleRowMod.type; return super.getItemViewType(position); }
Step 2: Create a viewHolder as per viewType . We can create different row layout using viewType .
Note: viewType in onCreateviewholder is retured from getItemViewType in Step 1.
Note: viewType in onCreateviewholder is retured from getItemViewType in Step 1.
@Override public MultipleRowViewHolder onCreateViewHolder(ViewGroup parent, int viewType) { View view = null; if (viewType == AppConstant.FIRST_ROW) view = inflater.inflate(R.layout.view_row_first, parent, false); else if (viewType == AppConstant.OTHER_ROW) view = inflater.inflate(R.layout.view_row_other, parent, false); return new MultipleRowViewHolder(view, viewType); }
Step 3: Lets integrated all this in one RecyclerView adapter.
public class MultipleRowAdapter extends RecyclerView.Adapter<MultipleRowViewHolder> { private LayoutInflater inflater = null; private List<MultipleRowModel> multipleRowModelList; public MultipleRowAdapter(Context context, List<MultipleRowModel> multipleRowModelList){ this.multipleRowModelList = multipleRowModelList; inflater = LayoutInflater.from(context); } @Override public MultipleRowViewHolder onCreateViewHolder(ViewGroup parent, int viewType) { View view = null; if (viewType == AppConstant.FIRST_ROW) view = inflater.inflate(R.layout.view_row_first, parent, false); else if (viewType == AppConstant.OTHER_ROW) view = inflater.inflate(R.layout.view_row_other, parent, false); return new MultipleRowViewHolder(view, viewType); } @Override public void onBindViewHolder(MultipleRowViewHolder holder, int position) { holder.multipleContent.setText(multipleRowModelList.get(position).modelContent); } @Override public int getItemCount() { return (multipleRowModelList!= null && multipleRowModelList.size() > 0 ) ? multipleRowModelList.size() : 0; } @Override public int getItemViewType(int position) { MultipleRowModel multipleRowModel = multipleRowModelList.get(position); if (multipleRowModel != null) return multipleRowModel.type; return super.getItemViewType(position); } }
Step 4: Finally our MainActivity looks something like this.
public class MainActivity extends AppCompatActivity { private RecyclerView multipleRowRecyclerView; private MultipleRowAdapter multipleRowAdapter; private List<MultipleRowModel> multipleRowModelList = new ArrayList<>(); @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); multipleRowRecyclerView = (RecyclerView)findViewById(R.id.multipleRowRecyclerView); multipleRowRecyclerView.setHasFixedSize(true); LinearLayoutManager linearLayoutManager = new LinearLayoutManager(this, OrientationHelper.VERTICAL, false); multipleRowRecyclerView.setLayoutManager(linearLayoutManager); multipleRowRecyclerView.setItemAnimator(new DefaultItemAnimator()); fillAdapter(); multipleRowAdapter = new MultipleRowAdapter(MainActivity.this, multipleRowModelList); multipleRowRecyclerView.setAdapter(multipleRowAdapter); } private void fillAdapter() { int type; String content; for (int i = 0; i < 10; i++) { if (i == 0 || i == 5 || i == 9) { type = AppConstant.FIRST_ROW; content = "Type 1: This is Multiple row layout"; } else { type = AppConstant.OTHER_ROW; content = "Type 2"; } multipleRowModelList.add(new MultipleRowModel(type , content)); } } }
It's interesting that many of the bloggers to helped clarify a few things for me as well as giving.Most of ideas can be nice content.The people to give them a good shake to get your point and across the command.
ReplyDeletesoftware testing course in chennai
Great Article. Thank you for sharing! Really an awesome post for every one.
DeleteProject Centers in Chennai
JavaScript Training in Chennai
Final Year Project Domains for IT
JavaScript Training in Chennai
Hello, I appreciate your valuable tutorials. Is it suitable for all devices? You know each device is different from others.. thanks all~ Michael Jason from CatLight-Notifier for developers
ReplyDelete
ReplyDeleteThis is an awesome post. Really very informative and creative contents. This concept is a good way to enhance knowledge. I like it and help me to development very well. Thank you for this brief explanation and very nice information. Well, got good knowledge.
WordPress development company in Chennai
Aivivu - đại lý vé máy bay, tham khảo
ReplyDeletevé máy bay đi Mỹ bao nhiêu tiền
vé máy bay tết 2021 Vietnam Airline
ve may bay di Phap gia re
thời gian bay từ Hà Nội sang Anh
vé máy bay giá rẻ khứ hồi
vé máy bay giá rẻ đi San Francisco
vé máy bay từ Hà nội đi Los Angeles
mua combo vinpearl phú quốc
combo du lịch đà nẵng hội an
Excellent post. I want to thank you for this informative read, I really appreciate sharing this great post. Keep up your work… mobile app development company
ReplyDelete