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
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
ReplyDeleteAivivu - đạ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Đặt mua vé máy bay liên hệ Aivivu
ReplyDeleteVé máy bay đi Mỹ
giá vé từ mỹ về việt nam
giá vé máy bay từ anh về việt nam
chuyến bay từ pháp về việt nam
I think you can try out the different version of it, you can also check out whatsapp lite apk download original apk it will provide you complete apk file with unlock features.
ReplyDeleteA very nice guide. Thank you for sharing such detailed article.
ReplyDeleteAre you planning to develop an On-Demand fuel delivery app? get the full details about the on-demand fuel delivery app development cost, features & benefits. Contact us now!
Recently, there have been requires the government to chill out the rules and permit on-line gambling these who|for many who|for people who} wish to enjoy it, but it remains to be seen whether or not this will turn out to be a actuality. Wagering is permitted on the country’s racetracks in Seoul and Busan. Since May 2018, an agreement between the Sports Information Service and the Korean 카지노 사이트 Racing Authority allowed stay broadcasting and footage from these races to retail and on-line bookmakers in the UK, Ireland and Spain. Are also allowed in South Korea, naturally underneath the identical monopoly answerable for the lottery and sports betting providers.
ReplyDelete