自定义ListView中的项目选择存在问题。
没有复选框,因此不需要checkable = false。我尝试过设置android:focusable="false"
和android:focusableInTouchMode="false"
。尝试“在顶部绘制选择器选项”。我可以看到标准的触摸动画,但是没有突出显示的项目。
我的ListView定义(activity_main.xml):
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<ListView
android:id="@+id/listview"
android:layout_width="fill_parent"
android:layout_height="fill_parent" />
</LinearLayout>
在主要活动的onCreate中创建ListView(MainActivity.java):
...
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
view = (ListView) findViewById(R.id.listview);
view.setChoiceMode(ListView.CHOICE_MODE_SINGLE);
list = new ArrayList<String>();
currentDirectory = Environment.getExternalStorageDirectory().getAbsolutePath();
adapter = new ArrayAdapter<String>(this, R.layout.listviewitem, R.id.firstLine, list);
view.setAdapter(adapter);
updateFileList(currentDirectory);
view.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View parent_view, int position, long id) {
String listItem = (String) view.getItemAtPosition(position);
Log.d("Hello", "file: " + listItem);
view.setSelected(true);
File tempFile = new File(currentDirectory + File.separator + listItem);
if (tempFile.isDirectory()) {
currentDirectory = currentDirectory + File.separator + listItem;
updateFileList(currentDirectory);
}
}
});
}
...
R.layout.listviewitem由ImageView和两个TextViews(listviewitem.xml)组成
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="?android:attr/listPreferredItemHeight"
android:background="@drawable/menu_item_background_selector"
android:padding="6dip" >
<ImageView
android:id="@+id/icon"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:layout_alignParentBottom="true"
android:layout_alignParentTop="true"
android:layout_marginRight="6dip"
android:contentDescription="TODO"
android:src="@drawable/ic_launcher" />
<TextView
android:id="@+id/secondLine"
android:layout_width="fill_parent"
android:layout_height="26dip"
android:layout_alignParentBottom="true"
android:layout_alignParentRight="true"
android:layout_toRightOf="@id/icon"
android:ellipsize="marquee"
android:singleLine="true"
android:text="Description"
android:textSize="12sp" />
<TextView
android:id="@+id/firstLine"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_above="@id/secondLine"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:layout_alignWithParentIfMissing="true"
android:layout_toRightOf="@id/icon"
android:gravity="center_vertical"
android:text="Example application"
android:textSize="16sp" />
</RelativeLayout>
where menu menu_item_background_selector(menu_item_background_selector.xml):
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_activated="true"></item>
<solid android:color="#bfced4"></solid>
</selector>
答案 0 :(得分:0)
选择器错了。该项应包含state
和color
。你应该有一个默认项目。 E.g。
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="#bfced4" android:state_activated="true" />
<item android:drawable="@color/default_color" />
</selector>
Android将从顶部检查状态。因此,如果android:state_activated
为真,则会拾取相应的颜色。如果为false,则检查选择器上的下一个,直到它到达最后一个