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

Adh*_*ham 29 android listview adaptor

我有一个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);


        }

    }


}
Run Code Online (Sandbox Code Playgroud)

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>
Run Code Online (Sandbox Code Playgroud)

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>
Run Code Online (Sandbox Code Playgroud)

Rot*_*miz 138

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

android:focusable="false"
Run Code Online (Sandbox Code Playgroud)

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

  • 仅供参考:这不仅发生在按钮上,而且发生在具有背景设置的TextViews上.我使用`focusable ="false"`绕过它. (10认同)
  • 谢谢,这应该标记为答案!解决了我的问题. (4认同)

c0d*_*0de 26

我对ListView只有一个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>
Run Code Online (Sandbox Code Playgroud)

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

radioButton.setFocusableInTouchMode(false);
radioButton.setFocusable(false);
Run Code Online (Sandbox Code Playgroud)

或者在XML文件中:

android:focusable="false" 
android:focusableInTouchMode="false"
Run Code Online (Sandbox Code Playgroud)

所以这是一个与焦点相关的问题......使用上面的修饰符,焦点将定向到ListView上.

  • 谢谢你的解释!我有一个`CheckBox`,想知道为什么我可以单击`CheckBox`而不是`ListView`项.对于记录,复选框(或在您的情况下是单选按钮)可以保持可点击,但不可聚焦.此外,您可以使用`android:focusable ="false"在XML中执行此操作:android:focusableInTouchMode ="false"` (5认同)

小智 13

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

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

android:descendantFocusability="blocksDescendants"
Run Code Online (Sandbox Code Playgroud)

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

android:focusable="false"
android:focusable="true"
android:clickable="true"
android:clickable="false"
Run Code Online (Sandbox Code Playgroud)


小智 5

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

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

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

@Override
public boolean isEnabled(int position) {
    return true;
}
Run Code Online (Sandbox Code Playgroud)

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


小智 2

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