短按不在Recyclerview项目中工作

时间:2015-11-27 08:51:42

标签: android android-recyclerview recycler-adapter

我有一个包含复选框的recyclerview。我已经为该复选框实现了单击侦听器。但我目前的问题是复选框仅在我长按时才有效。 我的代码是。

我的Recyclerview项目。

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="200dp"
    android:background="@color/cardview_light_background"
    android:orientation="vertical">

<RelativeLayout
    android:layout_width="match_parent"
    android:layout_height="200dp">

    <ImageView
        android:id="@+id/PROJECT_image"
        android:layout_width="match_parent"
        android:layout_height="@dimen/list_item_avatar_size"
        android:background="@drawable/mirlogo"
        android:scaleType="fitXY" />
    <RelativeLayout
        android:id="@+id/label"
        android:layout_width="100dp"
        android:layout_height="100dp"
        android:background="@drawable/labelsale"/>

    <CheckBox
        android:id="@+id/PROJECT_fav"
        android:layout_width="30sp"
        android:layout_height="30sp"
        android:background="@drawable/selector"
        android:button="@null"
        android:layout_margin="10dp"
        android:layout_alignParentRight="true"
        android:layout_alignParentEnd="true" />
</RelativeLayout>
</RelativeLayout>

我的适配器

public class HomeDataManager extends RecyclerView.Adapter<HomeDataManager.RecyclerViewHolder> {

    public static class RecyclerViewHolder extends RecyclerView.ViewHolder {

        TextView mProjectName;
        ImageView mImage;
        CheckBox mCheck;


        RecyclerViewHolder(View itemView) {
            super(itemView);
            mProjectName = (TextView) itemView.findViewById(R.id.PROJECT_name);
            mImage = (ImageView) itemView.findViewById(R.id.PROJECT_image);
            mCheck = (CheckBox) itemView.findViewById(R.id.PROJECT_fav);

        }
    }

    @Override
    public RecyclerViewHolder onCreateViewHolder(ViewGroup viewGroup, int i) {
        View v = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.recyclerview_item, viewGroup, false);
        return new RecyclerViewHolder(v);
    }

    @Override
    public void onBindViewHolder(final RecyclerViewHolder viewHolder, int i) {
        // get the single element from the main array

        final HomeProjects projects = HomeProjects.PROJECTS[i];
        // Set the values
        viewHolder.mProjectName.setText(projects.get(HomeProjects.Field.NAME));
        viewHolder.mImage.setImageResource(projects.geti(HomeProjects.Field.IMAGE));
        viewHolder.mCheck.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Snackbar snackbar = Snackbar.make(v, "Item Favorited", Snackbar.LENGTH_SHORT);
                snackbar.show();
            }
        });

    }


    @Override
    public int getItemCount() {

        return HomeProjects.PROJECTS.length;
    }


}

1 个答案:

答案 0 :(得分:2)

删除您的自定义RecyclerClickListener并在RecyclerViewHolder的构造函数中为复选框实现setOnCheckedChangeListener。

itemView.mCheck.setOnCheckedChangeListener(new OnCheckedChangeListener(){
@Override
public void onCheckedChanged(){
//Implement your code here}});

用于处理RecyclerlayoutClicks: 此代码应在RecyclerViewHolder类的构造函数中使用。

itemView.setOnClickListener(new OnClickListener(){
@Override
public void onClick(View v){
//Your Code}});