【问题标题】:Singular screen scrolling with two fragments (Fragment and ListFragment)带有两个片段(Fragment 和 ListFragment)的单屏滚动
【发布时间】: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


    【解决方案1】:

    我需要一些时间来设置一个示例来确定什么是可行的,但我有几个想法你可能想尝试一下:

    • 不要对 ListFragment 使用 SimpleCursorAdapter,而是使用 HeaderViewListAdapter 检查。我从未使用过其中之一,因此我无法真正告诉您如何设置 headerViewInfos ArrayList,但如果您知道如何使用它,描述听起来就像您想要的那样。

    • 您可以考虑在ScrollView 中组合这些片段。虽然我不知道这对 ListFragment 的效果如何,但你可以尝试一下。

    所以这并不是一个真正确定的答案,但这是你可以走的几个途径,所以如果没有其他人回答你就不会陷入死胡同。

    【讨论】:

    • 感谢您的回复!我尝试了 ScrollView 方法,但这似乎与 ListFragments 搭配得并不好。我发现了一些对类似问题的模糊参考,这些参考似乎证实了问题出在类本身。 HeaderViewListAdapter 看起来很有希望。我认为它也值得一试,我以前也没有使用过。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-10-25
    • 1970-01-01
    • 1970-01-01
    • 2021-11-11
    • 2011-07-16
    • 2019-01-10
    • 2017-01-04
    相关资源
    最近更新 更多