gpt4 book ai didi

android - 只有 recyclerview 单元格中的图标是可点击的

转载 作者:行者123 更新时间:2023-11-29 19:21:12 25 4
gpt4 key购买 nike

我检查了如何制作可点击的 RecyclerView 的各种示例,并创建了一个示例,其中每一行在左侧都有一个图标,在右侧有两个文本标签相互堆叠,但我只能在单击时触发单击处理程序在图标上。我希望能够单击行中的任意位置。我在这段代码中做错了什么?

<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.RecyclerView android:id="@+id/record_list"
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginLeft="16dp"
android:layout_marginRight="16dp"
android:clickable="true"
app:layoutManager="LinearLayoutManager"
tools:listitem="@layout/list_content"/>

<!-- list content -->
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:clickable="true"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<ImageView
android:id="@+id/icon"
android:layout_width="30dp"
android:layout_height="30dp"
android:src="@drawable/ic_icon"/>
<TextView
android:id="@+id/desc"
style="?android:attr/textAppearanceMedium"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_toEndOf="@id/icon"
android:layout_toRightOf="@id/icon"
android:layout_alignParentEnd="true"
android:layout_alignParentRight="true"
android:padding="0dp"
android:textIsSelectable="true"/>
<TextView
android:id="@+id/user_type"
style="?android:attr/textAppearanceSmall"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_toEndOf="@id/icon"
android:layout_toRightOf="@id/icon"
android:layout_below="@id/desc"
android:layout_alignParentEnd="true"
android:layout_alignParentRight="true"
android:padding="0dp"
android:textIsSelectable="true"
android:textStyle="italic"/>
</RelativeLayout>

public class SimpleItemRecyclerViewAdapter
extends RecyclerView.Adapter<SimpleItemRecyclerViewAdapter.ViewHolder> {

private final List<Record> mValues;

public SimpleItemRecyclerViewAdapter(List<Record> items) {
mValues = items;
}

@Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext())
.inflate(R.layout.list_content, parent, false);
return new ViewHolder(view);
}


@Override
public void onBindViewHolder(final ViewHolder holder, final int position) {
Record record = mValues.get(position);
holder.mIcon.setImageResource(record.iconRes);
holder.mDescText.setText(record.desc);
holder.mDetailText.setText(record.detail);
holder.itemView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Context context = v.getContext();
Intent intent = new Intent(context, DetailActivity.class);
context.startActivity(intent);
}
});
}

public class ViewHolder extends RecyclerView.ViewHolder {
public final ImageView mIcon;
public final TextView mDescText;
public final TextView mDetailText;

public ViewHolder(View view) {
super(view);
mIcon = (ImageView)view.findViewById(R.id.icon);
mDescText = (TextView) view.findViewById(R.id.desc);
mDetailText = (TextView) view.findViewById(R.id.detail);
}
}
}

已更新

当我点击该行时,我看到 logcat 记录了下面的行

I/ViewRootImpl: ViewRoot's Touch Event : ACTION_DOWN
I/ViewRootImpl: ViewRoot's Touch Event : ACTION_UP

所以有些东西正在响应,但它不是 itemView 并且 itemView 没有父级。

最佳答案

尝试处理列表内容的根元素的点击事件。在您的情况下,它将是 RelativeLayout。

您的列表内容如下。

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/rootLay"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:clickable="true">

<ImageView
android:id="@+id/icon"
android:layout_width="30dp"
android:layout_height="30dp"
android:src="@drawable/ic_icon" />

<TextView
android:id="@+id/desc"
style="?android:attr/textAppearanceMedium"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_alignParentEnd="true"
android:layout_alignParentRight="true"
android:layout_toEndOf="@id/icon"
android:layout_toRightOf="@id/icon"
android:padding="0dp"
android:textIsSelectable="true" />

<TextView
android:id="@+id/user_type"
style="?android:attr/textAppearanceSmall"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_alignParentEnd="true"
android:layout_alignParentRight="true"
android:layout_below="@id/desc"
android:layout_toEndOf="@id/icon"
android:layout_toRightOf="@id/icon"
android:padding="0dp"
android:textIsSelectable="true"
android:textStyle="italic" />
</RelativeLayout>

您的适配器文件将如下所示。

public class SimpleItemRecyclerViewAdapter
extends RecyclerView.Adapter<SimpleItemRecyclerViewAdapter.ViewHolder> {

private final List<Record> mValues;

public SimpleItemRecyclerViewAdapter(List<Record> items) {
mValues = items;
}

@Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext())
.inflate(R.layout.list_content, parent, false);
return new ViewHolder(view);
}


@Override
public void onBindViewHolder(final ViewHolder holder, final int position) {
Record record = mValues.get(position);
holder.mIcon.setImageResource(record.iconRes);
holder.mDescText.setText(record.desc);
holder.mDetailText.setText(record.detail);
/*holder.itemView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Context context = v.getContext();
Intent intent = new Intent(context, DetailActivity.class);
context.startActivity(intent);
}
});*/

holder.mRootLay.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Context context = v.getContext();
Intent intent = new Intent(context, DetailActivity.class);
context.startActivity(intent);
}
});
}

public class ViewHolder extends RecyclerView.ViewHolder {
public final ImageView mIcon;
public final TextView mDescText;
public final TextView mDetailText;
public final RelativeLayout mRootLay;

public ViewHolder(View view) {
super(view);
mIcon = (ImageView)view.findViewById(R.id.icon);
mDescText = (TextView) view.findViewById(R.id.desc);
mDetailText = (TextView) view.findViewById(R.id.detail);
mRootLay = (RelativeLayout) view.findViewById(R.id.rootLay);
}
}
}

关于android - 只有 recyclerview 单元格中的图标是可点击的,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42432052/

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