展示一个简单的二级列表,一个类搞定,
1,布局
<ExpandableListView
android:id="@+id/rpdb"
android:layout_width="match_parent"
android:layout_height="match_parent"></ExpandableListView>
2,Activity
package com.example.erji;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseExpandableListAdapter;
import android.widget.ExpandableListView;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity {
private ExpandableListView rpdb;
//数据源分两部分(一维数组和二维数组)
private String[] strings = new String[] {"< 杨晓","< 喜欢","< 听故事"};;
private String[][] strings1= new String[][] {
{"Y","A","N","G"},{"头发","艺术"},{"心灵鸡汤","妖言惑众"}
};;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initView();
}
private void initView() {
rpdb = (ExpandableListView) findViewById(R.id.rpdb);
//直接设置适配器
rpdb.setAdapter(new BaseExpandableListAdapter() {
private TextView text1;
@Override
public int getGroupCount() {
return strings.length;
}
@Override
public int getChildrenCount(int i) {
return strings1[i].length;
}
@Override
public Object getGroup(int i) {
return i;
}
@Override
public Object getChild(int i, int i1) {
return i1;
}
@Override
public long getGroupId(int i) {
return i;
}
@Override
public long getChildId(int i, int i1) {
return i1;
}
@Override
public boolean hasStableIds() {
return false;
}
@Override
public View getGroupView(int i, boolean b, View view, ViewGroup viewGroup) {
View inflate = View.inflate(MainActivity.this, R.layout.ii, null);
TextView textView= (TextView) inflate.findViewById(R.id.text1);
textView.setText(strings[i]);
return inflate;
}
@Override
public View getChildView(int i, int i1, boolean b, View view, ViewGroup viewGroup) {
View inflate = View.inflate(MainActivity.this, R.layout.ii, null);
TextView textView= (TextView) inflate.findViewById(R.id.text1);
textView.setText(strings1[i][i1]);
return inflate;
}
@Override
public boolean isChildSelectable(int i, int i1) {
return false;
}
});
}
}