java如何膨胀合并
我通过适配器创建了一个帐户设置页面,其中包含选项的部分标题和列表视图。列表视图是一个“合并”视图,因此可以将其包含到主文件中。然而,似乎合并文件的思想是不能膨胀的
这是我得到的错误:
E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.archive.pod, PID: 13046
安卓.view.InflateException: Binary XML file line #1 in com.archive.pod:layout/layout_center_accountsettings: <merge /> can be used only with a valid ViewGroup root and attachToRoot=true
Caused by: 安卓.view.InflateException: <merge /> can be used only with a valid ViewGroup root and attachToRoot=true
at 安卓.view.LayoutInflater.inflate(LayoutInflater.java:652)
at 安卓.view.LayoutInflater.inflate(LayoutInflater.java:534)
at com.archive.pod.Profile.AccountSettingsAdapter.getView(AccountSettingsAdapter.java:66)
at 安卓.widget.AbsListView.obtainView(AbsListView.java:2405)
at 安卓.widget.ListView.makeAndAddView(ListView.java:2071)
at 安卓.widget.ListView.fillDown(ListView.java:797)
at 安卓.widget.ListView.fillFromTop(ListView.java:859)
at 安卓.widget.ListView.layoutChildren(ListView.java:1842)
at 安卓.widget.AbsListView.onLayout(AbsListView.java:2202)
at 安卓.view.View.layout(View.java:22466)
at 安卓.view.ViewGroup.layout(ViewGroup.java:6304)
at 安卓.widget.RelativeLayout.onLayout(RelativeLayout.java:1103)
at 安卓.view.View.layout(View.java:22466)
at 安卓.view.ViewGroup.layout(ViewGroup.java:6304)
at 安卓.widget.RelativeLayout.onLayout(RelativeLayout.java:1103)
at 安卓.view.View.layout(View.java:22466)
at 安卓.view.ViewGroup.layout(ViewGroup.java:6304)
at 安卓.widget.RelativeLayout.onLayout(RelativeLayout.java:1103)
at 安卓.view.View.layout(View.java:22466)
at 安卓.view.ViewGroup.layout(ViewGroup.java:6304)
at 安卓x.coordinatorlayout.widget.CoordinatorLayout.layoutChild(CoordinatorLayout.java:1183)
at 安卓x.coordinatorlayout.widget.CoordinatorLayout.onLayoutChild(CoordinatorLayout.java:870)
at 安卓x.coordinatorlayout.widget.CoordinatorLayout.onLayout(CoordinatorLayout.java:889)
at 安卓.view.View.layout(View.java:22466)
at 安卓.view.ViewGroup.layout(ViewGroup.java:6304)
at 安卓.widget.FrameLayout.layoutChildren(FrameLayout.java:332)
at 安卓.widget.FrameLayout.onLayout(FrameLayout.java:270)
at 安卓.view.View.layout(View.java:22466)
at 安卓.view.ViewGroup.layout(ViewGroup.java:6304)
at 安卓.widget.LinearLayout.setChildFrame(LinearLayout.java:1829)
at 安卓.widget.LinearLayout.layoutVertical(LinearLayout.java:1673)
at 安卓.widget.LinearLayout.onLayout(LinearLayout.java:1582)
at 安卓.view.View.layout(View.java:22466)
at 安卓.view.ViewGroup.layout(ViewGroup.java:6304)
at 安卓.widget.FrameLayout.layoutChildren(FrameLayout.java:332)
at 安卓.widget.FrameLayout.onLayout(FrameLayout.java:270)
at 安卓.view.View.layout(View.java:22466)
at 安卓.view.ViewGroup.layout(ViewGroup.java:6304)
at 安卓.widget.LinearLayout.setChildFrame(LinearLayout.java:1829)
at 安卓.widget.LinearLayout.layoutVertical(LinearLayout.java:1673)
at 安卓.widget.LinearLayout.onLayout(LinearLayout.java:1582)
at 安卓.view.View.layout(View.java:22466)
at 安卓.view.ViewGroup.layout(ViewGroup.java:6304)
at 安卓.widget.FrameLayout.layoutChildren(FrameLayout.java:332)
at 安卓.widget.FrameLayout.onLayout(FrameLayout.java:270)
at com.安卓.internal.policy.DecorView.onLayout(DecorView.java:779)
at 安卓.view.View.layout(View.java:22466)
at 安卓.view.ViewGroup.layout(ViewGroup.java:6304)
at 安卓.view.ViewRootImpl.performLayout(ViewRootImpl.java:3157)
at 安卓.view.ViewRootImpl.performTraversals(ViewRootImpl.java:2614)
at 安卓.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1745)
at 安卓.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:7768)
at 安卓.view.Choreographer$CallbackRecord.run(Choreographer.java:967)
at 安卓.view.Choreographer.doCallbacks(Choreographer.java:791)
at 安卓.view.Choreographer.doFrame(Choreographer.java:726)
at 安卓.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:952)
at 安卓.os.Handler.handleCallback(Handler.java:883)
at 安卓.os.Handler.dispatchMessage(Handler.java:100)
at 安卓.os.Looper.loop(Looper.java:214)
at 安卓.app.ActivityThread.main(ActivityThread.java:7356)
at java.lang.reflect.Method.invoke(Native Method)
at com.安卓.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:492)
at com.安卓.internal.os.ZygoteInit.main(ZygoteInit.java:930)
合并ListView文件布局\u中心\u帐户设置。xml
<merge xmlns:安卓="http://schemas.安卓.com/apk/res/安卓">
<RelativeLayout
安卓:layout_width="match_parent"
安卓:layout_height="match_parent"
安卓:padding="30dp"
安卓:background="@color/windowBackground">
<ListView
安卓:id="@+id/lvAccountSettings"
安卓:layout_width="match_parent"
安卓:layout_height="match_parent"
安卓:divider="@null"
安卓:dividerHeight="0dp">
</ListView>
</RelativeLayout>
</merge>
帐户设置适配器。爪哇
package com.archive.pod.Profile;
import 安卓.content.Context;
import 安卓.view.LayoutInflater;
import 安卓.view.View;
import 安卓.view.ViewGroup;
import 安卓.widget.BaseAdapter;
import 安卓.widget.TextView;
import com.archive.pod.R;
import java.util.ArrayList;
public class AccountSettingsAdapter extends BaseAdapter {
ArrayList < Object > list;
private static final int OPTIONS = 0;
private static final int HEADER = 1;
private LayoutInflater inflater;
public AccountSettingsAdapter(Context context, ArrayList < Object > list) {
this.list = list;
inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
@Override
public int getItemViewType(int position) {
if (list.get(position) instanceof AccountSettingsOptions) {
return OPTIONS;
} else {
return HEADER;
}
}
@Override
public int getViewTypeCount() {
return 2;
}
@Override
public int getCount() {
return list.size();
}
@Override
public Object getItem(int i) {
return list.get(i);
}
@Override
public long getItemId(int i) {
return i;
}
/** This distinguishes between section title and list view options,
then using switch it beings to populate the options in the list
view with titles and descriptions */
@Override
public View getView(int i, View view, ViewGroup viewGroup) {
if (view == null) {
switch (getItemViewType(i)) {
case OPTIONS:
view = inflater.inflate(R.layout.layout_center_accountsettings, null);
break;
case HEADER:
view = inflater.inflate(R.layout.listview_section_header_accountsettings, null);
break;
}
}
switch (getItemViewType(i)) {
case OPTIONS:
TextView name = view.findViewById(R.id.listViewTitle);
TextView description = view.findViewById(R.id.listViewDescription);
name.setText(((AccountSettingsOptions) list.get(i)).getName());
description.setText(((AccountSettingsOptions) list.get(i)).getDescription());
break;
case HEADER:
TextView title = view.findViewById(R.id.optionListViewHeader);
title.setText(((String) list.get(i)));
break;
}
return view;
}
}
这个问题的简单解决方案是什么?我看过以前的帖子,但不能完全理解他们做了什么
# 1 楼答案
因为
getView
要求您只返回一个View
,很遗憾,您不能通过适配器将<merge>
作为ListView的直接子级<merge>
标记只能膨胀到父级中——这里不是这样,因为getView
需要使用attachToRoot = false
进行膨胀,以便它可以附加视图本身。您需要将其包装在单亲布局中,例如LinearLayout
另外一个警告是:您的项目布局中似乎有一个
ListView
ListView
不能很好地处理嵌套滚动——如果需要,可以查看RecyclerView