grouping

Grouping effect





image

Train of thought

  1. The data portion can be distinguished by content or flag bits, in this case by flag bits
  2. The Adapter getItemViewType method is used to get the flag bit and return an int value for identification
  3. OnCreateViewHolder loads different layouts by distinguishing viewTypes

implementation

The Activity of

  1. InitData () initializes data and gives flag bits to distinguish data
  2. Call the interface defined in SectionItemDecoration to implement data passing
package com.example.tuionf.recyclerviewlearn; import android.os.Bundle; import android.support.v7.app.AppCompatActivity; import android.support.v7.widget.LinearLayoutManager; import android.support.v7.widget.RecyclerView; import java.util.ArrayList; import java.util.List; public class SectionActivity extends AppCompatActivity { private RecyclerView sectionRv; private List<SectionBean> mList = new ArrayList<>(); @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_section); initData(); sectionRv = (RecyclerView) findViewById(R.id.section_rv); sectionRv.setLayoutManager(new LinearLayoutManager(this)); SectionAdapter sectionAdapter = new SectionAdapter(this,mList); sectionRv.addItemDecoration(new SectionItemDecoration(this, new SectionItemDecoration.DecorationCallback() { @Override public boolean isGroupFirst(int position) { return mList.get(position).isFirst(); } @Override public String getGroupFirstLine(int position) { return mList.get(position).getTag(); }})); sectionRv.setAdapter(sectionAdapter); } private void initData() { for (int i = 0; i < 10; i++) { SectionBean sectionBean = new SectionBean(); sectionBean.setText("--" + i); Sectionbean.settag (" shanxi "); if (i == 0){ sectionBean.setFirst(true); }else { sectionBean.setFirst(false); } mList.add(sectionBean); } for (int i = 0; i < 20; i++) { SectionBean sectionBean = new SectionBean(); sectionBean.setText("--" + i); Sectionbean.settag (" liaoning "); if (i == 0){ sectionBean.setFirst(true); }else { sectionBean.setFirst(false); } mList.add(sectionBean); } for (int i = 0; i < 25; i++) { SectionBean sectionBean = new SectionBean(); sectionBean.setText("--" + i); Sectionbean.settag (" henan "); if (i == 0){ sectionBean.setFirst(true); }else { sectionBean.setFirst(false); } mList.add(sectionBean); } for (int i = 0; i < 29; i++) { SectionBean sectionBean = new SectionBean(); sectionBean.setText("--" + i); Sectionbean.settag (" Guangdong province "); if (i == 0){ sectionBean.setFirst(true); }else { sectionBean.setFirst(false); } mList.add(sectionBean); } for (int i = 0; i < 20; i++) { SectionBean sectionBean = new SectionBean(); sectionBean.setText("--" + i); Sectionbean.settag (" Beijing "); if (i == 0){ sectionBean.setFirst(true); }else { sectionBean.setFirst(false); } mList.add(sectionBean); } for (int i = 0; i < 20; i++) { SectionBean sectionBean = new SectionBean(); sectionBean.setText("--" + i); Sectionbean.settag (" tianjin "); if (i == 0){ sectionBean.setFirst(true); }else { sectionBean.setFirst(false); } mList.add(sectionBean); } for (int i = 0; i < 20; i++) { SectionBean sectionBean = new SectionBean(); sectionBean.setText("--" + i); Sectionbean.settag (" Chongqing "); if (i == 0){ sectionBean.setFirst(true); }else { sectionBean.setFirst(false); } mList.add(sectionBean); } for (int i = 0; i < 19; i++) { SectionBean sectionBean = new SectionBean(); sectionBean.setText("--" + i); Sectionbean.settag (" shandong "); if (i == 0){ sectionBean.setFirst(true); }else { sectionBean.setFirst(false); } mList.add(sectionBean); }}}Copy the code

The two main methods in SectionAdapter. Java are getItemViewType() and onCreateViewHolder()

package com.example.tuionf.recyclerviewlearn; 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.TextView; import java.util.List; /** * Created by tuion on 2017/9/7. */ public class SectionAdapter extends RecyclerView.Adapter { private Context mContext; private List<SectionBean> mList; public SectionAdapter(Context mContext, List<SectionBean> mList) { this.mContext = mContext; this.mList = mList; } @Override public int getItemViewType(int position) { int viewType; if (mList.get(position).isFirst()){ viewType = 0; }else {viewType = 1; } return viewType; } @Override public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) { MyViewHolder myViewHolder = null; switch(viewType){ case 0: myViewHolder = new MyViewHolder(LayoutInflater.from(mContext).inflate(R.layout.item1,null)); break; case 1: myViewHolder = new MyViewHolder(LayoutInflater.from(mContext).inflate(R.layout.item,null)); break; } return myViewHolder; } @Override public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) { MyViewHolder myViewHolder = (MyViewHolder) holder; myViewHolder.textView.setText(mList.get(position).getText()); } @Override public int getItemCount() { return mList.size(); } class MyViewHolder extends RecyclerView.ViewHolder{ TextView textView; public MyViewHolder(View itemView) { super(itemView); textView = (TextView) itemView.findViewById(R.id.tv); }}}Copy the code