android - 可扩展 ListView 中组中的不同元素

标签 android expandablelistview expandablelistadapter

我有一个问题,我想将不同的元素添加到可扩展 ListView 中的一个组中,就像在图片中一样。有可能的? enter image description here我知道,我必须在适配器中更改 getChildView 方法?

 case 2:
                    if (childPosition==0){
                        infalInflater = (LayoutInflater) this._context
                                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);

                        convertView = infalInflater.inflate(R.layout.group_item, null);
                        txtListChild=(TextView)convertView
                                .findViewById(R.id.tv_group_name) ;
                        txtListChild.setText("Вот они, родненькие условия использования)");
                    }
                    else{
                    c = db.getAccamulativeListOfCompany(companyID);
                    infalInflater = (LayoutInflater) this._context
                            .getSystemService(Context.LAYOUT_INFLATER_SERVICE);

                    convertView = infalInflater.inflate(R.layout.item_accumulative_list, null);
                    txtListChild = (TextView) convertView
                            .findViewById(R.id.lblListItem);
                    Log.d("myLogs", "group_pos=" + groupPosition + "," + childPosition);
                    TextView tvDiscount = (TextView) convertView.findViewById(R.id.tv_accumulative_discount);
                    ImageView ivAccamulative = (ImageView) convertView.findViewById(R.id.iv_accamulative_item);

                    if (c != null) {
                        if (c.moveToFirst()) {
                            c.moveToPosition(childPosition-1);


                            txtListChild.setText(c.getString(1) + " руб.");

                            tvDiscount.setText(c.getString(2) + "%");
                            File imgFile = new File(c.getString(4));
                            if (imgFile.exists()) {
                                Bitmap myBitmap = BitmapFactory.decodeFile(imgFile.getAbsolutePath());
                                ivAccamulative.setImageBitmap(myBitmap);
                            }
                        }

                    }
                    }
                    break;

最佳答案

是的,我认为这些是您想要不同类型的 subview 。

在这种情况下,重写下面的函数,返回您期望在 getChildTypeCount 中生成的不同子布局的数量,以及使用组和子位置分配与每个布局相对应的唯一整数(1 到计数)的逻辑作为参数,在 getChildType 中。

在 getChildView 方法中,您将根据 getChildType 方法的逻辑来扩展不同的布局。在这里调用它可能是一个好主意,并使用返回值上的 switch 来确定要膨胀的布局。

如果这样做,您不必在每次回收 View 时都覆盖 View 的布局,ExpandedListView 将负责确保回收 View 时,您只会获得与逻辑匹配的 View 类型在您的 getChildType 方法中。

简单的替代方案是简单地在 getView/getChildView 方法中膨胀您需要的任何 View ,但这当然不是非常有效。

public class MyAwesomeAdapter extends BaseExpandableListAdapter {


@Override
public int getChildType(int groupPosition, int childPosition) {

            // Return a number here, 1 to whatever you return in getChildTypeCount.
            // Each number should correspond to a particular layout, using group
            // and child position to determine which layout to produce. 

    return super.getChildType(groupPosition, childPosition);
}

@Override
public int getChildTypeCount() {

            // Return the number of distinct layouts you expect to create

    return super.getChildTypeCount();
}

@Override
public View getChildView(int gp, int cp, boolean arg2, View view, ViewGroup arg4) {

    if(view == null){

      switch(getChildType(gp, cp)){

       case 1: 
           //inflate type 1
           break;


       case 2:
          //inflate type 2
           break;

       ....


       }
}

} }

关于android - 可扩展 ListView 中组中的不同元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21794959/

相关文章:

android - videoplayer 插件不适用于 cordova/phonegap

android - android中的不同口味

安卓 : getChild() EditText values on button click in ExpandableListView

Android 文件资源管理器访问 child 的 child

android - 可扩展 ListView 复选框正在选中多个框

android - 防止用户复制 Android 应用程序内容

android - 从后台关闭应用程序后取消通知

android - 如何防止 notifyDataSetChanged 折叠 ExpandableListView 中的打开组?

java - ExpandableListView parent 中的不同开关 Intent - 前一个不起作用

android - ExpandableListView 不显示 subview