2016-11-07 114 views
0

我是android新手,很少有时间做任务,所以我想通过Master/Detail Flow之类的“模板”来加速我的工作。也许这不是最好的决定,因为我需要很长时间才能理解给定的代码,甚至现在我仍然不明白所有事情......但是现在从头开始为时已晚。用ListView替换Master/Detail Flow中的NestedScrollView

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

现在它的工作方式:RecipeDetailActivity.java将一个带有recipe_detail_activity.xml的片段注册为容器,RecipeDetailFragment对recipe_detail.xml进行注入,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); 
} 
+0

我还是不明白,这是什么问题? –

+0

问题是ListView没有在NestedScrollView中工作,我不知道如何用其他东西替换它:/ – Kalina

回答

0

好吧,我固定它通过增加 “机器人:fillViewport =” 真 “” 我NestedScrollView。这样我可以在里面使用常规的ListView。我不知道这个解决方案是否是幻想,但它的工作原理。

+0

当你完成你的项目,当你有时间时,阅读RecyclerView它是如此的非常棒!你会爱上持有者,视图类型,适配器和RecyclerView,你可以制造魔术!很高兴听到你解决了你的问题。} –

+0

谢谢@Ninja编码:)我会尽量找一些时间来看看RecyclerView! – Kalina