【问题标题】:How can I put a ExpandableListView into a ScrollView without it collapsing?如何在不折叠的情况下将 ExpandableListView 放入 ScrollView?
【发布时间】:2015-07-08 17:32:48
【问题描述】:

我想在 ScrollView 中将 ExpandableListView 与其他视图一起使用,但我遇到了 ExpandableListView 中的自滚动问题我试图禁用它,但问题在于 ExpandableListView 的高度及其内部的布局。 所以我想

  • 禁用 ExpandableListView 滚动
  • 在单击 groupView 时调整 ExpandableListView 和 LinearLayout 的大小

我用谷歌搜索了一个解决方案,我找到了一个仅适用于 ListView 的解决方案

Listview in ScrollView

我想使用 ExpandableListView(使用自定义适配器)进行相同的锻炼。

这是我的代码: MainActivity.java

package fablabegypt.android.expandablelistview2;

import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ExpandableListView;
import android.widget.LinearLayout;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;


public class MainActivity extends ActionBarActivity {

    mListAdapter listAdapter;
    ExpandableListView expListView;
    LinearLayout linearLayout;
    List<String> listDataHeader;
    HashMap<String, List<String>> listDataChild;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        linearLayout = (LinearLayout) findViewById(R.id.linear_holder);

        prepareListData();
        expListView = (ExpandableListView) findViewById(R.id.expand_list);
    //expListView.setScrollContainer(false);
    expListView.setHorizontalScrollBarEnabled(false);
    expListView.setVerticalScrollBarEnabled(false);
    expListView.setFastScrollEnabled(false);
    expListView.setSmoothScrollbarEnabled(false);
    //expListView.setOverscrollHeader(null);
    expListView.setFooterDividersEnabled(false);
    //expListView.setOverscrollFooter(null);
    //expListView.setVerticalFadingEdgeEnabled(false);
    //expListView.setHorizontalFadingEdgeEnabled(false);
    expListView.setOnChildClickListener(new ExpandableListView.OnChildClickListener() {
        @Override
        public boolean onChildClick(ExpandableListView parent, View v, int groupPosition, int childPosition, long id) {
            return true;
        }
    });
    expListView.setOnGroupClickListener(new ExpandableListView.OnGroupClickListener() {
        @Override
        public boolean onGroupClick(ExpandableListView parent, View v, int groupPosition, long id) {
            if (parent.isGroupExpanded(groupPosition)) {
                parent.collapseGroup(groupPosition);
            } else {
                parent.expandGroup(groupPosition);
            }
            //telling the listView we have handled the group click, and don't want the default actions.
            return true;
        }
    });
    expListView.setOnTouchListener(new View.OnTouchListener() {
        @Override
        public boolean onTouch(View v, MotionEvent event) {
            if (event.getAction() == MotionEvent.ACTION_UP) {
                return false;
            }else {

            }
            return true;
        }
    });
    listAdapter = new mListAdapter(this, listDataHeader,listDataChild);
    expListView.setAdapter(listAdapter);
}
private void prepareListData() {
    listDataHeader = new ArrayList<String>();
    listDataChild = new HashMap<String, List<String>>();

    // Adding child data
    listDataHeader.add("Top 250");
    listDataHeader.add("Now Showing");
    listDataHeader.add("Coming Soon..");

    // Adding child data
    List<String> top250 = new ArrayList<String>();
    top250.add("The Shawshank Redemption");
    top250.add("The Godfather");
    top250.add("The Godfather: Part II");
    top250.add("Pulp Fiction");
    top250.add("The Good, the Bad and the Ugly");
    top250.add("The Dark Knight");
    top250.add("12 Angry Men");

    List<String> nowShowing = new ArrayList<String>();
    nowShowing.add("The Conjuring");
    nowShowing.add("Despicable Me 2");
    nowShowing.add("Turbo");
    nowShowing.add("Grown Ups 2");
    nowShowing.add("Red 2");
    nowShowing.add("The Wolverine");

    List<String> comingSoon = new ArrayList<String>();
    comingSoon.add("2 Guns");
    comingSoon.add("The Smurfs 2");
    comingSoon.add("The Spectacular Now");
    comingSoon.add("The Canyons");
    comingSoon.add("Europa Report");

    listDataChild.put(listDataHeader.get(0), top250); // Header, Child data
    listDataChild.put(listDataHeader.get(1), nowShowing);
    listDataChild.put(listDataHeader.get(2), comingSoon);
}



@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.menu_main, menu);
    return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    // Handle action bar item clicks here. The action bar will
    // automatically handle clicks on the Home/Up button, so long
    // as you specify a parent activity in AndroidManifest.xml.
    int id = item.getItemId();

    //noinspection SimplifiableIfStatement
    if (id == R.id.action_settings) {
        finish();
        return true;
    }

        return super.onOptionsItemSelected(item);
    }
}

mListAdapter.java

package fablabegypt.android.expandablelistview2;

import android.content.Context;
import android.database.DataSetObserver;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseExpandableListAdapter;
import android.widget.ExpandableListAdapter;
import android.widget.TextView;

import java.util.HashMap;
import java.util.List;

/**
 * Created by Mouso on 4/27/2015.
 */
public class mListAdapter extends BaseExpandableListAdapter {

    private Context context;
    private List<String> header_list;
    private HashMap<String, List<String>> children_data_list;

    public mListAdapter(Context context,List<String> header_list,HashMap<String,List<String>> children_data_list){
        this.context = context;
        this.header_list = header_list;
        this.children_data_list = children_data_list;
    }

    @Override
    public void registerDataSetObserver(DataSetObserver observer) {

    }

    @Override
    public void unregisterDataSetObserver(DataSetObserver observer) {

    }

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

    @Override
    public int getChildrenCount(int groupPosition) {
        return this.children_data_list.get(this.header_list.get(groupPosition)).size();
    }

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

    @Override
    public Object getChild(int groupPosition, int childPosition) {
        return this.children_data_list.get(this.header_list.get(groupPosition)).get(childPosition);
    }

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

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

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

    @Override
    public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) {
        final String headerText = (String) getGroup(groupPosition);
        if (convertView == null){
            LayoutInflater layoutInflater = (LayoutInflater) this.context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            convertView = layoutInflater.inflate(R.layout.list_group,null);
        }

        TextView headerTxt = (TextView) convertView.findViewById(R.id.list_group_txt);
        headerTxt.setTextSize(20);
        headerTxt.setText(headerText);
        //Log.d("Mouso",headerText);

        return convertView;
    }

    @Override
    public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View convertView, ViewGroup parent) {
        final String childText = (String) getChild(groupPosition,childPosition);

        if (convertView == null){
            LayoutInflater layoutInflater = (LayoutInflater) this.context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            convertView = layoutInflater.inflate(R.layout.list_item,null);
        }

        TextView childTxt = (TextView) convertView.findViewById(R.id.list_child_txt);
        childTxt.setText(childText);

        return convertView;
    }

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

    @Override
    public boolean areAllItemsEnabled() {
        return true;
    }

    @Override
    public boolean isEmpty() {
        if (header_list.size() > 0)
            return true;
        return false;
    }

    @Override
    public void onGroupExpanded(int groupPosition) {

    }

    @Override
    public void onGroupCollapsed(int groupPosition) {

    }

    @Override
    public long getCombinedChildId(long groupId, long childId) {
        return (groupId*100)+childId;
    }

    @Override
    public long getCombinedGroupId(long groupId) {
        return groupId;
    }

}

activity_main.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
    android:layout_height="match_parent" android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    android:paddingBottom="@dimen/activity_vertical_margin" tools:context=".MainActivity">

    <ScrollView
        android:layout_width="wrap_content"
        android:layout_height="500px"
        android:fillViewport="true">
    <LinearLayout
        android:id="@+id/linear_holder"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical">

        <TextView
            android:id="@+id/txt"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:text="we 7yat 3neak we fadaha 3neya \n\n\n dana ba7ebak ad 3naya"/>

        <ExpandableListView
            android:id="@+id/expand_list"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:isScrollContainer="false">

        </ExpandableListView>
    </LinearLayout>
    </ScrollView>

</RelativeLayout>

list_group.xml

<?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">

    <TextView
        android:id="@+id/list_group_txt"
        android:paddingLeft="?android:attr/expandableListPreferredItemPaddingLeft"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent" />


</LinearLayout>

list_item.xml

<?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">

    <TextView
        android:id="@+id/list_child_txt"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent" />

</LinearLayout>

我实施锻炼的尝试

Helper.java

package fablabegypt.android.expandablelistview2;

import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseExpandableListAdapter;
import android.widget.ExpandableListAdapter;
import android.widget.ExpandableListView;
import android.widget.ListAdapter;
import android.widget.ListView;

/**
 * Created by Mouso on 4/29/2015.
 */
public class ListViewHelper {
    public static void getListViewSize(ExpandableListView myListView) {
        ListAdapter myListAdapter = myListView.getAdapter();

        if (myListAdapter == null) {
            //do nothing return null
            return;
        }
        //set listAdapter in loop for getting final size
        int totalHeight = 0;
        for (int groupSize = 0; groupSize < myListAdapter.getGroupCount(); groupSize++) {
            View listItem = myListAdapter.getGroupView(groupSize, false, null, myListView);
            listItem.measure(0, 0);
            totalHeight += listItem.getMeasuredHeight();
            for (int size = 0; size < myListAdapter.getChildrenCount(groupSize); size++) {
                if (size == myListAdapter.getChildrenCount(groupSize)-1)
                    listItem = myListAdapter.getChildView(groupSize, size, true, null, myListView);
                listItem = myListAdapter.getChildView(groupSize, size, false, null, myListView);
                listItem.measure(0, 0);
                totalHeight += listItem.getMeasuredHeight();
            }
        }
        //setting listview item in adapter
        ViewGroup.LayoutParams params = myListView.getLayoutParams();
        params.height = totalHeight + (myListView.getDividerHeight() * (myListAdapter.getGroupCount() - 1));
        myListView.setLayoutParams(params);
        // print height of adapter on log
        Log.d("mouso", "height of listItem:"+String.valueOf(totalHeight));
    }

    //Read more: http://www.androidhub4you.com/2012/12/listview-into-scrollview-in-android.html#ixzz3Yh4m4MPG

}

【问题讨论】:

  • MANTRA从不scrollable View 放入 ScrollView。重复至少一百万次。
  • 参考我在这篇 SO 帖子上的回答,可能会给你一些想法或提示。 stackoverflow.com/a/36544003/4513962

标签: java android scrollview expandablelistview


【解决方案1】:

扩展 ExpandableListView 类并将 expand 设置为 false

    public class CustomExpandableListView extends ExpandableListView {

    boolean expanded = false;

    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);
    }

    public boolean isExpanded() {
        return expanded;
    }

    @Override
    public void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {

        if (isExpanded()) {
            int expandSpec = MeasureSpec.makeMeasureSpec(
                    Integer.MAX_VALUE >> 2, MeasureSpec.AT_MOST);
            super.onMeasure(widthMeasureSpec, expandSpec);
            ViewGroup.LayoutParams params = getLayoutParams();
            params.height = getMeasuredHeight();
        } else {
            super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        }
    }

    public void setExpanded(boolean expanded) {
        this.expanded = expanded;
    }
}

在你的活动课上

CustomExpandableListView customExpandableListView = (CustomExpandableListView ) findViewById(R.id.expandable_list);
customExpandableListView .setExpanded(true);

布局文件

 <com.xxx.xxx.util.ExpandableHeightListView
                    android:id="@+id/expandable_list"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    />

现在它将在滚动视图中工作

【讨论】:

    【解决方案2】:

    我也有同样的问题

    我解决了这个ExpandableListView中添加页眉和页脚的问题

     <ScrollView
            android:layout_width="match_parent"
            android:layout_height="wrap_content">
            <LinearLayout
                android:orientation="vertical"
                android:layout_width="match_parent"
                android:layout_height="wrap_content">
    
                 <ExpandableListView
                    android:id="@+id/listview"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content">
    
                </ExpandableListView>
    
            </LinearLayout>
        </ScrollView>
    

    /// 添加标题

    ExpandableListView listview
    
    ViewGroup headerView = (ViewGroup) getLayoutInflater().inflate(R.layout.item_header, listview, false);
    
    listview.addHeaderView(headerView_right);
    
    */// add footer*
    
    
    ViewGroup footerview= (ViewGroup) getLayoutInflater().inflate(R.layout.item_footer, listview, false);
    
    listview.addFooterView(headerView_right);
    

    【讨论】:

      【解决方案3】:

      将具有自己滚动功能的视图放在另一个具有滚动功能的视图中总是一个非常糟糕的主意。这可能会导致触摸操作被错误的视图截获、性能不佳等。此外,从您的主布局来看,除了使 TextView 与列表视图一起滚动之外,您似乎没有将 ScrollView 用于任何其他操作。为什么不将其添加为标题视图? expListView.setHeaderView(View) 并将 textview 膨胀到其中。

      【讨论】:

        【解决方案4】:

        正如我在您的“activity_main.layout”中看到的,我认为您想放置 ExpandableListView 以使 textview 始终与列表一起滚动,不是吗?

        你有两个解决方案。

        - 最漂亮和最简单的解决方案是使用

        将 textview 作为 ExpandableListView 的标题
        list.addHeaderView(listHeader,null,false); 
        

        重要:使用“false”作为第三个标题不能作为一行点击。

        -另一种解决方案是每次调用“parent.collapseGroup(groupPosition)”或“parent.expandGroup(groupPosition);”时调用“getListViewSize()” 我知道不建议在滚动视图中使用列表视图,但有时很有用。不幸的是,情况并非如此,我建议使用第一个解决方案。

        希望能帮到你。

        【讨论】:

          猜你喜欢
          • 2011-03-30
          • 1970-01-01
          • 2016-08-26
          • 2021-12-25
          • 1970-01-01
          • 2011-08-16
          • 2019-05-11
          • 2015-10-10
          • 2022-07-21
          相关资源
          最近更新 更多