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

发布于 2024-11-28 12:36:49 字数 2036 浏览 1 评论 0原文

我有一个带有 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

旧人 2024-12-05 12:36:49

你的错误代码在

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

请发布代码

但是这里有一些提示

  1. 扩展ArrayAdapter
  2. 添加一个ArrayList,其中包含所有项目
  3. 在这个ArrayList中保留每个ViewSwitcher的状态

一个简单的方法是使ArrayList成为视图切换器的数组列表

Your wrong code is in

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

Please post the code

However here are some Hints

  1. Extend ArrayAdapter
  2. Add an ArrayList with all the items inside
  3. Keep in this ArrayList the status of each ViewSwitcher

A simple way would be to make the ArrayList an ArrayList of ViewSwitchers

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文