ViewSwitcher作为ListView项目 - 如何在正确的ViewSwitcher项目上调用.showNext()?

时间:2011-08-09 23:36:54

标签: android listview android-layout viewswitcher

我有一个带有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>

我的活动找到@ 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()?

1 个答案:

答案 0 :(得分:1)

您的错误代码在

// Cursor/Adapter code here to populate list from SQLite DB.

请发布代码

但是这里有一些提示

  1. 扩展ArrayAdapter
  2. 添加包含
  3. 内所有项目的ArrayList
  4. 在此ArrayList中保留每个ViewSwitcher的状态
  5. 一种简单的方法是使ArrayList成为ViewSwitchers的ArrayList