簡體   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