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));
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);
Related
I wanted to set the data(result)into the recycler view, but when i run, the data is not displayed on the screen
RPCRequest.OnResponseListener orderListener = new RPCRequest.OnResponseListener<ArrayList<Person>>() {
#Override
public void onSuccessResponse(RPCRequest request, ArrayList<Person> response) {
ArrayList<Person> dups=new ArrayList<>();
for(int i=0;i<response.size();i++){
if(compare(response.get(i).getDate(),Person.getInstance().getDate()))
dups.add(response.get(i));
}
final Dialog dialog = new Dialog(context);
dialog.setContentView(R.layout.form);
View layout=(View) dialog.findViewById(R.id.row);
layout.setVisibility(View.GONE);
Toolbar toolbar = (Toolbar) dialog.findViewById(R.id.toolbar);
toolbar.setTitle(" Duplicates");
final RecyclerView dupOrders = (RecyclerView) dialog.findViewById(R.id.recycler_view);
dupOrders.setAdapter(adapter);
adapter.setData(dups);
dialog.show();
This is my code where i want to set the data in recycler list view. but when i debug i get the value in data. but its not displayed while executing. Can anyone please help.
You need to set a LayoutManager for the RecyclerView.
Add this line:
dupOrders.setLayoutManager(new LinearLayoutManager(context));
You should probably do this just once, so you might want to make your dupOrders variable into a field of your Activity and initialize it and set the LayoutManager in onCreate().
i hope that you could help me.
i want to use a expandablerecyclerview but i dont need to expand a list of other elements like the lib says
library example
I just need that the item expand and show more info of the item inside the recycler
You can use advanced recyclerview's expandable future
public class ExpandableWithHeaderFooterExampleActivity extends AppCompatActivity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_demo_minimal);
OnListItemClickMessageListener clickListener = new OnListItemClickMessageListener() {
#Override
public void onItemClicked(String message) {
View container = findViewById(R.id.container);
Snackbar.make(container, message, Snackbar.LENGTH_SHORT).show();
}
};
RecyclerView recyclerView = findViewById(R.id.recycler_view);
// Setup expandable feature and RecyclerView
RecyclerViewExpandableItemManager expMgr = new RecyclerViewExpandableItemManager(null);
// Create wrapped adapter: MyItemAdapter -> expMgr.createWrappedAdapter -> MyHeaderFooterAdapter
RecyclerView.Adapter adapter;
adapter = new SimpleDemoExpandableItemAdapter(expMgr, clickListener);
adapter = expMgr.createWrappedAdapter(adapter);
adapter = new DemoHeaderFooterAdapter(adapter, clickListener);
recyclerView.setAdapter(adapter);
recyclerView.setLayoutManager(new LinearLayoutManager(this));
// NOTE: need to disable change animations to ripple effect work properly
((SimpleItemAnimator) recyclerView.getItemAnimator()).setSupportsChangeAnimations(false);
expMgr.attachRecyclerView(recyclerView);
}
}
I'm creating RecyclerList for my application and need to set the most simple dividers between list-items. To achieve this I've decided to use DividerItemDecoration class from android.support.v7.widget package.
Here goes the code snippet for this:
DividerItemDecoration dividerItemDecoration = new DividerItemDecoration(recyclerView.getContext(),
layoutManager.getOrientation());
recyclerView.addItemDecoration(dividerItemDecoration);
My problem is that there is no divider to appear on a screen of app (I'm testing it on phone with API 18). So, I need a help to define where I'm going wrong and how can I solve my problem. Below I post the full Activity-code with described RecyclerView:
public class GroupsActivity extends AppCompatActivity {
private GroupsVcAdapter adapter;
private GroupsViewModel mGroupsViewModel;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_groups);
//Creating of toolbar with title Add Group
Toolbar myToolbar = (Toolbar) findViewById(R.id.toolbar_groups);
setSupportActionBar(myToolbar);
//Enable Up Button
ActionBar ab = getSupportActionBar();
ab.setDisplayHomeAsUpEnabled(true);
//RecyclerView containing the list of groups with sound icons
adapter = new GroupsVcAdapter(this);
RecyclerView recyclerView = (RecyclerView) findViewById(R.id.main_recycler_view);
LinearLayoutManager layoutManager = new LinearLayoutManager(this);
recyclerView.setLayoutManager(layoutManager);
DividerItemDecoration dividerItemDecoration = new DividerItemDecoration(recyclerView.getContext(),
layoutManager.getOrientation());
recyclerView.addItemDecoration(dividerItemDecoration);
//Using ViewModel to observe GroupVc data
mGroupsViewModel = ViewModelProviders.of(this).get(GroupsViewModel.class);
mGroupsViewModel.getAllGroups().observe(this, new Observer<List<GroupVc>>() {
#Override
public void onChanged(#Nullable List<GroupVc> groupVcs) {
adapter.setGroupsVc(groupVcs);
}
});
}
}
I've found the decision. This code works the way I like:
adapter = new GroupsVcAdapter(this);
final RecyclerView recyclerView = (RecyclerView) findViewById(R.id.main_recycler_view);
recyclerView.setAdapter(adapter);
LinearLayoutManager layoutManager = new LinearLayoutManager(this);
recyclerView.setLayoutManager(layoutManager);
DividerItemDecoration dividerItemDecoration = new DividerItemDecoration(recyclerView.getContext(),
layoutManager.getOrientation());
recyclerView.addItemDecoration(dividerItemDecoration);
recyclerView.setItemAnimator(new DefaultItemAnimator());
I would like to insert a tutorial on my list of Cardview using a library. For this library I need to give a view (element pointed) but my mRecyclerView.findViewHolderForItemId(mAdapter.getItemId(1)).itemView always returns null. Why does this return null? Because the recycle view is not yet created?
I also try to replace it with : mRecyclerView.findViewHolderForItemId(mAdapter.getItemId(1)).itemView)
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_list_players);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
mRecyclerView = (RecyclerView) findViewById(R.id.my_recycler_view_list_player);
// use this setting to improve performance if you know that changes
// in content do not change the layout size of the RecyclerView
mRecyclerView.setHasFixedSize(true);
// use a linear layout manager
mLayoutManager = new LinearLayoutManager(this);
mRecyclerView.setLayoutManager(mLayoutManager);
// specify an adapter (see also next example)
mDataset = Data.bdd.get_list_players();
mAdapter = new CardsViewAdapter(mDataset);
mRecyclerView.setAdapter(mAdapter);
// Tutorial
new MaterialIntroView.Builder(this)
.enableDotAnimation(true)
.enableIcon(true)
.setFocusGravity(FocusGravity.CENTER)
.setFocusType(Focus.MINIMUM)
.setDelayMillis(500)
.enableFadeAnimation(true)
.setInfoText("Hi There! Click this card and see what happens.")
.setShape(ShapeType.RECTANGLE)
.setTarget(mRecyclerView.findViewHolderForItemId(mAdapter.getItemId(1)))
.show();
}
I think your CardsViewAdapter library uses the default getItemId (which returns -1). You can use RecyclerView.getChild() instead.
The easiest way for me was to modify my builder like this and place my function directly in my adapter
CardsViewAdapter(List<Player> myDataset, Activity activity) {
mDataset = myDataset;
mActivity = activity;
}
I have got CardView with Views inside a RecyclerView. I have created adapter in which assets are attached to Views and everything works. Now, I would like to change these Views from my activity. Is it any simple way to do that?
public class MainActivity extends AppCompatActivity {
RecyclerView recyclerView;
private RecyclerView.LayoutManager layoutManager;
private List<Offer>offers;
TextView timer; //timer inside CardView
private void getViewReferences() {
recyclerView = (RecyclerView)findViewById(R.id.mainRecyclerView);
timer = (TextView)findViewById(R.id.timer);
}
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
getViewReferences();
layoutManager = new LinearLayoutManager(this);
recyclerView.setLayoutManager(layoutManager);
initializeData();
initializeAdapter();
timer.setText("13:16"); //NullPointerException here
}
private void initializeData(){
offers = new ArrayList<>();
offers.add(new Offer("godzina", R.drawable.zdj, 200));
offers.add(new Offer("godzina", R.drawable.zdj));
}
private void initializeAdapter() {
RecyclerViewAdapter adapter = new RecyclerViewAdapter(offers);
recyclerView.setAdapter(adapter);
}
}
So you don't want to change the Views but you want to update the text values of the views in your list.
You can't directly do that by trying to find the Views in the recyclerView and change the text. The adapter is responsible for giving values to your list. So you need to update the dataset in the adapter and call one of the notifyDataSetChanged methods on the adapter to update the recyclerView.
The big advantage of this design is that it places Views in Cache in order to reuse them multiple times without having to inflate and construct them again. This is a huge performance improvement.
You need to read the docs and some tutorials to understand the adapter design pattern better.