inverted_index
inverted_index

Reputation: 2437

Adding scrolllistener to a recyclerview and hide a view on another layout

I have some fragments let's say A, B, and C. I get the corresponding layouts for these fragments using inflate method.

In A and B, I have a Recycler View which I tend to add a Scroll Listener. Also, in fragment C, I have a view (let's say a tablayout) which I want to hide/show it by scrolling the recycler views (which are in A and B).

I am not sure how can I achieve that. Since the tablayout is in the layout of fragment C (and not in A and B).

I was trying to inflate the layout of C in A and B, but it didn't work.

This is the scrolllistener on recycler view (fragments A and B):

View view = inflater.inflate(R.layout.fragment_A, container, false);
recyclerView = (RecyclerView) view.findViewById(R.id.myChannelsRecyclerView);
tabLayout = (SmartTabLayout) view.findViewById(R.id.tabs);

recyclerView.addOnScrollListener(new HideShowScrollListener() {
                @Override
                public void onHide() {
                    //tabLayout.animate().translationY(250);
                    //tabLayout return null

                }

                @Override
                public void onShow() {
                    //tabLayout.animate().translationY(0);
                    //tabLayout return null again
                }
    });

I should note that HideShowScrollListener() is a helper method which is written by me.

fragment_A.xml:

<RelativeLayout 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">

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

</RelativeLayout>

main_fragment.xml (fragment C)

<LinearLayout 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:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <come.example.CustomViewPager
        android:id="@+id/pager"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1"/>

    <smartertablayout.SmartTabLayout
        android:id="@+id/tabs"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        app:stl_dividerThickness="1dp"
        app:stl_defaultTabTextSize="9sp"
        app:stl_distributeEvenly="true"
        app:stl_indicatorThickness="0dp"/>

</LinearLayout>

Main fragment (fragment C in example) is responsible to change and replace the other fragments (A and B) using a pager. It somehow is the parent of this fragments

Upvotes: 0

Views: 500

Answers (1)

Brian
Brian

Reputation: 944

//something like this from my suggestion above

class Fragment_A extends Fragemnt{

    YourScollListener animateCallback;
    RecyclerView recyclerView;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {

        View view = inflater.inflate(R.layout.fragment_A, container, false);
        recyclerView = (RecyclerView) view.findViewById(R.id.myChannelsRecyclerView);

        recyclerView.addOnScrollListener(new HideShowScrollListener() {
            @Override
            public void onHide() {
                animateCallback.animate();
            }

            @Override
            public void onShow() {

            }
       });

        return view;
    }

     public void onAttach(Context context) {
            super.onAttach(context);
            animateCallback = (YourScrollListener) getParentFragment();
     }

     public interface YourScrollListener{
        void animate();
     }

}


//main frag

class Main_Fragment extends Fragemnt implements YourScrollListener{

    animate(){
        //do stuff
    }

}

Upvotes: 1

Related Questions