【问题标题】:ListView items are not clickable. why?ListView 项目不可点击。为什么?
【发布时间】:2012-01-21 17:59:42
【问题描述】:

我有一个使用自定义适配器的ListView,但我无法单击 ListView 项..

列表视图的活动..

package com.adhamenaya.projects;

import java.util.ArrayList;

import android.app.Activity;
import android.content.Context;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.Filter;
import android.widget.Filterable;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import com.adhamenaya.classes.Place;

public class PlacesListActivity extends Activity {
    private ArrayList<Place> places;
    private ArrayList<String> items;
    GridviewAdapter mAdapter;
    private ListView lvPlaces;
    private EfficientAdapter adap;
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.places_list);
        lvPlaces = (ListView) this.findViewById(R.id.lvPlaces);
        new DowanloadPlaces().execute("");
    }
    private void bindList(ArrayList<Place> places) {
        this.places = places;
        // Start creating the list view to show articles
        items = new ArrayList<String>();
        for (int i = 0; i < places.size(); i++) {
            items.add(String.valueOf(places.get(i).mName));
        }
        adap = new EfficientAdapter(this);
        adap.notifyDataSetChanged();
        lvPlaces.setAdapter(adap);
    }

    // EfficientAdapter : to make a customized list view item
    public class EfficientAdapter extends BaseAdapter implements Filterable {

        // The function of inflater to convert objects from XML layout file (i.e. main.xml) to a programmable 
        LayoutInflater inflater;
        Context context;

        public EfficientAdapter(Context context) {
            inflater = LayoutInflater.from(context);
            this.context = context;
        }

        public int getCount() {
            // Get the number of items in the list
            return items.size();
        }

        public Object getItem(int position) {
            // To return item from a list in the given position 
            return items.get(position);
        }

        public long getItemId(int position) {
            // TODO Auto-generated method stub
            return 0;
        }

        public View getView(final int position, View convertView,ViewGroup parent) {
            ViewHolder holder;
            if (convertView == null) {
                convertView = inflater.inflate(R.layout.adaptor_content, null);

                holder = new ViewHolder();// Create an object to hold at components in the list view item
                holder.textLine = (TextView) convertView.findViewById(R.id.textLine);
                holder.buttonLine = (Button) convertView.findViewById(R.id.buttonLine);
                holder.buttonLine.setOnClickListener(new OnClickListener() {
                    private int pos = position;

                    public void onClick(View v) {
                        places.remove(pos);
                        bindList(places);// to bind list items
                        Toast.makeText(getApplicationContext(),"Deleted successfuly :)", Toast.LENGTH_LONG).show();
                    }
                });
                convertView.setTag(holder);
            } else {
                holder = (ViewHolder) convertView.getTag();
            }
            // Bind the data efficiently with the holder.
            holder.textLine.setText(String.valueOf(places.get(position).mName));
            return convertView;
        }

        public Filter getFilter() {
            // TODO Auto-generated method stub
            return null;
        }

    }

    // ViewHolder : class that represents a list view items
    static class ViewHolder {
        TextView textLine;
        Button buttonLine;
    }

    // DownloadRSSFeedsTask: works in a separate thread
    private class DowanloadPlaces extends AsyncTask<String, Void, ArrayList<Place>> {

        @Override
        protected ArrayList<Place> doInBackground(String... params) {
            ArrayList<Place> places = new ArrayList<Place>();
            Place p = new Place();
            for(int i =0;i<25;i++){
                p.mName = "Al Mathaf Hotel";
                places.add(p);              
            }

            return places;
        }

        @Override
        protected void onPostExecute(ArrayList<Place> places) {
            bindList(places);


        }

    }


}

places_list.xml 布局

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent">
    <ListView 
        android:layout_width="match_parent" 
        android:layout_height="wrap_content"
        android:id="@+id/lvPlaces">

    </ListView>
</LinearLayout>

adaptor_content.xml 布局

<ImageView
    android:id="@+id/imageView1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignLeft="@+id/textLine"
    android:layout_centerVertical="true"
    android:src="@drawable/settings" />

</RelativeLayout>

【问题讨论】:

  • ListView 的 OnClickListener 在哪里?
  • 您看到什么行为让您相信它不可点击?您确定 ListView 项目设置为可点击吗?如果不是,则不会生成 onClick 事件。一般来说,如果遇到问题: 1. 首先尽可能多地进行调试。 2. 如果您期望某个行为并且它没有发生,请确保您的代码达到了您期望该行为发生的点。如果没有,请发布代码和问题“为什么我的代码没有到达点 ”? 3. 如果您遇到崩溃,请发布崩溃消息和堆栈跟踪,然后问“为什么我的代码在这里中止?”米
  • 我也会为类似任务的列表项使用 ContextMenu(删除列表项),但我猜这取决于你......
  • 你可以试试这个解决方案:stackoverflow.com/a/10333241/1592183 这帮助我解决了类似的问题。
  • 检查这个答案。它对我有用stackoverflow.com/a/11624769/5112161

标签: android listview adaptor


【解决方案1】:

Android 不允许选择具有可聚焦元素(按钮)的列表项。 修改按钮的xml属性为:

android:focusable="false"

它应该仍然可以点击,只是不会获得焦点......

【讨论】:

  • 谢谢,这应该标记为答案!解决了我的问题。
  • 仅供参考:这不仅发生在按钮上,而且发生在设置了background 的 TextView 上。我使用focusable="false" 绕过它。
  • 正确我把所有东西都放在了 android:focusable="false" 下,现在一切都好了:D
  • 我目前遇到了同样的问题。但是在列表项中有editText。如果我这样做focusable="false" 我不能再更改我的editText 中的值有没有办法解决这个问题?
  • 我也认为这应该是最好的答案。有效。谢谢!。
【解决方案2】:

我对只包含 RadioButton 的 ListView 也有同样的问题:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal" >

<RadioButton
    android:id="@+id/userNameRadioButton"
    android:layout_width="match_parent"
    android:layout_height="wrap_content" />

</LinearLayout>

我在每一行中使用 RadioButton 来显示默认项目选择,以使 ListView 处理我必须使用的点击:

radioButton.setFocusableInTouchMode(false);
radioButton.setFocusable(false);

或在 XML 文件中:

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

所以这是一个与焦点相关的问题......使用上述修饰符,焦点在点击时被定向到 ListView。

【讨论】:

  • 感谢您的解释!我有一个CheckBox,我想知道为什么我可以点击CheckBox,但不能点击ListView。作为记录,复选框(或在您的情况下为单选按钮)可以保持可点击状态,但不可聚焦。此外,您可以使用android:focusable="false" android:focusableInTouchMode="false" 在 XML 中执行此操作
  • 这为我解决了问题。显然在 xml 中这样做是不够的。一旦我以编程方式完成它就可以工作。
  • 看起来很奇怪,您必须在 XML 和代码中都设置它,但这个解决方案对我有用。干得好!
  • 仅供参考:这不仅发生在按钮上,而且发生在设置了background 的 TextView 上。我使用focusable="false" 绕过它。
【解决方案3】:

这里的答案对我有用: https://stackoverflow.com/a/16536355/5112161

主要是他在LinearLayout或RelativeLayout中添加了以下内容:

android:descendantFocusability="blocksDescendants"

您还需要从所有 xml 中删除以下内容:

android:focusable="false"
android:focusable="true"
android:clickable="true"
android:clickable="false"

【讨论】:

  • android:descendantFocusability="blocksDescendants" 对此 +1
  • android:descendantFocusability="blocksDescendants" 已经为我成功了 +1
【解决方案4】:

我想在 rupps 回答中添加评论,但我没有足够的声誉。

如果您使用的是扩展 ArrayAdapter 的自定义适配器,您可以在您的类中使用 areAllItemsEnabled() 和 isEnabled(int position) 覆盖:

@Override
public boolean areAllItemsEnabled() {
    return true;
}

@Override
public boolean isEnabled(int position) {
    return true;
}

以上修复了我不可点击的列表视图。也许这条评论也对其他人有所帮助,因为“android list not clickable”搜索词在 Google 中的排名很高。

【讨论】:

  • @MarcioGranzotto 此答案仅适用于 ArrayAdapter。如何使用BaseAdapter
【解决方案5】:

考虑通过指定 android:textIsSelectable="true" 使文本值可选择

不要听谷歌。在行的布局中,设置

textIsSelectable="false"

谢谢你 Lint(不是!)

【讨论】:

  • 感谢您的 +1。 textIsSelectable="false" 应该在设置 android:focusable="false" 之后尝试旁边。我知道下次我会对 Lint 的建议持保留态度^^
  • 你也可以试试 Upvoting :) 很高兴知道另一个问题也解决了。
【解决方案6】:

试试这个来获得焦点: View.getFocus();

【讨论】:

  • 我认为这不再可用
【解决方案7】:

我在列表视图中使用了一个视图和按钮,所以我使用了:

android:focusable="false"

对于&lt;button&gt;&lt;view&gt; ...

之后,我在列表视图中使用了以下代码,并且成功了

// Attach the adapter to a ListView
ListView listView = (ListView) findViewById(R.id.shipping_item_list);
listView.setAdapter(adapter);
listView.setOnItemClickListener(listPairedClickItem);

private AdapterView.OnItemClickListener listPairedClickItem = new AdapterView.OnItemClickListener() {
    @Override
    public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {

    Toast.makeText(getBaseContext(), "Item ", Toast.LENGTH_LONG).show();
    }
};

【讨论】:

    【解决方案8】:

    列表视图项目是可点击的。要使用它,您必须在列表视图上设置项目单击侦听器。然后就可以了。

    【讨论】:

    • 您能否详细说明“设置点击侦听器”如何实现?
    【解决方案9】:

    我来晚了,但发现了一些我认为有趣的事情:

    如果您的适配器来自 ArrayAdapter,正如我所尝试的那样,不会调用 onItemClickListener。

    但是,如果您的适配器来自 BaseAdapter(因此您必须实现 getCount() 和 getItem(),这对于数组来说很简单),它总是被调用。

    【讨论】:

      【解决方案10】:

      对我来说,问题是我的 ListView 中的项目将 clickable 设置为 true

      【讨论】:

      • 您的意思是将其设置为“false”吗?我在 Listview 中有一个复合视图,我将复合视图中的每个按钮的 clickable 设置为 false 并且它可以工作。
      【解决方案11】:

      请注意,要让列表视图中的项目可点击,所有项目组件都必须将可点击设置为 false。 在xml中 安卓:可点击=“假” 节目中 setClickable(false)

      【讨论】:

      • 你确定这是正确的吗?好像没有。
      【解决方案12】:

      设置 机器人:可点击=“假” android:focusable="fasle"

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2012-06-06
        • 1970-01-01
        • 2012-01-22
        相关资源
        最近更新 更多