8

मेरे पास एक गतिविधि है जिसमें 3 टैब हैं। प्रत्येक टैब पृष्ठ एक खंड है जो एक RecyclerView प्रदर्शित करता है। खंड में से एक में फ़्लोटिंगएक्शन बटन है। मैं इस बटन को फ्रैगमेंट के लेआउट में कार्यान्वित कर रहा हूं। मैं इसे टुकड़े के निचले दाएं हिस्से में स्थिर बना रहा हूं।ViewPager स्वाइप पर फ़्लोटिंगएक्शन बटन को दिखाएं/छुपाएं

टुकड़ा लेआउट:

- CoordinatorLayout 
    - RecyclerView 
    - FAB (without any behavior) 

गतिविधि लेआउट में, मेरे पास है:

- CoordinatorLayout 
    - AppBarLayout 
     - Toolbar 
     - TabLayout (SmartTabLayout) 
    - ViewPager 

समस्या अब फैब देखने से आधा छिपा हुआ है है जब टूलबार का विस्तार किया गया है, लेकिन पूरी तरह से जब से पता चला टूलबार ध्वस्त हो गया है। हालांकि यह तब नहीं होता है जब मैं गतिविधि में एफएबी बटन को लागू करता हूं। लेकिन मैं सभी टुकड़ों में बटन नहीं चाहता हूं। मैं इसे केवल पहले टुकड़े में डाल रहा हूँ।

यहां एक स्पष्ट है जिसे मैंने स्पष्ट किया है। गतिविधि लेआउट के लिए

Fab hidden

एक्सएमएल:

<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/coordinatorLayout" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent"> 

    <android.support.design.widget.AppBarLayout 
     android:id="@+id/appbarLayout" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content"> 

     <android.support.v7.widget.Toolbar 
      android:id="@+id/toolbar" 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:minHeight="?attr/actionBarSize" 
      android:background="@color/color_primary" 
      app:layout_scrollFlags="scroll|enterAlways" /> 

     <com.ogaclejapan.smarttablayout.SmartTabLayout 
      android:id="@+id/viewpagertab" 
      android:layout_width="match_parent" 
      android:layout_height="@dimen/tab_height" 
      android:background="@color/color_primary" /> 

    </android.support.design.widget.AppBarLayout> 

    <android.support.v4.view.ViewPager 
     android:id="@+id/viewpager" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     app:layout_behavior="@string/appbar_scrolling_view_behavior" /> 

</android.support.design.widget.CoordinatorLayout> 

टुकड़ा लेआउट के लिए एक्सएमएल:

<?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/coordinatorLayout" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content"> 

    <android.support.v7.widget.RecyclerView 
     android:id="@+id/card_list" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" /> 

    <android.support.design.widget.FloatingActionButton 
     android:id="@+id/fab_add" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_margin="@dimen/fab_margin" 
     android:src="@drawable/ic_plus_white_48dp" 
     app:layout_anchor="@id/card_list" 
     app:layout_anchorGravity="bottom|right|end" /> 

</android.support.design.widget.CoordinatorLayout> 

मेरा प्रश्न है कैसे मैं इतना कर सकता हूँ बटन दृश्यमान बनी रहती है कि जब recyclerview स्क्रॉल किया है ?

उत्तर

4

सबसे अच्छा विकल्प केवल गतिविधि में फ़्लोटिंगएक्शन बटन डालना होगा, और show() और hide() पर ViewPager.OnPageChangeListener पर कॉल करना होगा। इस तरह आपको enter/exit animations अच्छा लगता है जो Material Design guidelines के अनुरूप है।

viewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() { 

    @Override 
    public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) { 

    } 

    @Override 
    public void onPageSelected(int position) { 
     if (position == 0) { 
      fabAdd.show(); 
     } else { 
      fabAdd.hide(); 
     } 
    } 

    @Override 
    public void onPageScrollStateChanged(int state) { 

    } 
}); 

परिणाम:

enter image description here

+1

मुझे लगता है कि मैं आपके समाधान का उपयोग कर रहा हूं क्योंकि यह अधिक कार्यक्षमता प्रदान करता है। तुम्हारे सुझाव के लिए धन्यवाद! – AimanB

+1

@AimanB एक और बात, समर्थन डिजाइन लाइब्रेरी के नवीनतम संस्करण का उपयोग करना सुनिश्चित करें, उन्होंने नवीनतम संस्करणों में कई मुद्दों को ठीक किया है, एक 'शो()' और 'hide()' विधियों के साथ दिखाए गए डिफ़ॉल्ट एनिमेशन हैं। –

1

भी RecyclerView को

app:layout_behavior="@string/appbar_scrolling_view_behavior" 

जोड़ें।

+0

यह सब पर कोई असर नहीं होता है। – AimanB

संबंधित मुद्दे