如何在scrollview android中使用多个expandablelistview?

Posted

技术标签:

【中文标题】如何在scrollview android中使用多个expandablelistview?【英文标题】:how to use multiple expandablelistview inside scrollview android? 【发布时间】:2018-09-28 04:24:46 【问题描述】:

我想在单个屏幕中设置 3 个 expandablelistview 我想添加多个 expandablelistview

XmlLayout

//this is my scorllview
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_
    android:layout_
    android:fillViewport="true"
    tools:context=".fragments.FragmentTestWellnessScoreBoard">


    <LinearLayout
        android:layout_
        android:layout_
        android:orientation="vertical">


        <LinearLayout
            android:id="@+id/llHealth"
            android:orientation="vertical"
            android:layout_
            android:layout_>

            <TextView
                android:id="@+id/tvHealthTitle"
                android:layout_
                android:layout_
                android:gravity="center"
                android:textStyle="bold"
                android:layout_margin="@dimen/margin_10"
                android:text="Health"/>

            <ExpandableListView
                android:id="@+id/elHealth"
                android:layout_
                android:layout_ />

        </LinearLayout>


        <LinearLayout
            android:id="@+id/llChallenge"
            android:orientation="vertical"
            android:layout_
            android:layout_>

            <TextView
                android:id="@+id/tvChallengeTitle"
                android:layout_
                android:layout_
                android:gravity="center"
                android:textStyle="bold"
                android:layout_margin="@dimen/margin_10"
                android:text="Challenge"/>

            <ExpandableListView
                android:id="@+id/elChallenge"
                android:layout_
                android:layout_ />

        </LinearLayout>


        <LinearLayout
            android:id="@+id/llRisk"
            android:orientation="vertical"
            android:layout_
            android:layout_>

            <TextView
                android:id="@+id/tvRiskTitle"
                android:layout_
                android:layout_
                android:gravity="center"
                android:textStyle="bold"
                android:layout_margin="@dimen/margin_10"
                android:text="Risk"/>

            <ExpandableListView
                android:id="@+id/elRisk"
                android:layout_
                android:layout_ />

        </LinearLayout>

    </LinearLayout>

我想在用户点击时一一设置适配器我不知道如何设置多个expandableList? Java 文件

private TestWellnessScoreBoardExpandableListAdapter mHealthaAdapter;
private TestWellnessScoreBoardExpandableListAdapter mChallengeAdapter;
private TestWellnessScoreBoardExpandableListAdapter mRiskAdapter;

private ExpandableListView elHealth;
private ExpandableListView elChallenge;
private ExpandableListView elRisk;

// this is my click listener to set adapter
private View.OnClickListener mOnClickListener = new View.OnClickListener() 
    @Override
    public void onClick(View v) 

        switch (v.getId()) 
            case R.id.tvHealthTitle:

                mHealthaAdapter = new TestWellnessScoreBoardExpandableListAdapter(getActivity(), mWellnessResult.getWellnessArrayList());
                elHealth.setAdapter(mHealthaAdapter);

                break;
            case R.id.tvChallengeTitle:
                mChallengeAdapter = new TestWellnessScoreBoardExpandableListAdapter(getActivity(), mWellnessResult.getWellnessArrayList());
                elChallenge.setAdapter(mChallengeAdapter);
                break;
            case R.id.tvRiskTitle:
                mRiskAdapter = new TestWellnessScoreBoardExpandableListAdapter(getActivity(), mWellnessResult.getWellnessArrayList());
                elRisk.setAdapter(mRiskAdapter);
                break;
        

    
;

一切正常,但视图未全屏显示 但它没有全屏显示

【问题讨论】:

尝试使用NestedScrollView thedeveloperworldisyours.com/android/… @VishvaDave NestedScrollView 不工作我已经尝试过 @VishvaDave 感谢链接 @VishvaDave 工作正常,非常感谢您 【参考方案1】:

试试这个:

xml代码:

<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/activity_expandable_scroll_view"
android:layout_
android:layout_
android:fillViewport="true">

<LinearLayout
    android:id="@+id/LinearLayout1"
    android:layout_
    android:layout_
    android:orientation="vertical"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin">


         <ExpandableListView
        android:id="@+id/activity_expandable_list_view"
        android:layout_
        android:layout_
        android:background="@color/white"/>

           </LinearLayout>
           </ScrollView>

java类

mListView = (ExpandableListView) findViewById(R.id.activity_expandable_list_view);
    MyExpandableListAdapter adapter = new MyExpandableListAdapter(this,
            mGroups);
    mListView.setAdapter(adapter);
    mListView.setOnGroupClickListener(new ExpandableListView.OnGroupClickListener() 

        @Override
        public boolean onGroupClick(ExpandableListView parent, View v,
                                    int groupPosition, long id) 
            setListViewHeight(parent, groupPosition);
            return false;
        
    );

需要这个功能:

private void setListViewHeight(ExpandableListView listView,
                           int group) 
ExpandableListAdapter listAdapter = (ExpandableListAdapter) listView.getExpandableListAdapter();
int totalHeight = 0;
int desiredWidth = View.MeasureSpec.makeMeasureSpec(listView.getWidth(),
        View.MeasureSpec.EXACTLY);
for (int i = 0; i < listAdapter.getGroupCount(); i++) 
    View groupItem = listAdapter.getGroupView(i, false, null, listView);
    groupItem.measure(desiredWidth, View.MeasureSpec.UNSPECIFIED);

    totalHeight += groupItem.getMeasuredHeight();

    if (((listView.isGroupExpanded(i)) && (i != group))
            || ((!listView.isGroupExpanded(i)) && (i == group))) 
        for (int j = 0; j < listAdapter.getChildrenCount(i); j++) 
            View listItem = listAdapter.getChildView(i, j, false, null,
                    listView);
            listItem.measure(desiredWidth, View.MeasureSpec.UNSPECIFIED);

            totalHeight += listItem.getMeasuredHeight();

        
    


ViewGroup.LayoutParams params = listView.getLayoutParams();
int height = totalHeight
        + (listView.getDividerHeight() * (listAdapter.getGroupCount() - 1));
if (height < 10)
    height = 200;
params.height = height;
listView.setLayoutParams(params);
listView.requestLayout();

【讨论】:

【参考方案2】:

您不能在滚动视图中使用可滚动视图,否则您将遇到子可滚动视图的高度问题。

在滚动视图中使用 ExpandableListView 的两种方式。

1.创建自定义不可滚动展开视图:

public class CustomExpandableListView extends ExpandableListView 

            public CustomExpandableListView(Context context) 
                super(context);
            

            public CustomExpandableListView(Context context, AttributeSet attrs) 
                super(context, attrs);
            

            public CustomExpandableListView(Context context, AttributeSet attrs, int defStyle) 
                super(context, attrs, defStyle);
            

            @Override
            public void onMeasure(int widthMeasureSpec, int heightMeasureSpec) 
                int heightMeasureSpec_custom = MeasureSpec.makeMeasureSpec(
                        Integer.MAX_VALUE >> 2, MeasureSpec.AT_MOST);
                super.onMeasure(widthMeasureSpec, heightMeasureSpec_custom);
                ViewGroup.LayoutParams params = getLayoutParams();
                params.height = getMeasuredHeight();
            
        

您的 XML:

     <ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_
android:layout_
android:fillViewport="true"
tools:context=".fragments.FragmentTestWellnessScoreBoard">


<LinearLayout
    android:layout_
    android:layout_
    android:orientation="vertical">


    <LinearLayout
        android:id="@+id/llHealth"
        android:orientation="vertical"
        android:layout_
        android:layout_>

        <TextView
            android:id="@+id/tvHealthTitle"
            android:layout_
            android:layout_
            android:gravity="center"
            android:textStyle="bold"
            android:layout_margin="@dimen/margin_10"
            android:text="Health"/>

        <com.yourpkg.CustomExpandableListView
            android:id="@+id/elHealth"
            android:layout_
            android:layout_ />

    </LinearLayout>


    <LinearLayout
        android:id="@+id/llChallenge"
        android:orientation="vertical"
        android:layout_
        android:layout_>

        <TextView
            android:id="@+id/tvChallengeTitle"
            android:layout_
            android:layout_
            android:gravity="center"
            android:textStyle="bold"
            android:layout_margin="@dimen/margin_10"
            android:text="Challenge"/>

        <com.yourpkg.CustomExpandableListView
            android:id="@+id/elChallenge"
            android:layout_
            android:layout_ />

    </LinearLayout>


    <LinearLayout
        android:id="@+id/llRisk"
        android:orientation="vertical"
        android:layout_
        android:layout_>

        <TextView
            android:id="@+id/tvRiskTitle"
            android:layout_
            android:layout_
            android:gravity="center"
            android:textStyle="bold"
            android:layout_margin="@dimen/margin_10"
            android:text="Risk"/>

        <com.yourpkg.CustomExpandableListView
            android:id="@+id/elRisk"
            android:layout_
            android:layout_ />

    </LinearLayout>

</LinearLayout>

    为您想在滚动视图中使用的所有 ExpandableListView 指定硬代码高度。

      <ExpandableListView               
            android:layout_
            android:layout_ />
    

【讨论】:

第一种方法是否适用于 4 级可扩展列表视图??【参考方案3】:

尝试我在您的 xml 设计中所做的以下更改:

<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_
    android:layout_
    android:fillViewport="true">


    <LinearLayout
        android:layout_
        android:layout_
        android:orientation="vertical"
        android:weightSum="3">


        <LinearLayout
            android:id="@+id/llHealth"
            android:layout_
            android:layout_
            android:layout_weight="1"
            android:orientation="vertical">

            <TextView
                android:id="@+id/tvHealthTitle"
                android:layout_
                android:layout_
                android:layout_margin="@dimen/margin_10"
                android:gravity="center"
                android:text="Health"
                android:textStyle="bold" />

            <ExpandableListView
                android:id="@+id/elHealth"
                android:layout_
                android:layout_ />

        </LinearLayout>


        <LinearLayout
            android:id="@+id/llChallenge"
            android:layout_
            android:layout_
            android:layout_weight="1"
            android:orientation="vertical">

            <TextView
                android:id="@+id/tvChallengeTitle"
                android:layout_
                android:layout_
                android:layout_margin="@dimen/margin_10"
                android:gravity="center"
                android:text="Challenge"
                android:textStyle="bold" />

            <ExpandableListView
                android:id="@+id/elChallenge"
                android:layout_
                android:layout_ />

        </LinearLayout>


        <LinearLayout
            android:id="@+id/llRisk"
            android:layout_
            android:layout_
            android:layout_weight="1"
            android:orientation="vertical">

            <TextView
                android:id="@+id/tvRiskTitle"
                android:layout_
                android:layout_
                android:layout_margin="@dimen/margin_10"
                android:gravity="center"
                android:text="Risk"
                android:textStyle="bold" />

            <ExpandableListView
                android:id="@+id/elRisk"
                android:layout_
                android:layout_ />

        </LinearLayout>

    </LinearLayout>
</ScrollView>

【讨论】:

以上是关于如何在scrollview android中使用多个expandablelistview?的主要内容,如果未能解决你的问题,请参考以下文章

android如何使用listview而不是scrollview

Android实践之ScrollView中滑动冲突处理

android 实现多textview的滚动

如何在ScrollView嵌套另一个ScrollView

防止 ScrollView 占用太多高度

在 Scrollview 中滚动时 Android v2 MapFragment 抖动