ViewSwitcher 作为 ListView 项目 - 如何在正确的 ViewSwitcher 项目上调用 .showNext() ?
我有一个带有 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() ?
I have a ListView with ViewSwitcher items. The goal is to tap a list item to switch between ViewSwitcher views. Tapping works as expected, but scrolling away from a tapped item and then back sometimes changes which item is displaying the second view from the ViewSwitcher.
Here is my ListView item layout, the ViewSwitcher is @+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>
My Activity that finds @id/details and calls .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();
}
});
}
}
I think my problem might be that @id/details is not actually unique, so undefined behavior happens when scrolling.
If this is the case, then how can I assign a unique ID to each ViewSwitcher or find the correct ViewSwitcher to call .showNext() on?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
你的错误代码在
请发布代码
但是这里有一些提示
一个简单的方法是使ArrayList成为视图切换器的数组列表
Your wrong code is in
Please post the code
However here are some Hints
A simple way would be to make the ArrayList an ArrayList of ViewSwitchers