豆花

与君共勉

RecyclerView 点击事件 ,条目的排列样式

RecyclerView 条目的点击事件 要自定义,在RecyclerViewAdapter 中定义接口提供回调,这里自定义了条目的点击事件和长按事件 如下
holder.mTextView.setText(mData.get(position));
holder.mTextView.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
        if (mItemClickListener!=null){
            mItemClickListener.onItemClick(position);
        }
    }
});
holder.mTextView.setOnLongClickListener(new View.OnLongClickListener() {
    @Override
    public boolean onLongClick(View v) {
        if (mItemClickListener!=null){
            mItemClickListener.onItemLongClick(position);
        }
        return false;
    }
});
Activity 中回调
recyclerViewAdapter.setItemClickListener(new RecyclerViewAdapter.OnItemClickListener() {
    @Override
    public void onItemClick(int position) {
        Toast.makeText(MainActivity.this, "点击OK--->" + position, Toast.LENGTH_SHORT).show();
        if (position == 1) {
            notification();
        }
    }

    @Override
    public void onItemLongClick(int position) {
        Toast.makeText(MainActivity.this, "长按--点击OK--->" + position, Toast.LENGTH_SHORT).show();

    }
});
设置条目的排列样式
  //设置条目的排列样式
        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(this);
        linearLayoutManager.setOrientation(LinearLayoutManager.VERTICAL);
//        StaggeredGridLayoutManager staggeredGridLayoutManager = new StaggeredGridLayoutManager(4, StaggeredGridLayoutManager.VERTICAL);
        mRecyclerView.setLayoutManager(linearLayoutManager);

import android.content.Context;
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;


 */
public class RecyclerViewAdapter extends RecyclerView.Adapter<RecyclerViewAdapter.mRecyclerViewViewHolder> {
    Context mContext;
    List<String> mData;
    private OnItemClickListener mItemClickListener;
    public RecyclerViewAdapter(Context context, List<String> mList) {
        this.mContext = context;
        this.mData = mList;
        notifyDataSetChanged();
    }



    public interface OnItemClickListener{
        void onItemClick(int position);
        void onItemLongClick(int position);
    }
    public void setItemClickListener(OnItemClickListener itemClickListener) {
      this. mItemClickListener = itemClickListener;
    }
    @Override
    public mRecyclerViewViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(mContext).inflate(R.layout.recyclerview_item, parent, false);
        return new mRecyclerViewViewHolder(view);
    }

    @Override
    public void onBindViewHolder(mRecyclerViewViewHolder holder, final int position) {

        holder.mTextView.setText(mData.get(position));
        holder.mTextView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (mItemClickListener!=null){
                    mItemClickListener.onItemClick(position);
                }
            }
        });
        holder.mTextView.setOnLongClickListener(new View.OnLongClickListener() {
            @Override
            public boolean onLongClick(View v) {
                if (mItemClickListener!=null){
                    mItemClickListener.onItemLongClick(position);
                }
                return false;
            }
        });
    }


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

    class mRecyclerViewViewHolder extends RecyclerView.ViewHolder {

        private final TextView mTextView;

        public mRecyclerViewViewHolder(View itemView) {
            super(itemView);
            mTextView = (TextView) itemView.findViewById(R.id.r_tv);
        }
    }
}
阅读更多
文章标签: RecyclerView
个人分类: RecyclerView
上一篇notification
下一篇Android pay 指纹
想对作者说点什么? 我来说一句

没有更多推荐了,返回首页

关闭
关闭