
我想在ScrollVIEw中使用ExpandableListVIEw和其他视图,但我遇到了ExpandableListVIEw中的自动滚动问题,我试图禁用它,但问题在于ExpandableListVIEw的高度及其内部的布局.
所以我想
>禁用ExpandableListVIEw滚动
>调整ExpandableListVIEw&的大小包含groupVIEw时包含的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);}@OverrIDepublic 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;}@OverrIDepublic 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}解决方法:
在具有滚动功能的另一个视图中放置具有自己的滚动功能的视图总是一个非常糟糕的主意.这可能导致触摸 *** 作被错误的视图,糟糕的性能等拦截.此外,从您的主布局中,除了使用列表视图进行TextVIEw滚动之外,您似乎没有使用ScrollVIEw.为什么不将它添加为标题视图? expListVIEw.setheaderVIEw(VIEw)并将textvIEw扩展到其中.
总结以上是内存溢出为你收集整理的java – 如何在不折叠的情况下将ExpandableListView放入ScrollView?全部内容,希望文章能够帮你解决java – 如何在不折叠的情况下将ExpandableListView放入ScrollView?所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
微信扫一扫
支付宝扫一扫
评论列表(0条)