ListView 项目不可点击。为什么?

发布于 2024-12-28 17:25:00 字数 5574 浏览 1 评论 0原文

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

列表视图活动 ..places_list.xml

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);


        }

    }


}

布局

<?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>

I have a ListView that uses a customized adapter, but I can't click on the ListView Item ..

Activity for list view ..

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 layout

<?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 layout

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(12

檐上三寸雪 2025-01-04 17:25:00

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

android:focusable="false"

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

Android doesn't allow to select list items that have focusable elements (buttons).
Modify the button's xml attribute to:

android:focusable="false"

It should still be clickable, just won't gain focus...

触ぅ动初心 2025-01-04 17:25:00

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

I had the same issue with a ListView which contained only a RadioButton:

<?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>

I used the RadioButton in each row to display the default item selection, to make ListView handle clicks I had to use:

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

or in the XML-file:

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

So it is a focus related issue... With the above modifiers the focus is directed to ListView on click.

把时间冻结 2025-01-04 17:25:00

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

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

android:descendantFocusability="blocksDescendants"

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

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

This answer here worked for me:
https://stackoverflow.com/a/16536355/5112161

Mainly he ADDED in the LinearLayout or RelativeLayout the following:

android:descendantFocusability="blocksDescendants"

You also need to REMOVE from all your xml the following:

android:focusable="false"
android:focusable="true"
android:clickable="true"
android:clickable="false"
灰色世界里的红玫瑰 2025-01-04 17:25:00

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

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

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

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

上面修复了我的不可点击列表视图。也许这个评论也可以帮助其他人,因为“android 列表不可点击”搜索词在 Google 中的搜索率相当高。

I wanted to add a comment to rupps answer, but I do not have enough reputation.

If you are using a custom adapter extending the ArrayAdapter you can overwrite with areAllItemsEnabled() and isEnabled(int position) in your class:

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

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

The above fixed my non clickable list view. Maybe this comment also helps others as "android list not clickable" search term is quite high in Google.

治碍 2025-01-04 17:25:00

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

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

textIsSelectable="false"

谢谢 Lint(不是!)

Consider making the text value selectable by specifying android:textIsSelectable="true"

Don't listen to Google. In the rows' layout, set

textIsSelectable="false"

Thank you Lint (not!)

献世佛 2025-01-04 17:25:00

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

try this to get the focus:
View.getFocus();

分开我的手 2025-01-04 17:25:00

我在列表视图中使用了视图和按钮,因此我使用了:

android:focusable="false"

for

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

// 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();
    }
};

I was using a view and button inside the list view so I have used:

android:focusable="false"

for <button> and <view> ...

After that I used following code for my list view and it worked

// 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();
    }
};
二货你真萌 2025-01-04 17:25:00

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

List view items are clickable. To use it, you have to set item click listener on your list view. Then it will work.

听闻余生 2025-01-04 17:25:00

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

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

但是,如果您的适配器源自 BaseAdapter(因此您必须实现 getCount() 和 getItem(),这对于数组而言很简单),则始终会调用它。

I'm pretty late but discovered something I think it's interesting about this:

if your adapter descends from ArrayAdapter, as much as I have tried, onItemClickListener is not called.

However, if your adapter descends from BaseAdapter (so you have to implement getCount() and getItem(), trivial for an array) it IS always called.

混吃等死 2025-01-04 17:25:00

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

For me, the problem was that my items within my ListView had clickable set to true.

压抑⊿情绪 2025-01-04 17:25:00

请注意,要使列表视图中的项目可单击,所有项目组件都必须将 clickable 设置为 false。
在 XML 中
Android:可点击=“假”
节目中
设置可点击(假)

Note that to have an item clickable in listview, all the item components must have clickable set to false.
In xml
Android : clickable= "false"
In program
setClickable(false)

旧伤慢歌 2025-01-04 17:25:00


安卓:可点击=“假”
android:focusable="fasle"

set
android:clickable="false"
android:focusable="fasle"

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文