gpt4 book ai didi

java - Android ListView CHOICE_MODE_SINGLE 不会设置

转载 作者:行者123 更新时间:2023-12-02 06:10:59 25 4
gpt4 key购买 nike

这是一个针对 Android 4.1+ 的应用。我希望 ListView 处于 Activity 状态。我正在其他应用程序中使用代码,这些代码可以正常工作。不确定这里有什么区别(除了 API 版本更高)。

我有这个Java:

    DrawerAdapter lAdapter = new DrawerAdapter(this, modeList);
mDrawerList.setAdapter(lAdapter);
mDrawerList.setChoiceMode(ListView.CHOICE_MODE_SINGLE);
mDrawerList.setItemChecked(0, true);

mDrawerList.setOnItemClickListener(new OnItemClickListener() {

public void onItemClick(AdapterView<?> arg0, View v, int pos,
long id) {

mDrawerList.setItemChecked(pos, true);
}

抽屉布局:

<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent" >

<FrameLayout
android:id="@+id/main_frag"
android:layout_width="match_parent"
android:layout_height="match_parent" />

<ListView
android:id="@+id/drawer_list"
android:layout_width="250dp"
android:layout_height="match_parent"
android:layout_gravity="start"
android:cacheColorHint="#ececec"
android:background="@drawable/tile_repeat"
android:fadingEdge="none"
android:listSelector="@drawable/nav_selector"
android:scrollbars="none" />

</android.support.v4.widget.DrawerLayout>

选择器:

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


<!-- pressed -->
<item android:drawable="@drawable/nav_press" android:state_activated="false" android:state_pressed="true"/>

<!-- CHECKED -->
<item android:drawable="@drawable/nav_active" android:state_activated="true" android:state_pressed="false" />

</selector>

nav_active:

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle" >
<solid android:color="#cecece" />
</shape>

我是不是少了一步? Press 状态工作正常,只是未激活。

DrawerAdapter 的 Root View row_drawer_layout.xml

    <?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="vertical" >

<TextView
android:id="@+id/tvMenuItem"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="TextView"
android:textColor="#005348"/>

</LinearLayout>

抽屉适配器:

public class DrawerAdapter extends ArrayAdapter<String> {
private final Context context;

public DrawerAdapter(Context context, String[] sizeList) {
super(context, 0, sizeList);
this.context = context;
}


static class ViewHolder {
public TextView tv1;
public TextView tv2;
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder;
View rowView = convertView;

if (rowView == null) {
LayoutInflater inflater = ((Activity) context).getLayoutInflater();
rowView = inflater.inflate(R.layout.row_drawer_layout, null, true);

holder = new ViewHolder();
holder.tv1 = (TextView) rowView.findViewById(R.id.tvMenuItem);
rowView.setTag(holder);
} else {
holder = (ViewHolder) rowView.getTag();
}

String s = getItem(position);
holder.tv1.setText(s);

return rowView;

}

}

最佳答案

您的ListView项目的 Root View 应该实现Checkable接口(interface)。在您的情况下,LinearLayout 确实实现了Checkable。您应该在布局中使用 CheckedTextView

它应该看起来像这样。

<?xml version="1.0" encoding="utf-8"?>
<CheckedTextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/tvMenuItem"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="TextView"
android:textColor="#005348"
android:background="@drawable/checkable_item"/>

如果您想在项目中拥有更复杂的布局,您可以扩展 Android 布局之一并向其添加 Checkable 接口(interface)。 Here是 `RelativeLayout`` 的一个例子

要更改选中项目的背景,您应该在选择器中使用选中状态:

checkable_item.xml
<selector>
...
<item android:state_checked="true" android:drawable="@drawable/some_drawable" />
...
</selector>

关于java - Android ListView CHOICE_MODE_SINGLE 不会设置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21886678/

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