儿童布局是重复多次ExpandableListView布局、儿童、ExpandableListView

2023-09-09 21:07:11 作者:别看有些人表面挺丧的,其实背后也有偷偷在努力生活。今日小编为

我有我实现了网格视图的ExpandableListView作为一个孩子的父母一方的一个问题。有在46 GridView的图像。因此,当我们打开第一个亲表明,它拥有超过46图像的46图像的插槽中的网格视图。意味着46显示的图像一个插槽然后又46图像再次显示了下然后又接着又。装置多次相同的布局被重复在一个布局。我无法找到问题的根源,因此我没能解决。请给我建议就同一任何解决方案。

I have a problem that I am implementing an ExpandableListView with a Grid View as a child in one parent. There are 46 images in GridView. Therefore when we open first parent it shown a grid view with more than 46 images with a slot of 46 images. Means one slot of 46 images shown then another 46 images again shown beneath that then another and then another. Means many times the same layout is repeating in one layout. I unable to find the source of the problem therefore I failed to resolve that. Please suggest me any solution regarding the same.

code(扩展列表):

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

    }

GridAdapter:

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;

    }


}

在此先感谢。

Thanks in advance.

推荐答案

您需要做的仅仅是通过只生一个孩子的扩展列表。所以,只有一个GridView控件会显示在扩展列表中,但所有的选项,你希望出现在网格视图。

What you need to do is just pass only one child to the expandable list. So, Only one gridview will be shown in the expandable list, but with all the options you want to appear in the grid view.

所以,如果你有另一组,与其他gridview的项目,只需添加一个项目到该组的可扩展列表中,与GridView的,但在这个GridView控件,请确保您传递,你希望它有数据。我的意思是,你应该使用相同的GridView的适配器,但使用不同的数据。我希望这帮助你。

so, if you have another group, with another gridview items, just add one item to that group of the expandable list, with the gridview, but in this gridview, make sure you are passing the data you want it to have. I mean, you should use the same gridview adapter, but with different data. I hope this help you.