gpt4 book ai didi

android - 将 ListView 项设置为选中

转载 作者:行者123 更新时间:2023-11-29 15:20:51 26 4
gpt4 key购买 nike

我有一个主从 ListView 设置,我想更改主 fragment 中所选项目的背景。

我曾经有以下代码,但这不允许我使用成熟的选择器:

    @Override
public View getView(int position, View convertView, ViewGroup parent) {
View view = super.getView(position, convertView, parent);

// Highlight selected item
if (position == selectedPos) {
view.setBackgroundColor((view.getResources()
.getColor(R.color.nw_white)));
} else {
view.setBackgroundColor((view.getResources()
.getColor(R.color.nw_grey)));
}
return view;
}

它工作正常,但我想使用选择器。 (因为我希望这些项目在焦点和点击时适本地改变背景。)

所以我改成了:

            @Override
public View getView(int position, View convertView, ViewGroup parent) {
View view = super.getView(position, convertView, parent);

// Highlight selected item
if (position == selectedPos) {
view.setSelected(true);
// view.setBackgroundColor((view.getResources()
// .getColor(R.color.nw_row_download_master_selected)));
} else {
view.setSelected(false);
// view.setBackgroundColor((view.getResources()
// .getColor(R.color.nw_row_download_master_normal)));
}
Log.d(TAG, "pos " + position + " is selected: "
+ view.isSelected());

return view;
}

列表行如下所示:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/LinearLayout1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@drawable/selector_row_collection_master"
android:orientation="horizontal"
>

...
</LinearLayout>

选择器是这样的:

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="@drawable/row_master_detail_pressed"
android:state_pressed="true" />
<item android:drawable="@drawable/row_master_detail_focused"
android:state_focused="true" />
<item android:drawable="@color/nw_row_master_master_selected"
android:state_selected="true" />
<item android:drawable="@color/nw_row_master_normal" />
</selector>

该行响应点击事件但不响应被选中,即使记录器说该 View 确实被选中!这是怎么回事?

最佳答案

我不知道为什么 View 不会显示为选中状态,即使它被设置为选中状态,但这段代码应该可以工作:

使用ListView.setItemChecked():

mListView.setChoiceMode(ListView.CHOICE_MODE_SINGLE);
mListView.setItemChecked(position, isActivated);

选择器:

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="@drawable/row_master_detail_pressed"
android:state_pressed="true" />
<item android:drawable="@drawable/row_master_detail_focused"
android:state_focused="true" />
<item android:drawable="@color/nw_row_master_master_selected"
android:state_activated="true" />
<item android:drawable="@color/nw_row_master_normal" />
</selector>

因为 android:state_activated 仅在 Honeycomb 后可用,将此添加到 getView():

if (Build.VERSION.SDK_INT < Build.VERSION_CODES.HONEYCOMB) {
// Highlight selected item
if (position == selectedPos) {
view.setBackgroundColor((view.getResources()
.getColor(R.color.nw_row_master_selected)));
} else {
Drawable sel = view.getResources().getDrawable(
R.drawable.selector_row_collection_master);
view.setBackgroundDrawable(sel);
}
}

关于android - 将 ListView 项设置为选中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18332146/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com