繁体   English   中英

可点击项构成ListView:setOnItemClickListener不起作用

[英]Clickable items form a ListView : setOnItemClickListener won't work

我目前正在开发一个Android项目,我需要从ListView的项目中启动一个活动。 我搜索了大约8个小时,发现总是相同的解决方案,这是行不通的。 这是我的代码:

在CatalogueActivity的onCreate方法中:

CtlgArrayAdapter adapter = new CtlgArrayAdapter(this, recipeName, recipeDescr, recipeDiff,
            recipePic, recipeEval);
    ListView list = (ListView)findViewById(android.R.id.list);
    list.setAdapter(adapter);
    list.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
            Log.d(TAG, "Item clicked");
        }
    });    

CtlgArrayAdapter类:

public class CtlgArrayAdapter extends ArrayAdapter<String> {
private static final String TAG = "CTLGADAPTER";

private final Activity context;
private final String[] recipeName;
private final String[] recipeDescr;
private final Integer[] recipeDiff;
private final Integer[] recipePic;
private final Integer[] recipeEval;

public CtlgArrayAdapter(Activity context, String[] recipeName, String[] recipeDescr,
                        Integer[] recipeDiff, Integer[] recipePic, Integer[] recipeEval) {
    super(context, R.layout.ctlg_list, recipeName);

    this.context = context;
    this.recipeName = recipeName;
    this.recipeDescr = recipeDescr;
    this.recipeDiff = recipeDiff;
    this.recipePic = recipePic;
    this.recipeEval = recipeEval;
}

public View getView(int position, View view, ViewGroup parent) {
    LayoutInflater inflater = context.getLayoutInflater();
    View rowView = inflater.inflate(R.layout.ctlg_list, null, true);

    TextView nameTile = (TextView) rowView.findViewById(R.id.ctlg_list_name);
    TextView diffTile = (TextView) rowView.findViewById(R.id.ctlg_list_diff);
    TextView descrTile = (TextView) rowView.findViewById(R.id.ctlg_list_descr);
    ImageView picTile = (ImageView) rowView.findViewById(R.id.ctlg_list_img);
    RatingBar rateTile = (RatingBar) rowView.findViewById(R.id.ctlg_list_rating);
    nameTile.setText(recipeName[position]);
    diffTile.setText(recipeDiff[position].toString());
    descrTile.setText(recipeDescr[position]);
    picTile.setImageResource(recipePic[position]);
    rateTile.setRating(recipeEval[position]);

    return rowView;
}

问题是,当我点击我的项目时,没有任何反应。 所有互联网似乎都说这是解决方案,但它不起作用:'(

这是我的.xml文件:目录布局:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<ListView
    android:id="@android:id/list"
    android:clickable="true"
    android:layout_alignParentStart="true"
    android:layout_below="@id/linear_ctlg"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"/> </RelativeLayout>    

和catalogue_item布局

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content">

<ImageView
    android:id="@+id/ctlg_list_img"
    android:layout_width="90dp"
    android:layout_height="90dp"
    android:focusable="false"
    android:scaleType="centerCrop"
    android:src="@drawable/no_image"
    android:layout_marginEnd="10dp"/>
<TextView
    android:id="@+id/ctlg_list_name"
    android:textColor="@color/colorPrimary"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentTop="true"
    android:layout_toEndOf="@id/ctlg_list_img"
    android:focusable="false"
    android:textStyle="bold"
    android:text="Nom de la recette sur son lit de char"/>
<TextView
    android:id="@+id/ctlg_list_diff"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_toEndOf="@id/ctlg_list_img"
    android:layout_below="@id/ctlg_list_name"
    android:text="Difficile"/>
<TextView
    android:id="@+id/ctlg_list_descr"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_below="@id/ctlg_list_diff"
    android:layout_toEndOf="@id/ctlg_list_img"
    android:focusable="false"
    android:textStyle="italic"
    android:text="Ceci est la description de la recette. J'en mets une longue pour être sûr qu'il y ait assez d'espace. Il fait beau aujourd'hui."/>
<RatingBar
    android:id="@+id/ctlg_list_rating"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentEnd="true"
    android:focusable="false"
    android:scaleX="0.4"
    android:scaleY="0.4"
    android:numStars="5"
    android:rating="3"
    android:stepSize="1"/> </RelativeLayout>

如果你发现我的代码出了什么问题,那么非常感谢sooooooo! :)

确保catelogue_item中的所有视图都不可聚焦或可点击,请使用下面的代码。

android:clickable="false" 
android:focusable="false"
android:focusableInTouchMode="false"

还尝试使用以下设置根布局: android:descendantFocusability="blocksDescendants"

原因是如果列表中的任何元素包含可聚焦项,则不会调用列表的onclick。

我不确定它是否是正确的解决方案。

你的传球活动而不是上下文

super(context, R.layout.ctlg_list, recipeName); ,所以尝试传递上下文。

查看此帖子 ,它类似于可能有用的代码。

你不需要任何焦点:false或clickable:false。 你可以删除所有这些。 只需在目录xml顶部相对布局中添加此代码android:descendantFocusability="blocksDescendants" 我用你的代码尝试了它并且工作了。

我找到了一个解决方案:我从catalogue_tiem布局中抑制了评级栏,现在一切都运行良好(我不需要将它保留在此布局中,因为它将在稍后显示)。 我想有一些解决方案可以让它与评级栏一起工作,通过修改xml文件中的属性,但是......永远不要:)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM