CoD*_*oDe 10 android android-toolbar android-coordinatorlayout android-collapsingtoolbarlayout stickyrecycleview
更新:
CoordinatorLayout没有RecycleView。 RecycleView我尝试TextView内部ScrollView,这是相同的问题。Toolbar的ActionBar,并使用CoordinatorLayout与其他ToolBar如粘头与底部滚动元素原版的:
我正在开发一个视图,该视图需要Sticky标头实现和底部的回收视图。我已经使用了协调器布局支持,如此处所述。
工作原理:
layout_collapseMode = pin和CollapsingToolbarLayout使用layout_scrollFlags = scroll|exitUntilCollapsed|snap属性的工具栏。app:layout_behavior="@string/appbar_scrolling_view_behavior“问题是什么:
Toolbar我用于粘性视图的大小相同。bottom_margin的粘性工具栏视图的大小。观察:
TextView将NestedScrollView。(PFA)放在了其中(此处布局未更新)。
CoordinatorLayout。我已经尝试过在这里(这里)提供多种解决方案,但是没有任何效果。
PFA,电流输出。
更新 PFA,尝试延迟文本视图。
这是布局文件。
<androidx.coordinatorlayout.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">
<com.google.android.material.appbar.AppBarLayout
android:id="@+id/summaryAppBar"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<com.google.android.material.appbar.CollapsingToolbarLayout
android:id="@+id/main.collapsing"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:layout_scrollFlags="scroll|exitUntilCollapsed|snap">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="256dp"
android:background="@drawable/fable_1"
app:layout_collapseMode="parallax"
app:layout_collapseParallaxMultiplier="0.3" />
<!-- This is sticky header-->
<androidx.appcompat.widget.Toolbar
android:id="@+id/summaryToolBar"
android:layout_width="match_parent"
android:layout_height="72dp"
android:layout_gravity="center"
android:background="@android:color/white"
android:padding="@dimen/common_layout_margin"
android:visibility="visible"
app:layout_collapseMode="pin"
app:popupTheme="@style/ThemeOverlay.AppCompat.Light">
<FrameLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="24sp"
android:text="Name" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="right"
android:textSize="24sp"
android:text="Offer"/>
</FrameLayout>
</androidx.appcompat.widget.Toolbar>
</com.google.android.material.appbar.CollapsingToolbarLayout>
</com.google.android.material.appbar.AppBarLayout>
<!-- Bottom margin if I do't use then it does not display last child item. Wired but true in this case-->
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/recyclerView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:clipToPadding="false"
android:visibility="visible"
android:layout_marginBottom="72dp"
app:layout_behavior="@string/appbar_scrolling_view_behavior"
tools:listItem="@layout/item_dessert" />
</androidx.coordinatorlayout.widget.CoordinatorLayout>
Run Code Online (Sandbox Code Playgroud)
由于工具栏发粘,我认为CoordinatorLayout(可能是- AppBarLayout或CollapsingToolbarLayout-不确定是哪个组件)记录了错误的高度CollapsingToolbarLayout。如果在初始布局之前或之后添加项目,则行为会有所不同。
请尝试以下操作:
android:layout_marginBottom="72dp"从XML中删除RecyclerView。android:minHeight="72dp"到XMLCollapsingToolbarLayout由于您的粘性工具栏设置为72dp,因此可以使用minHeight设置为72dp。
如果您对此有疑问,请发回此处。
这是使用NestedScrollView和上述更改的布局快速演示。
更新:我已经解决了使用RecyclerView哪个显示相同的问题。显示问题和修复程序的演示项目位于GitHub上。
这是代码:
MainActivity.java
public class MainActivity extends AppCompatActivity {
// Set to true to break the layout; false for it to work.
// The setting of this flag should only matter for the
// layout activity_not_working.
private boolean mBreakIt = true;
// private int mLayoutToUse = R.layout.activity_not_working;
private int mLayoutToUse = R.layout.activity_working;
private LinearLayout mLayout;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(mLayoutToUse);
mLayout = findViewById(R.id.linearLayout);
if (mBreakIt) {
mLayout.post(new Runnable() {
@Override
public void run() {
addViews();
}
});
} else {
addViews();
}
}
private void addViews() {
for (int i = 0; i < 50; i++) {
TextView tv = new TextView(MainActivity.this);
tv.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,
ViewGroup.LayoutParams.WRAP_CONTENT));
tv.setText("TextView #" + (i + 1));
mLayout.addView(tv);
}
}
}
Run Code Online (Sandbox Code Playgroud)
activity_working.xml
<androidx.coordinatorlayout.widget.CoordinatorLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<com.google.android.material.appbar.AppBarLayout
android:id="@+id/summaryAppBar"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<com.google.android.material.appbar.CollapsingToolbarLayout
android:id="@+id/main.collapsing"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:minHeight="72dp"
app:layout_scrollFlags="scroll|exitUntilCollapsed|snap">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="256dp"
android:background="@drawable/beach"
app:layout_collapseMode="parallax"
app:layout_collapseParallaxMultiplier="0.3" />
<!-- This is sticky header-->
<androidx.appcompat.widget.Toolbar
android:id="@+id/summaryToolBar"
android:layout_width="match_parent"
android:layout_height="72dp"
android:layout_gravity="center"
android:background="@android:color/white"
android:padding="@dimen/common_layout_margin"
android:visibility="visible"
app:layout_collapseMode="pin"
app:popupTheme="@style/ThemeOverlay.AppCompat.Light">
<FrameLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Name"
android:textSize="24sp" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="right"
android:text="Offer"
android:textSize="24sp" />
</FrameLayout>
</androidx.appcompat.widget.Toolbar>
</com.google.android.material.appbar.CollapsingToolbarLayout>
</com.google.android.material.appbar.AppBarLayout>
<!-- Bottom margin if I do't use then it does not display last child item. Wired but true in this case-->
<!-- Removed following: -->
<!--android:layout_marginBottom="72dp"-->
<androidx.core.widget.NestedScrollView
android:id="@+id/nestedScrollView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@android:color/holo_blue_light"
android:visibility="visible"
app:layout_behavior="@string/appbar_scrolling_view_behavior"
app:layout_insetEdge="bottom"
tools:listItem="@layout/item_dessert">
<LinearLayout
android:id="@+id/linearLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="A TextView" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="A TextView" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="A TextView" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="A TextView" />
</LinearLayout>
</androidx.core.widget.NestedScrollView>
</androidx.coordinatorlayout.widget.CoordinatorLayout>
Run Code Online (Sandbox Code Playgroud)
activity_not_working.xml
<androidx.coordinatorlayout.widget.CoordinatorLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<com.google.android.material.appbar.AppBarLayout
android:id="@+id/summaryAppBar"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<com.google.android.material.appbar.CollapsingToolbarLayout
android:id="@+id/main.collapsing"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:layout_scrollFlags="scroll|exitUntilCollapsed|snap">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="256dp"
android:background="@drawable/beach"
app:layout_collapseMode="parallax"
app:layout_collapseParallaxMultiplier="0.3" />
<!-- This is sticky header-->
<androidx.appcompat.widget.Toolbar
android:id="@+id/summaryToolBar"
android:layout_width="match_parent"
android:layout_height="72dp"
android:layout_gravity="center"
android:background="@android:color/white"
android:padding="@dimen/common_layout_margin"
android:visibility="visible"
app:layout_collapseMode="pin"
app:popupTheme="@style/ThemeOverlay.AppCompat.Light">
<FrameLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Name"
android:textSize="24sp" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="right"
android:text="Offer"
android:textSize="24sp" />
</FrameLayout>
</androidx.appcompat.widget.Toolbar>
</com.google.android.material.appbar.CollapsingToolbarLayout>
</com.google.android.material.appbar.AppBarLayout>
<!-- Bottom margin if I do't use then it does not display last child item. Wired but true in this case-->
<!-- Removed following: -->
<!--android:layout_marginBottom="72dp"-->
<androidx.core.widget.NestedScrollView
android:id="@+id/nestedScrollView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginBottom="72dp"
android:background="@android:color/holo_blue_light"
android:visibility="visible"
app:layout_behavior="@string/appbar_scrolling_view_behavior"
app:layout_insetEdge="bottom"
tools:listItem="@layout/item_dessert">
<LinearLayout
android:id="@+id/linearLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="A TextView" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="A TextView" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="A TextView" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="A TextView" />
</LinearLayout>
</androidx.core.widget.NestedScrollView>
</androidx.coordinatorlayout.widget.CoordinatorLayout>
Run Code Online (Sandbox Code Playgroud)
您可以尝试下面的布局文件吗?我已经测试过了,效果很好。您也可以从 recyclerview 中删除 marginButtom。
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="@string/appbar_scrolling_view_behavior">
<androidx.coordinatorlayout.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">
<com.google.android.material.appbar.AppBarLayout
android:id="@+id/summaryAppBar"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<com.google.android.material.appbar.CollapsingToolbarLayout
android:id="@+id/main.collapsing"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:layout_scrollFlags="scroll|exitUntilCollapsed|snap">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="256dp"
android:background="@drawable/fable_1"
app:layout_collapseMode="parallax"
app:layout_collapseParallaxMultiplier="0.3" />
<!-- This is sticky header-->
<androidx.appcompat.widget.Toolbar
android:id="@+id/summaryToolBar"
android:layout_width="match_parent"
android:layout_height="72dp"
android:layout_gravity="center"
android:background="@android:color/white"
android:padding="@dimen/common_layout_margin"
android:visibility="visible"
app:layout_collapseMode="pin"
app:popupTheme="@style/ThemeOverlay.AppCompat.Light">
<FrameLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="24sp"
android:text="Name" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="right"
android:textSize="24sp"
android:text="Offer"/>
</FrameLayout>
</androidx.appcompat.widget.Toolbar>
</com.google.android.material.appbar.CollapsingToolbarLayout>
</com.google.android.material.appbar.AppBarLayout>
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/recyclerView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:clipToPadding="false"
android:visibility="visible"
android:layout_marginBottom="72dp"
app:layout_behavior="@string/appbar_scrolling_view_behavior"
tools:listItem="@layout/item_dessert" />
</androidx.coordinatorlayout.widget.CoordinatorLayout>
</RelativeLayout>
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1000 次 |
| 最近记录: |