查看: 298|回复: 0

[手机开发] Recyclerview添加头布局和尾布局、item点击事件详解

发表于 2018-1-7 11:20:26

简介:

本篇博客主要包括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

  1. public class MyAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> implements View.OnClickListener {
  2. private Context context;
  3. private List<Person> datas;
  4. private static final int ITEM_ONE=1;
  5. private static final int ITEM_TWO=2;
  6. private static final int ITEM_HEAD=3;
  7. private static final int ITEM_FOOT=4;
  8. private int headViewCount=1;
  9. private int footViewCount=1;
  10. private OnItemClickListener onItemClickListener;
  11. public MyAdapter(Context context, List<Person> datas) {
  12. this.context = context;
  13. this.datas = datas;
  14. }
  15. @Override
  16. public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
  17. if(viewType==ITEM_HEAD){
  18. View view= LayoutInflater.from(context).inflate(R.layout.item_head,parent,false);
  19. return new HeaderViewHolder(view);
  20. }else if(viewType==ITEM_FOOT){
  21. View view=LayoutInflater.from(context).inflate(R.layout.item_foot,parent,false);
  22. return new FootViewHolder(view);
  23. }
  24. else if(viewType==ITEM_ONE){
  25. View view= LayoutInflater.from(context).inflate(R.layout.item_recyclerview,parent,false);
  26. return new ViewHolder(view);
  27. }else if(viewType==ITEM_TWO){
  28. View view=LayoutInflater.from(context).inflate(R.layout.item_recyclerview2,parent,false);
  29. return new ViewHolder(view);
  30. }
  31. return null;
  32. }
  33. @Override
  34. public void onBindViewHolder(RecyclerView.ViewHolder holder, final int position) {
  35. if(holder instanceof HeaderViewHolder){
  36. }else if(holder instanceof FootViewHolder){
  37. }
  38. else if(holder instanceof ViewHolder){
  39. //注意除去头布局
  40. holder.itemView.setTag(position-1);
  41. holder.itemView.setOnClickListener(this);
  42. ((ViewHolder)holder).img.setBackgroundResource(datas.get(position-1).getImgId());
  43. ((ViewHolder)holder).tv1.setText(datas.get(position-1).getName());
  44. ((ViewHolder)holder).tv2.setText(datas.get(position-1).getDetail());
  45. }
  46. }
  47. @Override
  48. public int getItemCount() {
  49. if(datas==null){
  50. return headViewCount+footViewCount;
  51. }
  52. return datas.size()+headViewCount+footViewCount;
  53. }
  54. @Override
  55. public int getItemViewType(int position) {
  56. if(isHeadView(position)){
  57. return ITEM_HEAD;
  58. }
  59. if(isFootView(position)){
  60. return ITEM_FOOT;
  61. }
  62. if(position%2==0){
  63. return ITEM_ONE;
  64. }else{
  65. return ITEM_TWO;
  66. }
  67. }
  68. public boolean isHeadView(int position){
  69. return headViewCount!=0&&position<headViewCount;
  70. }
  71. public boolean isFootView(int position){
  72. return footViewCount!=0&&position>=(datas.size()+headViewCount);
  73. }
  74. @Override
  75. public void onClick(View view) {
  76. if(onItemClickListener!=null){
  77. onItemClickListener.onItemClick(view, (Integer) view.getTag());
  78. }
  79. }
  80. class ViewHolder extends RecyclerView.ViewHolder{
  81. TextView tv1,tv2;
  82. ImageView img;
  83. public ViewHolder(View itemView) {
  84. super(itemView);
  85. tv1=itemView.findViewById(R.id.tv1);
  86. tv2=itemView.findViewById(R.id.tv2);
  87. img=itemView.findViewById(R.id.img);
  88. }
  89. }
  90. class HeaderViewHolder extends RecyclerView.ViewHolder{
  91. public HeaderViewHolder(View itemView) {
  92. super(itemView);
  93. }
  94. }
  95. class FootViewHolder extends RecyclerView.ViewHolder{
  96. public FootViewHolder(View itemView) {
  97. super(itemView);
  98. }
  99. }
  100. public void setOnItemClickListener(OnItemClickListener onItemClickListener){
  101. this.onItemClickListener=onItemClickListener;
  102. }
  103. interface OnItemClickListener{
  104. void onItemClick(View view,int position);
  105. }
  106. } 
复制代码

2.MainActivity

  1. public class MainActivity extends AppCompatActivity implements MyAdapter.OnItemClickListener {
  2. private RecyclerView recyclerView;
  3. private List<Person> datas;
  4. private MyAdapter adapter;
  5. private int[] imgs={R.drawable.p1,R.drawable.p2,R.drawable.p3,R.drawable.p4,R.drawable.p5,R.drawable.pic1,
  6. R.drawable.pic2,R.drawable.pic3,R.drawable.pic4,R.drawable.pic5,R.drawable.pic6};
  7. @Override
  8. protected void onCreate(Bundle savedInstanceState) {
  9. super.onCreate(savedInstanceState);
  10. setContentView(R.layout.activity_main);
  11. recyclerView= (RecyclerView) findViewById(R.id.recyclerview);
  12. initData();
  13. initRecyclerView();
  14. }
  15. private void initRecyclerView() {
  16. recyclerView.setLayoutManager(new LinearLayoutManager(this));
  17. adapter=new MyAdapter(this,datas);
  18. adapter.setOnItemClickListener(this);
  19. recyclerView.setAdapter(adapter);
  20. }
  21. private void initData() {
  22. datas=new ArrayList<>();
  23. for(int i=0;i<imgs.length;i++){
  24. Person p=new Person();
  25. p.setName("hahfa"+i);
  26. p.setDetail("今天是星期一,还有5天才能休息"+i);
  27. p.setImgId(imgs[i]);
  28. datas.add(p);
  29. }
  30. }
  31. @Override
  32. public void onItemClick(View view, int position) {
  33. Intent intent=new Intent(MainActivity.this,ImageActivity.class);
  34. intent.putExtra("imgId",datas.get(position).getImgId());
  35. startActivity(intent);
  36. }
  37. }
复制代码

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持程序员之家。



回复

使用道具 举报