我有一个带有 ViewSwitcher 项目的 ListView。目标是点击列表项以在 ViewSwitcher 视图之间切换。点击按预期工作,但是从点击的项目滚动然后返回有时会改变哪个项目正在显示来自 ViewSwitcher 的第二个视图。
这是我的 ListView 项目布局,ViewSwitcher 是@+id/details:
<?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 = "wrap_content" >
<ViewSwitcher
android:id = "@+id/details"
android:layout_width = "fill_parent"
android:layout_height = "fill_parent"
android:measureAllChildren = "false" >
<!-- two views I want to switch between are here -->
</ViewSwitcher>
</RelativeLayout>
我的 Activity 找到了@id/details 并调用了 .showNext()。
public class MyActivity extends ListActivity {
private MySQLiteOpenHelper data;
private SimpleCursorAdapter adapter;
private Cursor cursor;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
String[] FROM = { "_id", "_id", "name", "name" };
int[] TO = { R.id.my_list_id, R.id.my_list_id2, R.id.my_list_name, R.id.my_list_name_details };
data = new MySQLiteOpenHelper(this);
cursor = data.myList();
startManagingCursor(cursor);
adapter = new SimpleCursorAdapter(this, R.layout.list_item, cursor, FROM, TO);
setListAdapter(adapter);
ListView list = getListView();
list.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> adapter, View view, int position, long id) {
ViewSwitcher item = (ViewSwitcher) view.findViewById(R.id.details);
item.showNext();
}
});
}
}
我认为我的问题可能是 @id/details 实际上并不是唯一的,因此滚动时会发生未定义的行为。
如果是这种情况,那么如何为每个 ViewSwitcher 分配唯一 ID 或找到正确的 ViewSwitcher 来调用 .showNext() ?