RecyclerViewの複雑なページ実装

16045 ワード

RecyclerViewの複雑なページ実装の詳細
 
 
コード#コード#
 
Activity
package com.wjn.viewlistdemo.activity.recyclerview.complex;

import android.support.v4.content.ContextCompat;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.DividerItemDecoration;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.Toast;

import com.wjn.viewlistdemo.R;
import com.wjn.viewlistdemo.activity.recyclerview.MyRecycleViewAdapter;
import com.wjn.viewlistdemo.activity.recyclerview.RecycleViewAdapterInterface;
import com.wjn.viewlistdemo.activity.recyclerview.RecyclerViewActivity;
import com.wjn.viewlistdemo.activity.recyclerview.Student;

import java.util.List;

public class ComplexActivity extends AppCompatActivity {

    private RecyclerView recyclerView;
    private RecyclerViewAdapter adapter;
    private List list;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_complex);
        initView();
    }

    /**
     *      View
     */

    private void initView() {
        recyclerView = findViewById(R.id.activity_complex_recycleview);
        list = ComplexUtils.getInstance().getList();
        //1.  LinearLayoutManager ListView
        recyclerView.setLayoutManager(new LinearLayoutManager(this));
        //2.      
        recyclerView.setHasFixedSize(true);
        //3.      
        DividerItemDecoration divider = new DividerItemDecoration(this, DividerItemDecoration.VERTICAL);
        divider.setDrawable(ContextCompat.getDrawable(this, R.drawable.dy_line));
        recyclerView.addItemDecoration(divider);
        //4.  Adapter
        adapter = new RecyclerViewAdapter(this, list);
        recyclerView.setAdapter(adapter);
        //5.Item        
        setRecycleViewItemClickAndLongClick();
    }

    /**
     * Item      
     */

    private void setRecycleViewItemClickAndLongClick() {
        if (null != adapter) {
            adapter.setItemClickCallBack(new ItemClickCallBack() {

                @Override
                public void onItemCilck(View view, int position, int type) {
                    String text = "   :" + " Position:" + position + " Type:" + type;
                    Toast.makeText(ComplexActivity.this, text, Toast.LENGTH_SHORT).show();
                }

                @Override
                public void onItemLongCilck(View view, int position, int type) {
                    String text = "   :" + " Position:" + position + " Type:" + type;
                    Toast.makeText(ComplexActivity.this, text, Toast.LENGTH_SHORT).show();
                }
            });
        }
    }

}

 
 
 
Activityレイアウト



    

    


 
 
Adapter
package com.wjn.viewlistdemo.activity.recyclerview.complex;

import android.content.Context;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.wjn.viewlistdemo.R;
import com.wjn.viewlistdemo.activity.recyclerview.MyRecycleViewAdapter;
import com.wjn.viewlistdemo.activity.recyclerview.Student;

import java.util.List;

public class RecyclerViewAdapter extends RecyclerView.Adapter {

    public static final int TYPE_ONE = 1;
    public static final int TYPE_TWO = 2;
    public static final int TYPE_THREE = 3;

    private ItemClickCallBack itemClickCallBack;

    public void setItemClickCallBack(ItemClickCallBack itemClickCallBack) {
        this.itemClickCallBack = itemClickCallBack;
    }

    private Context context;
    private List list;
    private LayoutInflater mInflater;

    public RecyclerViewAdapter(Context context, List list) {
        this.context = context;
        this.list = list;
        mInflater = LayoutInflater.from(context);
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
        if (i == TYPE_ONE) {
            View view = mInflater.inflate(R.layout.complex_item1, viewGroup, false);
            OneViewHolder oneViewHolder = new OneViewHolder(view);
            return oneViewHolder;
        } else if (i == TYPE_TWO) {
            View view = mInflater.inflate(R.layout.complex_item2, viewGroup, false);
            TwoViewHolder twoViewHolder = new TwoViewHolder(view);
            return twoViewHolder;
        } else if (i == TYPE_THREE) {
            View view = mInflater.inflate(R.layout.complex_item3, viewGroup, false);
            ThreeViewHolder threeViewHolder = new ThreeViewHolder(view);
            return threeViewHolder;
        } else {
            return null;
        }
    }

    @Override
    public void onBindViewHolder(@NonNull RecyclerView.ViewHolder viewHolder, int i) {
        if (null == viewHolder) {
            return;
        }

        int viewType = getItemViewType(i);
        switch (viewType) {
            case TYPE_ONE://  1
                //  ViewHolder
                OneViewHolder oneViewHolder = (OneViewHolder) viewHolder;
                //    
                oneViewHolder.textView1.setText(list.get(i).getName() + "【" + (i + 1) + "】");
                oneViewHolder.textView2.setText(list.get(i).getDescribe());
                oneViewHolder.circleImageView.setBackgroundResource(R.mipmap.patient_ava);
                //    
                setClick(oneViewHolder, i, viewType);
                break;
            case TYPE_TWO://  2
                //  ViewHolder
                TwoViewHolder twoViewHolder = (TwoViewHolder) viewHolder;
                //    
                twoViewHolder.textView.setText(list.get(i).getDescribe());
                //    
                setClick(twoViewHolder, i, viewType);
                break;
            case TYPE_THREE://  3
                //  ViewHolder
                ThreeViewHolder threeViewHolder = (ThreeViewHolder) viewHolder;
                //    
                threeViewHolder.textView.setText(list.get(i).getName());
                threeViewHolder.imageView.setBackgroundResource(R.mipmap.home_body);
                //    
                setClick(threeViewHolder, i, viewType);
                break;
        }
    }

    @Override
    public int getItemCount() {
        return list.size();
    }

    @Override
    public int getItemViewType(int position) {
        return list.get(position).getType();
    }

    /**
     * OneViewHolder
     */

    public class OneViewHolder extends RecyclerView.ViewHolder {

        private ImageView circleImageView;
        private TextView textView1;
        private TextView textView2;

        public OneViewHolder(View itemView) {
            super(itemView);
            circleImageView = itemView.findViewById(R.id.recycleview_circleimageview);
            textView1 = itemView.findViewById(R.id.recycleview_item_textview1);
            textView2 = itemView.findViewById(R.id.recycleview_item_textview2);
        }
    }

    /**
     * TwoViewHolder
     */

    public class TwoViewHolder extends RecyclerView.ViewHolder {

        private TextView textView;

        public TwoViewHolder(View itemView) {
            super(itemView);
            textView = itemView.findViewById(R.id.complex_item2_textview);
        }
    }

    /**
     * ThreeViewHolder
     */

    public class ThreeViewHolder extends RecyclerView.ViewHolder {

        private ImageView imageView;
        private TextView textView;

        public ThreeViewHolder(View itemView) {
            super(itemView);
            imageView = itemView.findViewById(R.id.complex_item3_imageview);
            textView = itemView.findViewById(R.id.complex_item3_textview);
        }
    }

    /**
     *   RecycleView Item        
     */

    private void setClick(RecyclerView.ViewHolder viewHolder, final int position, final int type) {
        if (null == viewHolder || position < 0) {
            return;
        }

        //Item  
        viewHolder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (null != itemClickCallBack) {
                    itemClickCallBack.onItemCilck(v, position, type);
                }
            }
        });
        //Item  
        viewHolder.itemView.setOnLongClickListener(new View.OnLongClickListener() {
            @Override
            public boolean onLongClick(View v) {
                if (null != itemClickCallBack) {
                    itemClickCallBack.onItemLongCilck(v, position, type);
                }
                return true;
            }
        });
    }

}

 
 
 
Item 1レイアウト


    

        


        

            

            

        

    



 
 
Item 2レイアウト


    



 
 
Item 3レイアウト


    

    

    



 
 
インタフェース
package com.wjn.viewlistdemo.activity.recyclerview.complex;

import android.view.View;

public interface ItemClickCallBack {

    void onItemCilck(View view, int position, int type);

    void onItemLongCilck(View view, int position, int type);

}

 
 
ツールクラス
package com.wjn.viewlistdemo.activity.recyclerview.complex;

import com.wjn.viewlistdemo.activity.recyclerview.Student;

import java.util.ArrayList;
import java.util.List;

public class ComplexUtils {

    private ComplexUtils() {

    }

    private static class Holder {
        private static ComplexUtils complexUtils = new ComplexUtils();
    }

    public static ComplexUtils getInstance() {
        return Holder.complexUtils;
    }

    public List getList() {
        List list = new ArrayList<>();
        Student student1 = new Student();
        student1.setName("   ");
        student1.setType(1);
        student1.setDescribe(student1.getName() + ":        !");

        Student student2 = new Student();
        student2.setName("  ");
        student2.setType(1);
        student2.setDescribe(student2.getName() + ":        !");

        Student student3 = new Student();
        student3.setName("    ");
        student3.setType(2);
        student3.setDescribe(student3.getName() + ":        !");

        Student student4 = new Student();
        student4.setName("  ");
        student4.setType(2);
        student4.setDescribe(student4.getName() + ":        !");

        Student student5 = new Student();
        student5.setName("   ");
        student5.setType(3);
        student5.setDescribe(student5.getName() + ":        !");

        Student student6 = new Student();
        student6.setName("   ");
        student6.setType(1);
        student6.setDescribe(student6.getName() + ":        !");

        Student student7 = new Student();
        student7.setName("   ");
        student7.setType(2);
        student7.setDescribe(student7.getName() + ":        !");

        for (int i = 0; i < 20; i++) {
            list.add(student1);
            list.add(student2);
            list.add(student3);
            list.add(student4);
            list.add(student5);
            list.add(student6);
            list.add(student7);
        }

        return list;
    }

}

 
 
エンティティークラス
package com.wjn.viewlistdemo.activity.recyclerview;

public class Student {

    private String ava;
    private String name;
    private String describe;
    private int type;

    public String getAva() {
        return ava;
    }

    public void setAva(String ava) {
        this.ava = ava;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getDescribe() {
        return describe;
    }

    public void setDescribe(String describe) {
        this.describe = describe;
    }

    public int getType() {
        return type;
    }

    public void setType(int type) {
        this.type = type;
    }
}