Android: 如何将viewflipper 与listview 一起滚动?
Android: How to scroll viewflipper along with listview?
在 listview_main.xml 文件中,我在 RelativeLayout 中有 viewflipper 和 listview。当我滚动列表视图时,viewflipper 不会与列表视图一起滚动。 Viewflipper 与列表视图重叠。
我希望 viewflipper 与 listview 一起滚动,或者当我滚动 listview 时,我希望 viewflipper 与 listview 重叠。两者都可以。
我整天都在寻找解决方案,但我找不到任何解决方案。谁能帮我这个?提前致谢。
<?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="fill_parent" >
<ViewFlipper
android:layout_width="fill_parent"
android:layout_height="250dp"
android:background="#ffffff"
android:scaleType="centerCrop"
android:adjustViewBounds="true"
android:id="@+id/flipper" />
<ListView
android:id="@+id/listview"
android:layout_below="@id/flipper"
android:background="#ffffff"
android:layout_width="match_parent"
android:layout_height="fill_parent" />
</RelativeLayout>
我找到了解决办法。我将 viewflipper 作为 header 视图添加到 listview 并且它有效。现在,当我滚动列表视图时,viewflipper 会随之滚动。
我为 viewflipper (listview_header.xml) 和 listview (listview_main.xml) 创建了两个不同的 xml 文件。然后,我将 viewflipper 添加到列表视图中作为 header 视图。
listview_header.xml
<?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="fill_parent" >
<ViewFlipper
android:layout_width="fill_parent"
android:layout_height="260dp"
android:background="#ffffff"
android:scaleType="centerCrop"
android:adjustViewBounds="true"
android:layout_alignParentTop="true"
android:id="@+id/flipper" />
</RelativeLayout>
listview_main.xml
<?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="fill_parent" >
<ListView
android:id="@+id/listview"
android:layout_below="@id/flipper"
android:background="#ffffff"
android:layout_width="fill_parent"
android:layout_height="fill_parent" />
</RelativeLayout>
将 viewflipper(listview_header.xml) 作为 header 视图添加到 listview(listview_main.xml) 的代码。
ListView listview = (ListView) findViewById(R.id.listview);
LayoutInflater inflater = getLayoutInflater();
ViewGroup listviewHeader = (ViewGroup) inflater.inflate(R.layout.listview_header, listview, false);
//add header view to listview
listview.addHeaderView(listviewHeader, null, false);
ArrayAdapter adapter = new ArrayAdapter(this, arrayList);
//Set adapter
listview.setAdapter(adapter);
我希望这对面临同样问题的人有所帮助。
在 listview_main.xml 文件中,我在 RelativeLayout 中有 viewflipper 和 listview。当我滚动列表视图时,viewflipper 不会与列表视图一起滚动。 Viewflipper 与列表视图重叠。
我希望 viewflipper 与 listview 一起滚动,或者当我滚动 listview 时,我希望 viewflipper 与 listview 重叠。两者都可以。
我整天都在寻找解决方案,但我找不到任何解决方案。谁能帮我这个?提前致谢。
<?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="fill_parent" >
<ViewFlipper
android:layout_width="fill_parent"
android:layout_height="250dp"
android:background="#ffffff"
android:scaleType="centerCrop"
android:adjustViewBounds="true"
android:id="@+id/flipper" />
<ListView
android:id="@+id/listview"
android:layout_below="@id/flipper"
android:background="#ffffff"
android:layout_width="match_parent"
android:layout_height="fill_parent" />
</RelativeLayout>
我找到了解决办法。我将 viewflipper 作为 header 视图添加到 listview 并且它有效。现在,当我滚动列表视图时,viewflipper 会随之滚动。
我为 viewflipper (listview_header.xml) 和 listview (listview_main.xml) 创建了两个不同的 xml 文件。然后,我将 viewflipper 添加到列表视图中作为 header 视图。
listview_header.xml
<?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="fill_parent" >
<ViewFlipper
android:layout_width="fill_parent"
android:layout_height="260dp"
android:background="#ffffff"
android:scaleType="centerCrop"
android:adjustViewBounds="true"
android:layout_alignParentTop="true"
android:id="@+id/flipper" />
</RelativeLayout>
listview_main.xml
<?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="fill_parent" >
<ListView
android:id="@+id/listview"
android:layout_below="@id/flipper"
android:background="#ffffff"
android:layout_width="fill_parent"
android:layout_height="fill_parent" />
</RelativeLayout>
将 viewflipper(listview_header.xml) 作为 header 视图添加到 listview(listview_main.xml) 的代码。
ListView listview = (ListView) findViewById(R.id.listview);
LayoutInflater inflater = getLayoutInflater();
ViewGroup listviewHeader = (ViewGroup) inflater.inflate(R.layout.listview_header, listview, false);
//add header view to listview
listview.addHeaderView(listviewHeader, null, false);
ArrayAdapter adapter = new ArrayAdapter(this, arrayList);
//Set adapter
listview.setAdapter(adapter);
我希望这对面临同样问题的人有所帮助。