【发布时间】:2023-03-20 12:54:01
【问题描述】:
我是 android 新手,几乎没有时间做作业,所以我想我会使用像 Master/Detail Flow 这样的“模板”来加快我的工作。也许这不是最好的决定,因为我需要很长时间才能理解给定的代码,即使现在我仍然无法理解所有内容......但现在从头开始已经太晚了。
我有一个食谱列表,当点击一个食谱时,我可以看到详细信息(使用 Master/Detail-Template 完成)。食谱有一个成分列表和一个描述如何准备它的字符串。我编写了一个自定义适配器来显示详细信息片段中的成分列表,但起初它不起作用。原因是我使用的ListView放在了“recipe_detail_activity.xml”中的NestedScrollView中。正如我发现的那样,这是行不通的,因为它们都有滚动机制。我尝试用 LinearLayout 替换 NestedScrollView,因为我认为它只是一个简单的容器,但随后成分列表呈现在屏幕顶部(而不是 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);
}
【问题讨论】:
-
还是不明白,有什么问题?
-
问题是 ListView 在 NestedScrollView 中不起作用,我不知道如何用其他东西替换它:/
标签: android listview master-detail nestedscrollview