您好,登錄后才能下訂單哦!
這篇文章主要介紹了Recyclerview如何添加頭布局和尾布局、item點擊事件,具有一定借鑒價值,感興趣的朋友可以參考下,希望大家閱讀完這篇文章之后大有收獲,下面讓小編帶著大家一起了解一下。
思路:
主要重寫Recyclerview.Adapter中的一些方法
1.public int getItemCount() item熟練 +2(頭布局和尾布局)
2.public int getItemViewType(int position) 判斷position 設置itemType
3.創建不同的ViewHolder,分別用來加載頭布局,正常布局,尾布局
4.public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) 根據viewType的不同返回不同的viewholder
5.public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) 根據holder的不同綁定不同的數據
案例:
主布局中只有一個Recyclerview,里面的item除了基本的兩種類型,還有頭布局和尾布局,點擊基本類型跳轉到另外一個Activity,顯示圖片
下面主要貼出Adapter和MainActivity中的代碼,其他的布局文件,類比較簡單就不貼出了
1.Adapter
public class MyAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> implements View.OnClickListener { private Context context; private List<Person> datas; private static final int ITEM_ONE=1; private static final int ITEM_TWO=2; private static final int ITEM_HEAD=3; private static final int ITEM_FOOT=4; private int headViewCount=1; private int footViewCount=1; private OnItemClickListener onItemClickListener; public MyAdapter(Context context, List<Person> datas) { this.context = context; this.datas = datas; } @Override public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) { if(viewType==ITEM_HEAD){ View view= LayoutInflater.from(context).inflate(R.layout.item_head,parent,false); return new HeaderViewHolder(view); }else if(viewType==ITEM_FOOT){ View view=LayoutInflater.from(context).inflate(R.layout.item_foot,parent,false); return new FootViewHolder(view); } else if(viewType==ITEM_ONE){ View view= LayoutInflater.from(context).inflate(R.layout.item_recyclerview,parent,false); return new ViewHolder(view); }else if(viewType==ITEM_TWO){ View view=LayoutInflater.from(context).inflate(R.layout.item_recyclerview2,parent,false); return new ViewHolder(view); } return null; } @Override public void onBindViewHolder(RecyclerView.ViewHolder holder, final int position) { if(holder instanceof HeaderViewHolder){ }else if(holder instanceof FootViewHolder){ } else if(holder instanceof ViewHolder){ //注意除去頭布局 holder.itemView.setTag(position-1); holder.itemView.setOnClickListener(this); ((ViewHolder)holder).img.setBackgroundResource(datas.get(position-1).getImgId()); ((ViewHolder)holder).tv1.setText(datas.get(position-1).getName()); ((ViewHolder)holder).tv2.setText(datas.get(position-1).getDetail()); } } @Override public int getItemCount() { if(datas==null){ return headViewCount+footViewCount; } return datas.size()+headViewCount+footViewCount; } @Override public int getItemViewType(int position) { if(isHeadView(position)){ return ITEM_HEAD; } if(isFootView(position)){ return ITEM_FOOT; } if(position%2==0){ return ITEM_ONE; }else{ return ITEM_TWO; } } public boolean isHeadView(int position){ return headViewCount!=0&&position<headViewCount; } public boolean isFootView(int position){ return footViewCount!=0&&position>=(datas.size()+headViewCount); } @Override public void onClick(View view) { if(onItemClickListener!=null){ onItemClickListener.onItemClick(view, (Integer) view.getTag()); } } class ViewHolder extends RecyclerView.ViewHolder{ TextView tv1,tv2; ImageView img; public ViewHolder(View itemView) { super(itemView); tv1=itemView.findViewById(R.id.tv1); tv2=itemView.findViewById(R.id.tv2); img=itemView.findViewById(R.id.img); } } class HeaderViewHolder extends RecyclerView.ViewHolder{ public HeaderViewHolder(View itemView) { super(itemView); } } class FootViewHolder extends RecyclerView.ViewHolder{ public FootViewHolder(View itemView) { super(itemView); } } public void setOnItemClickListener(OnItemClickListener onItemClickListener){ this.onItemClickListener=onItemClickListener; } interface OnItemClickListener{ void onItemClick(View view,int position); } }
2.MainActivity
public class MainActivity extends AppCompatActivity implements MyAdapter.OnItemClickListener { private RecyclerView recyclerView; private List<Person> datas; private MyAdapter adapter; private int[] imgs={R.drawable.p1,R.drawable.p2,R.drawable.p3,R.drawable.p4,R.drawable.p5,R.drawable.pic1, R.drawable.pic2,R.drawable.pic3,R.drawable.pic4,R.drawable.pic5,R.drawable.pic6}; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); recyclerView= (RecyclerView) findViewById(R.id.recyclerview); initData(); initRecyclerView(); } private void initRecyclerView() { recyclerView.setLayoutManager(new LinearLayoutManager(this)); adapter=new MyAdapter(this,datas); adapter.setOnItemClickListener(this); recyclerView.setAdapter(adapter); } private void initData() { datas=new ArrayList<>(); for(int i=0;i<imgs.length;i++){ Person p=new Person(); p.setName("hahfa"+i); p.setDetail("今天是星期一,還有5天才能休息"+i); p.setImgId(imgs[i]); datas.add(p); } } @Override public void onItemClick(View view, int position) { Intent intent=new Intent(MainActivity.this,ImageActivity.class); intent.putExtra("imgId",datas.get(position).getImgId()); startActivity(intent); } }
感謝你能夠認真閱讀完這篇文章,希望小編分享的“Recyclerview如何添加頭布局和尾布局、item點擊事件”這篇文章對大家有幫助,同時也希望大家多多支持億速云,關注億速云行業資訊頻道,更多相關知識等著你來學習!
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。