RecyclerView展示多条目

依赖:

dependencies {
    compile 'com.android.support:recyclerview-v7:26.1.0'
    //    okhttp
    compile 'com.squareup.okhttp3:okhttp:3.3.0'

//   解析Gson
    compile 'com.google.code.gson:gson:2.4'

//  图片加载Glide
    compile 'com.github.bumptech.glide:glide:3.7.0'
    }

适配器

public class MyAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
    public final String goods_url = "http://365jia.cn/uploads/";
    public final int TYPE_ONE = 0;
    public final int TYPE_TWO = 1;
    List<GoodsBean.DataBeanX.DataBean> data;
    Context context;
    private MyLongItemClick onLongItemClick;

    public MyAdapter(List<GoodsBean.DataBeanX.DataBean> data, Context context) {
        this.data = data;
        this.context = context;
    }

    @Override
    public int getItemViewType(int position) {
//        根据集合内图片个数设置多条目
        int size = data.get(position).getPics().size();
        Log.d("MyAdapter-----", "size:" + size);
        if (size == 1) {
            return TYPE_ONE;
        } else if (size == 3) {
            return TYPE_TWO;
        }
        return 0;

    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
//        根据viewType选择多条目初始化布局
        if (viewType == TYPE_ONE) {
            Log.d("MyAdapter", "条目一");
            View view = View.inflate(context, R.layout.layout_recycler_1, null);
            return new MyViewHolder(view);
        } else if (viewType == TYPE_TWO) {
            Log.d("MyAdapter", "条目二");
            View view1 = View.inflate(context, R.layout.layout_recycler_2, null);
            return new MyViewHolder1(view1);
        }
        return null;

    }

    private static final String TAG = "MyAdapter----";

    @Override
    public void onBindViewHolder(final RecyclerView.ViewHolder holder, final int position) {
        if (holder instanceof MyViewHolder) {

            holder.itemView.setOnLongClickListener(new View.OnLongClickListener() {
                @Override
                public boolean onLongClick(View v) {
                    onLongItemClick.onItemLongClick(v, position);

                    return true;
                }
            });

            Log.d(TAG, "onBindViewHolder: " + ((MyViewHolder) holder).title1);
            ((MyViewHolder) holder).title1.setText(data.get(position).getTitle());

            Glide.with(context).load(goods_url + data.get(position).getPics().get(0)).into(((MyViewHolder) holder).image1);

//-----------------------------------------------------------------------------------
            //属性动画透明
            ((MyViewHolder) holder).image1.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    //属性动画透明
                    ObjectAnimator alpha = ObjectAnimator.ofFloat(((MyViewHolder) holder).image1, "alpha", 1, 0, 1);

                    alpha.setDuration(2000);
                    alpha.start();

                }
            });

//-----------------------------------------------------------------------------------------------

        }
        if (holder instanceof MyViewHolder1) {

            holder.itemView.setOnLongClickListener(new View.OnLongClickListener() {
                @Override
                public boolean onLongClick(View v) {
                    onLongItemClick.onItemLongClick(v, position);

                    return true;
                }
            });

            ((MyViewHolder1) holder).title2.setText(data.get(position).getTitle());
//            拼接图片地址
            Glide.with(context).load(goods_url + data.get(position).getPics().get(0)).into(((MyViewHolder1) holder).image2_1);
            Glide.with(context).load(goods_url + data.get(position).getPics().get(1)).into(((MyViewHolder1) holder).image2_2);
            Glide.with(context).load(goods_url + data.get(position).getPics().get(2)).into(((MyViewHolder1) holder).image2_3);
        }
    }

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


    class MyViewHolder extends RecyclerView.ViewHolder {


        public final TextView title1;
        public final ImageView image1;


        public MyViewHolder(View itemView) {
            super(itemView);

            title1 = itemView.findViewById(R.id.recy_1_title);
            image1 = itemView.findViewById(R.id.recy_1_image);

        }

    }


    class MyViewHolder1 extends RecyclerView.ViewHolder {

        private final TextView title2;
        private final ImageView image2_1;
        private final ImageView image2_2;
        private final ImageView image2_3;

        public MyViewHolder1(View itemView) {
            super(itemView);

            image2_1 = itemView.findViewById(R.id.recy_2_image1);
            title2 = itemView.findViewById(R.id.recy_2_title);
            image2_2 = itemView.findViewById(R.id.recy_2_image2);
            image2_3 = itemView.findViewById(R.id.recy_2_image3);

        }

    }

    //长按监听
    public interface MyLongItemClick {
        void onItemLongClick(View view, int postion);
    }

    //外部调用
    public void setOnLongItemClick(MyLongItemClick onLongItemClick) {
        this.onLongItemClick = onLongItemClick;
    }

}

主界面

public class MainActivity extends AppCompatActivity implements IMainView {

    private RecyclerView mMyrecycler;
    private MyAdapter myadapter;

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

        PresenterImpl presenter = new PresenterImpl();
        presenter.showGoodsToView(new ModleImpl(),this);

//        创建布局管理器
        LinearLayoutManager layoutManager = new LinearLayoutManager(this );
        layoutManager.setOrientation(LinearLayoutManager.VERTICAL);
//      设置recyclerview
        mMyrecycler.setLayoutManager(layoutManager);

    }

    @Override
    public void showGoodsList(final List<GoodsBean.DataBeanX.DataBean> data) {
        Toast.makeText(this, "data.size():" + data.size(), Toast.LENGTH_SHORT).show();
        myadapter = new MyAdapter(data,this);
        mMyrecycler.setAdapter(myadapter);
//      设置长按点击
        myadapter.setOnLongItemClick(new MyAdapter.MyLongItemClick() {
            @Override
            public void onItemLongClick(View view, final int postion) {

                AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
                builder.setTitle("确定要删除?");
                builder.setNegativeButton("取消", null);
                builder.setNeutralButton("确定", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        data.remove(postion);
                        myadapter.notifyDataSetChanged();

                    }
                });

                builder.show();

            }
        });

//        添加分割线
        mMyrecycler.addItemDecoration(new DividerItemDecoration(this,DividerItemDecoration.VERTICAL));
    }

    private void initView() {
        mMyrecycler = (RecyclerView) findViewById(R.id.myrecycler);
    }
}

条目
这里写图片描述
这里写图片描述

自定义点击事件

//    内部调用
    public interface HistroyRecycListener{
        void onItemClick(int position);
    }
//    外部调用
    public void setOnRecycListener(HistroyRecycListener histroyRecycListener){
        this.histroyRecycListener = histroyRecycListener;
    }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值