您的位置:首页 > 移动开发 > Android开发

Android之RecyclerView简单使用(二)

2016-02-14 16:04 633 查看
新滴一年开始啦,给亲们拜个晚年,短暂滴假期过去了,我想说:“假期还没开始,就结束了”。废话不多说,继续我们上一篇的内容。

在上一篇博客中,有小伙伴提议道:要使用RecyclerView必须要把依赖包和v7包同时导入才能使用,上篇博客忘了说v7包的导入,其实v7包和那个依赖包添加方式一样,输入:v7进行搜索,结果如图:





这样就把V7包添加到项目里面了。上篇博客中有个地方写的不规范,这里纠正一下啦:





那个findViewById写到这里面,不要在onCreateViewHolder方法里面写了(虽说那样写也不错,但不提倡)。

上次说过RecyclerView点击事件和长按事件需要自己写。这次我们来学习写点击事件和长按事件。这次我使用的回调,在RecyclerViewAdapter里面,代码如下:

[code]
//回调
public interface OnItemClickListener {
        //点击事件
        void OnItemClick(int position);
        //长按事件
        boolean OnItemLongClick(int position);
}
private OnItemClickListener mOnItemClickListener;
//设置回调   
public void setOnItemClickListener(OnItemClickListener mOnItemClickListener) {
        this.mOnItemClickListener = mOnItemClickListener;
}


这样回调写好了,下面我们需要在onBindViewHolder里面处理,代码如下:

[code]//设置点击事件
holder.itemView.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
                if(mOnItemClickListener != null) {
                //Returns the position of the ViewHolder in terms of the
                //latest layout pass.
                int pos = holder.getLayoutPosition();
                mOnItemClickListener.OnItemClick(pos);
                }
        }
});
//设置长按事件
holder.itemView.setOnLongClickListener(new View.OnLongClickListener() {
        @Override
        public boolean onLongClick(View v) {
                if (mOnItemClickListener != null) {
                        int pos = holder.getLayoutPosition();
                        return mOnItemClickListener.OnItemLongClick(pos);
                }
                return false;
        }
});


其中getLayoutPosition()是获取ViewHolder通过的最新位置,源码如下:

[code]/**
* Returns the position of the ViewHolder in terms of the latest *layout pass.
* <p>
* This position is mostly used by RecyclerView components to be *consistent while RecyclerView lazily processes adapter updates.
* <p>
* For performance and animation reasons, RecyclerView batches all  *adapter updates until the next layout pass. This may cause *mismatches between the Adapter position of the item and the position *it had in the latest layout calculations.
* <p>
*LayoutManagers should always call this method while doing *calculations based on item positions. All methods in 
* {@link RecyclerView.LayoutManager},{@link RecyclerView.State},         *{@link RecyclerView.Recycler} that receive a position expect it to *be the layout position of the item.
* <p>
* If LayoutManager needs to call an external method that requires the *adapter position of the item, it can use 
* {@link #getAdapterPosition()} or{@linkRecyclerView
* .Recycler#convertPreLayoutPositionToPostLayout(int)}.
* 
* @return Returns the adapter position of the ViewHolder in the *latest layout pass.
* @see #getAdapterPosition()
*/
public final int getLayoutPosition() {
        return mPreLayoutPosition == NO_POSITION ? mPosition : mPreLayoutPosition;
}


这样我们只需要在RecyclerActivity里面给adapter设置事件即可:

[code]adapter.setOnItemClickListener(new RecyclerViewAdapter.OnItemClickListener() {
        @Override
        public void OnItemClick(int position) {
                Toast.makeText(RecyclerActivity.this, "你点击了第" + position + "个图片", 
                        Toast.LENGTH_SHORT).show();
        }

        @Override
        public boolean OnItemLongClick(int position) {
                Toast.makeText(RecyclerActivity.this, "你长按了第" + position + "个图片",   
                        Toast.LENGTH_SHORT).show();
                return true;
        //return false;
        }
});


这样子就实现RecyclerView点击事件和长按事件。关于长按事件return值,如果为ture,则不允许其他处理,表示消费掉了长按事件,如果为false,则允许其他处理事件,表示没有消费掉长按事件。但是呢,点击有个时间限制,如果超过了这个时间,就会取消点击事件。是不是感觉晕晕滴呀,看一下图,就明白了。

当为true时:




当为false时,不超时:




当为false时,超时:




看了图是不是理解了呢?这次就说这么多,有不对的地方记得给我留言~~

喜欢我滴,顶一下呗~~

关于eclpise需要导入v7包:v7下载地址链接
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: