我通过扩展View
定义了一个简单的自定义LinearLayout
。关键是它包含ExpandableListView
并添加了标题视图:
public class MyView extends LinearLayout {
private View mHeaderView;
private ExpandableListView mListView;
@SuppressWarnings("UnusedDeclaration")
public MyView(Context context) {
super(context);
init(context);
}
@SuppressWarnings("UnusedDeclaration")
public MyView(Context context, AttributeSet attrs) {
super(context, attrs);
init(context);
}
private void init(Context context) {
setOrientation(LinearLayout.VERTICAL);
setDescendantFocusability(ViewGroup.FOCUS_AFTER_DESCENDANTS);
LayoutInflater inflater = LayoutInflater.from(context);
//Inflate custom view into LinearLayout
inflater.inflate(
R.layout.merge_myview, this);
//Inflate header with no parent so we can set it as the ListView header in onFinishInflate()
mHeaderView = inflater.inflate(R.layout.header_bundles_list, null);
}
@Override
protected void onFinishInflate() {
super.onFinishInflate();
mListView = (ExpandableListView) (findViewById(R.id.lv_expandable));
Log.d("MyView", "Almost done...");
//Commenting out this line makes it work
mListView.addHeaderView(mHeaderView, null, false);
}
}
这是merge_myview.xml
:
<merge xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ExpandableListView
android:id="@+id/lv_expandable"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1" />
</merge>
这是标题:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="wrap_content"
>
<TextView
android:id="@+id/tv_title"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:background="#EFEEFF"
android:clickable="true"
android:gravity="center"
android:minHeight="96dp"
android:onClick="openPane"
android:paddingBottom="24dp"
android:paddingTop="24dp"
android:text="HELLO!"
android:textColor="#333333"
android:textIsSelectable="true"
android:textSize="48sp" />
</RelativeLayout>
在我的活动中使用简单onCreate
:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_myview);
}
activity_myview.xml
仅包含我的自定义视图:
<com.example.app.MyView 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:paddingLeft="@dimen/activity_horizontal_margin"
android:orientation="vertical"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin"/>
然而,当我运行此应用程序时,我收到以下错误:
java.lang.NullPointerException at
android.widget.ListView.clearRecycledState(ListView.java:507) at
android.widget.ListView.resetList(ListView.java:493) at
android.widget.ListView.layoutChildren(ListView.java:1431) at
android.widget.AbsListView.onLayout(AbsListView.java:1147) at
android.view.View.layout(View.java:7035) at
android.widget.LinearLayout.setChildFrame(LinearLayout.java:1252) at
android.widget.LinearLayout.layoutVertical(LinearLayout.java:1128) at
android.widget.LinearLayout.onLayout(LinearLayout.java:1045) at
android.view.View.layout(View.java:7035) at
android.widget.FrameLayout.onLayout(FrameLayout.java:333) at
android.view.View.layout(View.java:7035) at
android.widget.LinearLayout.setChildFrame(LinearLayout.java:1252) at
android.widget.LinearLayout.layoutVertical(LinearLayout.java:1128) at
android.widget.LinearLayout.onLayout(LinearLayout.java:1045) at
android.view.View.layout(View.java:7035) at
android.widget.LinearLayout.setChildFrame(LinearLayout.java:1252) at
android.widget.LinearLayout.layoutVertical(LinearLayout.java:1128) at
android.widget.LinearLayout.onLayout(LinearLayout.java:1045) at
android.view.View.layout(View.java:7035)
我认为这是一个在Android 2.3.7和4.1.1之间修复的错误,因为我在使用前者测试时只会收到错误。 我在bug tracker找不到它。 那么为什么会出现这种错误呢?有没有解决办法?
我认为this是错误,已于2013年6月修复。
答案 0 :(得分:2)
尝试不使用合并。
private void init(Context context) {
setOrientation(LinearLayout.VERTICAL);
setDescendantFocusability(ViewGroup.FOCUS_AFTER_DESCENDANTS);
LayoutInflater inflater = LayoutInflater.from(context);
inflater.inflate(R.layout.expandablelistview, this, true);
inflater.inflate(R.layout.textview, this, true);
mHeaderView = inflater.inflate(R.layout.header_bundles_list, null);
}
expandablelistview.xml
<ExpandableListView
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/lv_expandable"
android:layout_width="match_parent"
android:background="#ffffd467"
android:layout_weight="2"
android:layout_height="0dp"/>
textview.xml
<TextView
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/textview"
android:text="HELLO"
android:background="#AA4242"
android:layout_width="match_parent"
android:layout_weight="1"
android:layout_height="0dp"/>
示例onCreate()
:
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_myview);
MyView myView = (MyView) findViewById(R.id.myview);
//Set up adapter
int groupnum=2;
int childnum=3;
List<Map<String, String>> groupData=new ArrayList<Map<String, String>>(groupnum);
List<List<Map<String,String>>> childData=new ArrayList<List<Map<String, String>>>(groupnum);
for(int i=0;i<groupnum;i++){
HashMap<String, String> map = new HashMap<String, String>(1);
map.put("title","I'm a group");
groupData.add(map);
List<Map<String, String>> thisChildData = new ArrayList<Map<String, String>>(childnum);
for(int j=0;j<childnum;j++){
HashMap<String, String> childmap = new HashMap<String, String>(1);
childmap .put("title","I'm a child");
thisChildData.add(childmap );
}
childData.add(thisChildData);
}
ExpandableListAdapter adapter=new SimpleExpandableListAdapter(
this,
groupData,
android.R.layout.simple_expandable_list_item_1,
new String[] {"title"},
new int[] {android.R.id.text1},
childData,
android.R.layout.simple_list_item_1,
new String[] {"title"},
new int[] {android.R.id.text1}
);
//Set adapter
myView.mListView.setAdapter(adapter);
}
答案 1 :(得分:0)
替换
mListView = (ExpandableListView) (findViewById(R.id.lv_expandable));
带
mListView = (ExpandableListView) (mHeaderView.findViewById(R.id.lv_expandable));