Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

recyclerview.setLayoutManager()

What is the use of recyclerview.setLayoutManager() in this code? Please elaborate it in detail.I know about recyclerview but i am confused on what is the use of setLayoutManager() here?

public class MainActivity extends AppCompatActivity {

public static final int NEW_WORD_ACTIVITY_REQUEST_CODE = 1;

private WordViewModel mWordViewModel;

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

    Toolbar toolbar = findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);

    RecyclerView recyclerView = findViewById(R.id.recyclerview);
    final WordListAdapter adapter = new WordListAdapter(this);
    recyclerView.setAdapter(adapter);
    recyclerView.setLayoutManager(new LinearLayoutManager(this));
like image 383
Anonymous Avatar asked May 04 '18 09:05

Anonymous


People also ask

What is layout manager in RecyclerView?

A LayoutManager is responsible for measuring and positioning item views within a RecyclerView as well as determining the policy for when to recycle item views that are no longer visible to the user.

How check RecyclerView is empty in Android?

size() != 0) { NovaAdapter novaAdapter = new NovaAdapter(getActivity(),jsonList); recyclerView. setAdapter(novaAdapter); } else { // Show something like a dialog that the json list is 0 or do whatever you want... here the jsonlist have a count of 0 so it's empty! } } });

How many types of layout managers are there in RecyclerView implementation?

There are two types of layout managers for Recycler View to implement.


1 Answers

More than a year late but the idea behind the setLayoutManager is to set the layout of the contents, i.e. list of repeating views in the recycler view. If you scroll down to the documentation here, it will tell you that there are several strategies for lists and grids so that should give you a clue. Furthermore, it tells you that without it, RecyclerView will not function i.e. there is no out of the box default.

So if you want e.g. to set the LinearLayoutto be horizontal (by default it is vertical) then you have to specify that.

LinearLayoutManager layoutManager
    = new LinearLayoutManager(this, LinearLayoutManager.HORIZONTAL, false);

RecyclerView myItems = findViewById(R.id.my_recycler_view);
myItems.setLayoutManager(layoutManager);
like image 154
BVB09 Avatar answered Sep 30 '22 17:09

BVB09