Android中三级可扩展列表视图的问题

时间:2017-02-13 11:16:38

标签: java android expandablelistview

我写了一个3级可扩展列表代码,就像那样:

A

|_B

  |_C

D

|_E

  |_F

但是在我的列表中,每一行都包含按钮,edittexts等。我正在动态添加子项,如下面的代码所示。

我需要一些关于2件事的帮助。

1)当我点击A时,B打开但是当我点击B时,C没有显示在屏幕上。

2)当我单击A或D时,组位置不会始终改变为0,因此A总是会扩展。

您可以在下面找到以下代码,谢谢:)

我写下这些活动的开始:

   public static int FIRST_LEVEL_COUNT = 1;
   public static int SECOND_LEVEL_COUNT = 1;
   public static int THIRD_LEVEL_COUNT = 1;

   boolean firstLevel_Expanded = false;
   boolean secondLevel_Expanded = false;

然后从xml调用可扩展列表视图:

  demand_table = (ExpandableListView) dialog.findViewById(R.id.demandresponsetable);

  demand_table.setDescendantFocusability(ViewGroup.FOCUS_AFTER_DESCENDANTS);

  demand_table.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(LoadDemandResponse.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.threelevel_listrow_first, null);
            final long position = getGroupId(groupPosition);
            convertView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    if(firstLevel_Expanded == true) {
                        firstLevel_Expanded = false;
                        demand_table.expandGroup((int) position);
                    }
                    else {
                        firstLevel_Expanded = true;
                        demand_table.collapseGroup((int) position);
                    }
                }
            });

            TextView text = (TextView) convertView.findViewById(R.id.Edit_eventsListEventRowText);
            text.setHint("Demand Response Name");

            ToggleButton toggle = (ToggleButton) convertView.findViewById(R.id.dr_status);
            toggle.setText("Passıve");
            toggle.setTextOff("Passıve");
            toggle.setTextOn("Actıve");
            toggle.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
                public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                    if (isChecked) {
                        // The toggle is enabled
                    } else {
                        // The toggle is disabled
                    }
                }
            });

            Button time = (Button) convertView.findViewById(R.id.add_time);
            time.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    SECOND_LEVEL_COUNT++;
                }
            });
        }
        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(999999, 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(999999, 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(final 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.threelevel_listrow_second, null);
            //text.setText("SECOND LEVEL");

            convertView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    if(secondLevel_Expanded == true) {
                        secondLevel_Expanded = false;
                        demand_table.expandGroup(groupPosition);
                    }
                    else {
                        secondLevel_Expanded = true;
                        demand_table.collapseGroup(groupPosition);
                    }
                }
            });

            final View finalConvertView = convertView;

            final EditText start = (EditText) convertView.findViewById(R.id.edit_starttime);
            start.setInputType(InputType.TYPE_NULL);
            start.requestFocus();
            start.setHint("Start Time");
            start.setOnClickListener(new View.OnClickListener() {

                @Override
                public void onClick(View v) {
                    // TODO Auto-generated method stub
                    Calendar mcurrentTime = Calendar.getInstance();
                    int hour = mcurrentTime.get(Calendar.HOUR_OF_DAY);
                    int minute = mcurrentTime.get(Calendar.MINUTE);
                    TimePickerDialog mTimePicker;
                    mTimePicker = new TimePickerDialog(finalConvertView.getContext(), new TimePickerDialog.OnTimeSetListener() {
                        @Override
                        public void onTimeSet(TimePicker timePicker, int selectedHour, int selectedMinute) {
                            start.setText( selectedHour + ":" + selectedMinute);
                        }
                    }, hour, minute, true);//Yes 24 hour time
                    mTimePicker.setTitle("Select Time");
                    mTimePicker.show();
                }
            });

            final EditText end = (EditText) convertView.findViewById(R.id.edit_endtime);
            end.setInputType(InputType.TYPE_NULL);
            end.requestFocus();
            end.setHint("End Time");
            end.setOnClickListener(new View.OnClickListener() {

                @Override
                public void onClick(View v) {
                    // TODO Auto-generated method stub
                    Calendar mcurrentTime = Calendar.getInstance();
                    int hour = mcurrentTime.get(Calendar.HOUR_OF_DAY);
                    int minute = mcurrentTime.get(Calendar.MINUTE);
                    TimePickerDialog mTimePicker;
                    mTimePicker = new TimePickerDialog(finalConvertView.getContext(), new TimePickerDialog.OnTimeSetListener() {
                        @Override
                        public void onTimeSet(TimePicker timePicker, int selectedHour, int selectedMinute) {
                            end.setText( selectedHour + ":" + selectedMinute);
                        }
                    }, hour, minute, true);//Yes 24 hour time
                    mTimePicker.setTitle("Select Time");
                    mTimePicker.show();
                }
            });

            Button offer = (Button) convertView.findViewById(R.id.add_offer);
            offer.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    THIRD_LEVEL_COUNT++;
                }
            });

        }
        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(final int groupPosition, int childPosition, boolean isLastChild, View convertView, ViewGroup parent) {
        ThirdLevelExpandableListView thirdLevelELV = new ThirdLevelExpandableListView(LoadDemandResponse.this);
        thirdLevelELV.setAdapter(new ThirdLevelAdapter(context));
        thirdLevelELV.setGroupIndicator(null);
        return thirdLevelELV;
    }

    @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 1;
    }

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

    @Override
    public View getGroupView(final 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.threelevel_listrow_third, null);

            EditText quantity = (EditText) convertView.findViewById(R.id.edit_quantity);
            quantity.setHint("Reserve Quantity");

            EditText price = (EditText) convertView.findViewById(R.id.edit_price);
            price.setHint("Offered Price");
        }
        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(final int groupPosition, int childPosition, boolean isLastChild, View convertView, ViewGroup parent) {
        if(convertView != null) {
            convertView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    demand_table.expandGroup(groupPosition);
                }
            });
        }
        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;
    }
}

0 个答案:

没有答案