4

嘿,我正在TreeView使用ExpandableListView. 但我有一些测量问题,很遗憾我无法解决。

这是问题的屏幕截图:

这张图片显示了肖申克的救赎之子

我这一个变戏法的孩子是可见的,但肖申克的两个孩子在教父的手下消失了

可以看到测量有问题。只要我是 Android 新手,我就不太了解onMeasure()方法。我有 1ExpandableListView并且在其中getChildView()我返回CustomExapndableListView-s。

这是代码:

可扩展列表适配器:

public class ExpandableListAdapter extends BaseExpandableListAdapter {
private Context context;
private List<String> listDataHeader;
private HashMap<String, List<String>> listDataChild;

public ExpandableListAdapter (Context context, List<String> listDataHeader, HashMap<String, List<String>> listChildData) {
    this.context = context;
    this.listDataHeader = listDataHeader;
    this.listDataChild = listChildData;
}

@Override
public int getGroupCount() {
    return this.listDataHeader.size();
}

@Override
public int getChildrenCount(int groupPosition) {
    return this.listDataChild.get(this.listDataHeader.get(groupPosition)).size();
}

@Override
public Object getGroup(int groupPosition) {
    return this.listDataHeader.get(groupPosition);
}

@Override
public Object getChild(int groupPosition, int childPosition) {
    return this.listDataChild.get(this.listDataHeader.get(groupPosition)).get(childPosition);
}

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

@Override
public long getChildId(int groupPosition, int childPosition) {
    return childPosition;
}

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

@Override
public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) {
    String headerTitle = (String) getGroup(groupPosition);
    Log.i("Header: ", " " + headerTitle);
    if(convertView == null) {
        LayoutInflater infalInflater = (LayoutInflater) this.context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        convertView = infalInflater.inflate(R.layout.list_group, null);
    }

    TextView lblListHeader = (TextView) convertView.findViewById(R.id.lblListHeader);
    lblListHeader.setTypeface(null, Typeface.BOLD);
    lblListHeader.setText(headerTitle);

    return convertView;
}

@Override
public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View convertView, ViewGroup parent) {
    /*final String childText = (String) getChild(groupPosition, childPosition);

    if(convertView == null) {
        LayoutInflater infalInflater = (LayoutInflater) this.context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        convertView = infalInflater.inflate(R.layout.list_item, null);
    }

    TextView txtListChild = (TextView) convertView.findViewById(R.id.lblListItem);

    txtListChild.setText(childText);*/

    String childText = (String) getChild(groupPosition, childPosition);
    if(listDataChild.containsKey(childText)){
        Log.i("Child", "" + childText);
        CustomExpandableListView explv = new CustomExpandableListView(context);
      //  explv.setRows(calculateRowCount((String)getGroup(groupPosition), null));
      //  ChildLayerExpandableListAdapter adapter = new ChildLayerExpandableListAdapter(context, listDataChild.get(getGroup(groupPosition)), listDataChild);
        Log.i("Opaaaa:", " " + getGroup(groupPosition));
        List<String> newHeaders = new ArrayList <String>();
        newHeaders.add(childText);
        // listDataChild.get(getGroup(groupPosition))
        ExpandableListAdapter adapter = new ExpandableListAdapter(context, newHeaders, listDataChild);
        explv.setAdapter(adapter);
        explv.setGroupIndicator(null);
        convertView = explv;
        convertView.setPadding(20, 0, 0, 0);
    }else{
      //  if(convertView == null) {
            LayoutInflater infalInflater = (LayoutInflater) this.context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            convertView = infalInflater.inflate(R.layout.list_item, null);
   //     }

        Log.i("Else:", " " + childText);

        TextView txtListChild = (TextView) convertView.findViewById(R.id.lblListItem);

        txtListChild.setText(childText);

        convertView.setPadding(20, 0, 0, 0);
    }

    return convertView;
}

private int calculateRowCount (String key, ExpandableListView listView) {
    int groupCount = listDataChild.get(key).size();
    int rowCtr = 0;
    for(int i = 0; i < groupCount; i++) {
        rowCtr++;
        if( (listView != null) && (listView.isGroupExpanded(i)))
            rowCtr += listDataChild.get(listDataChild.get(key).get(i)).size() - 1;
    }
    return rowCtr;
}

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

自定义扩展列表视图:

    public class CustomExpandableListView extends ExpandableListView {

    private static final int HEIGHT = 20;
    private int rows;

    public void setRows(int rows) {
        this.rows = rows;
    }

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

    public CustomExpandableListView(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        heightMeasureSpec = MeasureSpec.makeMeasureSpec(500, MeasureSpec.AT_MOST);
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
      //  setMeasuredDimension(getMeasuredWidth(), rows * HEIGHT);
    }
}

我想要做的是,当我展开视图时,我希望显示所有孩子,我可以向下滚动ListView. 现在有些孩子隐藏在不同的孩子之下。

在此先感谢您的帮助。

4

1 回答 1

5

@Code Word 对不起,我忘了分享我的solution.

正如我之前提到的,我treeView使用ListView. 这是我的source code

树元素I.java

    public interface TreeElementI extends Serializable{
      public void addChild(TreeElementI child);
      public String getId();
      public void setId(String id);
      public String getOutlineTitle();
      public void setOutlineTitle(String outlineTitle);
      public boolean isHasParent();
      public void setHasParent(boolean hasParent);
      public boolean isHasChild();
      public void setHasChild(boolean hasChild);
      public int getLevel();
      public void setLevel(int level);
      public boolean isExpanded();
      public void setExpanded(boolean expanded);
      public ArrayList<TreeElementI> getChildList();
      public TreeElementI getParent();
      public void setParent(TreeElementI parent);
}

树元素.java

    public class TreeElement implements TreeElementI{
    private String id;
    private String outlineTitle;
    private boolean hasParent;
    private boolean hasChild;
    private TreeElementI parent;
    private int level;
    private ArrayList<TreeElementI> childList;
    private boolean expanded;

    public TreeElement(String id, String outlineTitle) {
        super();
        this.childList = new ArrayList<TreeElementI>();
        this.id = id;
        this.outlineTitle = outlineTitle;
        this.level = 0;
        this.hasParent = true;
        this.hasChild = false;
        this.parent = null;
    }

    public TreeElement(String id, String outlineTitle, boolean hasParent, boolean hasChild, TreeElement parent, int level, boolean expanded) {
        super();
        this.childList = new ArrayList<TreeElementI>();
        this.id = id;
        this.outlineTitle = outlineTitle;
        this.hasParent = hasParent;
        this.hasChild = hasChild;
        this.parent = parent;
        if(parent != null) {
            this.parent.getChildList().add(this);
        }
        this.level = level;
        this.expanded = expanded;
    }

    @Override
    public void addChild(TreeElementI child) {
        this.getChildList().add(child);
        this.setHasParent(false);
        this.setHasChild(true);
        child.setParent(this);
        child.setLevel(this.getLevel() + 1);
    }

    @Override
    public String getId() {
        return this.id;
    }

    @Override
    public void setId(String id) {
        this.id = id;
    }

    @Override
    public String getOutlineTitle() {
        return this.outlineTitle;
    }

    @Override
    public void setOutlineTitle(String outlineTitle) {
        this.outlineTitle = outlineTitle;
    }

    @Override
    public boolean isHasParent() {
        return this.hasParent;
    }

    @Override
    public void setHasParent(boolean hasParent) {
        this.hasParent = hasParent;
    }

    @Override
    public boolean isHasChild() {
        return this.hasChild;
    }

    @Override
    public void setHasChild(boolean hasChild) {
        this.hasChild = hasChild;
    }

    @Override
    public int getLevel() {
        return this.level;
    }

    @Override
    public void setLevel(int level) {
        this.level = level;
    }

    @Override
    public boolean isExpanded() {
        return this.expanded;
    }

    @Override
    public void setExpanded(boolean expanded) {
        this.expanded = expanded;
    }

    @Override
    public ArrayList<TreeElementI> getChildList() {
        return this.childList;
    }

    @Override
    public TreeElementI getParent() {
        return this.parent;
    }

    @Override
    public void setParent(TreeElementI parent) {
        this.parent = parent;
    }
}

TreeViewClassifAdapter.java

    public class TreeViewClassifAdapter extends BaseAdapter {
    private static final int TREE_ELEMENT_PADDING_VAL = 25;
    private List<TreeElementI> fileList;
    private Context context;
    private Bitmap iconCollapse;
    private Bitmap iconExpand;
    private Dialog dialog;
    private EditText textLabel;
    private XTreeViewClassif treeView;

    public TreeViewClassifAdapter(Context context, List<TreeElementI> fileList, Dialog dialog, EditText textLabel, XTreeViewClassif treeView) {
        this.context = context;
        this.fileList = fileList;
        this.dialog = dialog;
        this.textLabel = textLabel;
        this.treeView = treeView;
        iconCollapse = BitmapFactory.decodeResource(context.getResources(), R.drawable.x_treeview_outline_list_collapse);
        iconExpand = BitmapFactory.decodeResource(context.getResources(), R.drawable.x_treeview_outline_list_expand);
    }

    public List<TreeElementI> getListData() {
        return this.fileList;
    }

    @Override
    public int getCount() {
        return this.fileList.size();
    }

    @Override
    public Object getItem(int position) {
        return this.fileList.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder;

        convertView = View.inflate(context, R.layout.x_treeview_classif_list_item, null);
        holder = new ViewHolder();
        holder.setTextView((TextView) convertView.findViewById(R.id.text));
        holder.setImageView((ImageView) convertView.findViewById(R.id.icon));
        convertView.setTag(holder);

        final TreeElementI elem = (TreeElementI) getItem(position);

        int level = elem.getLevel();
        holder.getIcon().setPadding(TREE_ELEMENT_PADDING_VAL * (level + 1), holder.icon.getPaddingTop(), 0, holder.icon.getPaddingBottom());
        holder.getText().setText(elem.getOutlineTitle());
        if (elem.isHasChild() && (elem.isExpanded() == false)) {
            holder.getIcon().setImageBitmap(iconCollapse);
        } else if (elem.isHasChild() && (elem.isExpanded() == true)) {
            holder.getIcon().setImageBitmap(iconExpand);
        } else if (!elem.isHasChild()) {
            holder.getIcon().setImageBitmap(iconCollapse);
            holder.getIcon().setVisibility(View.INVISIBLE);
        }

        IconClickListener iconListener = new IconClickListener(this, position);
        TextClickListener txtListener = new TextClickListener((ArrayList<TreeElementI>) this.getListData(), position);
        holder.getIcon().setOnClickListener(iconListener);
        holder.getText().setOnClickListener(txtListener);
        return convertView;
    }

    private class ViewHolder {
        ImageView icon;
        TextView text;

        public TextView getText() {
            return this.text;
        }

        public void setTextView(TextView text) {
            this.text = text;
        }

        public ImageView getIcon() {
            return this.icon;
        }

        public void setImageView(ImageView icon) {
            this.icon = icon;
        }
    }

    /**
     * Listener For TreeElement Text Click
     */
    private class TextClickListener implements View.OnClickListener {
        private ArrayList<TreeElementI> list;
        private int position;

        public TextClickListener(ArrayList<TreeElementI> list, int position) {
            this.list = list;
            this.position = position;
        }

        @Override
        public void onClick(View v) {
            treeView.setXValue(String.valueOf(list.get(position).getId()));
            dialog.dismiss();
        }
    }

    /**
     * Listener for TreeElement "Expand" button Click
     */
    private class IconClickListener implements View.OnClickListener {
        private ArrayList<TreeElementI> list;
        private TreeViewClassifAdapter adapter;
        private int position;

        public IconClickListener(TreeViewClassifAdapter adapter, int position) {
            this.list = (ArrayList<TreeElementI>) adapter.getListData();
            this.adapter = adapter;
            this.position = position;
        }

        @Override
        public void onClick(View v) {
            if (!list.get(position).isHasChild()) {
                return;
            }

            if (list.get(position).isExpanded()) {
                list.get(position).setExpanded(false);
                TreeElementI element = list.get(position);
                ArrayList<TreeElementI> temp = new ArrayList<TreeElementI>();

                for (int i = position + 1; i < list.size(); i++) {
                    if (element.getLevel() >= list.get(i).getLevel()) {
                        break;
                    }
                    temp.add(list.get(i));
                }
                list.removeAll(temp);
                adapter.notifyDataSetChanged();
            } else {
                TreeElementI obj = list.get(position);
                obj.setExpanded(true);
                int level = obj.getLevel();
                int nextLevel = level + 1;

                for (TreeElementI element : obj.getChildList()) {
                    element.setLevel(nextLevel);
                    element.setExpanded(false);
                    list.add(position + 1, element);
                }
                adapter.notifyDataSetChanged();
            }
        }
    }
}

我希望现在还为时不晚,这source code将对您有所帮助。

于 2013-12-11T11:35:11.023 回答