Android中Recyclerview使用3----添加条目得到点击事件和长按事件

时间:2023-01-19 22:32:21

效果图:

Android中Recyclerview使用3----添加条目得到点击事件和长按事件

核心代码:


Activity中:


/**
     * 实现条目的点击事件和长按事件
     */
    private void myOnClick() {
        honmeAdapter.setOnItemClickLitener(new HomeAdapter.OnItemClickLitener() {
            @Override
            public void onItemClick(View view, int position) {
                Toast.makeText(MainActivity.this, "click:" + position, Toast.LENGTH_SHORT).show();


            }


            @Override
            public void onItemLongClick(View view, int position) {
                Toast.makeText(MainActivity.this, "Long:" + position, Toast.LENGTH_SHORT).show();


            }
        });


    }


适配器中:


//  定义接口写条目点击事件和长按事件


    public interface OnItemClickLitener {
        void onItemClick(View view, int position);


        void onItemLongClick(View view, int position);
    }


    private OnItemClickLitener mOnItemClickLitener;


    public void setOnItemClickLitener(OnItemClickLitener mOnItemClickLitener) {
        this.mOnItemClickLitener = mOnItemClickLitener;
    }
    @Override
    public void onBindViewHolder(final MyViewHolder holder, int position) {
        holder.tv.setText(list.get(position));
        // 如果设置了回调,则设置点击事件
//        点击事件和长按事件
        if (mOnItemClickLitener != null) {
            holder.itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
//                  从布局中获取条目位置
                    int pos = holder.getLayoutPosition();
                    mOnItemClickLitener.onItemClick(holder.itemView, pos);
                }
            });


            holder.itemView.setOnLongClickListener(new View.OnLongClickListener() {
                @Override
                public boolean onLongClick(View v) {
                    //                  从布局中获取条目位置
                    int pos = holder.getLayoutPosition();
                    mOnItemClickLitener.onItemLongClick(holder.itemView, pos);
                    return false;
                }
            });
        }
    }

具体代码如下:

Activity中:

<span style="font-size:18px;">/**
* 添加条目的点击事件,和长按事件
*/
public class MainActivity extends Activity {
private RecyclerView mRecyclerView;
HomeAdapter honmeAdapter = null;


@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// 初始化RecyclerView
mRecyclerView = (RecyclerView) findViewById(R.id.id_recyclerview);
// listView纵向滑动样子,纵向滑动
LinearLayoutManager linearLayoutManager = new LinearLayoutManager(this);
mRecyclerView.setLayoutManager(linearLayoutManager);
// 获取数据,向适配器传数据,绑定适配器
ArrayList<String> datas = initData();
honmeAdapter = new HomeAdapter(MainActivity.this, datas);
mRecyclerView.setAdapter(honmeAdapter);
myOnClick();


}

/**
* 实现条目的点击事件和长按事件
*/
private void myOnClick() {
honmeAdapter.setOnItemClickLitener(new HomeAdapter.OnItemClickLitener() {
@Override
public void onItemClick(View view, int position) {
Toast.makeText(MainActivity.this, "click:" + position, Toast.LENGTH_SHORT).show();

}

@Override
public void onItemLongClick(View view, int position) {
Toast.makeText(MainActivity.this, "Long:" + position, Toast.LENGTH_SHORT).show();

}
});

}


/**
* 编写一套假数据
*/
protected ArrayList<String> initData() {
ArrayList<String> mDatas = new ArrayList<String>();
for (int i = 0; i < 100; i++) {
mDatas.add("我是条目" + i);
}
return mDatas;
}


}</span>
Recyclerview布局文件:
activity_main.xml

<span style="font-size:18px;"><?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
>
<android.support.v7.widget.RecyclerView
android:id="@+id/id_recyclerview"
android:layout_width="match_parent"
android:layout_height="match_parent" >

</android.support.v7.widget.RecyclerView>

</LinearLayout></span>

条目布局文件:
item_home.xml

<span style="font-size:18px;"><?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_margin="5dp"
android:background="@drawable/item_bg"
android:layout_height="wrap_content" >

<TextView
android:id="@+id/id_num"
android:layout_width="match_parent"
android:layout_height="50dp"
android:gravity="center"
android:text="1" />
</FrameLayout></span>

条目布局文件的背景选择器:

drawable----item_bg.xml

<span style="font-size:18px;"><?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android" >
<item android:state_pressed="true" android:drawable="@color/color_item_press"></item>
<item android:drawable="@color/color_item_normal"></item>
</selector></span>

适配器:

<span style="font-size:18px;">class HomeAdapter extends RecyclerView.Adapter<HomeAdapter.MyViewHolder> {
private Context context;
private ArrayList<String> list;

public HomeAdapter(Context context, ArrayList<String> list) {
this.context = context;
this.list = list;

}

// 定义接口写条目点击事件和长按事件

public interface OnItemClickLitener {
void onItemClick(View view, int position);

void onItemLongClick(View view, int position);
}

private OnItemClickLitener mOnItemClickLitener;

public void setOnItemClickLitener(OnItemClickLitener mOnItemClickLitener) {
this.mOnItemClickLitener = mOnItemClickLitener;
}


@Override
public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
MyViewHolder holder = new MyViewHolder(LayoutInflater.from(
context).inflate(R.layout.item_home, parent,
false));
return holder;
}

@Override
public void onBindViewHolder(final MyViewHolder holder, int position) {
holder.tv.setText(list.get(position));
// 如果设置了回调,则设置点击事件
// 点击事件和长按事件
if (mOnItemClickLitener != null) {
holder.itemView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// 从布局中获取条目位置
int pos = holder.getLayoutPosition();
mOnItemClickLitener.onItemClick(holder.itemView, pos);
}
});

holder.itemView.setOnLongClickListener(new View.OnLongClickListener() {
@Override
public boolean onLongClick(View v) {
// 从布局中获取条目位置
int pos = holder.getLayoutPosition();
mOnItemClickLitener.onItemLongClick(holder.itemView, pos);
return false;
}
});
}
}


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


/**
* ViewHolder的类,用于缓存控件
*/
class MyViewHolder extends RecyclerView.ViewHolder {

TextView tv;

public MyViewHolder(View view) {
super(view);
tv = (TextView) view.findViewById(R.id.id_num);
}


}
}</span>


源码下载:
MyRecyclerView----recyclerview3

http://download.csdn.net/detail/zhaihaohao1/9572945

参考博客:

http://blog.csdn.net/lmj623565791/article/details/45059587

参考视频:
http://www.imooc.com/learn/424