好的,我知道,可能有人已经问过这个问题了.我已经在this link上找到了针对我的问题的可能解决方案.但是,由于未知原因,该解决方案对我不起作用.
我想在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(); } }); }}@OverrIDepublic 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);}@OverrIDepublic 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());}@OverrIDepublic int getItemCount() { return mainInfo.size();}}
这是我的cardVIEw的布局文件:
<androID.support.v7.Widget.CardVIEwxmlns: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>
解决方法:
问题解决了!这是工作代码:
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上单击-我会看到它的位置!
积分:link
总结以上是内存溢出为你收集整理的android-单击项目RecyclerView时不显示吐司全部内容,希望文章能够帮你解决android-单击项目RecyclerView时不显示吐司所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)