Тут такая проблема.
Грамотное решение пригодится всем )
Реализую многоуровневый ExpandableListView, за основу взял этот
http://www.allappsdevelopers.com/TopicD ... 91a4247a84 и перепилил его под иcпользование с cursor. Но есть одна засада, если уровней больше 3-х, то при раскрытии списков не расширяются (по высоте) родительские ExpandableListView.
Исходное состояние
вот два уровня открылось, все ка надо растянулось
вот она, засада.
Как при раскрытии Third level и отображении Child просигналить верхним уровням и раздвинуть их?
Надеюсь, что я сам верно понял проблему )
Код прилагаю.
Код: Выделить всё
package com.example.expandablelistviewmultilevel;
import android.app.Activity;
import android.content.Context;
import android.content.res.ColorStateList;
import android.graphics.Color;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseExpandableListAdapter;
import android.widget.ExpandableListView;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.LinearLayout.LayoutParams;
import android.app.Activity;
import android.view.Menu;
public class MainActivity extends Activity {
ExpandableListView explvlist;
CustExpListview SecondLevelexplv;
SecondLevelAdapter1 secadapter;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
explvlist = (ExpandableListView) findViewById(R.id.ParentLevel);
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) {
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 3;
}
@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);
}
@Override
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 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) {
CustExpListview SecondLevelexplv = new CustExpListview(MainActivity.this);
secadapter=new SecondLevelAdapter1();
SecondLevelexplv.setAdapter(secadapter);
SecondLevelexplv.setGroupIndicator(null);
SecondLevelexplv.setLayoutParams(new ListView.LayoutParams(
LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT));
return SecondLevelexplv;
/*
TextView tv = new TextView(MainActivity.this);
tv.setText("child");
tv.setTextColor(Color.BLACK);
tv.setTextSize(20);
tv.setPadding(15, 5, 5, 5);
tv.setBackgroundColor(Color.YELLOW);
// tv.setLayoutParams(new ListView.LayoutParams(
// LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT));
return tv;
*/
}
@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);
tv.setLayoutParams(new ListView.LayoutParams(
LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT));
SecondLevelexplv.setLayoutParams(new ListView.LayoutParams(
LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT));
SecondLevelexplv.invalidateViews();
return tv;
}
@Override
public boolean hasStableIds() {
// TODO Auto-generated method stub
return true;
}
@Override
public boolean isChildSelectable(int groupPosition, int childPosition) {
// TODO Auto-generated method stub
return true;
}
}
public class SecondLevelAdapter1 extends BaseExpandableListAdapter {
public void onGroupExpanded()
{
Log.d("LOG","Развернули");
}
@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) {
TextView tv = new TextView(MainActivity.this);
tv.setText("child");
tv.setTextColor(Color.BLACK);
tv.setTextSize(20);
tv.setPadding(15, 5, 5, 5);
tv.setBackgroundColor(Color.YELLOW);
tv.setLayoutParams(new ListView.LayoutParams(
LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT));
SecondLevelexplv.setLayoutParams(new ListView.LayoutParams(
LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT));
SecondLevelexplv.invalidateViews();
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("-->Third Level");
tv.setTextColor(Color.BLACK);
tv.setTextSize(20);
tv.setPadding(12, 7, 7, 7);
tv.setBackgroundColor(Color.GREEN);
parent.setLayoutParams(new ListView.LayoutParams(
LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT));
// parent.invalidateViews();
// secadapter.notifyDataSetInvalidated();
return tv;
}
@Override
public boolean hasStableIds() {
// TODO Auto-generated method stub
return true;
}
@Override
public boolean isChildSelectable(int groupPosition, int childPosition) {
// TODO Auto-generated method stub
return true;
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}