学习笔记 Tianmao 篇 recyclerView 的自定义使用

第一步 在build中编写recyclerView的dependence依赖

dependencies {
    compile "com.android.support:recyclerview-v7:23.0.1"
}

第二步 在相应的布局中编写recyclorView的控件

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

第三步 在Activity中实例化recyclorView

1.实例化recyclorView
2.Adapter添加list
3.设置布局recyclerView.setLayoutManager();
4.设置分割线recyclerView.addItemDecoration(new HomeItemDecoration());默认可以用RecyclerView.ItemDecoration
5.对每一个item进行点击监听
private void initRecyclerView(View view) {

        recyclerView= (RecyclerView) view.findViewById(R.id.recycler_view);
        initList();

        homeAdapter=new HomeAdapter(list);

        //加载adapter
        recyclerView.setAdapter(homeAdapter);

        //必须写才能出现recyclorView 使用LinearLayoutManager也可以用GirdLayoutManager,只是显示item的图形不一样

        recyclerView.setLayoutManager(new LinearLayoutManager(view.getContext()));

        //这里设置了自定义分割线  自定义分割线的类继承了RecyclerView.ItemDecoration
        recyclerView.addItemDecoration(new HomeItemDecoration());

        homeAdapter.setOnItemClickListener(new HomeAdapter.OnItemClickListener() {
            @Override
            public void onClick(View v, int position, String city) {

                Toast.makeText(getContext(),"点击"+position+"成功",Toast.LENGTH_SHORT).show();
            }
        });


    }

    private void initList(){
    for(int i = 0;i<=6;i++)
        list.add("第"+i+"个");
    }

第四步 编写adapter中item的布局

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
    <TextView
        android:id="@+id/item_text_view"
        android:layout_width="match_parent"
        android:layout_height="90sp"
        />

</LinearLayout>

第五步 编写adapter继承自RecyclorView的adapter

1.重写onCreateViewHolder
2.重写onBindViewHolder
3.重写getItemCount()
4.重写class ViewHolder extends RecyclerView.ViewHolder
5.自己写OnItemClickListener接口用来实现自己的功能
package pers.lijunxue.tianmao.other;

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

import java.util.List;

import pers.lijunxue.tianmao.R;



public class HomeAdapter extends RecyclerView.Adapter<HomeAdapter.ViewHolder> {

        public OnItemClickListener listener;              //创建item监听器对象

        private List<String> list;

        private LayoutInflater layoutInflater;

        public HomeAdapter(List<String> list){
                this.list=list;

                }

        绑定item布局文件,然后在内部类ViewHolder中绑定item布局文件中的子控件

        @Override
        public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
                layoutInflater=LayoutInflater.from(parent.getContext());
                View view=layoutInflater.inflate(R.layout.home_item_layout,parent,false);
                return new ViewHolder(view);
                }

        绑定数据,即绑定子空间中的数据
        @Override
        public void onBindViewHolder(ViewHolder holder, int position) {

                holder.textView.setText(list.get(position));

                }

        返回数据的大小
        @Override
        public int getItemCount() {
                return list.size();
                }


         绑定item布局文件中的子控件 监听动作
        class ViewHolder extends RecyclerView.ViewHolder{

            private TextView textView;
            public ViewHolder(View itemView) {
                super(itemView);
                textView= (TextView) itemView.findViewById(R.id.item_text_view);
                //绑定item的点击事件
                itemView.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        if(listener!=null){
                            listener.onClick(v,getLayoutPosition(),list.get(getLayoutPosition()));
                        }
                    }
                });

            }
        }



    public  void setOnItemClickListener(OnItemClickListener listener){            创建构造函数
        this.listener=listener;
    }

        创建recyclorview的监听事件

    public interface OnItemClickListener{
            void onClick(View v,int position,String city);

    }


}

第六步 自定义HomeItemDecoration继承RecyclerView.ItemDecoration,用于渲染recyclorView分割线

1.实现渲染item之前调用的方法 onDraw()
2.实现渲染item之后调用的方法onDrawOver()
3.实现设置item的边距的方法 getItemOffsets()
package pers.lijunxue.tianmao.other;


import android.graphics.Canvas;
import android.graphics.Rect;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;


public class HomeItemDecoration extends RecyclerView.ItemDecoration {

    渲染item之前调用的方法
    @Override
    public void onDraw(Canvas c, RecyclerView parent, RecyclerView.State state) {
        super.onDraw(c, parent, state);
    }

    渲染完之后调用的方法
    @Override
    public void onDrawOver(Canvas c, RecyclerView parent, RecyclerView.State state) {
        super.onDrawOver(c, parent, state);
    }

    设置item的边距
    @Override
    public void getItemOffsets(Rect outRect, View view, RecyclerView parent, RecyclerView.State state) {
        super.getItemOffsets(outRect, view, parent, state);
        int layoutOrientation = getOrientation(parent);
        if (layoutOrientation == LinearLayoutManager.VERTICAL) {
            outRect.top = 10;
            outRect.left=5;
            outRect.right=5;
        } else if(layoutOrientation == LinearLayoutManager.HORIZONTAL) {
            outRect.left = 5;
        }
    }
    private int getOrientation(RecyclerView parent) {
        if (parent.getLayoutManager() instanceof LinearLayoutManager) {
            LinearLayoutManager layoutManager = (LinearLayoutManager) parent.getLayoutManager();
            return layoutManager.getOrientation();
        } else throw new IllegalStateException("DividerItemDecoration can only be used with a LinearLayoutManager.");
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值