关闭

RecyclerView的相关用法简介

标签: java语言Android
32人阅读 评论(0) 收藏 举报
分类:
public class MainActivity extends AppCompatActivity {

    private RecyclerView mRecyclerView;
    List<String> mDatas;
    MyAdapter mAdapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        mRecyclerView = (RecyclerView) findViewById(R.id.recyclerview);
        initData();//初始化数据
        mAdapter = new MyAdapter(mDatas, this);//初始化Adapter(设配器)
        mRecyclerView.setAdapter(mAdapter);//设置Adapter
        //设置分隔线
//        mRecyclerView.addItemDecoration(new DividerItemDecoration(this,DividerItemDecoration.VERTICAL));
        //必须要设置布局管理器,不然界面是空的

        ////////////////////////////////   完成listview的效果   //////////////////////////////
//        LinearLayoutManager layout = new LinearLayoutManager(this,LinearLayoutManager.VERTICAL,true);
//        mRecyclerView.setLayoutManager(layout);
        ////////////////////////////////   完成listview的效果   //////////////////////////////

        //设置gridview 的效果
//        mRecyclerView.setLayoutManager(new GridLayoutManager(this,3));

        //这里如果是水平的,item的宽度item的个数不足以填充宽度,itemitem之间会自动有空白来代替
        mRecyclerView.setLayoutManager(new StaggeredGridLayoutManager(3, StaggeredGridLayoutManager.VERTICAL));
        mAdapter.setOnItemClickListener(new MyAdapter.OnItemClickListener() {  //RecyclerViewitem点击事件
            @Override
            public void onItemClickListener(int position, View v) {
                mAdapter.add(position);
            }

            @Override
            public void onItemLongClickListener(int position, View v) {
                mAdapter.delete(position);
            }
        });
    }

    private void initData() {
        mDatas = new ArrayList<>();
        for (int i = 'A'; i < 'G'; i++) {
            mDatas.add((char) i + "");
        }
    }
}

Adapter类的代码:

public class MyAdapter extends RecyclerView.Adapter<MyViewHolder> {
    List<String> mList;
    Context mContext;
    private LayoutInflater mInflater;

    public MyAdapter(List<String> list, Context context) {
        mList = list;
        mContext = context;
        mInflater = LayoutInflater.from(mContext);
    }

    @Override
    public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        //注意这里的参数只能改第一个参数resource,只能使用LayoutInflater.inflate.(resource,viewGroup,attachToRoot);
        View view = mInflater.inflate(R.layout.item, parent, false);
        MyViewHolder myViewHolder = new MyViewHolder(view);
        return myViewHolder;
    }

    public interface OnItemClickListener {
        void onItemClickListener(int position,View v);
        void onItemLongClickListener(int position,View v);
    }
    private OnItemClickListener mListener;
    public void setOnItemClickListener(OnItemClickListener listener) {
        mListener = listener;
    }
    @Override
    public void onBindViewHolder(final MyViewHolder holder, final int position) {
        if (mListener != null) {
            holder.itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    int layoutPosition = holder.getLayoutPosition();
                    mListener.onItemClickListener(layoutPosition,v);//要用holder.getLayoutPosition()的数,得到布局上的item的排序
                }
            });
            holder.itemView.setOnLongClickListener(new View.OnLongClickListener() {
                @Override
                public boolean onLongClick(View v) {
                    int layoutPosition = holder.getLayoutPosition();
                    mListener.onItemLongClickListener(layoutPosition,v);//这个position是不准确的,
                    // 因为当增删item,position会不准确,还是会依照一开始的条目排序,有时position并不是会随item改变而变
                    return false;
                }
            });
        }
        holder.mTv.setText(mList.get(position));
        //在这里设置随机高度
        ViewGroup.LayoutParams layoutParams = holder.itemView.getLayoutParams();
        layoutParams.height = 100 + r.nextInt(200);
        holder.itemView.setLayoutParams(layoutParams);
    }
    Random r = new Random();
    @Override
    public int getItemCount() {
        if (mList == null)
            return 0;
        return mList.size();
    }

    public void add(int position) {
        mList.add(position, "insert one");
        notifyItemInserted(position);//代表插入第几个item
    }

    public void delete(int position) {
        mList.remove(position);
        notifyItemRemoved(position);
    }
}

class MyViewHolder extends RecyclerView.ViewHolder {

    TextView mTv;

    public MyViewHolder(View itemView) {
        super(itemView);
        mTv = (TextView) itemView.findViewById(R.id.tv);
    }
}
item布局文件代码里面就一个TextView自己可以修改:


<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/activity_main"
    android:layout_width="72dp"
    android:layout_margin="3dp"
    android:orientation="vertical"
    android:layout_height="50dp"
    android:background="#5f00">

    <TextView
        android:id="@+id/tv"
        android:gravity="center"
        android:text="A"
        android:textColor="#fff"
        android:layout_width="72dp"
        android:layout_height="50dp"/>
</LinearLayout>

如果要简单自定义分隔线,可以在src/res/drawable里画些样式:
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
       android:shape="rectangle">
    <size android:height="4dp"></size>
    <gradient
        android:centerColor="#0f0"
        android:endColor="#00f"
        android:startColor="#f00"
        android:type="linear"></gradient>
</shape>
记得要生效的话必须要在使用的主题里添加一行代码:
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
    <!--这是从DividerItemDecoration里面的属性来的,在你使用的主题里改变一下就可以-->
    <item name="android:listDivider">@drawable/divider02(这里是自己画的xml文件名)</item>
</style>

总结:RecyclerView比较强大,但是要自己实现item点击事件,主要的item里的position要注意,使用这个方法
holder.getLayoutPosition()来替代position,还有的话是

public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
    //注意这里的参数只能改第一个参数resource,只能使用LayoutInflater.inflate.(resource,viewGroup,attachToRoot);
    View view = mInflater.inflate(R.layout.item, parent, false);
//mInflater=android.view.LayoutInflater.from(mContext)
    MyViewHolder myViewHolder = new MyViewHolder(view);
    return myViewHolder;
}
不可以使用View.Inflate()方法

0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:2283次
    • 积分:163
    • 等级:
    • 排名:千里之外
    • 原创:11篇
    • 转载:12篇
    • 译文:1篇
    • 评论:0条
    文章分类