【发布时间】:2015-07-03 03:46:52
【问题描述】:
我有一个活动有时会显示两个片段,其中一个直接在另一个之上。顶部片段只包含几个 TextView 并且很好。下面是一个 ListFragment,它自己的布局使用 SimpleCursorAdapter 填充。
问题在于下部片段的滚动独立于其他所有内容,因此顶部片段始终可见。我希望滚动整个屏幕。
我在顶部布局中尝试了一个静态片段(这会导致其他问题),将 FrameLayout 调整为线性/相对,嵌套片段无济于事。我得到的最好的方法是硬编码下部片段的高度(例如 1000dp),然后至少改变滚动行为。不幸的是,这不是一个可用的解决方案。我知道主布局中的顶级 RelativeLayout 需要位于 a 中,但这并不能解决 ListFragment 嵌套滚动问题。
这两个片段本身功能都很好,当我同时显示它们时,问题就出现了。
有什么方法可以强制我的 ListFragment 以不可滚动的方式显示其所有数据,以便我可以使整个主屏幕滚动为一个?
当前的主布局,activity_main.xml,是:
<android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:id="@+id/container_toolbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="top"
android:orientation="vertical">
<include
android:id="@+id/toolbar"
layout="@layout/toolbar" />
</LinearLayout>
<FrameLayout
android:id="@+id/container_top"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@id/container_toolbar"/>
<FrameLayout
android:id="@+id/container_bottom"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@id/container_bottom"/>
</RelativeLayout>
<fragment
android:id="@+id/fragment_navigation_drawer"
android:name="activity.FragmentDrawer"
android:layout_width="@dimen/nav_drawer_width"
android:layout_height="match_parent"
android:layout_gravity="start"
app:layout="@layout/fragment_navigation_drawer"
tools:layout="@layout/fragment_navigation_drawer" />
</android.support.v4.widget.DrawerLayout>
第二个(ListFragment)布局,fragment_calculations_list,是:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
style="@style/listview_row"
android:background="@drawable/border_style"
android:id="@+id/calclist">
<TextView
android:id="@+id/date_m_d"
android:text="05.23"
style="@style/listview_row_left_top"
/>
<TextView
android:id="@+id/title"
android:layout_below="@id/date_m_d"
style="@style/listview_row_left_bottom"
/>
<TextView
android:id="@+id/value"
android:layout_toRightOf="@id/date_m_d"
style="@style/listview_row_right_top" />
<TextView
android:id="@+id/value_detail"
android:layout_below="@+id/value"
android:layout_toRightOf="@id/date_m_d"
style="@style/listview_row_right_bottom" />
<TextView
android:id="@+id/value_detail2"
android:layout_below="@+id/value"
android:layout_toRightOf="@id/value_detail"
style="@style/listview_row_right_bottom_second" />
</RelativeLayout>
由于它被引用(删除了不必要的东西,如颜色)
<style name="listview_row">
<item name="android:layout_width">match_parent</item>
<item name="android:layout_height">wrap_content</item>
</style>
ListFragment 适配器在 onCreate 中设置:
@Override
public void onCreate(Bundle savedInstanceState) {
if (getArguments() != null) {
pID = getArguments().getInt("id");
}
else {
throw new IllegalArgumentException("Referenced without valid id");
}
String[] selectArgs = {Integer.toString(pID)};
super.query = "myqueryhere";
super.queryArgs = myArgs;
SimpleCursorAdapter adapter=
new SimpleCursorAdapter(getActivity(), R.layout.fragment_calculations_list, current,
new String[] {
"fielda","fieldb", "fieldc", "fieldd", "fielde"
},
new int[] {R.id.date_m_d, R.id.title, R.id.value, R.id.value_detail, R.id.value_detail2},
0);
setListAdapter(adapter);
if (current==null) {
db = new DatabaseHelper(getActivity().getApplicationContext(), "my.db", 1);
task=new LoadCursorTask().execute();
}
}
【问题讨论】:
标签: android android-layout android-listfragment