android - 子布局在 ExpandableListView 中重复多次

标签 android

我有一个问题,我正在实现一个带有 GridView 的 ExpandableListView 作为一个父项中的一个子项。 GridView 中有 46 张图像。因此,当我们打开第一个父级时,它会显示一个包含超过 46 张图像的 GridView ,其中一个插槽包含 46 张图像。表示一个插槽显示 46 张图像,然后再显示另一张 46 张图像,然后再显示另一张,然后再显示另一张。意味着在一个布局中多次重复相同的布局。我无法找到问题的根源,因此我未能解决该问题。请向我建议有关相同的任何解决方案。

代码(可扩展列表):

public class ExpResearchListAdapter extends BaseExpandableListAdapter {
    // Sample data set. children[i] contains the children (String[]) for
    // groups[i].
    Context context;
    /*private String[] groups = { "Research by Brand", "Research by Category",
        "Research by Price" , "Research by Fuel Economy", "Recently Viewed"};*/
    GridView label;
    TextView groupText;
    ImageView groupImg;
    ArrayList<String> groups = new ArrayList<String>();
    ArrayList<Integer> groupImage = new ArrayList<Integer>();
    ArrayList<Integer> childElement = new ArrayList<Integer>();
    private Integer[][] children = { 
            { R.drawable.icon_1, R.drawable.icon_2, R.drawable.icon_3, R.drawable.icon_4, R.drawable.icon_5, R.drawable.icon_6, R.drawable.icon_7, R.drawable.icon_8, R.drawable.icon_9,
                R.drawable.icon_10, R.drawable.icon_11, R.drawable.icon_12, R.drawable.icon_13, R.drawable.icon_14, R.drawable.icon_15, R.drawable.icon_16, R.drawable.icon_17, R.drawable.icon_18,
                R.drawable.icon_19, R.drawable.icon_20, R.drawable.icon_21, R.drawable.icon_22, R.drawable.icon_23, R.drawable.icon_24, R.drawable.icon_25, R.drawable.icon_26, R.drawable.icon_27,
                R.drawable.icon_28, R.drawable.icon_29, R.drawable.icon_30, R.drawable.icon_31, R.drawable.icon_32, R.drawable.icon_33, R.drawable.icon_34, R.drawable.icon_35, R.drawable.icon_36,
                R.drawable.icon_37, R.drawable.icon_38, R.drawable.icon_39, R.drawable.icon_40, R.drawable.icon_41, R.drawable.icon_42, R.drawable.icon_43, R.drawable.icon_44, R.drawable.icon_45,
                R.drawable.icon_46} 
            };
    LinearLayout linear;

    public ExpResearchListAdapter(Context context, ArrayList<String> groups, ArrayList<Integer> groupImage, ArrayList<Integer> childElement, LinearLayout linear)
    {
        this.context=context;
        this.groups = groups;
        this.groupImage = groupImage;
        this.childElement = childElement;
        this.linear = linear;
    }

    public Object getChild(int groupPosition, int childPosition) {
        return children[groupPosition][childPosition];
    }

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

    public int getChildrenCount(int groupPosition) {
        int i = 0;
        try {
        i = children[groupPosition].length;

        } catch (Exception e) {
        }

        return i;
    }

    public TextView getGenericView() {
        // Layout parameters for the ExpandableListView
        AbsListView.LayoutParams lp = new AbsListView.LayoutParams(
            ViewGroup.LayoutParams.FILL_PARENT, 64);

        TextView textView = new TextView(context);
        textView.setLayoutParams(lp);
        // Center the text vertically
        textView.setGravity(Gravity.CENTER_VERTICAL | Gravity.LEFT);
        //textView.setTextColor(R.color.marcyred);
        // Set the text starting position
        textView.setPadding(36, 0, 0, 0);
        return textView;
    }

    public View getChildView(int groupPosition, int childPosition,
        boolean isLastChild, View convertView, ViewGroup parent) {
        if(convertView==null)
        {
            LayoutInflater inflater = LayoutInflater.from(context);
            convertView = (View) inflater.inflate(R.layout.brand_research_grid, null);
            label = (GridView) convertView.findViewById(R.id.gv_ResearchList_Child);
            label.setAdapter(new GridAdapter(context, children));
            label.setCacheColorHint(Color.WHITE);
            // initialize the following variables (i've done it based on your layout
            // note: rowHeightDp is based on my grid_cell.xml, that is the height i've
            //    assigned to the items in the grid.
            final int spacingDp = 10;
            final int colWidthDp = 50;
            //final int rowHeightDp = (childElement.size()/3)*10;
            final int rowHeightDp = 107;

            // convert the dp values to pixels
            final float COL_WIDTH = context.getResources().getDisplayMetrics().density * colWidthDp;
            final float ROW_HEIGHT = context.getResources().getDisplayMetrics().density * rowHeightDp;
            final float SPACING = context.getResources().getDisplayMetrics().density * spacingDp;
            System.out.println("===================================RowHeight"+ROW_HEIGHT);
            System.out.println("===================================RowHeightDP"+rowHeightDp);


            // calculate the column and row counts based on your display
            final int colCount = (int)Math.floor((linear.getWidth() - (2 * SPACING)) / (COL_WIDTH + SPACING));
            //final int rowCount = (int)Math.ceil((childElement.size() + 0d) / 3);
            final int rowCount = 16;

            // calculate the height for the current grid
            final int GRID_HEIGHT = Math.round(rowCount * (ROW_HEIGHT + SPACING));
            System.out.println("===================================GHieght"+GRID_HEIGHT);
            System.out.println("===================================colCount"+colCount);
            System.out.println("===================================rowCount"+rowCount);
            // set the height of the current grid
            label.getLayoutParams().height = GRID_HEIGHT;
        }





        return convertView;
    }

    public Object getGroup(int groupPosition) {
        return groups.get(groupPosition);
    }

    public int getGroupCount() {
        return groups.size();
    }

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

    public View getGroupView(int groupPosition, boolean isExpanded,
        View convertView, ViewGroup parent) {
        String group = (String) getGroup(groupPosition);
        if (convertView == null) {
            LayoutInflater infalInflater = (LayoutInflater) context
                    .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            convertView = infalInflater.inflate(
                    R.layout.research_list_exp_group, null);
            groupText = (TextView) convertView
                    .findViewById(R.id.tv_ResearchList_ExpParentElement);
            groupImg = (ImageView) convertView
                    .findViewById(R.id.img_ResearchList_GroupParentImage);
            // convertView.setClickable(false);
        }
        groupText.setText(group);
        groupImg.setImageResource(groupImage.get(groupPosition));

        return convertView;
    }

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

    public boolean hasStableIds() {
        return true;
    }

    }

网格适配器:

public class GridAdapter extends BaseAdapter{

    public Context context;
    private LayoutInflater mInflater;
    //public ArrayList<Integer> childElements = new ArrayList<Integer>();
    Integer[][] childElements;
    ImageView imgGridItem;

    public GridAdapter(Context context, Integer[][] childElements)
    {
        this.context = context;
        this.childElements = childElements;
        mInflater = LayoutInflater.from(context);
    }
    @Override
    public int getCount() {
        // TODO Auto-generated method stub
        return childElements.length;
    }

    @Override
    public Object getItem(int arg0) {
        // TODO Auto-generated method stub
        return arg0;
    }

    @Override
    public long getItemId(int arg0) {
        // TODO Auto-generated method stub
        return arg0;
    }

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

        if (convertView == null) {
            convertView = mInflater.inflate(R.layout.grid_item, null);
            holder = new ViewHolder();

            imgGridItem = (ImageView)convertView.findViewById(R.id.img_GridItem);
        }
        imgGridItem.setImageResource(childElements[position][0]);

        return convertView;
    }
    class ViewHolder {
        private ImageView imgGridItem;

    }


}

提前致谢。

最佳答案

找了几个小时都没有解决方案。这是答案。只需像这样在父适配器中创建函数 getChildrenCount:

@Override
public ing getChildrenCount(int groupPosition){
     return 1;
}

关于android - 子布局在 ExpandableListView 中重复多次,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10339777/

相关文章:

java - 如何修复从 LinkedTreeMap 到 ArrayList 的 Gson JSON 解析?

android - 第一次单击时,Android App不会录制声音

Android:无法将背景可绘制颜色设置为 percentRelativeLayout

android - 无法获取我的位置和位置按钮不显示

android - 将属性传递给 Android 库项目构建文件

android - 屏幕 ImageView 中的替代图像

java - android中识别第二次点击事件

android - RxJava2 与 Kotlin 协程

android - 通过 Android 中的 Share Intent 共享 res/raw 文件夹中的音频文件

android - 添加延迟以开始等待 admob 显示插页式广告的 Activity