30

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

列表视图的活动..

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>

adapter_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>
4

12 回答 12

140

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

android:focusable="false"

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

于 2012-01-21T18:20:04.693 回答
27

我对只包含 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。

于 2012-02-26T19:56:21.810 回答
14

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

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

android:descendantFocusability="blocksDescendants"

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

android:focusable="false"
android:focusable="true"
android:clickable="true"
android:clickable="false"
于 2015-10-06T23:29:06.760 回答
5

我想为 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 中的排名很高。

于 2014-07-14T08:23:06.033 回答
3

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

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

textIsSelectable="false"

谢谢 Lint(不是!)

于 2013-05-30T09:13:55.260 回答
2

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

于 2012-01-21T18:15:02.293 回答
2

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

android:focusable="false"

对于<button><view>...

之后,我为我的列表视图使用了以下代码并且它有效

// 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();
    }
};
于 2015-04-09T09:19:18.763 回答
1

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

于 2012-01-21T18:37:28.193 回答
1

我很晚了,但发现了一些我认为这很有趣的东西:

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

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

于 2013-05-28T21:02:50.130 回答
0

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

于 2016-03-30T00:28:55.140 回答
0

请注意,要使列表视图中的项目可点击,所有项目组件都必须将可点击设置为 false。在 xml Android 中:clickable="false" 在程序中 setClickable(false)

于 2021-07-04T23:12:55.563 回答
-2

设置 android:clickable="false" android:focusable="fasle"

于 2013-02-21T12:17:15.990 回答