Android-处理ExpandableListView高度

Rom*_*get 5 android expandablelistview expandablelistadapter

我在LinearLayout中实现了ExpandableListView,但是当我运行该应用程序时,得到的是: 在此处输入图片说明

首先,标题出现两次是否正常?

当单击上方的标题时,将得到展开的视图,即我希望的样子: 在此处输入图片说明

如果我触摸第二个标头(我认为不应该存在),它就会消失,让上一个标头单独出现(就像我希望的那样): 在此处输入图片说明

但是 可扩展高度已更改!: 在此处输入图片说明

我不知道为什么会这样。我只想有一个标题,并且我想固定大小。以下是带有相关代码的文件:

ExpandableListView所在的布局:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/main_sv"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:background="@color/white" >

    <LinearLayout 
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical"
        android:layout_marginTop="20dp"
        android:layout_marginBottom="20dp" 
        android:layout_marginLeft="20dp"
        android:layout_marginRight="20dp" >

        ...

        <ExpandableListView
            android:id="@+id/learnabletm"
            android:layout_width="match_parent"
            android:layout_height="200dp" />

    </LinearLayout>

</LinearLayout>
Run Code Online (Sandbox Code Playgroud)

expandable_list_view_child.xml

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

    <TextView
        android:id="@+id/child"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:textSize="17dip"
        android:paddingTop="5dp"
        android:paddingBottom="5dp"
        android:paddingLeft="?android:attr/expandableListPreferredChildPaddingLeft" />

</LinearLayout>
Run Code Online (Sandbox Code Playgroud)

expandable_list_view_group.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical"
    android:padding="8dp">

    <TextView
        android:id="@+id/group"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:paddingLeft="?android:attr/expandableListPreferredItemPaddingLeft"
        android:textSize="18dp" />

</LinearLayout>
Run Code Online (Sandbox Code Playgroud)

ExpAdapter.java

public class ExpAdapter extends BaseExpandableListAdapter {

    public static final String ARG_POS = "pos";
    private Context myContext;
    Bundle data;

    publicExpAdapter(Context context, Bundle data) {
        myContext = context;
        this.data = data;
    }

    @Override
    public Object getChild(int groupPosition, int childPosition) {
        return null;
    }

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

    @Override
    public View getChildView(int groupPosition, int childPosition,
                             boolean isLastChild, View convertView, ViewGroup parent) {

        if (convertView == null) {
            LayoutInflater inflater = (LayoutInflater) myContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            convertView = inflater.inflate(R.layout.expandable_list_view_child, null);
        }

        String text = "";
        for(int i = 0; i < bytes.length; i++){
            for(int j = 0; j < 8; j++){
                text = text + " " + (i*8+j+1) + "\n";
            }
        }
        TextView child = (TextView) convertView.findViewById(R.id.child);
        child.setText(text);

        return convertView;
    }

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

    @Override
    public Object getGroup(int groupPosition) {
        return null;
    }

    @Override
    public int getGroupCount() {
        return 1;
    }

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

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

        if (convertView == null) {
            LayoutInflater inflater =  (LayoutInflater) myContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            convertView = inflater.inflate(R.layout.expandable_list_view_group, null);
        }

        TextView group = (TextView) convertView.findViewById(R.id.group);
        group.setText("Numbers");

        return convertView;
    }

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

    @Override
    public boolean isChildSelectable(int groupPosition, int childPosition) {
        return false;
    }
}
Run Code Online (Sandbox Code Playgroud)

最后是ExpandableListView的片段:

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        ...
        expList = (ExpandableListView) rootView.findViewById(R.id.learnabletm);
        width = args.getInt(ARG_WIDTH);

        expList.setIndicatorBounds(width - GetDipsFromPixel(50), width - GetDipsFromPixel(10));
        expList.setAdapter(new ExpAdapter(getActivity(), data));
        expList.expandGroup(ExpandableListView.PACKED_POSITION_TYPE_CHILD);
        expList.setOnGroupExpandListener(new ExpandableListView.OnGroupExpandListener(){
            @Override
            public void onGroupExpand(int groupPosition){
                LinearLayout.LayoutParams param = (LinearLayout.LayoutParams) expList.getLayoutParams();
                param.height = expList.getHeight();
                expList.setLayoutParams(param);
                expList.refreshDrawableState();
            }
        });

        expList.setOnGroupCollapseListener(new ExpandableListView.OnGroupCollapseListener(){
            @Override
            public void onGroupCollapse(int groupPosition){
                LinearLayout.LayoutParams param = (LinearLayout.LayoutParams) expList.getLayoutParams();
                param.height = LinearLayout.LayoutParams.WRAP_CONTENT;
                expList.setLayoutParams(param);
                expList.refreshDrawableState();
            }
        });

        expList.setOnChildClickListener(new ExpandableListView.OnChildClickListener(){
            @Override
            public boolean onChildClick(ExpandableListView parent, View v,
                                        int groupPosition, int childPosition, long id){
                Log.d("OnChildClickListener", "OK");
                return false;
            }
        });
    }
Run Code Online (Sandbox Code Playgroud)