【问题标题】:How to change a ListFragment's ListView after using ViewFlipper's showNext()?使用 ViewFlipper 的 showNext() 后如何更改 ListFragment 的 ListView?
【发布时间】:2013-09-24 22:16:45
【问题描述】:

我有一个带有自定义布局的 ListFragment。我要做的是使用 ViewFlipper 动画将我的 ListView 切换到相同的布局中。这实际上可行,但是当我使用 ArrayAdapter 填充两个 ListViews 时,setListAdapter() 似乎并没有刷新我的第二个 ListView,即使我在调用 showNext() 后使用它也很困难。如何告诉我的 ListFragment 我已更改视图?有没有可能?

这是我的自定义布局,因为我也在使用自定义 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));

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

【问题讨论】:

    标签: java android android-layout android-listview android-listfragment


    【解决方案1】:

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

    请记住,根据您是否使用支持库,其中一些类名可能会发生变化,但总体而言,它们之间几乎没有区别。

    对于布局,只需将片段的两个 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>
    

    然后对于每个片段,您可以使用如下内容:

    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>
    

    代码方面,您将拥有协调数据和所有内容的主要活动,以及分别处理每个列表视图的两个列表视图片段。主要活动还需要跟踪哪个是可见的,等等。

    主要活动 - 扩展 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.
    }
    

    扩展 ListFragment 的 Fragment 活动应该类似于普通的列表活动,除了您将使用 onCreateView 而不是 onCreate

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-04-16
      • 1970-01-01
      • 1970-01-01
      • 2012-04-19
      • 1970-01-01
      • 2012-09-15
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多