用ListView替换主/详细流程中的NestedScrollView

时间:2016-11-07 14:22:37

标签: android listview master-detail nestedscrollview

我是Android新手,没有时间做任务,所以我发现我会使用"模板"喜欢Master / Detail Flow来加快我的工作。也许这不是最好的决定,因为我需要很长时间来理解给定代码,即使现在我仍然不了解所有内容......但是现在从头开始已经太晚了。 / p>

我有一份食谱清单,当点击食谱时我可以看到详细信息(使用主/详细模板完成)。配方有一个成分列表和一个字符串,描述如何准备它。我写了一个自定义适配器来显示细节片段中的成分列表,它起初并没有起作用。原因是我使用的ListView被放置在" recipe_detail_activity.xml"中的NestedScrollView中。正如我所发现的那样,这是行不通的,因为它们都有滚动机制。我试着用的LinearLayout更换NestedScrollView,因为我认为这' S只是一个简单的容器,但随后的成分表在屏幕上(而不是在NestedScrollView曾经是)的顶部渲染和描述不是渲染一点都不。

它现在的工作方式:RecipeDetailActivity.java将recipe_detail_activity.xml的片段注册为容器,RecipeDetailFragment注释recipe_detail.xml,其中包含成分的ListView和描述的TextView。

我知道,我显然缺乏知识,应该从简单的事情开始,但我的教授要求很高......如果有人能给我提示,我会很高兴。

recipe_activity_detail.xml:

<android.support.design.widget.CoordinatorLayout 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:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
tools:context="com.kalina.kochapp.RecipeDetailActivity"
tools:ignore="MergeRootFrame">

<android.support.design.widget.AppBarLayout
    android:id="@+id/app_bar"
    android:layout_width="match_parent"
    android:layout_height="@dimen/app_bar_height"
    android:fitsSystemWindows="true"
    android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar">

    <android.support.design.widget.CollapsingToolbarLayout
        android:id="@+id/toolbar_layout"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:fitsSystemWindows="true"
        app:contentScrim="?attr/colorPrimary"
        app:layout_scrollFlags="scroll|exitUntilCollapsed"
        app:toolbarId="@+id/toolbar">

        <android.support.v7.widget.Toolbar
            android:id="@+id/detail_toolbar"
            android:layout_width="match_parent"
            android:layout_height="?attr/actionBarSize"
            app:layout_collapseMode="pin"
            app:popupTheme="@style/ThemeOverlay.AppCompat.Light" />

    </android.support.design.widget.CollapsingToolbarLayout>

</android.support.design.widget.AppBarLayout>

<android.support.v4.widget.NestedScrollView
    android:id="@+id/fragment_container"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    app:layout_behavior="@string/appbar_scrolling_view_behavior" >
</android.support.v4.widget.NestedScrollView>

<android.support.design.widget.FloatingActionButton
    android:id="@+id/fab"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_gravity="center_vertical|start"
    android:layout_margin="@dimen/fab_margin"
    app:layout_anchor="@+id/recipe_detail_container"
    app:layout_anchorGravity="top|end"
    app:srcCompat="@android:drawable/stat_notify_chat" />

</android.support.design.widget.CoordinatorLayout>

recipe_detail.xml:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">

<ListView
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:id="@+id/lv_recipe_ingredients"
    style="?android:attr/textAppearanceLarge"
    android:padding="16dp"
    tools:context="com.kalina.kochapp.RecipeDetailFragment"/>

<TextView xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/recipe_instructions"
    style="?android:attr/textAppearanceLarge"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:padding="16dp"
    android:textIsSelectable="true"
    tools:context="com.kalina.kochapp.RecipeDetailFragment" />

</LinearLayout>

RecipeDetailActivity.java:

protected void onCreate(Bundle savedInstanceState) {
[...]
if (savedInstanceState == null) {
    // Create the detail fragment and add it to the activity
    // using a fragment transaction.
    Bundle arguments = new Bundle();
    arguments.putString(RecipeDetailFragment.ARG_ITEM_ID,
            getIntent().getStringExtra(RecipeDetailFragment.ARG_ITEM_ID));
    RecipeDetailFragment fragment = new RecipeDetailFragment();
    fragment.setArguments(arguments);
    getSupportFragmentManager().beginTransaction()
            .add(R.id.recipe_detail_container, fragment)
            .commit();
    }
}

RecipeDetailFragment.java:

 @Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    View rootView = inflater.inflate(R.layout.recipe_detail, container, false);

    // Show the dummy content as text in a TextView.
    if (mItem != null) {
        ((TextView) rootView.findViewById(R.id.recipe_instructions)).setText(mItem.instructions);
    }

    return rootView;
}

@Override
public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
    super.onViewCreated(view, savedInstanceState);

    ingredients = (ListView)getView().findViewById(R.id.lv_recipe_ingredients);
    IngredientsListAdapter ila = new IngredientsListAdapter(this.getContext(), mItem.ingredients);
    ingredients.setAdapter(ila);
}

1 个答案:

答案 0 :(得分:0)

好的,我通过添加&#34; android:fillViewport =&#34; true&#34;&#34;来修复它。到我的NestedScrollView。这样我就可以在里面使用常规的ListView。我不知道这个解决方案是否很花哨,但它确实有效。