【问题标题】:Listview onItemClick doesn't work as expectedListview onItemClick 无法按预期工作
【发布时间】:2016-03-29 13:36:59
【问题描述】:

这是一个我非常好奇的奇怪问题。我有一个配置文件列表视图,当我单击一个特定的配置文件时,我想单独查看该特定配置文件。当我尝试在列表视图中选择一个项目时,它通常不起作用,但由于某些奇怪的原因,它有时会起作用(大约十分之一)。这让它更令人沮丧。有没有人遇到过这个问题?谢谢你。 (我发布了一个与此类似的问题,认为它根本不起作用,所以请不要重复,谢谢)。

public class CowListView extends ListActivity {

    RegisterCowAdapter cowAdapter;
    private DataBaseHelper databaseHelper;
    public ListView listView;
    TextView student_Id;

    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.cow_list_item);
        cowAdapter = new RegisterCowAdapter(this);

        cowAdapter.open();
        updateCowUI();
        listView = (ListView) findViewById(android.R.id.list);

        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            public void onItemClick(AdapterView<?> parent, View view,
                                    int position, long id) {
                student_Id = (TextView) view.findViewById(R.id.cowtagnolist);
                String studentId = student_Id.getText().toString();
                Intent i = new Intent(view.getContext(), CowProfile.class);
                Toast.makeText(CowListView.this, "Clicked", Toast.LENGTH_SHORT).show();
                i.putExtra("student_Id", Integer.parseInt(studentId));
                Log.d("StartingCow", studentId);
                startActivityForResult(i, 0);

            }

        });

XML

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/RelativeLayout1"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:descendantFocusability="blocksDescendants"
    android:longClickable="true"
    android:focusableInTouchMode="false"
    android:clickable="false"
    android:focusable="false">

    <ListView
        android:id="@android:id/list"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content">
    </ListView>

    <TextView
        android:id="@+id/cowtagnolist"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true"
        android:textAppearance="?android:attr/textAppearanceLarge"
        android:textColor="@color/black"
        android:clickable="true">
    </TextView>

    <TextView
        android:id="@+id/cowdoblist"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"    
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true"
        android:layout_marginTop="17dp"
        android:clickable="true"
        android:textColor="@color/black">    
    </TextView>
</LinearLayout>

【问题讨论】:

  • 你调试了吗?
  • 请发布您的项目适配器的布局,我做了一个适用于您的布局的示例,但我需要您的适配器布局来检查您那里有什么。
  • 您好,感谢您的回复,但我使用的是我上面发布的 xml 中的简单列表适配器?还是这种做法不正确?再次感谢。

标签: android android-intent onitemclick


【解决方案1】:

我发现出了什么问题,我没有正确定义我的子对象和我的根项目。希望这可以帮助任何有类似问题的人。 子项应该已经实现,如下所示:

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

在根项中:

android:clickable="false"
android:descendantFocusability="blocksDescendants"
android:focusable="false"
android:focusableInTouchMode="false"

【讨论】:

  • 谢谢。正是我需要的。在第一次 onItemLongClick 调用之后,通常的 OnItemClick 方法刚刚停止工作。而原因只是xml中的几个标签。
【解决方案2】:

你应该保持 Listview 宽度为 Match_parent ,如果你 wrap_content 它,它不会得到超出其子大小或宽度的点击事件。对于我认为这可能是问题所在。如果你的 Listview 单行布局也包含宽度为 wrap_content 的子视图,那么问题可能会增加。

【讨论】:

  • 感谢您的回答,但仍然无法正常工作。当我在列表视图上随机按下一段时间但我不知道为什么时,意图会起作用!当它实际工作时还添加了我的日志
  • 你调试过,看看它是否真的在clickListener上。或者打印一个 Log.d("Listview","Clicked");就在你的 OnItemClickListener
  • 当它在一段时间后实际工作时,它确实在日志中说它被点击了!但通常绝对不会发生任何事情。
【解决方案3】:

也许问题出在 xml 中??

我看到了下一个字符串

android:longClickable="true" android:clickable="false"

看起来 10 次成功点击中的 1 次发生在您进行长按而不是通常情况下。为了解决这个问题,将 clickable 改为 true

android:clickable="true"

如果我正确理解了这个问题 - 它应该会有所帮助

【讨论】:

  • 感谢您的回答,但仍然无法正常工作!不过谢谢你的回答。当我在列表视图上随机按下一段时间时,它仍然随机工作吗?当它实际工作时还添加了我的日志
猜你喜欢
  • 1970-01-01
  • 2017-11-15
  • 2013-12-23
  • 2014-12-09
  • 2016-01-13
  • 2020-09-21
  • 2011-08-17
  • 2012-04-29
  • 2021-08-12
相关资源
最近更新 更多