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));
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.
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! } } });
There are two types of layout managers for Recycler View to implement.
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 LinearLayout
to 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);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With