gpt4 book ai didi

android - 回收站 View 中的行项目不响应点击事件

转载 作者:行者123 更新时间:2023-11-29 00:07:42 27 4
gpt4 key购买 nike

以下是我的行项目的布局:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:background="@android:color/white"
android:orientation="horizontal">

<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:clickable="true"
android:background="?android:attr/selectableItemBackground"
android:orientation="vertical">


<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<ImageView
android:id="@+id/img_query"
android:layout_width="64dp"
android:layout_height="64dp"
android:layout_gravity="left|top"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:layout_marginTop="2dp"
android:src="@drawable/ic_anoun_red" />

<TextView
android:id="@+id/txt_ques_sub"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginRight="4dp"
android:layout_marginTop="12dp"
android:layout_toRightOf="@+id/img_query"
android:text="Problem in Merge sort"
android:textAppearance="?android:attr/textAppearanceSmall"
android:textColor="@android:color/primary_text_light"
android:textStyle="bold" />



<TextView

android:id="@+id/txt_ques_detail"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginRight="4dp"
android:layout_marginLeft="4dp"
android:layout_marginTop="2dp"
android:layout_below="@+id/img_query"
android:text="@string/sample_query_detail"
android:textAppearance="?android:attr/textAppearanceSmall"
android:textColor="@android:color/primary_text_light" />

<TextView
android:id="@+id/txt_asked_by"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_below="@+id/txt_ques_detail"
android:layout_marginRight="4dp"
android:layout_marginTop="2dp"
android:text="Tabish Butt"
android:textAppearance="?android:attr/textAppearanceSmall"
android:textColor="@color/lightorange" />

<TextView
android:id="@+id/txt_ques_time"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_below="@+id/txt_asked_by"
android:layout_marginRight="4dp"
android:text="07:15 am"
android:textAppearance="?android:attr/textAppearanceSmall"
android:textColor="@android:color/secondary_text_dark" />

<TextView
android:id="@+id/txt_ques_date"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_below="@+id/txt_ques_time"
android:layout_marginRight="4dp"
android:text="25 SEP 2015"
android:textAppearance="?android:attr/textAppearanceSmall"
android:textColor="@android:color/secondary_text_dark" />

<TextView
android:id="@+id/txt_replies"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="@+id/txt_ques_date"
android:layout_alignParentLeft="true"
android:layout_marginLeft="4dp"
android:text="@string/default_num_answers"
android:textAppearance="?android:attr/textAppearanceSmall"
android:textColor="@color/primary_dark_material_light" />


<View
android:id="@+id/divider"
android:layout_width="match_parent"
android:layout_height="0.5dp"
android:layout_below="@+id/txt_replies"
android:alpha="0.12"
android:background="@android:color/black" />
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/divider"
android:orientation="vertical">

<Button
android:id="@+id/btn_answer"
style="?android:attr/borderlessButtonStyle"
android:layout_width="wrap_content"
android:layout_height="50dp"
android:layout_gravity="center"
android:drawableLeft="@android:drawable/ic_media_play"
android:text="ANSWER"
android:textColor="@color/primary_dark_material_light" />
</LinearLayout>
</RelativeLayout>
</LinearLayout>

</LinearLayout>

以下是onClick代码:

holder.setClickListener(new MyViewHolder.ClickListener() {
@Override
public void onClick(View v, int position, boolean isLongClick) {
if(isLongClick)
{
/* TASK TO PERFORM AT LONG CLICKS */
Toast.makeText(context, "Long click acheived", Toast.LENGTH_LONG).show();
}
else
{
/* TASK TO PERFORM AT CLICKS */
Toast.makeText(context, "Click Event", Toast.LENGTH_LONG).show();
}
}
});

现在,当我运行它时,一切正常,除了单击行项目不会产生任何响应(在本例中为 toast )

任何帮助将不胜感激。需要帮助!

MyViewHolder类中点击事件的处理方法如下:

static class MyViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener,View.OnLongClickListener {

//Following represent grid_layout item
TextView txtFileName;
TextView txtFileIssueTime;
Button btnAnswer;
private ClickListener clickListener;
public MyViewHolder(View itemView) {
super(itemView);

//VIEWS IN LAYOUT BEING INITIALIZED

btnAnswer = (Button) itemView.findViewById(R.id.btn_answer);
itemView.setOnClickListener(this);
itemView.setOnLongClickListener(this);
}

/* Interface for handling clicks - both normal and long ones. */
public interface ClickListener {

/**
* Called when the view is clicked.
*
* @param v view that is clicked
* @param position of the clicked item
* @param isLongClick true if long click, false otherwise
*/
public void onClick(View v, int position, boolean isLongClick);

}

/* Setter for listener. */
public void setClickListener(ClickListener clickListener) {
this.clickListener = clickListener;
}

@Override
public void onClick(View v) {

// If not long clicked, pass last variable as false.
clickListener.onClick(v, getAdapterPosition(), false);
}

@Override
public boolean onLongClick(View v) {

// If long clicked, passed last variable as true.
clickListener.onClick(v, getAdapterPosition(), true);
return true;
}

}

最佳答案

尝试将 clickable 移动到父 LinearLayout 中,而不是嵌套的 LinearLayout

编辑:

实际上,你的布局应该是

<FrameLayout
android:background:"@color/white"
android:foreground:"?selectableItemBackground">

<RelativeLayout />

</FrameLayout>

关于android - 回收站 View 中的行项目不响应点击事件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32660000/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com