2

I have 2 recyclerView as the below image, the problem when I scroll the second recylcer view the first one recycler still fixed. I want the first to disappear when i scroll down in the second recyclerview. and if i scrolled above , the recyclerview 1 to be appeared. how to do that ?

enter image description here

Main activity code:

   protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);
       // initToolbars();

        recyclerView = (RecyclerView) findViewById(R.id.recyclerview1);
        recylerViewLayoutManager = new LinearLayoutManager(context,LinearLayoutManager.HORIZONTAL,false);
        recyclerView.setLayoutManager(recylerViewLayoutManager);
        recyclerViewAdapter = new RecyclerViewAdapter(MainActivity.this, subjects,images);
        recyclerView.setAdapter(recyclerViewAdapter);


        MainGridRecView = (RecyclerView) findViewById(R.id.maingridrc);
        MainGridRecViewLayoutManager = new GridLayoutManager(context,2);
        MainGridRecView.setLayoutManager(MainGridRecViewLayoutManager);
        MainGridRecViewAdapter = new MainGridRecyclerViewAdapter(MainActivity.this, subjects,images);
        MainGridRecView.setAdapter(MainGridRecViewAdapter);

activity xml:

<android.support.v4.widget.NestedScrollView
    android:layout_width="match_parent"
    android:layout_height="wrap_content">

    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/liner1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:orientation="vertical">

    <android.support.design.widget.AppBarLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:theme="@style/AppTheme.AppBarOverlay">

        <android.support.v7.widget.Toolbar
            android:id="@+id/toolbar"
            android:layout_width="match_parent"
            android:layout_height="?attr/actionBarSize"
            android:background="?attr/colorPrimary"
            app:popupTheme="@style/AppTheme.PopupOverlay" />

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


    <TextView
        android:id="@+id/textView3"
        android:layout_width="match_parent"
        android:layout_height="25dp"
        android:layout_above="@+id/recyclerview1"
        android:gravity="center"
        android:background="#10bcc9"
        android:textColor="@android:color/white"
        android:text="New Games." />

    <android.support.v7.widget.RecyclerView
        android:id="@+id/recyclerview1"
        android:layout_width="wrap_content"
        android:layout_height="140dp"
        android:scrollbars="horizontal" />

    <TextView
        android:id="@+id/textView4"
        android:layout_width="match_parent"
        android:layout_height="25dp"
        android:layout_below="@+id/recyclerview1"
        android:gravity="center"
        android:background="#10bcc9"
        android:textColor="@android:color/white"
        android:text="Used Games." />

    <android.support.v7.widget.RecyclerView
        android:id="@+id/maingridrc"
        android:layout_width="wrap_content"
        android:layout_height="330dp"
        android:layout_marginTop="5dp"
        android:scrollbars="vertical" />

  <!--  <android.support.design.widget.FloatingActionButton
        android:id="@+id/fab"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="bottom|end"
        android:layout_margin="@dimen/fab_margin"
        android:src="@android:drawable/ic_dialog_email" />-->
</LinearLayout>

adapter

public class RecyclerViewAdapter extends RecyclerView.Adapter<Viewholder>{

    String[] SubjectValues;
    String[] imageValues;
    Context mContext;
    View view1;
    ViewHolder viewHolder1;
  // TextView textView;

    public RecyclerViewAdapter(Context context,String[] SubjectValues1, String[] images){

        SubjectValues = SubjectValues1;
        imageValues= images;
        mContext = context;
    }

    public static class ViewHolder extends RecyclerView.ViewHolder{

        public ViewHolder(View v){

            super(v);

        }
    }

    @Override
    public Viewholder onCreateViewHolder(ViewGroup parent, int viewType){

        view1 = LayoutInflater.from(parent.getContext()).inflate(R.layout.recyclerview_items,parent,false);

      //  viewHolder1 = new Viewholder(view1);

        Viewholder vh = new Viewholder(view1, new Viewholder.IMyViewHolderClicks() {
            public void onPotato(View caller) { Log.d("VEGETABLES", "Poh-tah-tos");
                Intent intent = new Intent(mContext,SingleObjectActivity.class);
                mContext.startActivity(intent);
            };
            public void onTomato(ImageView callerImage) {
                Log.d("VEGETABLES", "To-m8-tohs");

                //  v1    CategoryList mDataset = new CategoryList(getOrder(), getId(), item.getUrl(), item.getUserName(), item.getLikes());

                Intent intent = new Intent(mContext,SingleObjectActivity.class);
                mContext.startActivity(intent);
            }
        });

        return vh;
    }

    @Override
    public void onBindViewHolder(Viewholder holder, int position){

        holder.txtViewTitle.setText(SubjectValues[position]);
        holder.imgViewIcon.setImageResource(R.drawable.ghost_recon);
    }

    @Override
    public int getItemCount(){

        return SubjectValues.length;
    }
}

recycler xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:orientation="horizontal">

    <ImageView
        android:id="@+id/imagecateg"
        android:layout_width="180dp"
        android:layout_height="160dp"
        android:layout_alignParentTop="true"
        />

    <TextView
        android:id="@+id/txtview"
        android:layout_width="200dp"
        android:layout_height="wrap_content"
        android:layout_below="@+id/imagecateg"
        android:paddingLeft="35dp"
        android:textSize="15dp"
        android:textStyle="bold"
        android:background="#f4e04c"
        android:textColor="@color/colorPrimary"
        />
    </RelativeLayout>

edit

enter image description here

Moudiz
  • 7,211
  • 22
  • 78
  • 156

2 Answers2

1

Edit*

Here is another approach , Code is based on here, I tried the Code myself in my project and result is shown below image .

enter image description here

recyclerView.setOnScrollListener(new HidingScrollListener() {
    @Override
    public void yourhideviewmethod() {
      hideview();
    }
    @Override
    public void yourshowviewmethod() {
      showview();
    }
  });
}

// animation code to hide and show

public void hideview(){
    yourview.animate().translationY(-yourview.getHeight()).setInterpolator(new AccelerateInterpolator(2)).
            setListener(new AnimatorListenerAdapter() {
                @Override
                public void onAnimationEnd(Animator animation) {
                    super.onAnimationEnd(animation);
                    yourview.setVisibility(View.GONE);
                }
            });
}

public void showview(){
    yourview.animate().translationY(0).setInterpolator(new DecelerateInterpolator(2)).
            setListener(new AnimatorListenerAdapter() {
                @Override
                public void onAnimationEnd(Animator animation) {
                    super.onAnimationEnd(animation);
                    yourview.setVisibility(View.VISIBLE);
                }
            });
}

then create class which extends RecyclerView.OnScrollListener

public abstract class HidingScrollListener extends RecyclerView.OnScrollListener {
  private static final int HIDE_THRESHOLD = 20;
  private int scrolledDistance = 0;
  private boolean controlsVisible = true;

  @Override
  public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
    super.onScrolled(recyclerView, dx, dy);

    if (scrolledDistance > HIDE_THRESHOLD && controlsVisible) {
      yourhideviewmethod();
      controlsVisible = false;
      scrolledDistance = 0;
    } else if (scrolledDistance < -HIDE_THRESHOLD && !controlsVisible) {
      yourshowviewmethod();
      controlsVisible = true;
      scrolledDistance = 0;
    }

    if((controlsVisible && dy>0) || (!controlsVisible && dy<0)) {
      scrolledDistance += dy;
    }
  }

  public abstract void yourhideviewmethod();;
  public abstract void yourshowviewmethod();;

}
Omar Dhanish
  • 885
  • 7
  • 18
1

Have a look at below example,

MainActivity.class

public class MainActivity extends AppCompatActivity {

    List<String> horizontalArrayList = new ArrayList<>();
    List<String> verticalArrayList = new ArrayList<>();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        verticalArrayList.add("fake entry");

        for(int i=0;i<20;i++){
            horizontalArrayList.add("user "+i);
        }
        for(int i=0;i<40;i++){
            verticalArrayList.add("user "+i);
        }

        RecyclerView verticalRecyclerView = (RecyclerView) findViewById(R.id.verticalRecyclerView);
        verticalRecyclerView.setLayoutManager(new LinearLayoutManager(this));
        VerticalAdapter mAdapter = new VerticalAdapter(this, horizontalArrayList,verticalArrayList);
        verticalRecyclerView.setAdapter(mAdapter);
    }
}

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<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"
    tools:context="com.recyclerviewdemo.android.activities.MainActivity">

    <android.support.v7.widget.RecyclerView
        android:id="@+id/verticalRecyclerView"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:scrollbars="vertical" />
</RelativeLayout>

HorizontalAdapter.class

public class HorizontalAdapter extends RecyclerView.Adapter<HorizontalAdapter.MyViewHolder> {

    private List<String> mArrayList = new ArrayList<>();

    private Context mContext;

    public HorizontalAdapter(Context mContext, List<String> mArrayList) {
        this.mContext = mContext;
        this.mArrayList = mArrayList;
    }

    @Override
    public HorizontalAdapter.MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(mContext).inflate(R.layout.item_horizontal, parent, false);
        return new MyViewHolder(view);
    }

    @Override
    public void onBindViewHolder(HorizontalAdapter.MyViewHolder holder, int position) {
        holder.textView.setText(mArrayList.get(position));
    }

    @Override
    public int getItemCount() {
        return mArrayList.size();
    }

    class MyViewHolder extends RecyclerView.ViewHolder {
        TextView textView;

        public MyViewHolder(View v) {
            super(v);
            textView = (TextView) v.findViewById(R.id.textView);
        }
    }
}

item_horizontal.xml

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

    <TextView
        android:id="@+id/textView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_margin="5dp"
        android:background="@color/colorAccent"
        android:padding="5dp" />
</LinearLayout>

VerticalAdapter.class

public class VerticalAdapter extends RecyclerView.Adapter<VerticalAdapter.MyViewHolder> {

    private int ITEM_TEXT_VIEW = 0, ITEM_HEADER_VIEW = 1;

    private List<String> mVerticalArrayList = new ArrayList<>();
    private List<String> mHorizontalArrayList = new ArrayList<>();

    private Context mContext;


    public VerticalAdapter(Context mContext, List<String> mHorizontalArrayList,List<String> mVerticalArrayList) {
        this.mContext = mContext;
        this.mVerticalArrayList = mVerticalArrayList;
        this.mHorizontalArrayList = mHorizontalArrayList;
    }

    @Override
    public VerticalAdapter.MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(mContext).inflate(R.layout.item_vertical, parent, false);
        return new MyViewHolder(view);
    }

    @Override
    public void onBindViewHolder(VerticalAdapter.MyViewHolder holder, int position) {
        if (getItemViewType(position) == ITEM_HEADER_VIEW) {
            holder.recyclerView.setLayoutManager(new LinearLayoutManager(mContext,
                LinearLayoutManager.HORIZONTAL, false));
            HorizontalAdapter mAdapter = new HorizontalAdapter(mContext, mHorizontalArrayList);
            holder.recyclerView.setAdapter(mAdapter);
            holder.textView.setVisibility(View.GONE);
        } else {
            holder.textView.setText(mVerticalArrayList.get(position));
            holder.recyclerView.setVisibility(View.GONE);
        }
    }

    @Override
    public int getItemCount() {
        return mVerticalArrayList.size();
    }

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

    class MyViewHolder extends RecyclerView.ViewHolder {
        TextView textView;
        RecyclerView recyclerView;

        public MyViewHolder(View v) {
            super(v);
            textView = (TextView) v.findViewById(R.id.textView);
            recyclerView = (RecyclerView) v.findViewById(R.id.horizontalRecyclerView);
        }
    }
}

item_vertical.xml

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

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

    <TextView
        android:id="@+id/textView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />
</LinearLayout>
Maddy
  • 4,525
  • 4
  • 33
  • 53
  • Error:(17, 8) error: VerticalAdapter is not abstract and does not override abstract method onBindViewHolder(ViewHolder,int) in Adapter and another error Error:(39, 5) error: method does not override or implement a method from a supertype – Moudiz Aug 15 '17 at 11:36
  • I have just edited VerticalAdapter.class have a look. – Maddy Aug 15 '17 at 11:40
  • check this https://stackoverflow.com/questions/34569217/how-to-add-a-recyclerview-inside-another-recyclerview – Moudiz Aug 15 '17 at 12:09