我混淆了如何在一个片段中组合viewpager和listview

时间:2015-02-05 02:21:36

标签: android android-listview

当我在同一个片段中使用viewpager和listview时,我遇到了一个问题。 首先,更新您的列表视图; 其次,更改viewpager的currentitem,然后你的listview将改变它的位置。 如何将listview保持在同一位置? 这是我的xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="#e6e6e6"
    android:orientation="vertical">
    <TextView
        android:id="@id/knowledge2_title"
        android:layout_width="match_parent"
        android:layout_height="@dimen/title_height"
        android:background="@color/titlebg"
        android:gravity="center"
        android:text="知识"
        android:textColor="@android:color/white"
        android:textSize="@dimen/titletextsize" />
    <RelativeLayout
        android:id="@id/knowledge2_fragment"
        android:layout_width="match_parent"
        android:layout_height="@dimen/knowledge_image_height"
        android:layout_below="@id/knowledge2_title" >
        <android.support.v4.view.ViewPager
            android:id="@id/knowledge_pager"
            android:layout_width="match_parent"
            android:layout_height="@dimen/knowledge_image_height"
            android:layout_alignParentTop="true"/>
        <RelativeLayout
            android:id="@id/knowledge_rl1"
            android:layout_width="match_parent"
            android:layout_height="@dimen/knowledge_image_texth"
            android:layout_alignBottom="@id/knowledge_pager"
            android:background="#80000000">
            <TextView
                android:id="@id/knowledge_pagertext"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:padding="13dip"
                android:textSize="20sp"
                android:textColor="@android:color/white"/>
            <RadioGroup
                android:id="@id/knowledge_radiogroup"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:orientation="horizontal"
                android:layout_centerHorizontal="true"
                android:layout_alignParentBottom="true"
                android:layout_marginBottom="10dip">
                <RadioButton
                    android:id="@id/knowledge_rb1"
                    style="@style/image_dotstyle"
                    android:layout_marginRight="10dip"/>
                <RadioButton
                    android:id="@id/knowledge_rb2"
                    style="@style/image_dotstyle"
                    android:layout_marginRight="10dip"/>
                <RadioButton
                    android:id="@id/knowledge_rb3"
                    style="@style/image_dotstyle"
                    android:layout_marginRight="10dip"/>
                <RadioButton
                    android:id="@id/knowledge_rb4"
                    style="@style/image_dotstyle"/>
            </RadioGroup>
        </RelativeLayout>
    </RelativeLayout>
    <com.yeahis.swimcoach.widget.MySwipeRefreshLayout
        android:id="@id/knowledge2_swiperefresh"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true">
        <ListView
            android:id="@id/knowledge2_recycler"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginTop="10dip"
            android:background="@android:color/transparent"
            android:scrollbars="vertical"
            android:divider="@null"/>
    </com.yeahis.swimcoach.widget.MySwipeRefreshLayout>
</LinearLayout>

com.yeahis.swimcoach.widget.MySwipeRefreshLayout与android.support.v4.widget.SwipeRefreshLayout相同。

1 个答案:

答案 0 :(得分:0)

我认为你应该做的是创建2个布局:一个处理viewpager内容,一个处理viewpagerlistview

layout_1.xml
<LinearLayout...>
   Content to be refresh in this layout
</LinearLayout>

layout_2.xml
<LinearLayout...>
   <ViewPager .../>
   <ListView ..../>
</LinearLayout>

您创建了一个适配器来处理viewpager中显示layout_1.xml的viewpager和内容,每当您滑动时,listview都将保持不变。

要使用onPageChangeListener

更新您可以监控的listview