リストビュー用Expandable
。私は次のコードを書きました:
public class MainActivity extends Activity {
ExpandableListView explvlist;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
explvlist = (ExpandableListView) findViewById(R.id.expandableListView1);
explvlist.setAdapter(new ParentLevel());
}
public class ParentLevel extends BaseExpandableListAdapter {
@Override
public Object getChild(int arg0, int arg1) {
return arg1;
}
@Override
public long getChildId(int groupPosition, int childPosition) {
return childPosition;
}
@Override
public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View convertView, ViewGroup parent) {
CustExpListview SecondLevelexplv = new CustExpListview(MainActivity.this);
SecondLevelexplv.setAdapter(new SecondLevelAdapter());
SecondLevelexplv.setGroupIndicator(null);
return SecondLevelexplv;
}
@Override
public int getChildrenCount(int groupPosition) {
return 3;
}
@Override
public Object getGroup(int groupPosition) {
return groupPosition;
}
@Override
public int getGroupCount() {
return 8;
}
@Override
public long getGroupId(int groupPosition) {
return groupPosition;
}
@Override
public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) {
TextView tv = new TextView(MainActivity.this);
tv.setText("->FirstLevel");
tv.setTextColor(Color.BLACK);
tv.setTextSize(20);
tv.setBackgroundColor(Color.BLUE);
tv.setPadding(10, 7, 7, 7);
return tv;
}
@Override
public boolean hasStableIds() {
return true;
}
@Override
public boolean isChildSelectable(int groupPosition, int childPosition) {
return true;
}
}
public class CustExpListview extends ExpandableListView {
int intGroupPosition, intChildPosition, intGroupid;
public CustExpListview(Context context) {
super(context);
}
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
widthMeasureSpec = MeasureSpec.makeMeasureSpec(960, MeasureSpec.AT_MOST);
heightMeasureSpec = MeasureSpec.makeMeasureSpec(600, MeasureSpec.AT_MOST);
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
}
}
public class CustExpListview2 extends ExpandableListView {
int intGroupPosition2, intChildPosition2, intGroupid2;
public CustExpListview2(Context context) {
super(context);
}
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
widthMeasureSpec = MeasureSpec.makeMeasureSpec(2000, MeasureSpec.AT_MOST);
heightMeasureSpec = MeasureSpec.makeMeasureSpec(1000, MeasureSpec.AT_MOST);
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
}
}
public class CustExpListview3 extends ExpandableListView {
int intGroupPosition2, intChildPosition2, intGroupid2;
public CustExpListview3(Context context) {
super(context);
}
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
widthMeasureSpec = MeasureSpec.makeMeasureSpec(2000, MeasureSpec.AT_MOST);
heightMeasureSpec = MeasureSpec.makeMeasureSpec(1000, MeasureSpec.AT_MOST);
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
}
}
public class SecondLevelAdapter extends BaseExpandableListAdapter {
@Override
public Object getChild(int groupPosition, int childPosition) {
return childPosition;
}
@Override
public long getChildId(int groupPosition, int childPosition) {
return childPosition;
}
@Override
public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View convertView, ViewGroup parent) {
CustExpListview2 SecondLevelexplv2 = new CustExpListview2(MainActivity.this);
SecondLevelexplv2.setAdapter(new ThreeLevelAdapter());
SecondLevelexplv2.setGroupIndicator(null);
return SecondLevelexplv2;
}
@Override
public int getChildrenCount(int groupPosition) {
return 5;
}
@Override
public Object getGroup(int groupPosition) {
return groupPosition;
}
Override
public int getGroupCount() {
return 1;
}
@Override
public long getGroupId(int groupPosition) {
return groupPosition;
}
@Override
public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) {
TextView tv = new TextView(MainActivity.this);
tv.setText("-->Second Level");
tv.setTextColor(Color.BLACK);
tv.setTextSize(20);
tv.setPadding(12, 7, 7, 7);
tv.setBackgroundColor(Color.RED);
return tv;
}
@Override
public boolean hasStableIds() {
return true;
}
@Override
public boolean isChildSelectable(int groupPosition, int childPosition) {
return true;
}
}
public class ThreeLevelAdapter extends BaseExpandableListAdapter {
@Override
public Object getChild(int arg0, int arg1) {
return arg1;
}
@Override
public long getChildId(int groupPosition, int childPosition) {
return childPosition;
}
@Override
public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View convertView, ViewGroup parent) {
CustExpListview3 SecondLevelexplv3 = new CustExpListview3(MainActivity.this);
SecondLevelexplv3.setAdapter(new FourLevelAdapter());
SecondLevelexplv3.setGroupIndicator(null);
return SecondLevelexplv3;
}
@Override
public int getChildrenCount(int groupPosition) {
return 3;
}
@Override
public Object getGroup(int groupPosition) {
return groupPosition;
}
@Override
public int getGroupCount() {
return 1;
}
@Override
public long getGroupId(int groupPosition) {
return groupPosition;
}
@Override
public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) {
TextView tv = new TextView(MainActivity.this);
tv.setText("Level --Three");
tv.setTextColor(Color.BLACK);
tv.setTextSize(20);
tv.setPadding(15, 5, 5, 5);
tv.setBackgroundColor(Color.YELLOW);
return tv;
}
@Override
public boolean hasStableIds() {
return true;
}
@Override
public boolean isChildSelectable(int groupPosition, int childPosition) {
return true;
}
}
public class FourLevelAdapter extends BaseExpandableListAdapter {
@Override
public Object getChild(int arg0, int arg1) {
return arg1;
}
@Override
public long getChildId(int groupPosition, int childPosition) {
return childPosition;
}
@Override
public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View convertView, ViewGroup parent) {
TextView tv = new TextView(MainActivity.this);
tv.setText("->five level");
tv.setTextColor(Color.BLACK);
tv.setTextSize(20);
tv.setBackgroundColor(Color.MAGENTA);
tv.setPadding(24, 5, 5, 5);
return tv;
}
@Override
public int getChildrenCount(int groupPosition) {
return 3;
}
@Override
public Object getGroup(int groupPosition) {
return groupPosition;
}
@Override
public int getGroupCount() {
return 1;
}
@Override
public long getGroupId(int groupPosition) {
return groupPosition;
}
@Override
public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) {
TextView tv = new TextView(MainActivity.this);
tv.setText("->four level");
tv.setTextColor(Color.BLACK);
tv.setTextSize(20);
tv.setBackgroundColor(Color.CYAN);
tv.setPadding(21, 5, 5, 5);
return tv;
}
@Override
public boolean hasStableIds() {
return true;
}
@Override
public boolean isChildSelectable(int groupPosition, int childPosition) {
return true;
}
}
}
onCreateI の ID を取得するには、次のコードを使用します。
explvlist.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View v, int groupPosition, long id) {
// TODO Auto-generated method stub
int itemType = ExpandableListView.getPackedPositionType(groupPosition);
Log.i("item type", "" + itemType);
if (itemType == ExpandableListView.PACKED_POSITION_TYPE_CHILD) {
childPosition = ExpandableListView.getPackedPositionChild(id);
groupPosition = ExpandableListView.getPackedPositionGroup(id);
Log.i("child", "" + childPosition);
Log.i("child Group", "" + groupPosition);
} else if (itemType == ExpandableListView.PACKED_POSITION_TYPE_GROUP) {
groupPosition = ExpandableListView.getPackedPositionGroup(id);
Log.i("Group", "" + groupPosition);
}
}
});
そして、次のコード:
explvlist.setOnChildClickListener(new OnChildClickListener() {
@Override
public boolean onChildClick(ExpandableListView parent, View v, int groupPosition, int childPosition, long id) {
Toast.makeText(getBaseContext(), "Child clicked", Toast.LENGTH_SHORT).show();
System.out.println(getBaseContext() + "Child clicked");
return true;
}
});
しかし、それは何の助けにもなりませんでした 出力には何も表示されません! 助けてください