单击项目时没有显示吐司 RecyclerView

No toast shown when item clicked RecyclerView

好的,我知道,可能已经有人问过这个问题了。我已经在 上为我的问题找到了可能的解决方案。但是,由于未知原因,该解决方案对我不起作用。 当在 RecyclerView 中单击项目(CardView)时,我想显示 Toast。这个 Toast 必须告诉我点击的项目的位置。 当我点击 CardView 时,我看到波纹 - 这意味着卡片被点击 - 但不幸的是,我没有看到任何吐司。有人可以向我解释为什么吗?提前致谢。

这是我的适配器:

public class MainRecyclerViewAdapter extends RecyclerView.Adapter<MainRecyclerViewAdapter.MyMainViewHolder> {


private List<Information> mainInfo;
MyDatabase myDatabase;


public MainRecyclerViewAdapter(List<Information> mainInfo) {
    this.mainInfo = mainInfo;
}

public static class MyMainViewHolder extends RecyclerView.ViewHolder {
    TextView titleTextView, notesTextView, dateTextView, timeTextView;
    ImageView imageView;
    LinearLayout linearLayout;

    public MyMainViewHolder(View itemView) {
        super(itemView);
        linearLayout = (LinearLayout) itemView.findViewById(R.id.mainLayout);
        imageView = (ImageView) itemView.findViewById(R.id.imgIcon);
        titleTextView = (TextView) itemView.findViewById(R.id.title_TextView);
        notesTextView = (TextView) itemView.findViewById(R.id.notes_TextView);
        dateTextView = (TextView) itemView.findViewById(R.id.date_TextView);
        timeTextView = (TextView) itemView.findViewById(R.id.time_TextView);

        itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Toast.makeText(v.getContext(), "Position: " + Integer.toString(getAdapterPosition()), Toast.LENGTH_LONG).show();
            }
        });

    }

}



@Override
public MyMainViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
    View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.main_single_raw, parent, false);
    MyMainViewHolder holder = new MyMainViewHolder(v);
    return holder;
}

public void delete(int position){
    mainInfo.remove(position);
    notifyItemRemoved(position);
}

@Override
public void onBindViewHolder(final MyMainViewHolder holder, final int position) {
    holder.imageView.setImageResource(mainInfo.get(position).getICON_ID());
    holder.titleTextView.setText(mainInfo.get(position).getTITLE());
    holder.notesTextView.setText(mainInfo.get(position).getNOTES());
    holder.dateTextView.setText(mainInfo.get(position).getDATE());
    holder.timeTextView.setText(mainInfo.get(position).getTIME());


}




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

这是我的 cardView 的布局文件:

<android.support.v7.widget.CardView
xmlns:card_view="http://schemas.android.com/apk/res-auto"
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/card_view1"
android:clickable="true"
android:layout_width="match_parent"
android:layout_height="match_parent"
card_view:cardCornerRadius="3dp"
android:layout_margin="5dp">

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/mainLayout"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_marginTop="0.5dp"
    android:clickable="true"
    android:background="?android:selectableItemBackground"
    android:orientation="horizontal"
    android:weightSum="10">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical">


        <RelativeLayout
            android:layout_width="match_parent"
            android:layout_height="fill_parent"
            android:layout_marginLeft="16dp"
            android:layout_weight="8.5">

            <TextView
                android:id="@+id/title_TextView"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginBottom="5dp"
                android:layout_marginLeft="0dp"
                android:layout_marginRight="8dp"
                android:layout_marginTop="5dp"
                android:text="Title"
                android:fontFamily="sans-serif-light"
                android:textColor="#F57C00"
                android:textSize="25sp"
                />



        </RelativeLayout>

        <View
            android:layout_width="fill_parent"
            android:id="@+id/divider1"
            android:layout_height="0.1dp"
            android:layout_below="@+id/title_TextView"
            android:background="@android:color/darker_gray"/>

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="horizontal">

            <RelativeLayout
                android:layout_width="match_parent"
                android:layout_height="fill_parent"
                android:layout_marginLeft="16dp"
                android:layout_weight="8.5">

                <ImageView
                    android:id="@+id/imgIcon"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_alignParentLeft="true"
                    android:layout_centerHorizontal="true"
                    android:layout_centerVertical="true"
                    android:src="@mipmap/ic_alarm_check_grey600_48dp" />


            </RelativeLayout>


            <LinearLayout
                android:id="@+id/linear"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_weight="1.5"
                android:orientation="vertical">


                <TextView
                    android:id="@+id/notes_TextView"
                    android:paddingTop="5dp"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:layout_marginBottom="5dp"
                    android:layout_marginLeft="16dp"
                    android:layout_marginRight="8dp"
                    android:text="notes"
                    android:textColor="#ff565656"
                    android:textSize="12sp" />

                <TextView
                    android:id="@+id/date_TextView"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_marginBottom="5dp"
                    android:layout_marginLeft="16dp"
                    android:layout_marginRight="8dp"
                    android:text="date"
                    android:textColor="#ff565656"
                    android:textSize="12sp" />

                <TextView
                    android:id="@+id/time_TextView"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_marginBottom="5dp"
                    android:layout_marginLeft="16dp"
                    android:layout_marginRight="8dp"
                    android:text="time"
                    android:textColor="#ff565656"
                    android:textSize="12sp" />


            </LinearLayout>


        </LinearLayout>


    </LinearLayout>


</LinearLayout>

在您的 view.xml 中添加以下行 android:clickable=true

从第一个 LinearLayout 中删除以下行 android:clickable="true"

问题解决了!这是工作代码:

public static class MyMainViewHolder extends RecyclerView.ViewHolder {
    TextView titleTextView, notesTextView, dateTextView, timeTextView;
    ImageView imageView;
    LinearLayout linearLayout;

    public MyMainViewHolder(final View itemView) {
        super(itemView);
        linearLayout = (LinearLayout) itemView.findViewById(R.id.mainLayout);
        imageView = (ImageView) itemView.findViewById(R.id.imgIcon);
        titleTextView = (TextView) itemView.findViewById(R.id.title_TextView);
        notesTextView = (TextView) itemView.findViewById(R.id.notes_TextView);
        dateTextView = (TextView) itemView.findViewById(R.id.date_TextView);
        timeTextView = (TextView) itemView.findViewById(R.id.time_TextView);



        itemView.setClickable(true);
        itemView.setFocusableInTouchMode(true);

        itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                Toast.makeText(itemView.getContext(), "Position: " + Integer.toString(getAdapterPosition()), Toast.LENGTH_LONG).show();
            }
        });

        linearLayout.setOnClickListener(new View.OnClickListener(){

            @Override
            public void onClick(View v) {

                Toast.makeText(itemView.getContext(), "Position: " + Integer.toString(getAdapterPosition()), Toast.LENGTH_LONG).show();
            }

        });

    }

}

所以现在,如果我单击 linearLayout - 所以在整个 cardView 上 - 我会看到它的位置!

学分:

try this,
@Override
public MyMainViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
    View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.main_single_raw, parent, false);
    MyMainViewHolder holder = new MyMainViewHolder(v);
    return holder;
}

public void delete(int position){
    mainInfo.remove(position);
    notifyItemRemoved(position);
}

    @Override
    public void onBindViewHolder(final MyMainViewHolder holder, final int position) {
        holder.imageView.setImageResource(mainInfo.get(position).getICON_ID());
        holder.titleTextView.setText(mainInfo.get(position).getTITLE());
        holder.notesTextView.setText(mainInfo.get(position).getNOTES());
        holder.dateTextView.setText(mainInfo.get(position).getDATE());
        holder.timeTextView.setText(mainInfo.get(position).getTIME());

          holder.titleTextView.setOnClickListener(new    View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                    Toast.makeText(context,""+position,Toast.LENGTH_SHORT).show();

        }
        });

}




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

注意cardview scope clickable是true

   <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="match_parent"
    xmlns:card_view="http://schemas.android.com/apk/res-auto"
    android:layout_weight="1"
    android:gravity="center_vertical|center|center_horizontal"
    android:layout_height="wrap_content">
        <android.support.v7.widget.CardView
            android:id="@+id/card_view"
            android:layout_width="match_parent"
            card_view:contentPadding="10dp"
            android:layout_height="200dp"
            android:layout_gravity="center"
            android:clickable="true"
            android:transitionGroup="false"
            android:layout_margin="10dp">
            <ImageView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:id="@+id/select_photo"
                android:layout_alignParentTop="true"
                android:layout_gravity="center_horizontal"
                android:layout_margin="5dp"
                />

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:id="@+id/select_name"
                android:textSize="25sp"
                android:textStyle="bold"
                android:layout_gravity="bottom|center_horizontal" />
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textAppearance="?android:attr/textAppearanceMedium"
            android:id="@+id/txt_zoom_in"
            />`enter code here`

    </android.support.v7.widget.CardView>
</LinearLayout>