【问题标题】:Android ListView not detecting selectionAndroid ListView 未检测到选择
【发布时间】:2014-06-20 12:18:05
【问题描述】:

我被难住了。这个很简单,我错过了一些简单的东西,但我什么都没有。

我有一个 ListView,其中包含非常简单的项目 - TextView 包含“N.name”形式的文本,其中 N 是列表中的位置,name 是关联对象的名称。我创建了一个 ListAdapter,将这些信息呈现到 ListView 中。

演示文稿似乎工作正常。这是当我点击不起作用的行时的部分。我只是没有收到通知。如果我在项目上设置了一个 onClickListener(当它在适配器中生成时),我确实会收到一个通知,但是要让它按照我想要的方式工作是很复杂的。我已经尝试了很多东西 - 改变后代的可聚焦性,启用,禁用,将 itemsCanFocus 设置为 false,...... - 但其中任何一个都没有运气。

我已经包含了用于在 ListView 中配置视图和元素的相关代码。

特别是,我想要做的是让OnItemClickLister.onItemClick() 在我点击一行时被调用。对我做错了什么有什么想法吗?

活动:

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    model = FZMModel.getInstance(this);
    setContentView(R.layout.activity_deploy);

    TextView viewTitle = (TextView) findViewById(R.id.deploy_header);

    if (model.getProfiles().size() == 0) {
        viewTitle.setText(R.string.deploy_header_create);
    } else {
        viewTitle.setText(R.string.deploy_header_choose);
    }

    profileListView = (ListView) findViewById(R.id.profileListView);
    profileListAdapter = new ProfileListAdapter(this, model);
    profileListView.setAdapter(profileListAdapter);

    profileListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> adapterView, View view, int position, long id) {
            Log.d(TAG, String.format("Item %d selected (with id=0x%x)", position, id));
        }
    });
}

适配器: ... getView() 中的相关代码:

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    final Profile profile = model.getProfileAtIndex(position);
    final int rowNum = position;

    if (convertView == null) {
        LayoutInflater layoutInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        convertView = layoutInflater.inflate(R.layout.profile_list_layout, parent, false);
    }

    TextView profileIndexView;
    TextView profileNameView;

    profileIndexView = (TextView) convertView.findViewById(R.id.profileIndexLabel);
    profileNameView = (TextView) convertView.findViewById(R.id.profileNameLabel);

    profileIndexView.setText(String.format("%d.", position));
    profileNameView.setText(profile.getName());

//     convertView.setOnClickListener(new View.OnClickListener() {
//         @Override
//         public void onClick(View view) {
//             Toast.makeText(context, String.format("Did select row #%d (index=%d)",rowNum, profile.getPosition()), Toast.LENGTH_SHORT).show();
//         }
//     });
    return convertView;
}

活动布局:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.nsn.flexizonemobile.FZMConcreteActionDeploy"
    tools:ignore="MergeRootFrame" >

    <TextView
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:textAppearance="?android:attr/textAppearanceLarge"
        android:text="@string/deploy_header_choose"
        android:id="@+id/deploy_header"
        android:layout_margin="10dp"
        android:layout_centerHorizontal="true" />

    <ListView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/profileListView"
        android:layout_gravity="left|top"
        android:layout_alignParentLeft="true"
        android:layout_marginLeft="0dp"
        android:choiceMode="none"
        android:layout_below="@+id/deploy_header"
        android:divider="@android:color/darker_gray"
        android:dividerHeight="2dp"
        android:clickable="true"
        android:descendantFocusability="beforeDescendants" />

</RelativeLayout>

profile_list_layout 布局:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="horizontal" android:layout_width="match_parent"
    android:layout_height="wrap_content">

    <TextView
        android:layout_width="@dimen/profile_index_width"
        android:layout_height="wrap_content"
        android:textAppearance="?android:attr/textAppearanceMedium"
        android:text="A.         "
        android:id="@+id/profileIndexLabel"
        android:gravity="center_vertical|right"
        android:layout_centerVertical="true"
        android:paddingTop="5dp"
        android:paddingBottom="5dp"
        android:textSize="24sp"
        android:textIsSelectable="true"
        android:layout_alignParentStart="false"
        android:layout_alignParentTop="true" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textAppearance="?android:attr/textAppearanceMedium"
        android:text="Profile Name"
        android:id="@+id/profileNameLabel"
        android:layout_alignParentLeft="false"
        android:layout_alignParentTop="false"
        android:gravity="center_vertical"
        android:layout_toRightOf="@+id/profileIndexLabel"
        android:layout_marginLeft="10dp"
        android:layout_centerVertical="true"
        android:textSize="24sp"
        android:paddingTop="10dp"
        android:paddingBottom="10dp" />
</RelativeLayout>

【问题讨论】:

  • 请将 choisemode="none" 改为 singleChoice

标签: android listview android-listview onitemclicklistener


【解决方案1】:

在您的充气机布局的文本视图中添加:

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

在您的充气机的父布局中,即在充气机的相对布局中添加:

android:descendantFocusability="blocksDescendants" 

删除:

android:descendantFocusability="beforeDescendants"

来自您的列表视图。并将列表视图的高度设置为 match_parent 将解决您的问题。

【讨论】:

  • 这似乎明白了。我不确定我是否完全理解它的工作原理或原因,但禁用列表项视图的可聚焦性允许列表视图查看点击。感谢您的帮助!
  • 我想我都赞成并被选为答案?我错过了什么?向上箭头亮起,复选标记出现
  • @CubsFanRon 您的 OnItemClickLisener 不起作用的原因是您的 TextView “消耗”了触摸事件(我假设它是 textIsSelectable 属性)。基本上你的 TextView 告诉它的父母,父母父母等:“不要打扰,这个点击是为了我”通过移除焦点,你的 TextView 不会误解你的点击为“用户想要专注于我”和事件可以冒泡到它的父级
【解决方案2】:

从您的 ListView 中删除以下属性:

android:choiceMode="none"
android:clickable="true"
android:descendantFocusability="beforeDescendants"

还有

android:layout_width="wrap_content"
android:layout_height="wrap_content"

在这种情况下没有意义(尤其是高度)

【讨论】:

  • 在你的行布局中没有看到 android:textIsSelectable="true",它可能会消耗触摸事件......你必须删除它
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-03-24
  • 1970-01-01
  • 2016-06-17
相关资源
最近更新 更多