java 设计库 - CoordinatorLayout/CollapsingToolbarLayout with GridView/listView
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/30696611/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Design lib - CoordinatorLayout/CollapsingToolbarLayout with GridView/listView
提问by Tarun Varshney
This might be silly question but I didn't understand Design lib well. I am following this referenceto create below layout. The Blue area should work as parallax when I scroll the GridView
.
But when I scroll grid View nothing happens in AppBarLayout.
这可能是一个愚蠢的问题,但我不太了解 Design lib。我正在按照此参考创建以下布局。当我滚动GridView
. 但是当我滚动网格视图时,AppBarLayout 中没有任何反应。
But This works with NestedScrollView
and RecyclerView
但这适用于NestedScrollView
和RecyclerView
Below is My Layout file-
下面是我的布局文件-
<?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:id="@+id/main_content"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true">
<android.support.design.widget.AppBarLayout
android:id="@+id/appbar"
android:layout_width="match_parent"
android:background="#500403"
android:layout_height="@dimen/detail_backdrop_height"
android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
android:fitsSystemWindows="true">
<android.support.design.widget.CollapsingToolbarLayout
android:id="@+id/collapsing_toolbar"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#122453"
app:layout_scrollFlags="scroll|exitUntilCollapsed"
android:fitsSystemWindows="true"
app:contentScrim="?attr/colorPrimary"
app:expandedTitleMarginStart="48dp"
app:expandedTitleMarginEnd="64dp">
<ImageView
android:id="@+id/backdrop"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:scaleType="centerCrop"
android:fitsSystemWindows="true"
app:layout_collapseMode="parallax" />
<ImageView
android:id="@+id/backdrop1"
android:layout_width="50dp"
android:layout_height="50dp"
android:scaleType="fitCenter"
android:fitsSystemWindows="true"
android:layout_gravity="center"
android:src="@drawable/bar_offline"
app:layout_collapseMode="parallax" />
<android.support.v7.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="#982223"
app:popupTheme="@style/ThemeOverlay.AppCompat.Light"
app:layout_collapseMode="pin" />
</android.support.design.widget.CollapsingToolbarLayout>
</android.support.design.widget.AppBarLayout>
<GridView
android:id="@+id/grid"
android:numColumns="4"
android:background="#367723"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="@string/appbar_scrolling_view_behavior"
/>
<android.support.design.widget.FloatingActionButton
android:layout_height="wrap_content"
android:layout_width="wrap_content"
app:layout_anchor="@id/appbar"
app:layout_anchorGravity="bottom|right|end"
android:src="@drawable/bar_offline"
android:layout_margin="@dimen/fab_margin"
android:clickable="true"/>
</android.support.design.widget.CoordinatorLayout>
Any help would be appreciated.
任何帮助,将不胜感激。
回答by Tarun Varshney
With ListView/GridView, it works only on Lollipop by following code-
使用 ListView/GridView,它只能通过以下代码在 Lollipop 上工作-
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
listView.setNestedScrollingEnabled(true);
}
I think for Now CoordinatorLayout works only with RecyclerView
and NestedScrollView
我认为现在 CoordinatorLayout 只适用于RecyclerView
和NestedScrollView
EDIT :
编辑 :
use -
利用 -
ViewCompat.setNestedScrollingEnabled(listView/gridview,true); (add Android Support v4 Library 23.1 or +)
回答by daemmie
A simple solution was added to the support lib:
支持库中添加了一个简单的解决方案:
ViewCompat.setNestedScrollingEnabled(listView,true);
I've tested it with Android Support v4 Library 23.1and it works well.
我已经使用Android Support v4 Library 23.1对其进行了测试, 并且运行良好。
回答by Gabriele Mariotti
Currently the ListView
and the GridView
have the expected behavior with the CoordinatorLayout
only with API>21.
目前ListView
和GridView
具有预期的行为,CoordinatorLayout
只有 API>21。
To obtain this behavior you have to set:
要获得此行为,您必须设置:
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
setNestedScrollingEnabled(true);
}
It is not enough to implement the NestedScrollingChild
.
The AbsListView
isn't deployed with support libraries , then it depends by the SO running in the device.
仅实施NestedScrollingChild
. 在AbsListView
未部署与支持库,那就要看通过在设备上运行的SO。
You have to override some methods in the AbsListView. For example you can check the onInterceptTouchEvent
method.
您必须覆盖 AbsListView 中的一些方法。例如,您可以检查onInterceptTouchEvent
方法。
Inside this code you can see:
在这段代码中,您可以看到:
case MotionEvent.ACTION_DOWN: {
//......
startNestedScroll(SCROLL_AXIS_VERTICAL);
//....
}
case MotionEvent.ACTION_MOVE: {
//.....
if (startScrollIfNeeded((int) ev.getX(pointerIndex), y, null)) {
//....
}
case MotionEvent.ACTION_CANCEL:
case MotionEvent.ACTION_UP: {
//..
stopNestedScroll();
break;
}
This code is only in the implementation of AbsListView v21+. If you check the AbsListView with API 20or lower, you will not find any nested scroll reference.
此代码仅在 AbsListView v21+ 的实现中。如果您使用API 20或更低版本检查 AbsListView ,您将找不到任何嵌套滚动引用。
回答by Anantha Babu
You have to put gridview inside NestedScrollview as usual then you have to add gridview height dynamically. then everything would work good.!!!
您必须像往常一样将 gridview 放在 NestedScrollview 中,然后您必须动态添加 gridview 高度。那么一切都会好起来的。!!!
<android.support.v4.widget.NestedScrollView
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:layout_gravity="fill_vertical"
android:fillViewport="true">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<GridView
android:id="@+id/camp_list"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_below="@id/toolbar"
android:layout_margin="10dp"
android:gravity="center"
android:horizontalSpacing="10dp"
android:numColumns="3"
android:stretchMode="columnWidth"
android:verticalSpacing="10dp"
android:visibility="visible" >
</GridView>
</LinearLayout>
</android.support.v4.widget.NestedScrollView>
回答by Kishan Vaghela
This is working for me.
这对我有用。
https://gist.github.com/sakurabird/6868765
https://gist.github.com/sakurabird/6868765
I use GridView inside NestedScrollView
我在 NestedScrollView 中使用 GridView
回答by Shirane85
For convenience i've created a subclass with the new ViewCompat solution:
为方便起见,我使用新的 ViewCompat 解决方案创建了一个子类:
public class CoordinatedListView extends ListView {
public CoordinatedListView(Context context) {
super(context);
init();
}
public CoordinatedListView(Context context, AttributeSet attrs) {
super(context, attrs);
init();
}
public CoordinatedListView(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
init();
}
@RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
public CoordinatedListView(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
super(context, attrs, defStyleAttr, defStyleRes);
init();
}
private void init() {
ViewCompat.setNestedScrollingEnabled(this, true);
}
}
Enjoy :)
享受 :)