gpt4 book ai didi

java - 如何在使用 ViewFlipper 的 showNext() 后更改 ListFragment 的 ListView?

转载 作者:太空狗 更新时间:2023-10-29 13:28:24 26 4
gpt4 key购买 nike

我有一个带有自定义布局的 ListFragment。我想要做的是通过使用 ViewFlipper 动画以相同的布局切换我的 ListView。这实际上有效,但因为我使用 ArrayAdapter 来填充两个 ListView,所以 setListAdapter() 似乎没有刷新我的第二个 ListView,即使我在调用 showNext() 之后使用它也是如此。我如何告诉我的 ListFragment 我已经更改了 View ?有可能吗?

这是我的自定义布局,因为我也在使用自定义 ListView,所以我必须对两个列表都使用 android:id="@id/android:list"。

   <LinearLayout>
<TextView/>

<ListView
android:id="@id/android:list"
android:layout_width="match_parent"
android:layout_height="0dip"
android:layout_weight="1"
android:drawSelectorOnTop="false"/>
</LinearLayout>

<LinearLayout>
<TextView/>

<ListView
android:id="@id/android:list"
android:layout_width="match_parent"
android:layout_height="0dip"
android:layout_weight="1"
android:drawSelectorOnTop="false"/>
</LinearLayout>

</ViewFlipper>

这是我的部分代码:

headerFlipper = (ViewFlipper)getActivity().findViewById(R.id.viewFlipper);
headerFlipper.showNext();

// at this point a simple getListView() returns the same first ListView object.

// this changes the first ListView (not visible anymore)
// how to change the second one?
setListAdapter(new ArrayAdapter<DummyContent.DummyMessage>(getActivity(),
android.R.layout.simple_list_item_activated_1,
android.R.id.text1, DummyContent.MESSAGES));

有什么建议吗?提前致谢。

最佳答案

您的 viewflipper 中有两个具有相同 ID 的项目,因此该 Activity 将只使用一个。为此,您需要将当前的 ListActivity 更改为 FragmentActivity然后创建两个单独的 ListFragment处理每个列表的 Activity 。

请记住,其中一些类名可能会根据您是否使用支持库而改变,但总体而言它们之间的差异很小。

对于布局,只需将 fragment 的两个 LinearLayouts 放在它们自己的 xml 文件中,并将它们包含在 viewFlipper xml 中。

viewflipper.xml

<?xml version="1.0" encoding="utf-8"?>
<ViewFlipper xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/viewFlipper"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >

<include
android:id="@+id/fragListOne"
layout="@layout/listfragmentone" />

<include
android:id="@+id/fragListTwo"
layout="@layout/listfragmenttwo" />

</ViewFlipper>

然后对于每个 fragment 你可以使用类似的东西:

listfragmentone & listfragmenttwo

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingLeft="8dp"
android:paddingRight="8dp">

<ListView android:id="@+id/android:list"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#00FF00"
android:layout_weight="1"
android:drawSelectorOnTop="false"/>

<TextView android:id="@id/android:empty"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#FF0000"
android:text="No data"/>
</LinearLayout>

在代码方面,您将拥有协调数据和所有内容的主要 Activity ,以及分别处理每个 ListView 的两个 ListView fragment 。主 Activity 还需要跟踪哪一个是可见的,等等。

主要 Activity - 扩展 FragmentActivity

public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);

setContentView(R.id.viewFlipper);

// use fragment manager to see which fragments are instantiated
// and available, then reuse any available in case of
// orientation change

FragmentManager.enableDebugLogging(true)
FragmentManager fragManager = getSupportFragmentManager();
ListFragmentOne fragOne = fragManager.findFragmentById("frag one tag");

// if fragOne is null add a new instance via an add transaction
// etc.
}

fragment Activity ,扩展ListFragment , 应该类似于正常的列表 Activity ,除了你将使用 onCreateView而不是 onCreate .

关于java - 如何在使用 ViewFlipper 的 showNext() 后更改 ListFragment 的 ListView?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18993049/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com