RecyclerView的点击事件

RecyclerView

一、简单介绍

这个是谷歌官方出的控件。使我们能够很easy的做出列表装的一个控件,当然recyclerview的功能不止这些,它还能够做出瀑布流的效果,这是一个很强大的控件,内部自带viewholder能够使我们很easy的完毕很多操作,正在一步一步代替listview这个控件。当然它也有一些小的缺点。那就是谷歌官方并没有直接给我写出它的点击事件的接口,可是这并难不倒我们,我们能够自己写一个回调的接口。实现点击事件。在这里我不仅要为大家介绍recyclerview的item的点击事件,还要为大家介绍,一个item中局部的点击事件,还有加入header、footer,还有加入不同类别的item的布局。能够说彻底的读懂了这篇文章,我们对recyclerview就有了一个新的认识了。

二、涉及到的知识点

  • item的点击事件
  • item里面内容的点击事件
  • 为recycler view加入header和footer
  • 为item加入不同的布局

三、实现代码

/**
 * Created by linSir on 16/7/24.管理地址界面的适配器
 */
public class ManageAddressAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {//在这里我们要继承自官方为我们写好的适配器

    public OnTitleClickListener mListener;
    private List<AllAddress> mList;  //用户列表

    public ManageAddressAdapter() {
        mList = new ArrayList<>();
    }

    public void setList(List<AllAddress> list) {//从外界传入一个list
        mList.clear();
        mList.addAll(list);
        notifyDataSetChanged();
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {//重写方法,用上我们以下写好的viewHoler
        return new ManageAddressViewHolder(LayoutInflater.from(parent.getContext()).inflate(R.layout.item_manage_address, parent, false));
    }

    @Override public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {//在这里我们将数据和控件绑定起来
        ManageAddressViewHolder mHolder = (ManageAddressViewHolder) holder;
        mHolder.userName.setText(mList.get(position).getShipName());
        mHolder.userTel.setText(mList.get(position).getPhone());

        String address = mList.get(position).getProvince() + " " + mList.get(position).getCity()
                + " " + mList.get(position).getArea() + " " + mList.get(position).getDetail();
        mHolder.address.setText(address);

        mHolder._default.setOnClickListener(new ClickListener(String.valueOf(position)));//在这里我们设置了点击事件

    }

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

    public static class ManageAddressViewHolder extends RecyclerView.ViewHolder {

        private TextView userName;
        private TextView userTel;
        private TextView address;
        private TextView _default;

        public ManageAddressViewHolder(View itemView) {
            super(itemView);
            userName = (TextView) itemView.findViewById(R.id.manage_userName);
            userTel = (TextView) itemView.findViewById(R.id.manage_userTel);
            address = (TextView) itemView.findViewById(R.id.manage_userAddress);
            _default = (TextView) itemView.findViewById(R.id.manage_default);

        }
    }

    public class ClickListener implements View.OnClickListener {//在这里我们重写了点击事件
        private String id;

        public ClickListener(String id) {
            this.id = id;
        }

        @Override public void onClick(View view) {
            if (mListener != null) {
                mListener.onTitleClick(id);
            }
        }
    }

    public void setOnTitleClickListener(OnTitleClickListener listener) {//自己写了一个方法,用上我们的接口
        mListener = listener;
    }

    public interface OnTitleClickListener {//自己写了一个点击事件的接口
        void onTitleClick(String id);
    }

}

通过以上的代码,我们已经为recyclerView里面的item里面的textview加入成功了点击事件,我们仅仅须要在调用它的界面实现这个接口。然后重写点击事件的方法。就能够实现这个textview的点击事件了,以下我们一起看一下代码:

/**
 * Created by linSir on 16/7/24.管理地址界面
 */
public class ManageAddressActivity extends AppCompatActivity implements ManageAddressAdapter.OnTitleClickListener {//实现上文我们写好的点击事件的接口

    private ManageAddressAdapter mAdapter;
    private List<AllAddress> list;
    @BindView(R.id.manage_address_recyclerView) RecyclerView mRecyclerView;

    @Override protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_manage);
        ButterKnife.bind(this);
        list = new ArrayList<AllAddress>();
        mAdapter = new ManageAddressAdapter();
        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(this);
        mAdapter.setOnTitleClickListener(this);//声明一下
        mRecyclerView.setAdapter(mAdapter);
        mRecyclerView.setLayoutManager(linearLayoutManager);//这里千万不要了为recyclerview设置布局

    }

    @OnClick(R.id.back_manage_address)
    public void back() {
        finish();
    }

    @OnClick(R.id.manage_add_address)
    public void add() {
        Intent intent = new Intent(ManageAddressActivity.this, EditAddress.class);
        startActivity(intent);
    }

    @Override
    protected void onResume() {
        super.onResume();
        final HttpResultListener<List<AllAddress>> listener;

        listener = new HttpResultListener<List<AllAddress>>() {
            @Override
            public void onSuccess(List<AllAddress> allAddresses) {
                Toast.makeText(ManageAddressActivity.this, "获取收货成功", Toast.LENGTH_SHORT).show();
                mAdapter.setList(allAddresses);
                list = allAddresses;

            }

            @Override
            public void onError(Throwable e) {
                Log.i("lin", "----lin----> 获取收货地址失败 " + e.toString());
            }
        };
        ApiService5.getInstance().allAddress(listener, 1);
    }

    @Override public void onTitleClick(String id) {//这里便是我们重写了的点击事件
        Log.i("lin", "----lin----> onTitleClick 的 id " + id);
    }
}

以上的代码,我们实现了,为recyclerView的一个item中的textview加入的点击事件,以下我要为大家介绍一下,怎样为recyclerView中item加入不同的布局文件,而且怎样为item整个加入点击事件:

/**
 * Created by lin_sir on 2016/7/7.部分商品的展示,的适配器
 */
public class BuyerRecyclerAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {

    public static final int FOOTER_TYPE = 0;//最后一个的类型
    public static final int HAS_IMG_TYPE = 1;//有图片的类型

    private List<FamousPageModel> dataList;

    private ProgressBar mProgress;
    private TextView mNoMore;

    public BuyerRecyclerAdapter() {
        dataList = new ArrayList<>();
    }

    public void addData(List<FamousPageModel> list) {
        dataList.addAll(list);
        notifyDataSetChanged();
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        if (viewType == FOOTER_TYPE) {
            return new FooterItemViewHolder(LayoutInflater.from(parent.getContext()).inflate(R.layout.item_footer, parent, false));
        } else {
            return new BuyerItemViewHolder(LayoutInflater.from(parent.getContext()).inflate(R.layout.item_buyer, parent, false));
        }

    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
        int type = getItemViewType(position);
        if (type == FOOTER_TYPE) {
            bindFooterView((FooterItemViewHolder) holder);
        } else {
            bindView((BuyerItemViewHolder) holder, dataList.get(position));
        }
    }

    @Override
    public int getItemViewType(int position) {
        if (position + 1 == getItemCount()) {
            return FOOTER_TYPE;
        } else {
            FamousPageModel news = dataList.get(position);
            return HAS_IMG_TYPE;
        }
    }

    private void bindView(BuyerItemViewHolder holder, FamousPageModel data) {

        String productName = data.getProductName();
        String[] products = productName.split(" ");
        if (products.length != 1) {
            productName = products[1] + " 等商品";
        }

        String count = data.getNum();
        int sum = 0;
        String[] counts = count.split(" ");
        if (counts.length == 2) {
            sum = Integer.parseInt(counts[0]) + Integer.parseInt(counts[1]);
            count = String.valueOf(sum);
        }

        if (counts.length == 3) {
            sum = Integer.parseInt(counts[0]) + Integer.parseInt(counts[1]) + Integer.parseInt(counts[2]);
            count = String.valueOf(sum);
        }

        holder.count.setText(count);
        holder.goods_name.setText(productName);
        holder.price.setText(data.getTotal());
        if (data.getUser() != null) {
            holder.user_name.setText(data.getUser().getName());

        }

        String imgUrl = data.getPicture();

        if (imgUrl != null) {
            ImageUtil.requestImg(BaseApplication.get().getAppContext(), imgUrl, holder.img);
        }

//        Picasso.with(BaseApplication.get().getAppContext()).load(data.getPicture()).into(holder.img);
    }

    @Override
    public int getItemCount() {
        return dataList == null ? 0 : dataList.size() + 1;
    }

    public static class BuyerItemViewHolder extends RecyclerView.ViewHolder {

        private ImageView img;
        private TextView price;
        private TextView goods_name;
        private TextView count;
        private TextView user_name;

        public BuyerItemViewHolder(View itemView) {

            super(itemView);

            img = (ImageView) itemView.findViewById(R.id.iv_user_item_buyer2);
            price = (TextView) itemView.findViewById(R.id.tv_product_price);
            goods_name = (TextView) itemView.findViewById(R.id.tv_product_name);
            count = (TextView) itemView.findViewById(R.id.tv_product_number);
            user_name = (TextView) itemView.findViewById(R.id.tv_user_name);

        }
    }

    /**
     * 刷新列表
     */
    public void refreshList(List<FamousPageModel> list) {
        dataList.clear();
        dataList.addAll(list);
        notifyDataSetChanged();
    }

    /**
     * 载入很多其它
     */
    public void loadMoreList(List<FamousPageModel> list) {
        dataList.addAll(list);
        notifyDataSetChanged();
    }

    /**
     * 显示没有很多其它
     */
    public void showNoMore() {
        if (getItemCount() > 0) {
            if (mProgress != null && mNoMore != null) {
                mNoMore.setVisibility(View.VISIBLE);
                mProgress.setVisibility(View.GONE);
            }
        }
    }

    /**
     * 显示载入很多其它
     */
    public void showLoadMore() {
        if (mProgress != null && mNoMore != null) {
            mProgress.setVisibility(View.VISIBLE);
            mNoMore.setVisibility(View.GONE);
        }
    }

    private void bindFooterView(FooterItemViewHolder viewHolder) {
        mProgress = viewHolder.mProgress;
        mNoMore = viewHolder.tvNoMore;
    }

    public static class FooterItemViewHolder extends RecyclerView.ViewHolder {

        private ProgressBar mProgress;
        private TextView tvNoMore;

        public FooterItemViewHolder(View itemView) {
            super(itemView);
            mProgress = (ProgressBar) itemView.findViewById(R.id.pb_footer_load_more);
            tvNoMore = (TextView) itemView.findViewById(R.id.tv_footer_no_more);
        }
    }

    /**
     * 获取点击的 item,假设是最后一个,则返回 null
     */
    public FamousPageModel getClickItem(int position) {
        if (position < dataList.size()) {
            return dataList.get(position);
        } else {
            return null;
        }
    }

}

在这里我们首先重写了getItemViewType。这里面我眼下仅仅写了两种类型。那就是带有图片的类型。和不带有图片的类型,而且我让不带图片的类型出如今了最后面,当然大家能够任意的设置。我们仅仅须要依据它们的特征为他们分好类,然后依据不同的类型载入不同的布局文件就可以。

/**
 * Created by lin_sir on 2016/7/7. buyer界面
 */
public class FragmentBuyer extends Fragment implements SwipeRefreshLayout.OnRefreshListener {

    private static int CURRENT_PAGE = 1;                            //获取须要请求的页号
    private RecyclerView recyclerView;                              //recyclerView
    private BuyerRecyclerAdapter madapter;                          //适配器
    private List<FamousPageModel> mlist;                            //一个装载数据的集合
    private LinearLayoutManager linearLayoutManager;                //linearLayoutManger
    private HttpResultListener<List<FamousPageModel>> listener;     //数据请求的回调接口
    private SwipeRefreshLayout refreshLayout;                       //下拉刷新控件
    private boolean isLoadMore;                                     //是否载入很多其它

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_buyer, container, false);
        initviews(view);
        initListener();
        refreshData();
        ButterKnife.bind(this, view);
        return view;
    }

    private void initListener() {
        listener = new HttpResultListener<List<FamousPageModel>>() {
            @Override
            public void onSuccess(List<FamousPageModel> list) {
                Log.i("lin", "----lin---->  refresh  success");
                refreshLayout.setRefreshing(false);
                if (isLoadMore) {
                    mlist = list;
                    madapter.loadMoreList(mlist);
                    madapter.notifyDataSetChanged();
                    madapter.showNoMore();
                } else {
                    mlist = list;
                    madapter.refreshList(list);
                    madapter.notifyDataSetChanged();
                }
            }

            @Override
            public void onError(Throwable e) {
                Log.i("lin", "----lin---->   refresh  error  " + e.toString());
                refreshLayout.setRefreshing(false);
                madapter.showNoMore();
            }
        };
    }

    private void initviews(View view) {

        mlist = new ArrayList<>();

        refreshLayout = (SwipeRefreshLayout) view.findViewById(R.id.refresh_news);
        recyclerView = (RecyclerView) view.findViewById(R.id.rv_buyer);

        refreshLayout.setColorSchemeResources(R.color.blue_500, R.color.purple_500, R.color.green_500);
        refreshLayout.setOnRefreshListener(this);
        linearLayoutManager = new LinearLayoutManager(getActivity());
        linearLayoutManager.setOrientation(OrientationHelper.VERTICAL);

        madapter = new BuyerRecyclerAdapter();
        madapter.addData(mlist);

        recyclerView.setLayoutManager(linearLayoutManager);
        recyclerView.setAdapter(madapter);
        recyclerView.addOnScrollListener(new OnRecyclerScrollListener());
        recyclerView.addOnItemTouchListener(new RecyclerItemClickListener(getActivity(), onItemClickListener));
    }

    /**
     * 刷新时,默认请求第一页的数据
     */
    private void refreshData() {
        refreshLayout.setRefreshing(true);
        isLoadMore = false;
        CURRENT_PAGE = 1;
        requestData(1);
    }

    /**
     * 载入很多其它
     */
    private void loadMoreData() {
        refreshLayout.setRefreshing(false);// 载入很多其它与刷新不能同一时候存在
        isLoadMore = true;
        requestData(++CURRENT_PAGE);
    }

    public class OnRecyclerScrollListener extends RecyclerView.OnScrollListener {

        int lastVisibleItem = 0;

        @Override
        public void onScrollStateChanged(RecyclerView recyclerView, int newState) {
            super.onScrollStateChanged(recyclerView, newState);

            if (madapter != null && newState == RecyclerView.SCROLL_STATE_IDLE && lastVisibleItem + 1 == madapter.getItemCount()) {
                loadMoreData();
            }
        }

        @Override
        public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
            super.onScrolled(recyclerView, dx, dy);
            lastVisibleItem = linearLayoutManager.findLastVisibleItemPosition();
        }
    }

    /**
     * 请求数据
     */
    private void requestData(int page) {
        madapter.showLoadMore();
        ApiService2.getInstance().famous(listener, page);
    }

    @Override
    public void onRefresh() {
        refreshData();
    }

    private RecyclerItemClickListener.OnItemClickListener onItemClickListener = new RecyclerItemClickListener.OnItemClickListener() {//这里我们便实现了点击事件
        @Override
        public void onItemClick(View view, int position) {
            Toast.makeText(getActivity(), "您点击的是:   " + position, Toast.LENGTH_SHORT).show();

        }
    };

    @OnClick(R.id.release_distance_buyer)
    public void release() {
        Intent intent = new Intent(getActivity(), ReleaseOrderActivity.class);
        startActivity(intent);

    }

    @Override public void onDestroy() {
        super.onDestroy();
    }

}

以上便是依据我近期做的项目粗略的整理了一下。recyclerview的简单使用方法。在这里我也用到了,下拉刷新,上拉载入很多其它,等等等等的方法,写这些也是为了让自己在以后遇到相同的需求的时候,能够很快的写出,假设大家也有这些小问题的能够和我一起交流。

原文地址:https://www.cnblogs.com/llguanli/p/8451985.html

时间: 2024-10-08 02:12:13

RecyclerView的点击事件的相关文章

揭开RecyclerView的神秘面纱(二):处理RecyclerView的点击事件

前言 上一篇文章揭开RecyclerView的神秘面纱(一):RecyclerView的基本使用中,主要讲述了RecyclerView的基本使用方法,不同的布局管理器而造成的多样化展示方式,展示了数据之后,一般都会与用户进行交互,因此我们需要处理用户的点击事件.在ListView和GridView提供了onItemClickListener这个监听器,然而我们查找RecyclerView的API却没有类似的监听器,因此我们需要自己手动处理它的点击事件. 以下提供两种方法来实现处理Recycler

RecyclerView的点击事件添加-------接口回调的形式添加

1 package com.example.recyclerviewdemo; 2 3 import android.support.v7.widget.RecyclerView; 4 import android.util.Log; 5 import android.view.LayoutInflater; 6 import android.view.View; 7 import android.view.ViewGroup; 8 import android.widget.TextView;

android v7兼容包RecyclerView的使用(四)——点击事件的不同方式处理

前三篇文章 android v7兼容包RecyclerView的使用(三)--布局管理器的使用 android v7兼容包RecyclerView的使用(二) android v7兼容包RecyclerView的使用(一) 介绍了RecyclerView的使用以及常见的相关类和布局管理器的灵活之处.写了这么多篇,还没涉及到用户交互,那么怎么处理点击事件呢. 在RecyclerView中你会惊奇的发现,该类中并没有OnItemClickListener监听器监听我们的单击事件,也没有OnItemL

Recycleview点击事件监听器(转自:http://www.jianshu.com/p/f2e0463e5aef)

package com.taven.uav.view; import android.content.Context;import android.support.v7.widget.RecyclerView;import android.view.GestureDetector;import android.view.MotionEvent;import android.view.View; /** * @Author Weizh * @Date 2017/11/7 * @Descriptor

手机商城第五天,用recyclerview设置秒杀区域的布局,以及为该recyclerview设置item级的点击事件

代码已经托管到码云上,有兴趣的小伙伴可以下载看看: https://git.oschina.net/joy_yuan/ShoppingMall 之前可能忘记了web端,也就是这个APP里面内容的来源.其实就是一个简单的Tomcat里的小项目,目的是给这个APP提供一个json字符串,以及字符串里对应的内容,如图片,文字,这个web项目会放到下面. 这次的效果图如下,设置秒杀的布局以及给秒杀的每个item设置点击事件 这个秒杀布局,用的是recyclerview的横向布局. 一.在homeFrag

从源码角度入手实现RecyclerView的Item点击事件

转载请注明出处:http://www.cnblogs.com/cnwutianhao/p/6758373.html RecyclerView 作为 ListView 和 GridView 的替代产物,相信在Android界已广为流传. RecyclerView 本是不会有类似 ListView 的那种点击事件,但是知道和会用又是两种情况,跟随我一起从源码角度分析,RecyclerView 点击事件. 首先看一下 Google 对 ListView 家族谱的介绍: 可以看出 ListView 归根

从头开始学 RecyclerView(二) 添加item点击事件

不管了,先来张图 偶吐了个槽 item点击事件必须手动添加,默认并没有一个显式的API接口可供调用. 为了节约学习时间,网上找了篇很不错的文章.这里基本就复制了. 添加点击事件 RecyclerView#addOnItemTouchListener 分析 查看RecyclerView源码可以看到,RecyclerView预留了一个Item的触摸事件方法: public void addOnItemTouchListener(OnItemTouchListener listener) { mOnI

精通RecyclerView:打造ListView、GridView、瀑布流;学会添加分割线、 添加删除动画 、Item点击事件

转载请注明出处:http://blog.csdn.net/linglongxin24/article/details/53126706 本文出自[DylanAndroid的博客] 精通RecyclerView:打造ListView.GridView.瀑布流:学会添加分割线. 添加删除动画 .Item点击事件 在上一篇Android用RecyclerView练手仿美团分类界面写了RecyclerView的基本用法, 今天想想,在这里重新学习一下RecyclerView的完整用法.包括如何打造一个普

RecyclerView点击事件

package zhangphil.myrecyclerview; import android.support.v7.app.ActionBarActivity; import android.support.v7.widget.LinearLayoutManager; import android.support.v7.widget.RecyclerView; import android.support.v7.widget.RecyclerView.ViewHolder; import a