【问题标题】:Android listview item click is not workingAndroid listview 项目点击不起作用
【发布时间】:2014-05-12 07:04:37
【问题描述】:

我已经看到了类似的问题,并尝试了诸如将 focusable 设置为 false 之类的解决方案,但它仍然无法正常工作。这是我的包含列表视图的布局:

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


<ListView 
    android:id="@+id/listView_open_groups"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_above="@+id/button_add_group"
    android:layout_alignParentTop="true"
    ></ListView>

<Button 
    android:id="@+id/button_add_group"
    android:text="Add Group"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentBottom="true"
    android:focusable="false"
    />

<Button 
    android:id="@+id/button_add_entry"
    android:text="Add Entry"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentBottom="true"
    android:layout_toRightOf="@+id/button_add_group"
    android:focusable="false"
    />


</RelativeLayout>

还有我的 custom_list_row

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


<ImageView 
    android:id="@+id/imgView_group_entry_icon"
    android:layout_width="50dp"
    android:layout_height="wrap_content"
    android:focusable="false"
    />
<TextView 
    android:id="@+id/textView_group_entry_name"
    android:layout_toRightOf="@id/imgView_group_entry_icon"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:focusable="false"/>
<TextView 
    android:id="@+id/textView_group_entry_type"
    android:layout_alignParentRight="true"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:focusable="false"/>


</RelativeLayout>

还有我的自定义适配器:

public class OpenDbListAdapter extends ArrayAdapter<KeepassDBGroupv1>{

Context context;

public OpenDbListAdapter(Context context, int resource,
        List<KeepassDBGroupv1> objects) {
    super(context, resource, objects);
    this.context=context;
}

public View getView(int position, View convertView, ViewGroup parent) {
    ViewHolder holder = null;
    KeepassDBGroupv1 rowItem = getItem(position);

    LayoutInflater mInflater = (LayoutInflater) context
            .getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
    if (convertView == null) {
        convertView = mInflater.inflate(R.layout.custom_list_row, null);
        holder = new ViewHolder();
        holder.name = (TextView) convertView.findViewById(R.id.textView_group_entry_name);
        holder.imageView = (ImageView) convertView.findViewById(R.id.imgView_group_entry_icon);
        holder.type = (TextView) convertView.findViewById(R.id.textView_group_entry_type);
        convertView.setTag(holder);
    } else 
        holder = (ViewHolder) convertView.getTag();

    holder.name.setText(rowItem.getGroupName());
    holder.type.setText("Group");
    Drawable d = context.getResources().getDrawable(context.getResources().getIdentifier("ic"+Integer.toString(rowItem.getImageId()), "drawable", context.getPackageName()));
    holder.imageView.setImageDrawable(d);
    //App.getDB().drawFactory.assignDrawableTo(holder.imageView, context.getResources(), rowItem.icon);

    return convertView;
}

private class ViewHolder {
    ImageView imageView;
    TextView name;
    TextView type;
}

}

最后,我是这样设置监听器的:

public class OpenDbActivity extends Activity{    
    static ListView lv;  

    @Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_opendb);
    db = MyApplication.keepassdb;
    InitializeComponents();

}

    private void InitializeComponents() {

    lv = (ListView) findViewById(R.id.listView_open_groups);
    OpenDbListAdapter adapter = new OpenDbListAdapter(this, R.layout.custom_list_row, childGroups);
    lv.setAdapter(adapter);

    lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {

        @Override
        public void onItemClick(AdapterView<?> parent, View view,
                int position, long id) {

            Log.d("Something");

        }
    });



}

谁能看出问题出在哪里?

谢谢

【问题讨论】:

  • 在您的custom_list_row.xmlandroid:descendantFocusability="blocksDescendants" 中尝试此操作
  • @SimplePlan 感谢我在包含我的 listview 标签的 relativelayout 中尝试过,但仍然没有工作
  • @SimplePlan 我想我在错误的布局文件中尝试过会再试一次
  • @SimplePlan 不行,还是不行
  • 并尝试从您的Relative Layout 的所有孩子中删除android:focusable="false"

标签: android listview onclicklistener


【解决方案1】:

我也遇到过这个问题,我通过将点击侦听器设置为convertViewcustom adapter 来克服这个问题。我不知道这是不是一个好方法,但它解决了我的问题。

public View getView(int position, View convertView, ViewGroup parent) {
    ViewHolder holder = null;
    KeepassDBGroupv1 rowItem = getItem(position);

    LayoutInflater mInflater = (LayoutInflater) context
            .getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
    if (convertView == null) {
        convertView = mInflater.inflate(R.layout.custom_list_row, null);
        holder = new ViewHolder();
        holder.name = (TextView) convertView.findViewById(R.id.textView_group_entry_name);
        holder.imageView = (ImageView) convertView.findViewById(R.id.imgView_group_entry_icon);
        holder.type = (TextView) convertView.findViewById(R.id.textView_group_entry_type);
        convertView.setTag(holder);
    } else 
        holder = (ViewHolder) convertView.getTag();

    holder.name.setText(rowItem.getGroupName());
    holder.type.setText("Group");
    Drawable d = context.getResources().getDrawable(context.getResources().getIdentifier("ic"+Integer.toString(rowItem.getImageId()), "drawable", context.getPackageName()));
    holder.imageView.setImageDrawable(d);
    //App.getDB().drawFactory.assignDrawableTo(holder.imageView, context.getResources(), rowItem.icon);
    convertView.setOnClickListener(new OnClickListener(){
          @Override
          public void onClick(View v) {
              Log.v("OpenDbListAdapter ","List View Clicked");
          }
    });
    return convertView;
}

【讨论】:

  • 谢谢你的工作,我不确定这是否是一个好方法,但现在对我有用!
【解决方案2】:

其实什么都没有。

但问题出在custom_list_row.xml,当你点击列表视图项时,

你实际上点击了custom_list_row.xml中的其他标签

这些标签不负责触发ItemClickListener

custom_list_row.xml 中标签的android:layout_width 属性值更改为"wrap_content" 或删除标签并重试。

【讨论】:

    【解决方案3】:

    设置后

    android:focusable="false"

    为你的ListView内容,在你的ListView标签中添加

    android:clickable="true"

    【讨论】:

    • @bigO : SimplePlan 建议的 android:descendantFocusability="blocksDescendants" 有效吗?
    • 我的带有 TextView 和 Checkbox 的列表适用于此解决方案。我为我的复选框设置了 android:focusable='false' 并为 ListView 本身设置了 clickable="true" !
    猜你喜欢
    • 2019-01-13
    • 2014-01-08
    • 1970-01-01
    • 2012-06-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多