在Android中的GridView里面的Expandable列表 [英] GridView inside Expandable list in android

查看:160
本文介绍了在Android中的GridView里面的Expandable列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在一个可扩展的列表中放置一个带有图像的gridview ...我已经做到了,但gridview不显示所有的项目...



如何让我的可扩展列表子适应gridview大小?



LIST ADAPTER

  public class CustomListAdapter extends BaseExpandableListAdapter 
{
String [] catg = {Administração,Escritorio e Industria,Cultura e Entretenimento,Educação eCrianças,Eventos e Estado do tempo,Amigos,Familia eHabitações,Multimédia,Diversos,Númerose Letras,Restaurantes e Hoteis,Desporto,Saude e Beleza,Lojas ,Turismo e Natureza,Transportes};

上下文myctx;

$ b @覆盖
public Object getChild(int groupPosition,int childPosition)
{
// TODO自动生成的方法存根
返回空值;
}

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

$ b @Override
public View getChildView(int groupPosition,int childPosition,boolean isLastChild,View convertView,ViewGroup parent)
{
ViewGroup item = getViewGroupChild(convertView,parent);
GridView标签=(GridView)item.findViewById(ipvc.estg.placebook.R.id.gridview);
label.setAdapter(new GridAdapter(parent.getContext(),groupPosition + 1));
退货项目;
}

private ViewGroup getViewGroupChild(View convertView,ViewGroup parent)
{
//父类将是ListActivity中的ListView
if(convertView instanceof ViewGroup)
{
return(ViewGroup)convertView;
}
上下文context = parent.getContext();
LayoutInflater inflater = LayoutInflater.from(context);
ViewGroup item =(ViewGroup)inflater.inflate(ipvc.estg.placebook.R.layout.expandable_list_row,null);
退货项目;
}

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

@Override
public Object getGroup(int groupPosition)
{
return catg [groupPosition];
}

@Override
public int getGroupCount()
{
return catg.length;
}

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



$ b @Override
public View getGroupView(int groupPosition,boolean isExpanded,View convertView,ViewGroup parent)
{
View item = getViewGroupGroup(convertView,parent);

TextView text =(TextView)item.findViewById(android.R.id.text1);
text.setText(catg [groupPosition]);
退货项目;
}

private查看getViewGroupGroup(查看convertView,ViewGroup父类)
{
//父类将是ListActivity中的ListView
if(convertView instanceof View)
{
return(View)convertView;
}
上下文context = parent.getContext();
LayoutInflater inflater = LayoutInflater.from(context);
查看item1 =(查看)inflater.inflate(android.R.layout.simple_expandable_list_item_1,null);

返回item1;
}

@Override
public boolean hasStableIds()
{
// TODO自动生成的方法存根
return false;

$ b @Override
public boolean isChildSelectable(int groupPosition,int childPosition)
{
// TODO自动生成方法存根
返回true;


$ b $ / code $ / pre
$ b

LIST ROW LAYOUT

 <?xml version =1.0encoding =utf-8?> 
< LinearLayout
android:id =@ + id / linearLayout1
android:layout_width =fill_parent
android:layout_height =fill_parent
xmlns:机器人= http://schemas.android.com/apk/res/android >
< GridView
xmlns:android =http://schemas.android.com/apk/res/android
android:id =@ + id / gridview
android:layout_width =fill_parent
android:layout_height =fill_parent
android:columnWidth =50dp
android:numColumns =auto_fit
android:verticalSpacing = 10dp
android:horizo​​ntalSpacing =10dp
android:stretchMode =columnWidth
android:gravity =center/>
< / LinearLayout>


解决方案

这种情况,但到目前为止没有找到任何东西,所以我做了一个解决方法。



这种解决方法假设您知道/可以检索列的宽度,以及网格单元格渲染器的高度(布局xmls中设置的dp大小)。



您应该在 ExpandableListAdapter中插入更多行的 getChildView 方法:

  @覆盖
public View getChildView(int groupPosition,int childPosition,boolean isLastChild,View convertView,ViewGroup parent)
{
ViewGroup item = getViewGroupChild(convertView,parent);
GridView标签=(GridView)item.findViewById(ipvc.estg.placebook.R.id.gridview);
label.setAdapter(new GridAdapter(parent.getContext(),groupPosition + 1));

//初始化下面的变量(我已经根据你的布局完成了
//注意:rowHeightDp基于我的grid_cell.xml,这就是我的$ b的高度$ b //分配给网格中的项目
final int spacingDp = 10;
final int colWidthDp = 50;
final int rowHeightDp = 20;

//将dp值转换为像素
final float COL_WIDTH = getBaseContext()。getResources()。getDisplayMetrics()。density * colWidthDp;
final float ROW_HEIGHT = getBaseContext()。getResources()。getDisplayMetrics() ).density * rowHeightDp;
final float SPACING = getBaseContext()。getResources()。getDisplayMetrics()。density * spacingDp;

//根据您的显示计算列数和行数
final int colCount =(int)Math.floor((parentView.getWidth() - (2 * SPACING))/(COL_WIDTH + SPACING));
final int rowCount =(int)Math.ceil ((intValues.size()+ 0d)/ colCount);

//计算e当前网格的高度
final int GRID_HEIGHT = Math.round(rowCount *(ROW_HEIGHT + SPACING));

//设置当前网格的高度
label.getLayoutParams()。height = GRID_HEIGHT;

退货项目;
}

通过上述添加,我可以生成以下显示布局: p>







我希望它可以帮助你。


I want to put an gridview with images inside an expandable list...I've already make that, but the gridview don't show all item...

How can i make my expandable list child adapt to the gridview size?

LIST ADAPTER

public class CustomListAdapter extends BaseExpandableListAdapter
{
    String[]            catg    = { "Administração, Escritorio e Industria", "Cultura e Entretenimento", "Educação e Crianças", "Eventos e Estado do tempo", "Amigos, Familia e Habitações", "Multimédia", "Diversos", "Números e Letras", "Restaurantes e Hoteis", "Desporto, Saude e Beleza", "Lojas", "Turismo e Natureza", "Transportes" };

    Context             myctx;


    @Override
    public Object getChild(int groupPosition, int childPosition)
    {
        // TODO Auto-generated method stub
        return null;
    }

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

    @Override
    public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View convertView, ViewGroup parent)
    {
        ViewGroup item = getViewGroupChild(convertView, parent);
        GridView label = (GridView) item.findViewById(ipvc.estg.placebook.R.id.gridview);
        label.setAdapter(new GridAdapter(parent.getContext(), groupPosition+1));
        return item;
    }

    private ViewGroup getViewGroupChild(View convertView, ViewGroup parent)
    {
        // The parent will be our ListView from the ListActivity
        if (convertView instanceof ViewGroup)
        {
            return (ViewGroup) convertView;
        }
        Context context = parent.getContext();
        LayoutInflater inflater = LayoutInflater.from(context);
        ViewGroup item = (ViewGroup) inflater.inflate(ipvc.estg.placebook.R.layout.expandable_list_row, null);
        return item;
    }

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

    @Override
    public Object getGroup(int groupPosition)
    {
        return catg[groupPosition];
    }

    @Override
    public int getGroupCount()
    {
        return catg.length;
    }

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



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

        TextView text = (TextView) item.findViewById(android.R.id.text1);
        text.setText(catg[groupPosition]);
        return item;
    }

    private View getViewGroupGroup(View convertView, ViewGroup parent)
    {
        // The parent will be our ListView from the ListActivity
        if (convertView instanceof View)
        {
            return (View) convertView;
        }
        Context context = parent.getContext();
        LayoutInflater inflater = LayoutInflater.from(context);
        View item1 = (View) inflater.inflate(android.R.layout.simple_expandable_list_item_1, null);

        return item1;
    }

    @Override
    public boolean hasStableIds()
    {
        // TODO Auto-generated method stub
        return false;
    }

    @Override
    public boolean isChildSelectable(int groupPosition, int childPosition)
    {
        // TODO Auto-generated method stub
        return true;
    }

}

LIST ROW LAYOUT

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    android:id="@+id/linearLayout1"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    xmlns:android="http://schemas.android.com/apk/res/android">
    <GridView
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/gridview"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:columnWidth="50dp"
        android:numColumns="auto_fit"
        android:verticalSpacing="10dp"
        android:horizontalSpacing="10dp"
        android:stretchMode="columnWidth"
        android:gravity="center" />
</LinearLayout>

解决方案

I've done tons of searches my own for solving this situation, but didn't find anything so far, so i've made a workaround.

This workaround assumes that you know / can retrieve the width of your columns, and the height of your grid cell renderers (the dp size set in your layout xmls).

You should insert a few more lines inside your ExpandableListAdapter's getChildView method:

@Override
public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View convertView, ViewGroup parent)
{
    ViewGroup item = getViewGroupChild(convertView, parent);
    GridView label = (GridView) item.findViewById(ipvc.estg.placebook.R.id.gridview);
    label.setAdapter(new GridAdapter(parent.getContext(), groupPosition+1));

    // 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 = 20;

    // convert the dp values to pixels
    final float COL_WIDTH = getBaseContext().getResources().getDisplayMetrics().density * colWidthDp;
    final float ROW_HEIGHT = getBaseContext().getResources().getDisplayMetrics().density * rowHeightDp;
    final float SPACING = getBaseContext().getResources().getDisplayMetrics().density * spacingDp;

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

    // calculate the height for the current grid
    final int GRID_HEIGHT = Math.round(rowCount * (ROW_HEIGHT + SPACING));

    // set the height of the current grid
    label.getLayoutParams().height = GRID_HEIGHT;

    return item;
}

With the addition above i was able to produce the following displayed layouts:

and

I hope it can help you as well.

这篇关于在Android中的GridView里面的Expandable列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆