ExpandableListView的高度会影响ChildView的显示

时间:2015-06-03 17:16:04

标签: android expandablelistview

我已经搜索了几天这个问题。这是应用程序的结构。

activity_main.xml中

            <android.support.v4.widget.DrawerLayout
                xmlns:android="http://schemas.android.com/apk/res/android"
                xmlns:tools="http://schemas.android.com/tools"
                android:id="@+id/drawer_layout"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                tools:context=".MainActivity">
                <!-- The main content view -->
                <FrameLayout
                    android:id="@+id/container"
                    android:layout_width="match_parent"
                    android:layout_height="match_parent" />

                <!-- The navigation drawer -->
                <fragment android:id="@+id/fragment_drawer"
                    android:layout_width="240dp"
                    android:layout_height="match_parent"
                    android:layout_gravity="start"
                    android:name="ca.bluecross.ab.view.fragments.DrawerFragment"
                    tools:layout="@layout/abc_drawer_layout" />

            </android.support.v4.widget.DrawerLayout>

这里是片段的布局(不包括一些元素以节省发布空间):

            <?xml version="1.0" encoding="utf-8"?>
            <ScrollView
                android:layout_height="match_parent"
                android:layout_width="fill_parent"

                android:id="@+id/scrollView"
                xmlns:android="http://schemas.android.com/apk/res/android">

                    <LinearLayout
                        android:orientation="vertical"
                        android:layout_width="match_parent"
                        android:layout_height="fill_parent">


                        <TextView
                            android:layout_width="fill_parent"
                            android:layout_height="wrap_content"
        ..                        
        />

                        <RelativeLayout
                            android:layout_width="fill_parent"
                            android:layout_height="wrap_content"
                            android:paddingTop="2dp"
                            android:paddingBottom="5dp"
                            android:background="#21c1c1c1">        
                            ..        
                        </RelativeLayout>

                        ..
                            <ExpandableListView xmlns:android="http://schemas.android.com/apk/res/android"
                                android:layout_width="match_parent"
                                android:layout_height="wrap_content"
                                android:id="@+id/expLVClaims"
                                />
                       .    
                        ..

                        <TableLayout
                            android:layout_width="match_parent"
                            android:layout_height="wrap_content">

                            <TableRow
                                android:layout_width="fill_parent"
                                android:layout_height="wrap_content"
                                android:gravity="center_vertical">

                            <TableRow
                                ..
                            </TableRow>

                            <TableRow
                                android:layout_width="fill_parent"
                                android:layout_height="wrap_content">

                                ..
                            </TableRow>

                            <TableRow
                                android:layout_width="fill_parent"
                                android:layout_height="wrap_content">

                                ..
                            </TableRow>

                        </TableLayout>

                        <RelativeLayout
                            android:orientation="horizontal"
                            android:layout_width="match_parent"
                            android:layout_height="wrap_content"
                            android:layout_marginTop="5dp">

                            <Button
                                android:layout_width="160dp"
                                android:layout_height="wrap_content"
                                android:text="I have more to add"
                                android:id="@+id/btnAddMoreClaim"
                                android:layout_gravity="center_horizontal"
                                android:layout_span="2"
                                android:layout_centerVertical="true"
                                android:layout_marginLeft="40dp"
                                android:background="@color/abc_blue"
                                android:textColor="#FFFFFF" />

                            <Button
                                android:layout_width="160dp"
                                android:layout_height="wrap_content"
                                android:text="I&apos;m ready to submit"
                                android:id="@+id/btnReadyToSubmitClaim"
                                android:layout_span="2"
                                android:layout_marginLeft="5dp"
                                android:layout_toRightOf="@+id/btnAddMoreClaim"
                                android:background="@color/abc_blue"
                                android:textColor="#FFF" />
                        </RelativeLayout>

                        <RelativeLayout
                            android:layout_width="match_parent"
                            android:layout_height="wrap_content"
                            android:layout_marginTop="5dp">

                            <Button
                                android:layout_width="160dp"
                                android:layout_height="wrap_content"
                                android:text="Cancel"
                                android:id="@+id/btnCancelEnterClaim"
                                android:layout_alignWithParentIfMissing="false"
                                android:layout_centerInParent="true"
                                android:background="@color/abc_blue"
                                android:textColor="#FFF" />
                        </RelativeLayout>

                    </LinearLayout>
                </ScrollView>

这是组头的布局:

        <?xml version="1.0" encoding="utf-8"?>
        <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
            android:layout_width="match_parent" android:layout_height="wrap_content">

            <ImageView
                android:layout_width="20dp"
                android:layout_height="20dp"
                android:id="@+id/imgIndicator"
                android:src="@drawable/icon_collapse"
                android:layout_alignParentLeft="true"
                android:layout_alignParentStart="true" />

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="Provider name"
                android:id="@+id/txtProviderName"
                android:layout_toRightOf="@+id/imgIndicator" />

            <ImageView
                android:layout_width="20dp"
                android:layout_height="20dp"
                android:id="@+id/imgDelete"
                android:src="@drawable/icon_cross_delete"
                android:layout_toLeftOf="@+id/imgEdit" />

            <ImageView
                android:layout_width="20dp"
                android:layout_height="20dp"
                android:id="@+id/imgEdit"
                android:layout_alignParentEnd="true"
                android:src="@drawable/icon_edit" />
        </RelativeLayout>

这里是儿童布局:

        <?xml version="1.0" encoding="utf-8"?>
        <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical"
            android:layout_width="match_parent" android:layout_height="match_parent"
            android:background="#59868686">
            <RelativeLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginTop="2dp"
                android:layout_marginBottom="2dp"
                android:id="@+id/relativeLayoutServiceDate">
                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="Date of service"
                    android:id="@+id/txtClaimDetailServiceDateLabel"
                    android:layout_alignParentStart="true"
                    android:textStyle="bold" />

                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="Feb 10, 2015"
                    android:id="@+id/txtClaimDetailServiceDate"
                    android:layout_alignParentEnd="true" />
            </RelativeLayout>

            <RelativeLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginTop="2dp"
                android:layout_marginBottom="2dp"
                android:id="@+id/relativeLayoutProvider">

                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="Provider"
                    android:id="@+id/txtClaimDetailProviderLabel"
                    android:textStyle="bold"
                    android:layout_alignParentStart="true" />

                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="provider&apos;s name"
                    android:id="@+id/txtClaimDetailProviderName"
                    android:layout_alignParentEnd="true" />
            </RelativeLayout>

            <RelativeLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:id="@+id/relativeLayoutProduct">

                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="Product or Service"
                    android:id="@+id/txtProductLabel"
                    android:textStyle="bold"
                    android:layout_alignParentStart="true" />

                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="Product/service"
                    android:id="@+id/txtProduct"
                    android:layout_alignParentEnd="true" />
            </RelativeLayout>

            <RelativeLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginTop="2dp"
                android:layout_marginBottom="2dp"
                android:id="@+id/relativeLayoutClaimAmount">

                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="Claim amount($)"
                    android:id="@+id/txtClaimDetailClaimAmountLabel"
                    android:textStyle="bold"
                    android:layout_alignParentStart="true" />

                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="0.00"
                    android:id="@+id/txtClaimDetailClaimAmount"
                    android:layout_alignParentEnd="true" />
            </RelativeLayout>

            <RelativeLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginTop="2dp"
                android:layout_marginBottom="2dp"
                android:id="@+id/relativeLayoutOtherAmount">

                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="Other amount($)"
                    android:id="@+id/txtClaimDetailOtherAmountLabel"
                    android:textStyle="bold"
                    android:layout_alignParentStart="true" />

                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="0.00"
                    android:id="@+id/txtClaimDetailOtherAmount"
                    android:layout_alignParentEnd="true" />
            </RelativeLayout>

        </LinearLayout>

这里是ExpandableListView的适配器:

            public class EclaimsExpandableListAdapter extends BaseExpandableListAdapter {
                private ArrayList<ClaimGroup> groups;
                public LayoutInflater inflater;
                public Activity activity;
                float mDensity;
                private static final int MAX_ITEMS_MEASURED = 15;
                public EclaimsExpandableListAdapter(Activity act, ArrayList<ClaimGroup> groups){
                    activity = act;
                    this.groups = groups;
                    inflater = act.getLayoutInflater();
                    mDensity = activity.getResources().getDisplayMetrics().density;
                }

                @Override
                public Object getChild(int groupPosition, int childPosition) {
                    return groups.get(groupPosition).getChildren().get(childPosition);
                }

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

                @Override
                public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View convertView, ViewGroup parent) {
                    Log.d("EclaimsExpandableListAdapter :: getChildView() :: ", "Start");
                    ClaimDetail child = (ClaimDetail)getChild(groupPosition, childPosition);

                    if(convertView == null){
                        convertView = inflater.inflate(R.layout.eclaims_claim_detail_health_layout,null);
                    }

                    TextView txtServiceDate = (TextView)convertView.findViewById(R.id.txtClaimDetailServiceDate);
                    txtServiceDate.setText(child.getmServiceDate());
                    TextView txtProviderName = (TextView)convertView.findViewById(R.id.txtClaimDetailProviderName);
                    txtProviderName.setText(child.getProvider().getName());
                    TextView txtClaimAmount = (TextView)convertView.findViewById(R.id.txtClaimDetailClaimAmount);
                    txtClaimAmount.setText(child.getClaimAmount().toString());
                    TextView txtOtherAmout = (TextView)convertView.findViewById(R.id.txtClaimDetailOtherAmount);
                    if(child.getOtherAmount() == null){
                        txtOtherAmout.setVisibility(View.GONE);
                    }else{
                        txtOtherAmout.setText(child.getOtherAmount().toString());
                    }

                    return convertView;
                }

                @Override
                public int getChildrenCount(int grpPostion) {
                    System.out.println("Group position :: " + grpPostion);
                    return groups.get(grpPostion).getChildren().size();
                }

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

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

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

                @Override
                public View getGroupView(final int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) {
                    Log.d("EclaimsExpandableListAdapter :: getGroupView() :: ", "Start");
                    if (convertView == null) {
                        convertView = inflater.inflate(R.layout.eclaims_claim_group_row, null);
                    }
                    /*TextView claimsLabel = (TextView)activity.findViewById(R.id.txtClaimItemsLabel);
                    if(groups.isEmpty()){
                        claimsLabel.setVisibility(View.GONE);
                    }else{
                        claimsLabel.setVisibility(View.VISIBLE);
                    }*/

                    ClaimGroup group = (ClaimGroup) getGroup(groupPosition);
                    ImageView indicator = (ImageView)convertView.findViewById(R.id.imgIndicator);

                    int nH = 0;
                    if(isExpanded){
                        indicator.setImageResource(R.drawable.icon_expand);
                        //nH = measureChildrenHeight(groupPosition);
                    }else{
                        indicator.setImageResource(R.drawable.icon_collapse);
                        //nH = convertView.getMeasuredHeight();
                    }
                    //parent.getLayoutParams().height = (int) (nH * mDensity);
                    //parent.invalidate();

                    TextView providerName = (TextView)convertView.findViewById(R.id.txtProviderName);
                    providerName.setText(group.getProvider().getName());

                    ImageView imgDelete = (ImageView)convertView.findViewById(R.id.imgDelete);
                    imgDelete.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View view) {
                            AlertDialog.Builder builder = new AlertDialog.Builder(activity);
                            String confirmation = activity.getString(R.string.eclaims_delete_claim_confirmation);
                            builder.setMessage(confirmation);
                            builder.setPositiveButton(R.string.alert_Cancel, new DialogInterface.OnClickListener() {
                                @Override
                                public void onClick(DialogInterface dialog, int which) {
                                    // Do nothing
                                }
                            });
                            builder.setNegativeButton(R.string.alert_OK, new DialogInterface.OnClickListener() {
                                @Override
                                public void onClick(DialogInterface dialog, int which) {
                                    groups.remove(groupPosition);
                                    notifyDataSetChanged();
                                }
                            });

                            AlertDialog dialog = builder.create();
                            dialog.show();
                        }
                    });

                    ImageView imgEdit = (ImageView)convertView.findViewById(R.id.imgEdit);
                    imgEdit.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View view) {
                            AlertDialog.Builder builder = new AlertDialog.Builder(activity);
                            String confirmation = activity.getString(R.string.eclaims_edit_claim_confirmation);
                            builder.setMessage(confirmation);
                            builder.setPositiveButton(R.string.alert_Cancel, new DialogInterface.OnClickListener() {
                                @Override
                                public void onClick(DialogInterface dialog, int which) {
                                    // Do nothing
                                }
                            });
                            builder.setNegativeButton(R.string.alert_OK, new DialogInterface.OnClickListener() {
                                @Override
                                public void onClick(DialogInterface dialog, int which) {
                                    // first: remove from the completed claims
                                    Provider selectedProvider = ((ClaimGroup)getGroup(groupPosition)).getProvider();
                                    ClaimGroup parent = groups.remove(groupPosition);
                                    List<Product> prodList = parent.getProductList(); // retrieved the saved product list
                                    ClaimDetail child = parent.getChildren().get(0); // there is only one child
                                    Product selectedProduct = child.getProduct();
                                    notifyDataSetChanged();
                                    // second: populate the fields in claim detail block
                                    TextView serviceDate = (TextView)activity.findViewById(R.id.txtServiceDate);
                                    serviceDate.setText(child.getmServiceDate());

                                    Spinner spinnerProvider = (Spinner)activity.findViewById(R.id.spinnerProviders);
                                    ArrayAdapter<Provider> adapterProvider = (ArrayAdapter< Provider >)spinnerProvider.getAdapter();
                                    int size = adapterProvider.getCount();
                                    int pos_seletion = 0;
                                    for(int i=0; i<size; i++){
                                        Provider prvd = adapterProvider.getItem(i);
                                        if(selectedProvider.equals(prvd)){
                                            pos_seletion = i;
                                            break;
                                        }
                                    }
                                    System.out.println("Selected position for provider list :: " + pos_seletion);
                                    spinnerProvider.setSelection(pos_seletion);
                                    adapterProvider.notifyDataSetChanged();// do we need to notify?

                                    Spinner spinnerProduct = (Spinner)activity.findViewById(R.id.spinnerProducts);
                                    ArrayAdapter<Product> adapterProduct = (ArrayAdapter < Product >)spinnerProduct.getAdapter();
                                    size = prodList.size();
                                    pos_seletion = 0;
                                    for(int j=0; j<size; j++){
                                        Product prod = prodList.get(j);
                                        if(selectedProduct.equals(prod)){
                                            pos_seletion = j;
                                            break;
                                        }
                                    }
                                    System.out.println("Selected position for product list :: " + pos_seletion);
                                    adapterProduct.clear();
                                    adapterProduct.addAll(prodList);
                                    spinnerProduct.setSelection(pos_seletion);
                                    adapterProduct.notifyDataSetChanged(); // do we need to notify?

                                    EditText claimAmt = (EditText)activity.findViewById(R.id.editClaimAmount);
                                    claimAmt.setText(child.getClaimAmount().toString());

                                    EditText otherAmt = (EditText)activity.findViewById(R.id.editOtherAmount);
                                    if(child.getOtherAmount() != null){
                                        otherAmt.setText(child.getOtherAmount().toString());
                                    }
                                }
                            });

                            AlertDialog dialog = builder.create();
                            dialog.show();
                        }
                    });

                    return convertView;
                }

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

                @Override
                public boolean isChildSelectable(int groupPosition, int childPosition) {
                    return false;
                }

                @Override
                public void onGroupCollapsed(int groupPosition) {
                    super.onGroupCollapsed(groupPosition);
                }

                @Override
                public void onGroupExpanded(int groupPosition) {
                    super.onGroupExpanded(groupPosition);
                }


            }

以下是行为:单击组标题时,不显示子视图。从LogCat中,我可以看到适配器中的getViewGroup()被调用了两次,但根本没有调用getChildView()。在搜索互联网并试用它们之后,我仍然无法在点击组标题时显示子视图,但是当我尝试将ExpanandableListView高度设置为固定高度时,比如400dp,就像这样:

            <ExpandableListView xmlns:
                                android:layout_width="match_parent"
                                android:layout_height="400dp"
                                android:id="@+id/expLVClaims"/>

然后按预期展开和折叠子视图。但问题是因为固定高度会使ExpandableListView垂直占用400dp空间,即使适配器中根本没有数据,这也是不需要的行为。

有人可以提一些提示吗?

提前致谢! 肖恩

1 个答案:

答案 0 :(得分:1)

第一件事: 您不应该在滚动视图中使用任何滚动组件(如可扩展列表),这是Listview inside ScrollView is not scrolling on Android的原因。

第二件事: ExpandableListView的高度应为match_parent。

我也遇到了与高度相关的问题,并且使得ExpandableListView高度为match_parent解决了我的问题。我想这会对你有帮助。