0

I am new to android and have little time to do an assignment, so I figured out I will use "Templates" like Master/Detail Flow to speed up my work. Maybe it wasn't the best decision, since I needed ages to understand given code and even now I still don't understand everything... But now it's too late to start from a scratch.

I have a list of recipes, when a recipe is clicked I can see details (done with the Master/Detail-Template). A recipe has a list of ingredients and a string with description how to prepare it. I wrote a custom adapter to show the list of ingredients in the details fragment and it didn't work at first. The reason was that the ListView I used was placed in the NestedScrollView in the "recipe_detail_activity.xml". This, as I found out, can not work, because both of them have scrolling mechanisms. I tried to replace NestedScrollView with a LinearLayout, as I thought it's only a simple container, but then the ingredients list is rendered at the top of the screen (instead of where the NestedScrollView once was) and the description is not rendered at all.

The way it works now: RecipeDetailActivity.java registers a fragment with recipe_detail_activity.xml as container and RecipeDetailFragment inflates recipe_detail.xml, which contains a ListView for ingredients and a TextView for the description.

I know, I'm obviously lacking of knowladge and should start with easy things, but my professor is pretty demanding... I would be very glad if someone could give me a hint.

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);
}
Kalina
  • 11
  • 4

1 Answers1

0

Ok, I fixed it by adding "android:fillViewport="true"" to my NestedScrollView. This way I can use a regular ListView inside of it. I don't know whether this solution is fancy, but it works.

Kalina
  • 11
  • 4
  • After you finish your project and when you have time, read about RecyclerView it's so fking awesome!! you will fall in love with Holders, view types, adapters and RecyclerView, you can make magic!! nice to hear you fixed your problem.} – Ninja Coding Nov 08 '16 at 13:57
  • Thank you @Ninja Coding :) I will try to find some time to take a look on RecyclerView! – Kalina Nov 21 '16 at 11:30