我正在尝试在 Coordinatorlayout
中实现 ExpandableListView
。
我能够填充数据并显示,但是当我保留时 ExpandableList 没有得到扩展
android:layout_height="wrap_content/match_parent/fill_parent"
但当我执行 android:layout_height="500dp"
时,它会展开。
我的 subview 的高度为 100dp,因此对于列表中的 5 个项目,500dp 可以正常工作。
下面是部分代码。
布局.xml
<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
tools:context="com..example.MainActivity">
<android.support.design.widget.AppBarLayout
android:id="@+id/app_bar"
android:layout_width="match_parent"
android:layout_height="@dimen/app_bar_height"
android:fitsSystemWindows="true"
android:theme="@style/AppTheme.AppBarOverlay">
<android.support.design.widget.CollapsingToolbarLayout
android:id="@+id/toolbar_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
app:contentScrim="?attr/colorPrimary"
app:layout_scrollFlags="scroll|exitUntilCollapsed">
<android.support.v7.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
app:layout_collapseMode="pin"
app:popupTheme="@style/AppTheme.PopupOverlay" />
</android.support.design.widget.CollapsingToolbarLayout>
</android.support.design.widget.AppBarLayout>
<android.support.v4.widget.NestedScrollView
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="@string/appbar_scrolling_view_behavior">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:paddingBottom="24dp"
android:paddingTop="24dp">
<View
android:layout_width="fill_parent"
android:layout_height="1dp"
android:layout_marginTop="10dp"
android:background="#cccccc"/>
<ExpandableListView
android:layout_weight="1"
android:id="@+id/lvExp"
android:layout_width="match_parent"
android:layout_height="500dp"
android:cacheColorHint="#00000000"/>
</LinearLayout>
</android.support.v4.widget.NestedScrollView>
<android.support.design.widget.FloatingActionButton
android:id="@+id/fab"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="@dimen/fab_margin"
android:src="@android:drawable/ic_dialog_email"
app:layout_anchor="@id/app_bar"
app:layout_anchorGravity="bottom|end" />
</android.support.design.widget.CoordinatorLayout>
主 Activity .java
public class MainActivity extends AppCompatActivity implements ViewInterface{
private Intent myIntent;
private RequestQueue queue;
AdapterView listAdapter;
ExpandableListView expListView;
List<String> listDataHeader;
HashMap<String, List<ITEM>> listDataChild;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
// get the listview
expListView = (ExpandableListView) findViewById(R.id.lvExp);
expListView.setFocusable(false);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
expListView.setNestedScrollingEnabled(true);
}
FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
fab.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
.setAction("Action", null).show();
}
});
prepdata();
// Listview Group click listener
expListView.setOnGroupClickListener(new ExpandableListView.OnGroupClickListener() {
@Override
public boolean onGroupClick(ExpandableListView parent, View v, int groupPosition, long id) {
// Toast.makeText(getApplicationContext(),
// "Group Clicked " + listDataHeader.get(groupPosition),
// Toast.LENGTH_SHORT).show();
return false;
}
});
// Listview Group expanded listener
expListView.setOnGroupExpandListener(new ExpandableListView.OnGroupExpandListener() {
@Override
public void onGroupExpand(int groupPosition) {
Toast.makeText(getApplicationContext(),
listDataHeader.get(groupPosition) + " Expanded"+(height+6)*10,
Toast.LENGTH_SHORT).show();
}
});
// Listview Group collasped listener
expListView.setOnGroupCollapseListener(new ExpandableListView.OnGroupCollapseListener() {
@Override
public void onGroupCollapse(int groupPosition) {
Toast.makeText(getApplicationContext(),
listDataHeader.get(groupPosition) + " Collapsed",
Toast.LENGTH_SHORT).show();
}
});
// Listview on child click listener
expListView.setOnChildClickListener(new ExpandableListView.OnChildClickListener() {
@Override
public boolean onChildClick(ExpandableListView parent, View v,
int groupPosition, int childPosition, long id) {
// TODO Auto-generated method stub
Toast.makeText(
getApplicationContext(),
listDataHeader.get(groupPosition)
+ " : "
+ listDataChild.get(
listDataHeader.get(groupPosition)).get(
childPosition), Toast.LENGTH_SHORT)
.show();
return false;
}
});
}
@Override
public void prepdata(){
//Start Data Prep
listDataHeader = new ArrayList<String>();
listDataChild = new HashMap<String, List<ITEM>>();
// Adding child data
listDataHeader.add("Choose your options1");
listDataHeader.add("Choose your options2");
// Adding child data
List<ITEM> options = new ArrayList<ITEM>();
ITEM item1 = new ITEM("Burger King1", "Rs 491", "YesOff","Haveit1");
ITEM item2 = new ITEM("Burger King2", "Rs 492", "YesOff","Haveit2");
ITEM item3 = new ITEM("Burger King3", "Rs 493", "YesOff","Haveit3");
ITEM item4 = new ITEM("Burger King4", "Rs 494", "YesOff","Haveit4");
ITEM item5 = new ITEM("Burger King5", "Rs 495", "YesOff","Haveit5");
options.add(item1);
options.add(item2);
options.add(item3);
options.add(item4);
options.add(item5);
// Header, Child data
listDataChild.put(listDataHeader.get(0), options);
listDataChild.put(listDataHeader.get(1), options);
//End Data Prep
listAdapter = new AdapterView(this, listDataHeader, listDataChild);
// setting list adapter
expListView.setAdapter(listAdapter);
}
}
最佳答案
ExpandableListView
与 CoordinatorLayout
很奇怪。此外,当您将它们包装到 CoordinatorLayout
中时,您找不到很多关于它们的 ScrollBehaviour
的文档。
我遇到了同样的问题,但我通过将我的 ExpandableListView 放入 SwipeRefreshLayout
中解决了这个问题,告诉它们应该如何表现。
这是我的工作布局:
<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal">
<android.support.design.widget.AppBarLayout
android:id="@+id/app_bar_expandable_list_view"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar">
<android.support.v7.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="?attr/colorPrimary"
app:layout_scrollFlags="scroll|enterAlways"
app:popupTheme="@style/ThemeOverlay.AppCompat.Light" />
</android.support.design.widget.AppBarLayout>
<android.support.v4.widget.SwipeRefreshLayout
android:id="@+id/swipeToRefresh"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="@string/appbar_scrolling_view_behavior">
<ExpandableListView
android:id="@+id/expandable_list"
android:layout_height="match_parent"
android:layout_width="match_parent"
app:layout_behavior="@string/appbar_scrolling_view_behavior"/>
</android.support.v4.widget.SwipeRefreshLayout>
</android.support.design.widget.CoordinatorLayout>
请注意,使用 SwipeRefreshLayout
将允许滚动到顶部的操作“视觉上进行刷新数据的操作”。
要禁用此功能,请在您的 onCreate() 方法中扩充您的 xml 并编写:
SwipeRefreshLayout swipeRefreshLayout = (SwipeRefreshLayout) findViewById(R.id.swipeToRefresh);
swipeRefreshLayout.setEnabled(false); // disables swipe bottom scroll AND refresh dialog
PS : 我不知道你是否会遇到我当时遇到的同样问题,这是我的 AppBar
包装了我最后一个列表组的一部分.我不得不添加这个 block 来解决这个问题(一切都在评论中):
// Enables scroll under appbar, AND makes parent view consider the height of the AppBar to add
// to the bottom of the page so the ExpandableScrollView last group isn't wrapped.
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) { // >= API 21 version
listView.setNestedScrollingEnabled(true);
}else { // < API 21 version
CoordinatorLayout.LayoutParams params = (CoordinatorLayout.LayoutParams) mSwipeLayout.getLayoutParams();
AppBarLayout appBarLayout = (AppBarLayout) findViewById(R.id.app_bar_expandable_list_view);
TypedValue tv = new TypedValue();
getTheme().resolveAttribute(android.R.attr.actionBarSize, tv, true);
int heightOfAppBarCompat = getResources().getDimensionPixelSize(tv.resourceId); //gets the height of the AppBar
params.bottomMargin = heightOfAppBarCompat;
mSwipeLayout.setLayoutParams(params); //sets the AppBar height as layout_marginBottom of the ExpandableListView
}
希望对您有所帮助。
关于java - Coordinatorlayout 中的 ExpandableListView,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38687303/