2

I'm looking to implement Button to switch the Recycle view inbetween horizontal and vertical layout. If Recycleview is originally in Vertical Position then change to Horizontal on Click of button and vice versa.

My Logic is

 mButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
 //If Recycleview is in Horizontal then 
   mRecyclerView.setLayoutManager(new    LinearLayoutManager(getActivity(), LinearLayoutManager.VERTICAL, false));

 //If Recycleview is in Vertical then 
mRecyclerView.setLayoutManager(new LinearLayoutManager(getActivity(), LinearLayoutManager.HORIZONTAL, false));

}

Thanks in advance.

Blueyeti
  • 43
  • 8
  • What exactly are you looking for? Have you tried this already? what is the issue? – Hossain Khan May 02 '19 at 22:20
  • Issue is I don't know how to implement to switch back and forth between two gridlayout i.e Horizontal and vertical on click of button.. – Blueyeti May 02 '19 at 22:23
  • Look at following SO question https://stackoverflow.com/a/30393588/132121 or here https://www.reddit.com/r/androiddev/comments/2s6zd5/recyclerview_change_layout_between_listgrid_on/ – Hossain Khan May 02 '19 at 22:36

1 Answers1

1

Late answer But hope someone will find this helpful

Kotlin code

var directionFlag = 0

    binding.yourView.setOnClickListener {
    
                binding.yourRecycleView.layoutManager = LinearLayoutManager(
                    this,
                    1 - directionFlag, false
                )
                directionFlag=1-directionFlag
            }
        }

Java code

view.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                recyclerView.setLayoutManager(new
                        LinearLayoutManager(this,
                        1 - directionFlag, false));
                directionFlag =1-directionFlag;

            }
        });
Mohammad Taqi
  • 161
  • 2
  • 8