How can we scroll parent layout while scrolling recyclerview android

2.5k Views Asked by At

Please help me with the following situation.

I am developing an application where in one page I have a Linearlayout with a background image and RecyclerView with list of names.What I need is when i scroll up the RecyclerView I need the LinearLayout above also to move up so that the list in the recyclerView does not go under the LinearLayout above and when I scroll down the RecyclerView I need the LinearLayout above to scroll down so that we could see the image fully.

What i have done already is I used the setOnScrollListener of recyclerview and in the onScrolled() function I am getting the scrolling down and scrolling up event.But now i am stuck how to proceed further.

Below is the layout:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:card_view="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fitsSystemWindows="true">

    <include layout="@layout/toolbar"
        android:id="@+id/toolbar" />

    <android.support.v7.widget.CardView
        android:id="@+id/cardview"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_below="@+id/toolbar"
        android:layout_margin="8dp"
        card_view:cardBackgroundColor="@android:color/white"
        card_view:cardCornerRadius="8dp">

            <ScrollView
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:id="@+id/scrollview"
                android:fillViewport="true">

                <LinearLayout
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:weightSum="5"
                    android:orientation="vertical">

                        <LinearLayout
                            android:id="@+id/map"
                            android:layout_width="match_parent"
                            android:layout_height="0dp"
                            android:orientation="vertical"
                            android:background="@drawable/hydeparkmap"
                            android:layout_weight="3" ></LinearLayout>


                        <android.support.v7.widget.RecyclerView
                            android:id="@+id/recycler_view"
                            android:layout_width="match_parent"
                            android:layout_height="0dp"
                            android:layout_weight="2"
                            android:scrollbars="vertical" />

                    </LinearLayout>

            </ScrollView>

    </android.support.v7.widget.CardView>

</RelativeLayout>

Here is the code, i used in corresponding java class:

    @InjectView(R.id.scrollview)
    ScrollView scrollview;

    protected void onCreate(Bundle savedInstanceState) {


        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_shoplist);
        ButterKnife.inject(this);

        setSupportActionBar(toolbar);
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);
        getSupportActionBar().setHomeButtonEnabled(true);
        getSupportActionBar().setTitle("ShopList");

        scrollview.setVerticalScrollBarEnabled(true);
        lm=new LinearLayoutManager(ShopListActivity.this);
        recyclerView.setLayoutManager(lm);
        firstVisibleInListview = lm.findFirstVisibleItemPosition();
        adapter = new RecyclerViewAdapter(ShopListActivity.this, getData());
        recyclerView.setAdapter(adapter);

        recyclerView.addOnItemTouchListener(
                new RecyclerItemClickListener(ShopListActivity.this, new RecyclerItemClickListener.OnItemClickListener() {
                    @Override
                    public void onItemClick(View view, int position) {

                        Intent intent1 = new Intent(ShopListActivity.this, ShopProductListActivity.class);
                        intent1.putExtra("position", String.valueOf(position));
                        intent1.putExtra("shopname", it.get(position).getTitle());
                        intent1.putExtra("shopimage", String.valueOf(it.get(position).getImgIcon()));
                        intent1.putExtra("subcategory", subcategory);
                        startActivity(intent1);

                    }
                })
        );
        recyclerView.setOnScrollListener(new RecyclerView.OnScrollListener() {
            @Override
            public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
                super.onScrolled(recyclerView, dx, dy);
                int currentFirstVisible = lm.findFirstVisibleItemPosition();

                if(currentFirstVisible > firstVisibleInListview){
                   Toast.makeText(getBaseContext(),"Scrolled up ",Toast.LENGTH_SHORT).show();
                    scrollview.fullScroll(ScrollView.FOCUS_UP);
               }
                else
                    Toast.makeText(getBaseContext(),"Scrolled down ",Toast.LENGTH_SHORT).show();

                firstVisibleInListview = currentFirstVisible;
            }
        });

    }
1

There are 1 best solutions below

0
On BEST ANSWER

so if I understand your question completely, you some View that should scroll with RcyclerView. if so in this situation you should delete your ScrollView cause RecyclerView have that within itself. what I write here is step by step guide to put a header( not sticky) in top of your RecyclerView so they would scroll together:

1- first you need to create a layout containing your header in my case i had an image for header so it looked like this:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">

<ImageView
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:src="@mipmap/winter"/>

</LinearLayout>

let's call this header_layout

2- you need to implement another method of RecyclerAdapter called getItemViewType it's output would be in as second parameter onCreateViewHolder(ViewGroup parent,int viewType) here you inflate the layout for each kind of view you need( for me these two look like this) :

@Override
public int getItemViewType(int position){
    if(position == 0){
        return 0;
    } else {
        return 1;
    }
}

@Override
public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
    View itemView;
    if(viewType==1){
        itemView = LayoutInflater.from(parent.getContext())
                .inflate(R.layout.report_row_layout, parent, false);
    } else {
        itemView = LayoutInflater.from(parent.getContext())
                .inflate(R.layout.report_header_layout, parent, false);
    }

    return new MyViewHolder(itemView);
}

note that my first position (header) differs and other are the same you could have multiple views for multiple positions.

3- you should change your onBindViewHolder if needed in my case I needed to make it do nothing for my first position

4- remove your ScrollView and implement the layouts in positions and your main layout should look like this:

<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:card_view="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true">

<include layout="@layout/toolbar"
    android:id="@+id/toolbar" />

<android.support.v7.widget.CardView
    android:id="@+id/cardview"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_below="@+id/toolbar"
    android:layout_margin="8dp"
    card_view:cardBackgroundColor="@android:color/white"
    card_view:cardCornerRadius="8dp">

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

</android.support.v7.widget.CardView>

</RelativeLayout>

I don't know what your CardView is doing but delete it if you think it's not needed. if you need you can make your header a LinearLayout or anything else.

hope this helps.