我是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);
}
我還是不明白,這是什麼問題? –
問題是ListView沒有在NestedScrollView中工作,我不知道如何用其他東西替換它:/ – Kalina