Android RecyclerView

RecyclerView是一个比ListView更灵活高效的适配器类型空间,使用它需要一个LayoutManager和Adapter进行展示,RecyclerView提供了三种布局管理器:

1、LinearLayoutManager:线性布局管理器,支持水平和垂直效果

2、GridLayoutManager:网格布局管理器,支持水平和垂直效果

3、StaggeregGridLayoutManager:分布型管理器,瀑布流效果

recyclerView = (RecyclerView) findViewById(R.id.recycler);
//设置布局管理器
recyclerView.setLayoutManager(layout);
//设置adapter
recyclerView.setAdapter(adapter);
//设置Item增加、移除动画
recyclerView.setItemAnimator(new DefaultItemAnimator());
//添加分割线
recyclerView.addItemDecoration(new DividerItemDecoration(this,DividerItemDecoration.HORIZONTAL_LIST));

RecyclerView的使用:

1、应用RecyclerView依赖包:

compile 'com.android.support:recyclerview-v7:23.2.1'

2、在xml布局中声明,在Java代码中初始化

3、设置布局管理器

4、创建适配器、数据源,绑定适配器

5、RecyclerView的点击事件需要自己设置。

RecyclerView是一个比ListView更灵活高效的适配器类型空间,使用它需要一个LayoutManager和Adapter进行展示,RecyclerView提供了三种布局管理器:

1、LinearLayoutManager:线性布局管理器,支持水平和垂直效果

2、GridLayoutManager:网格布局管理器,支持水平和垂直效果

3、StaggeregGridLayoutManager:分布型管理器,瀑布流效果

RecyclerView的使用:

1、应用RecyclerView依赖包:

compile 'com.android.support:recyclerview-v7:23.2.1'

2、在xml布局中声明,在Java代码中初始化

3、设置布局管理器

4、创建适配器、数据源,绑定适配器

5、RecyclerView的点击事件需要自己设置。

示例:

xml:activity_recycler

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content">

    <android.support.v7.widget.RecyclerView
        android:id="@+id/recycler"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />

</LinearLayout>
list_r1_item
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical">

    <TextView
        android:id="@+id/tvR1"
        android:layout_width="match_parent"
        android:layout_height="30dp" />
</LinearLayout>

java

public class RecyclerActivity extends AppCompatActivity {
    private RecyclerView recyclerView;
    private MyRecyclerAdapter adapter;
    private List<String> mDatas;

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

        recyclerView = (RecyclerView) findViewById(R.id.recycler);
        initData();
        adapter = new MyRecyclerAdapter(RecyclerActivity.this, mDatas);
        LinearLayoutManager layoutManager = new LinearLayoutManager(this);
        recyclerView.setLayoutManager(layoutManager);
        layoutManager.setOrientation(OrientationHelper.VERTICAL);
        recyclerView.setAdapter(adapter);
        recyclerView.setItemAnimator(new DefaultItemAnimator());
        adapter.setOnItemClickListener(new MyRecyclerAdapter.OnItemClickListener() {
            @Override
            public void onClick(int position) {
                Toast.makeText(RecyclerActivity.this, "onClick", Toast.LENGTH_SHORT).show();
            }

            @Override
            public void onLongClick(int position) {
                Toast.makeText(RecyclerActivity.this, "onLongClick", Toast.LENGTH_SHORT).show();
            }
        });
    }

    private void initData() {
        mDatas = new ArrayList<String>();
        for (int i = 0; i < 40; i++) {
            mDatas.add("item" + i);
        }
    }

adapter:

public class MyRecyclerAdapter extends RecyclerView.Adapter<MyRecyclerAdapter.MyViewHolder> {

    private List<String> mDatas;
    private Context context;
    private LayoutInflater inflater;

    public MyRecyclerAdapter(Context context, List<String> datas) {
        this.context = context;
        this.mDatas = datas;
        inflater = LayoutInflater.from(context);
    }

    @Override
    public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = inflater.inflate(R.layout.list_r1_item, parent, false);
        MyViewHolder holder = new MyViewHolder(view);
        return holder;
    }

    @Override
    public void onBindViewHolder(final MyViewHolder holder, final int position) {
        holder.tv.setText(mDatas.get(position));

        //点击事件
        if (mOnClickListener!=null){
            holder.itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    mOnClickListener.onClick(position);
                }
            });
            holder. itemView.setOnLongClickListener( new View.OnLongClickListener() {
                @Override
                public boolean onLongClick(View v) {
                    mOnClickListener.onLongClick(position);
                    return false;
                }
            });
        }
        }

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

    public class MyViewHolder extends RecyclerView.ViewHolder {
        TextView tv;

        public MyViewHolder(View view) {

            super(view);
            tv = (TextView) view.findViewById(R.id.tvR1);
        }
    }

    //给RecyclerView添加点击事件
    public interface OnItemClickListener{
        void onClick(int position);
        void onLongClick(int position);
    }
    private OnItemClickListener mOnClickListener;
    public void setOnItemClickListener(OnItemClickListener onItemClickListener){
        this.mOnClickListener = onItemClickListener;
    }
}

 RecyclerView的点击事件需要自己设置,方法:

1.在Adapter中添加

//给RecyclerView添加点击事件
    public interface OnItemClickListener{
        void onClick(int position);
        void onLongClick(int position);
    }
    private OnItemClickListener mOnClickListener;
    public void setOnItemClickListener(OnItemClickListener onItemClickListener){
        this.mOnClickListener = onItemClickListener;
    }

再在onBindViewHolder方法中添加

//点击事件
        if (mOnClickListener!=null){
            holder.itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    mOnClickListener.onClick(position);
                }
            });
            holder. itemView.setOnLongClickListener( new View.OnLongClickListener() {
                @Override
                public boolean onLongClick(View v) {
                    mOnClickListener.onLongClick(position);
                    return false;
                }
            });

在Activity中添加

adapter.setOnItemClickListener(new MyRecyclerAdapter.OnItemClickListener() {
            @Override
            public void onClick(int position) {
                Toast.makeText(RecyclerActivity.this, "onClick", Toast.LENGTH_SHORT).show();
            }

            @Override
            public void onLongClick(int position) {
                Toast.makeText(RecyclerActivity.this, "onLongClick", Toast.LENGTH_SHORT).show();
            }
        });

即添加点击事件完成。

posted @ 2016-11-14 15:37  ChHM  阅读(155)  评论(0编辑  收藏  举报