关闭

RecyclerView的使用

标签: 布局控件
250人阅读 评论(0) 收藏 举报
分类:

一.RecyclerView的介绍

  • 通过布局管理器LayoutManager,控制其显示的方式
  • 通过ItemDecoration,控制Item间的间隔
  • 通过ItemAnimator,控制Item增删的动画
  • 点击、长按事件,需要自己实现

二.RecyclerView的使用

直接上代码:

MainActiivity.java

package recycleviewdemo.zhoujian.com.recycleviewdemo.activity;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.DefaultItemAnimator;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.TextView;
import android.widget.Toast;

import java.util.ArrayList;

import recycleviewdemo.zhoujian.com.recycleviewdemo.R;
import recycleviewdemo.zhoujian.com.recycleviewdemo.adapter.MainAdapter;
import recycleviewdemo.zhoujian.com.recycleviewdemo.adapter.MainAdapter.OnItemClickLitener;
import recycleviewdemo.zhoujian.com.recycleviewdemo.bean.Person;
import recycleviewdemo.zhoujian.com.recycleviewdemo.view.DividerGridItemDecoration;
import recycleviewdemo.zhoujian.com.recycleviewdemo.view.DividerItemDecoration;

public class MainActivity extends Activity
{

    private RecyclerView mRecyclerView;
    private ArrayList<Person> personList;
    private MainAdapter mAdapter;

    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);

        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,WindowManager.LayoutParams.FLAG_FULLSCREEN);
        setContentView(R.layout.activity_main);
        initData();
        initView();
        initListView();
        //initGridView();
        initEvent();

    }

    private void initGridView()
    {

        mRecyclerView.setLayoutManager(new GridLayoutManager(this, 3));
        mRecyclerView.addItemDecoration(new DividerGridItemDecoration(this));
        // 设置item动画
        mRecyclerView.setItemAnimator(new DefaultItemAnimator());
        mRecyclerView.setAdapter(mAdapter);

    }

    private void initListView()
    {
        mRecyclerView.setLayoutManager(new LinearLayoutManager(this));
        mRecyclerView.addItemDecoration(new DividerItemDecoration(this,DividerItemDecoration.VERTICAL_LIST));
        // 设置item动画
        mRecyclerView.setItemAnimator(new DefaultItemAnimator());
        mRecyclerView.setAdapter(mAdapter);


    }

    private void initView() {


        TextView tv_start= (TextView) findViewById(R.id.tv_start);

        tv_start.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                startActivity(new Intent(MainActivity.this,WaterFallActivity.class));
            }
        });

        mRecyclerView = (RecyclerView) findViewById(R.id.id_recyclerview);
        mAdapter = new MainAdapter(this, personList);


    }

    private void initEvent()
    {
        mAdapter.setOnItemClickLitener(new OnItemClickLitener()
        {
            @Override
            public void onItemClick(View view, int position)
            {
                Toast.makeText(MainActivity.this, personList.get(position).getName(),Toast.LENGTH_SHORT).show();
            }

            @Override
            public void onItemLongClick(View view, int position)
            {
                Toast.makeText(MainActivity.this, personList.get(position).getName(),Toast.LENGTH_SHORT).show();
            }
        });
    }

    protected void initData()
    {
        personList = new ArrayList<Person>();
        Person mPerson0= new Person("宋江");
        personList.add(mPerson0);
        Person mPerson1= new Person("卢俊义");
        personList.add(mPerson1);
        Person mPerson2= new Person("吴用");
        personList.add(mPerson2);
        Person mPerson3= new Person("公孙胜");
        personList.add(mPerson3);
        Person mPerson4= new Person("关胜");
        personList.add(mPerson4);
        Person mPerson5= new Person("林冲");
        personList.add(mPerson5);
        Person mPerson6= new Person("秦明");
        personList.add(mPerson6);
        Person mPerson7= new Person("呼延灼");
        personList.add(mPerson7);
        Person mPerson8= new Person("花荣");
        personList.add(mPerson8);
        Person mPerson9= new Person("柴进");
        personList.add(mPerson9);
        Person mPerson10= new Person("李应");
        personList.add(mPerson10);
        Person mPerson11= new Person("鲁智深");
        personList.add(mPerson11);
    }
}

MainAdapter.java

package recycleviewdemo.zhoujian.com.recycleviewdemo.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.RecyclerView.ViewHolder;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnLongClickListener;
import android.view.ViewGroup;
import android.widget.TextView;
import java.util.ArrayList;
import recycleviewdemo.zhoujian.com.recycleviewdemo.R;
import recycleviewdemo.zhoujian.com.recycleviewdemo.bean.Person;

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

    private ArrayList<Person> mDatas;
    private LayoutInflater mInflater;
    private OnItemClickLitener mOnItemClickLitener;

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

    public void setOnItemClickLitener(OnItemClickLitener mOnItemClickLitener)
    {
        this.mOnItemClickLitener = mOnItemClickLitener;
    }


    public MainAdapter(Context context, ArrayList<Person> datas)
    {
        mInflater = LayoutInflater.from(context);
        mDatas = datas;
    }

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

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

        // 如果设置了回调,则设置点击事件
        if (mOnItemClickLitener != null)
        {
            holder.itemView.setOnClickListener(new OnClickListener()
            {
                @Override
                public void onClick(View v)
                {
                    int pos = holder.getLayoutPosition();
                    mOnItemClickLitener.onItemClick(holder.itemView, pos);
                }
            });

            holder.itemView.setOnLongClickListener(new OnLongClickListener()
            {
                @Override
                public boolean onLongClick(View v)
                {
                    int pos = holder.getLayoutPosition();
                    mOnItemClickLitener.onItemLongClick(holder.itemView, pos);
                    removeData(pos);
                    return false;
                }
            });
        }
    }

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

    public void removeData(int position)
    {
        mDatas.remove(position);
        notifyItemRemoved(position);
    }

    class MyViewHolder extends ViewHolder
    {

        TextView tv;
        public MyViewHolder(View view)
        {
            super(view);
            tv = (TextView) view.findViewById(R.id.id_num);

        }
    }
}

ListView的效果

这里写图片描述

GridView的效果

这里写图片描述

瀑布流效果

这里写图片描述

三.RecyclerView的分割线

上面的分割线是默认的显示效果,也可以自定义显示效果

在styles.xml中添加代码:

<item name="android:listDivider">@drawable/divider_bg</item>

三.源码下载

源码下载:https://github.com/zeke123/RecycleViewDemo

0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:62642次
    • 积分:2557
    • 等级:
    • 排名:第14363名
    • 原创:152篇
    • 转载:4篇
    • 译文:53篇
    • 评论:6条
    博客专栏
    最新评论