2017-05-09 46 views
0

我正在研究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

我得到了答案,我使用treeeview而不是擴張recyclarview的。要實現它只是做幾個步驟...

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

或者你從https://github.com/bmelnychuk/AndroidTreeView這裏得到的例子。

謝謝