当前位置: 首页 > 知识库问答 >
问题:

用列表视图替换主/详细流中的NestedScrollView

越昊穹
2023-03-14

我对Android不熟悉,几乎没有时间做作业,所以我想我会使用“模板”,比如“主/细节流”来加快我的工作速度。也许这不是最好的决定,因为我需要很长时间才能理解给定的代码,即使现在我仍然什么都不理解...但是现在从头开始已经太晚了。

我有一个食谱列表,当一个食谱被点击时,我可以看到细节(用主/细节-模板完成)。一个食谱有一个配料列表和一个描述如何准备它的字符串。我写了一个自定义适配器来显示细节片段中的配料列表,起初它不起作用。原因是我使用的ListView被放在“recipe_detail_activity.xml”中的NestedScrollView中。这,正如我发现的,不能工作,因为它们都有滚动机制。我试图用LinearLayout替换NestedScrollView,因为我认为这只是一个简单的容器,但是然后成分列表呈现在屏幕顶部(而不是NestedScrollView曾经的位置),并且描述根本没有呈现。

它现在的工作方式:RecipeDetailActivity。java用recipe_detail_活动注册一个片段。xml作为容器,RecipeDetailFragment将配方详细信息膨胀。xml,其中包含用于成分的列表视图和用于描述的文本视图。

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

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>

ctivity.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();
    }
}

倒数第二个碎片。爪哇:

 @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个答案

能正青
2023-03-14

好的,我通过在NestedScrollView中添加“android:fillViewport=“true”来修复它。这样我就可以在它里面使用一个常规的列表视图。我不知道这个解决方案是否花哨,但它是有效的。

 类似资料:
  • 问题内容: 我有一个列表,我想用condition()返回True的None替换值。 例如,如果条件检查bool(item%2)应该返回: 最有效的方法是什么? 问题答案: 使用列表理解来构建新列表: 您可以根据需要修改原始列表,但实际上并不能节省时间: 以下是(Python 3.6.3)演示非节省时间的时间: 和Python 2.7.6计时:

  • 问题内容: 抱歉,找不到答案,我几乎可以肯定有人提出过这个问题。 我的问题是我正在编写一些系统库来运行嵌入式设备。我有可以通过无线电广播发送到这些设备的命令。这只能通过文本来完成。在系统库中,我有一个线程来处理看起来像这样的命令 问题在于,要执行的命令很多,很快就会变成无法控制的事情。在几个月的时间里,看起来很可怕,调试起来很痛苦,而且难以理解。 问题答案: 使用命令模式: 然后构建一个对象并用实

  • 问题内容: 我有一个目标iPhone 6应用程序的拆分视图界面。在首次启动该应用程序时,它将打开到“详细信息视图”。我希望它可以打开“主视图”。我努力了: 这是在其他地方建议的,但是它似乎没有任何作用,并且在启动时不会打开Master视图。我还尝试将以下行添加到我的AppDelegate中: 但是,尽管返回true或false另一个先前的堆栈溢出问题,我还是没有成功。 我确实在Xcode中启动了示

  • 我有一个android布局,它有一个,里面有很多元素。在的底部有一个,然后由适配器填充。 我遇到的问题是,android将从中排除,因为已经具有可滚动的功能。我希望与内容一样长,并且主滚动视图是可滚动的。 我怎样才能达到这种行为呢? 下面是我的主要布局: 然后以编程方式将组件添加到具有ID:的linearlayour中。下面是加载到LinearLayout中的一个视图。就是这个给我卷轴带来麻烦的。

  • 本文向大家介绍Sql Server中的系统视图详细介绍,包括了Sql Server中的系统视图详细介绍的使用技巧和注意事项,需要的朋友参考一下 本来想这个系列写点什么好呢,后来想想大家作为程序员,用的最多的莫过于数据库了,但是事实上很多像我这样工作在一线的码农,对sql都一知半解,别谈优化和对数据库底层的认识了,我也是这样。。。  一:那些系统视图 1. 系统视图是干什么呢?   从名字上看就知道

  • Navicat 提供三种在主窗口查看对象的类型。在默认情况下,Navicat 在对象列表窗格使用 列表 查看。它只显示对象的名字。你可以从主菜单选择 查看 -> 列表 或在右下角点击 。 详细信息 查看以列显示数个对象的属性。要改变到详细信息查看,从主菜单选择 查看 -> 详细信息 或在右下角点击 。 要改变显示属性的列,从主菜单选择 查看 -> 选择列 并在弹出窗口为不同的对象选择显示的列。 除