2017-05-09 12 views
0

4レベルの展開可能リストビューで作業していますが、3レベルまでは問題ありませんが、プログラム可能なようにリストビューを展開すると、展開可能なリストビューは4レベルでは正しく展開できません

public class MainActivity extends AppCompatActivity { 
public static final int FIRST_LEVEL_COUNT = 1; 
public static final int SECOND_LEVEL_COUNT = 2; 
public static final int THIRD_LEVEL_COUNT = 3; 
public static final int FORTH_LEVEL_COUNT = 4; 
private ExpandableListView expandableListView; 



@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 
    expandableListView = (ExpandableListView) findViewById(R.id.mainList); 
    expandableListView.setAdapter(new ParentLevel(this)); 

} 

public class ParentLevel extends BaseExpandableListAdapter { 

    private Context context; 

    public ParentLevel(Context context) { 
     this.context = context; 
    } 

    @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) { 
     SecondLevelExpandableListView secondLevelELV = new SecondLevelExpandableListView(MainActivity.this); 
     secondLevelELV.setAdapter(new SecondLevelAdapter(context)); 
     secondLevelELV.setGroupIndicator(null); 
     return secondLevelELV; 
    } 

    @Override 
    public int getChildrenCount(int groupPosition) { 
     return SECOND_LEVEL_COUNT; 
    } 

    @Override 
    public Object getGroup(int groupPosition) { 
     return groupPosition; 
    } 

    @Override 
    public int getGroupCount() { 
     return FIRST_LEVEL_COUNT; 
    } 

    @Override 
    public long getGroupId(int groupPosition) { 
     return groupPosition; 
    } 

    @Override 
    public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) { 
     if (convertView == null) { 
      LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
      convertView = inflater.inflate(R.layout.row_first, null); 
      TextView text = (TextView) convertView.findViewById(R.id.eventsListEventRowText); 
      text.setText("FIRST LEVEL"); 

     } 
     return convertView; 
    } 

    @Override 
    public boolean hasStableIds() { 
     return true; 
    } 

    @Override 
    public boolean isChildSelectable(int groupPosition, int childPosition) { 
     return true; 
    } 
} 

public class SecondLevelExpandableListView extends ExpandableListView { 

    public SecondLevelExpandableListView(Context context) { 
     super(context); 
    } 

    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { 
     //999999 is a size in pixels. ExpandableListView requires a maximum height in order to do measurement calculations. 
     heightMeasureSpec = MeasureSpec.makeMeasureSpec(1500, MeasureSpec.AT_MOST); 
     super.onMeasure(widthMeasureSpec, heightMeasureSpec); 
    } 
} 

public class ThirdLevelExpandableListView extends ExpandableListView { 



    public ThirdLevelExpandableListView(Context context) { 
     super(context); 
    } 

    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { 
     //999999 is a size in pixels. ExpandableListView requires a maximum height in order to do measurement calculations. 
     heightMeasureSpec = MeasureSpec.makeMeasureSpec(500 , MeasureSpec.AT_MOST); 
     super.onMeasure(widthMeasureSpec, heightMeasureSpec); 
    } 
} 

public class SecondLevelAdapter extends BaseExpandableListAdapter { 

    private Context context; 

    public SecondLevelAdapter(Context context) { 
     this.context = context; 
    } 

    @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) { 
     if (convertView == null) { 
      LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
      convertView = inflater.inflate(R.layout.row_third, null); 
      TextView text = (TextView) convertView.findViewById(R.id.eventsListEventRowText); 
      text.setText("SECOND LEVEL"); 
     } 
     return convertView; 
    } 

    @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) { 
     if (convertView == null) { 
      /* LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
      convertView = inflater.inflate(R.layout.row_second, null); 
      TextView text = (TextView) convertView.findViewById(R.id.eventsListEventRowText); 
      text.setText("THIRD LEVEL");*/ 
      ThirdLevelExpandableListView thirdlevelELV= new ThirdLevelExpandableListView(MainActivity.this); 
      /* ExpandableListView thirdlevelELV=new ExpandableListView(MainActivity.this); 
      LinearLayout.LayoutParams layoutParams=new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,LinearLayout.LayoutParams.MATCH_PARENT); 
      thirdlevelELV.setLayoutParams(layoutParams);*/ 
      thirdlevelELV.setAdapter(new ThirdLevelAdapter(context)); 
      thirdlevelELV.setGroupIndicator(null); 
      return thirdlevelELV; 
     } 

     return convertView; 
    } 

    @Override 
    public int getChildrenCount(int groupPosition) { 
     return THIRD_LEVEL_COUNT; 
    } 

    @Override 
    public boolean hasStableIds() { 
     return true; 
    } 

    @Override 
    public boolean isChildSelectable(int groupPosition, int childPosition) { 
     return true; 
    } 
} 
public class ThirdLevelAdapter extends BaseExpandableListAdapter { 

    private Context context; 

    public ThirdLevelAdapter(Context context) { 
     this.context = context; 
    } 

    @Override 
    public Object getGroup(int groupPosition) { 
     return groupPosition; 
    } 

    @Override 
    public int getGroupCount() { 
     return THIRD_LEVEL_COUNT; 
    } 

    @Override 
    public long getGroupId(int groupPosition) { 
     return groupPosition; 
    } 

    @Override 
    public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) { 
     if (convertView == null) { 
      LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
      convertView = inflater.inflate(R.layout.row_third, null); 
      TextView text = (TextView) convertView.findViewById(R.id.eventsListEventRowText); 
      text.setText("THIRD LEVEL"); 
     } 
     return convertView; 
    } 

    @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) { 
     if (convertView == null) { 
      LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
      convertView = inflater.inflate(R.layout.row_forth, null); 
      TextView text = (TextView) convertView.findViewById(R.id.eventsListEventRowText); 
      text.setText("FORTH LEVEL"); 
     } 
     return convertView; 
    } 

    @Override 
    public int getChildrenCount(int groupPosition) { 
     return FORTH_LEVEL_COUNT; 
    } 

    @Override 
    public boolean hasStableIds() { 
     return true; 
    } 

    @Override 
    public boolean isChildSelectable(int groupPosition, int childPosition) { 
     return true; 
    } 
} 

}

答えて

1

私は答えを持って、私は拡張可能recyclarviewの代わりにtreeeviewを使用しています:

は、ここに私のコードです。 ...それだけでいくつかのステップを実行します

import 'com.github.bmelnychuk:atv:1.2.+' 

を実装するか、ここhttps://github.com/bmelnychuk/AndroidTreeViewから例を取得します。

ありがとうございました

関連する問題