RecyclerView封装详解完美用法二

原创 2016年05月30日 16:15:28

这篇主要是recyclerView实现瀑布流:

一、RecyclerView通用ViewHolder同上篇。

二、然后是适配器

package com.example.recyclerview;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

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

/**
 * Created by 11981 on 2016/5/28.
 * 瀑布流适配器
 */
public abstract class StaggeredAdapter extends RecyclerView.Adapter<MyViewHolder>{
    private List<Integer> mHeights;
    private LayoutInflater mInflater;
    private Context mContext;
    public List<DataDTO> mDatas;
    private int mlayoutId;

    public interface OnItemClickListener {
        void onItemClick(View view, int position);
        void onItemLongClick(View view, int position);
    }

    private OnItemClickListener onItemClickListener;

    public void setOnItemClickListener(OnItemClickListener listener) {
        this.onItemClickListener = listener;
    }

    public StaggeredAdapter(Context context, List<DataDTO> datas,int layoutId) {
        this.mContext = context;
        this.mDatas = datas;
        this.mlayoutId = layoutId;
        mInflater = LayoutInflater.from(context);
        mHeights = new ArrayList<Integer>();
        for (int i = 0; i <mDatas.size() ; i++) {
            mHeights.add((int)(100+Math.random()*300));
        }
    }

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

    @Override
    public void onBindViewHolder(final MyViewHolder holder, final int position) {
        ViewGroup.LayoutParams lp=holder.itemView.getLayoutParams();
        lp.height =mHeights.get(position);
        holder.itemView.setLayoutParams(lp);
        convert(holder,mDatas.get(position));
        setUpItemEvent(holder);
    }
    public abstract  void convert(MyViewHolder holder,DataDTO datadto);

    public void setUpItemEvent(final MyViewHolder holder) {
        if (onItemClickListener != null) {
            holder.itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    int layoutPosition=holder.getAdapterPosition();
                    onItemClickListener.onItemClick(holder.itemView, layoutPosition);

                }
            });
            //longclick
            holder.itemView.setOnLongClickListener(new View.OnLongClickListener() {
                @Override
                public boolean onLongClick(View v) {
                    int layoutPosition=holder.getAdapterPosition();
                    onItemClickListener.onItemLongClick(holder.itemView,layoutPosition);
                    return false;
                }
            });
        }
    }

    @Override
    public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        MyViewHolder viewHolder = new MyViewHolder(mInflater.inflate(mlayoutId, parent, false));
        return viewHolder;
    }

//    public void addData(int pos,DataDTO datas) {
//        mDatas.add(pos, datas);
//        notifyItemInserted(pos);
//    }
//
    public void deleteData(int pos) {
        mDatas.remove(pos);
        notifyItemRemoved(pos);
    }
}
三、最后是主类:

package com.example.recyclerview;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.StaggeredGridLayoutManager;
import android.view.View;

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

/***
 * 瀑布流主类
 */
public class StaggereGridLayoutActivity extends AppCompatActivity{

    private RecyclerView mRecyclerView;
    private List<DataDTO> mDatas;
    private StaggeredAdapter mAdapter;

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

        initDatas();
        initViews();
        mAdapter = new StaggeredAdapter(this, mDatas, R.layout.item_single_view) {
            @Override
            public void convert(MyViewHolder holder, DataDTO datadto) {
                holder.setText(R.id.id_tv,datadto.getAaa()).setImageResource(R.id.id_img,datadto.getBbb());
            }
        };
        mRecyclerView.setAdapter(mAdapter);
        //设置recyclerview布局管理器
        mRecyclerView.setLayoutManager(new StaggeredGridLayoutManager(3,StaggeredGridLayoutManager.VERTICAL));

        mAdapter.setOnItemClickListener(new StaggeredAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(View view, int position) {

            }

            @Override
            public void onItemLongClick(View view, int position) {
                mAdapter.deleteData(position);
            }
        });

        //设置recycler的Item分割线
        //listview转gridview没有列的分割
       // mRecyclerView.addItemDecoration(new DividerItemDecoration(this, DividerItemDecoration.VERTICAL_LIST));
    }

    private void initDatas() {
        mDatas = new ArrayList<DataDTO>();
        DataDTO data=new DataDTO();
        for (int i = 0; i <20 ; i++) {
            data.setAaa("abcd");
            data.setBbb(R.drawable.ic_menu_delete);
            mDatas.add(data);
        }
//        mDatas = new ArrayList<String>();
//        for (int i = 'A'; i <= 'z'; i++) {
//            mDatas.add("" + (char) i);
//        }
    }

    private void initViews() {
        mRecyclerView = (RecyclerView) findViewById(R.id.id_recyclerview);
    }

}
四、RecyclerView依赖工具类:同上

五、实体类:同上

六、其它的代码:同上

最后在清单文件中注册activity:

<activity android:name=".StaggereGridLayoutActivity">

</activity>

完成



版权声明:本文为博主原创文章,未经博主允许不得转载。 举报

相关文章推荐

RecyclerView封装详解完美用法一

通过对RecyclerView的了解,个人认为此控件适用于需要listview与GridView切换的界面以及瀑布流界面。 第一篇主要是listView与Gridview切换代码: 一、先上Recyc...

真实项目运用-RecyclerView封装

原文链接:从零开始搭建android框架系列 项目地址:MVPCommon 前言很久没有发表从零开始搭建android框架系列这个系列的文章了 。由于最近工作确实有点忙碌,也在脚踏实地的花时间研...

我是如何成为一名python大咖的?

人生苦短,都说必须python,那么我分享下我是如何从小白成为Python资深开发者的吧。2014年我大学刚毕业..

RecyclerView用法详解

使用RecyclerView首先需要导入相应的架包: compile 'com.android.support:recyclerview-v7:24.0.0-alpha2' 主界面: <andr...
返回顶部
收藏助手
不良信息举报
您举报文章:深度学习:神经网络中的前向传播和反向传播算法推导
举报原因:
原因补充:

(最多只允许输入30个字)