4

TreeViewを使用して実装している皆さんExpandableListView。しかし、残念ながら解決できない測定問題があります。

問題のスクリーンショットは次のとおりです。

この画像はショーシャンクの空に子供たちを示しています

この手品師の子は 1 人見えますが、ショーシャンクの子 2 人はゴッドファーザーの下で消えます

測定に問題があることがわかります。私がAndroidを初めて使用する限り、onMeasure()方法はよくわかりません。私は 1 を持っていてExpandableListView、その中で私は-sをgetChildView()返します。CustomExapndableListView

コードは次のとおりです。

ExpandableListAdapter :

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

CustomExpandableListView :

    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です。

TreeElementI.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);
}

TreeElement.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 に答える