Dies bezieht sich auf das Problem der Koordinierung des Verhaltens einer unverankerten Aktionsschaltfläche in einer Aktivität, die 5 Fragmente enthält.Unterschiedliche Implementierung für eine unverankerte Aktionsschaltfläche in jedem Fragment einer Aktivität
In meiner App habe ich eine Aktivität, die einen ViewPager und ein mit FloatingActionButton definiertes XML enthält. Der ViewPager enthält Fragmente, und in jedem Fragment befindet sich ein RecyclerView. Beim Klicken auf den Floating-Action-Button habe ich scrollToPosition (0) implementiert. Da ich Floating ActionButton in der XML-Datei der Hauptaktivität implementiert habe, funktioniert scrollToPosition() nur im letzten Fragment, wenn ich auf die Schaltfläche mit der schwebenden Aktion klicke. Wie kann ich dies umgehen, wenn die Recycler-Ansicht nach oben blättert?
Ich habe versucht, die Implementierung von Floatingactionbutton in Fragment-Layout-Datei und es funktioniert völlig in Ordnung. Aber das Problem mit diesem Floating-Action-Button wird sich bewegen, wenn ich von einem Fragment zum anderen fragment.So gibt es eine Möglichkeit, so dass ich kann Implementieren Sie in der XML-Datei für die Hauptaktivität den Befehl pivotingactionbutton und können Sie nach dem Klicken auf FloatingActionButton in jedem Fragment scrollToPosotion (0) erreichen. Vielen Dank für Anregungen.
Die folgende Datei ist die XML-Datei, die eine Floating-Aktion Schaltfläche enthält.
<?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:background="#f4f4f4"
tools:context=".MainActivity">
<android.support.design.widget.AppBarLayout
android:id="@+id/appbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:fitsSystemWindows="true">
<android.support.v7.widget.Toolbar
android:id="@+id/tb_main"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="?attr/colorPrimary"
android:elevation="0dp"
android:fitsSystemWindows="true"
app:layout_scrollFlags="scroll|enterAlways"
app:popupTheme="@style/ThemeOverlay.AppCompat.Dark" />
<com.myapp.now.extras.SlidingTabLayout
android:id="@+id/sl_tab"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:fitsSystemWindows="true">
</com.myapp.now.extras.SlidingTabLayout>
</android.support.design.widget.AppBarLayout>
<android.support.v4.view.ViewPager
android:id="@+id/vp_tab"
android:layout_width="match_parent"
android:layout_height="match_parent"
/>
<android.support.design.widget.FloatingActionButton
android:id="@+id/fab"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="bottom|right|end"
android:layout_marginBottom="@dimen/fab_margin_bottom"
android:layout_marginEnd="@dimen/fab_margin_right"
android:layout_marginRight="@dimen/fab_margin_right"
android:clickable="true"
android:src="@mipmap/top_scroll"
app:backgroundTint="@color/colorFAB"
app:borderWidth="0dp"
app:fabSize="normal" />
Das Folgende ist eine XML-Datei eines Fragments
<FrameLayout 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"
xmlns:app="http://schemas.android.com/apk/res-auto">
<android.support.v4.widget.SwipeRefreshLayout
android:id="@+id/srl_swipe"
android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.v7.widget.RecyclerView
android:id="@+id/rv_list"
android:scrollbars="vertical"
android:fadeScrollbars="true"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
</android.support.v4.widget.SwipeRefreshLayout>
<ProgressBar
android:visibility="gone"
android:layout_gravity="center"
android:id="@+id/pbar"
android:layout_width="40dp"
android:layout_height="40dp" />
Das Folgende ist die Umsetzung von schwimmenden Aktionsschaltfläche in jedem Fragment
fab = (android.support.design.widget.FloatingActionButton) view.findViewById(R.id.fab);
fab.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
LinearLayoutManager llm = (LinearLayoutManager) mRecyclerView.getLayoutManager();
llm.scrollToPositionWithOffset(0, 0);
}
});
Bitte geben Sie einen Code bitte –
Sie können meine Antwort hier überprüfen: http://StackOverflow.com/A/33586571/2023280 –
@RBhagwat haben Sie dieses Problem lösen? –