【问题标题】:BaseExpandableListAdapter - getChildView random positionBaseExpandableListAdapter - getChildView 随机位置
【发布时间】:2014-10-30 21:34:32
【问题描述】:

我正在摆弄我的自定义 BaseExpandableListAdapter,在我的 getChildView 方法中,我似乎得到了子视图的随机位置。无论回收如何,我都会得到随机的位置顺序。例如,在 convertView = null 条件分支中,我得到:

LogCat 在 (ConvertView==null) 中打印:

childPosition﹕childPosition - 0 childPosition﹕childPosition - 1 childPosition﹕childPosition - 0 childPosition﹕childPosition - 3 childPosition﹕childPosition - 4 childPosition﹕childPosition - 0

LogCat 在 (ConvertView!=null) 中打印:

childPosition﹕childPosition - 1 childPosition﹕childPosition - 2 childPosition﹕childPosition - 3 childPosition﹕childPosition - 4 childPosition﹕childPosition - 5 childPosition﹕childPosition - 6 childPosition﹕childPosition - 7 childPosition﹕childPosition - 8 childPosition﹕childPosition - 0 childPosition﹕childPosition - 1 childPosition﹕childPosition - 0 childPosition﹕childPosition - 1

这是我的适配器的代码:

public class MyExapndableListAdapter extends BaseExpandableListAdapter {

    private Context context;
    private List<String> headerList;
    private HashMap<String, List<String>> _listDataChild;

    public MyExapndableListAdapter(Context context, List<String> listDataHeader, HashMap<String, List<String>> listChildData) {
        this.context = context;
        this._listDataChild = listChildData;
        this.headerList = listDataHeader;

        Log.d("LIST CHILD", "" + _listDataChild.size());
        Log.d("LIST HEADER", "" + this.headerList.size());
    }


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

    @Override
    public int getChildrenCount(int i) {
        return this._listDataChild.get(this.headerList.get(i)).size();
    }

    @Override
    public Object getGroup(int i) {
        return this.headerList.get(i);
    }

    @Override
    public Object getChild(int groupPosition, int childPosititon) {
        return this._listDataChild.get(this.headerList.get(groupPosition)).get(childPosititon);
    }

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

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

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

    @Override
    public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) {

        View v = convertView;

        String headerTitle = (String) getGroup(groupPosition);

        if (v == null) {
            LayoutInflater inflater = (LayoutInflater) this.context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            v = inflater.inflate(R.layout.list_group, null);
            ViewHolder holder = new ViewHolder();
            holder.addView(v.findViewById(R.id.blaGroup));
            v.setTag(holder);
        }

        ViewHolder holder = (ViewHolder) v.getTag();

        MyText title = (MyText) holder.getView(R.id.blaGroup);
        title.setText(headerTitle);

        return v;
    }

    @Override
    public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View convertView, ViewGroup parent) {


        final String childText = (String) getChild(groupPosition, childPosition);



        View row = convertView;

        if (row == null) {

//            Log.d("NULL VIEW", "childText - " +childText);
            Log.d("childPosition", "childPosition - " +childPosition);

            LayoutInflater inflater = (LayoutInflater) this.context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            row = inflater.inflate(R.layout.list_child, null);
            ViewGroup.LayoutParams lp = new ActionBar.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);

            final float GESTURE_THRESHOLD_DIP = 30.0f;

            // Convert the dips to pixels
            final float scale = context.getResources().getDisplayMetrics().density;
            int pixel = (int) (GESTURE_THRESHOLD_DIP * scale + 0.5f);

            Typeface font = Typeface.createFromAsset(context.getAssets(), "fonts/pacifico.ttf");
            int fontColor = context.getResources().getColor(R.color.testColor);
            int strokeWidth = 5;
            int strokeColor = context.getResources().getColor(R.color.card_white);

            // Add TextView on the fly
            MyText text = new MyText(context, pixel, childText, font, fontColor, strokeWidth, strokeColor);
            text.setLayoutParams(lp);
            text.setText(childText);
            text.setTextSize(GESTURE_THRESHOLD_DIP);
            text.setId(3005);

            ((ViewGroup) row).addView(text);


            ViewHolder holder = new ViewHolder();
            holder.addView(text);
            row.setTag(holder);



        }

        else {
//            Log.d("NOT NULL VIEW", "childText - " +childText);
            Log.d("NOT NULL VIEW childPosition", "childPosition - " +childPosition);
        }

        ViewHolder holder = (ViewHolder) row.getTag();
        MyText text = (MyText) holder.getView(3005);
        text.setText(childText);

        return row;
    }

    public static class ViewHolder {

        private HashMap<Integer, View> storedViews = new HashMap<Integer, View>();

        public ViewHolder() {
        }

        public ViewHolder addView(View view) {
            int id = view.getId();
            storedViews.put(id, view);
            return this;
        }

        public View getView(int id) {
            return storedViews.get(id);
        }
    }

    @Override
    public boolean isChildSelectable(int i, int i2) {
        return true;
    }

}

【问题讨论】:

  • 你是不是用 getchild 方法弄错了项目。

标签: android expandablelistadapter


【解决方案1】:

因为您使用的是 Hashmap,所以您会得到一个随机位置。 Hashmap 有自己的方式对其中的数据进行排序。所以我建议使用自定义对象而不是 Hashmap。

【讨论】:

  • @Yosi199 我猜他是在谈论扩展List,并自己进行“关键发现”,但正在处理List&lt;View&gt;。
  • 抱歉回复晚了。如果您的 _listDataChild 只是一个字符串列表,您可以使用字符串数组。看看这个tutorial
【解决方案2】:

好吧,childView 的位置不是随机的,只是它们是来自不同组的子位置,将它们与 groupId 一起打印你就会明白发生了什么。此外 childPosition 不依赖于 convertview 是否为 null ,通常会为滚动时出现在屏幕上的列表项调用 getChildView 。 希望这会有所帮助,如果你想要一个例子,请回复。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-07-31
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-11-13
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多