2

I have a list and its size is x numbers. I want to show 6 items on the screen even user scrolled to view the list , it should show 6 items on the screen.

I tried this Answer but my item's height was cropped and it didn't work for me.

Edit I want to retrieve all list as usual. I just want to make screen carry 6 items .

Ahmed Elsayed
  • 231
  • 3
  • 23

2 Answers2

0

// get the reference of RecyclerView

RecyclerView recyclerView = (RecyclerView) findViewById(R.id.recyclerView);

// set a GridLayoutManager with 6 number of columns, Vertical gravity

GridLayoutManager gridLayoutManager = new GridLayoutManager(getApplicationContext(),6,LinearLayoutManager.VERTICAL,false); recyclerView.setLayoutManager(gridLayoutManager); // set LayoutManager to RecyclerView

AllwiN
  • 693
  • 2
  • 12
  • 26
  • First : I want to scroll vertically Second : I changed the LinearLayoutManager.VERTICAL to HORIZONTAL and I got 6 items per page . But I have a hidden image and it doesn't appear because item has a fixed height and the item became a cropped item – Ahmed Elsayed Nov 27 '19 at 12:26
0

In lack of code I only can assume you like to get a list of X number from Api or database, etc and you only want to show max of 6 items

it would have been easier to see some code of what you have done so-far so the correction/suggestion will suite you better, in lack of code I give you few suggestions:

  1. your view should not know anything on how to handle data , it should simply show the data , in you case it should receive a 6 items and show them in the view (recyclerView in your case).

  2. I assume you are not using any pattern (like MVVM or MVP) and you just obtaining your list from (api or database) inside your Activity - (extra tip -> it is always better to use sort of Architecture design to separate your logic into a Presenter / viewModel layer for easy unit test purposes later)

lets assume you define your Adapter in Activity and passing your list like:

In your activity

var listOfAllYourItems =  arrayListOf(object1,object2,...,object100)
    val myAdapter = MyAdapter() 
    myAdapter.setItems(listOfAllYourItems.sublist(1,6)) //this will only pass the first 6 items
    the rest of normal stuff ...

so this is like you have only 6 items and displaying it in recyclerView - simple (view does not hold any logic on how to handle items over 6 etc, view just know how to display item and should not care on how to handle anything else)


Extra point : you can add a next / previous button in your view (perhaps below you RecyclerView in you activity xml), and then you add some logic to them to show next 6 items from list onNextTapped or previous 6 items when tapped previous button like :

In your activity

    fun noNextTapped(){
myAdapter.setItems(listOfYour6Items.sublist(6,12))
    //of course you need to add your logic to handle edge cases like what if the list size does not have enough items (like if it is only have 10 items) etc , and some other logic on how to know which page you are displaying so you can pass the correct number of items to the adapter onNextTapped fun (each page shows 6 items)   I leave those to you

    }

in your MyAdapter :

class MyAdapter : RecyclerView.Adapter<RecyclerView.ViewHolder> {
    var list List<Objects> ;
fun setItems(new6Items : List<Objects>){
            list.clear()
            list.addAll(new6Items) 
            notifyDataSetChanged()

}

...

}
bastami82
  • 5,955
  • 7
  • 33
  • 44
  • Thank you for your effort that I learned a lot from your answer . Your answer for showing only six items . My problem is , I want to show all list as usual but when user scrolled , screen should carry 6 items regardless item's height . – Ahmed Elsayed Nov 27 '19 at 13:22
  • 1
    np, sorry I did not understood your question first time around (without code it is pretty difficult), I think what you are actually looking for is a snappy recycler view, check this https://stackoverflow.com/questions/26370289/snappy-scrolling-in-recyclerview (I personally do not like forcing a fix size item in screen as this depend on many elements like what happen if item in your list size are not the same , what if the size of screen changes given there are many android devices with different width and height ) hope that link help you – bastami82 Nov 27 '19 at 13:52