How can I change the layout manager for Recycler View? - android

In my APP, a fragment contains a RecyclerView which shows the content in a Grid(initially). I Just want to show the content in a List when the user clicks on the Secondary Button on Toolbar. Content is almost same in the both case that's why I am trying to achieve this with one RecyclerView/Adapter/Fragment but I am not getting how to get it done.

you can do something like this:
btnChange.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
linearLayoutManager = new LinearLayoutManager(this, LinearLayoutManager.VERTICAL, false);
recyclerView.setLayoutManager(linearLayoutManager);
recyclerView.setAdapter(adapter);
}
});

Related

Re-order Query after click on Button - FirestoreRecyclerOptions

Currently, i can sort my documents in a Query, with it's:
query = anuncioRef.orderBy(campo, Query.Direction.DESCENDING).limit(100);
options = new FirestoreRecyclerOptions.Builder<AnuncioPrincipal>().setQuery(query,
AnuncioPrincipal.class).build();
adapter = new AnuncioAdapter(options);
RecyclerView recyclerView = root.findViewById(R.id.recyclerCadAnun);
recyclerView.setLayoutManager(new LinearLayoutManager(context));
recyclerView.setAdapter(adapter);
This works fine, but, i need that it change when the user click on button, i need that var "campo" must be changed to "dataPreco" and the list in recycler view must be updated:
botaoFiltrar.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
campo = "dataAnuncio";
}
});
In spite of "campo" be changed to "dataAnuncio", the recyclerView still remains equals.
I need that the button click, the recyclerView change to the new query orderBy() method.
The FirestoreRecyclerAdapter has an updateOptions method that you can call to update the options.
So you could call that with something like:
query = anuncioRef.orderBy(campo, Query.Direction.DESCENDING).limit(100); // new campo
options = new FirestoreRecyclerOptions.Builder<AnuncioPrincipal>().setQuery(query, AnuncioPrincipal.class).build();
adapter.updateOptions(options);

More info inside expandable recycler view

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);
}
}

Recyclerview scrollToPosition does not work

I have tried everything from the answers to the questions on stackoverflow, but I still could not figure this out. In one of my recyclerview, I was able to scroll to the desired position after a click of a button. On a different recyclerview (it was built similar to the first one), I was not able to scroll to the desired position.
This is the code for the recyclerview that worked:
LinearLayoutManager layoutManager = new LinearLayoutManager(getContext());
layoutManager.setOrientation(LinearLayoutManager.VERTICAL);
Button button = (Button) view.findViewById(R.id.button2);
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
newFeedRecyclerView.smoothScrollToPosition(2);
}
});
FeedRecyclerAdapter adapter = new FeedRecyclerAdapter(view.getContext(), allRows);
newFeedRecyclerView.setHasFixedSize(true);
newFeedRecyclerView.setLayoutManager(layoutManager);
newFeedRecyclerView.addItemDecoration(new VerticalSpaceItemDecorator(30));
newFeedRecyclerView.setAdapter(adapter);
And this is for the recyclerview that did not work:
allImageRecycleView.addItemDecoration(new VerticalSpaceItemDecorator(30));
allImageRecycleView.setNestedScrollingEnabled(false);
allImageRecycleView.setHasFixedSize(true);
LinearLayoutManager layoutManager = new >LinearLayoutManagerWithSmoothScroller(getContext());
Button button = (Button)view.findViewById(R.id.button3);
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
allImageRecycleView.smoothScrollToPosition(2);
}
});
layoutManager.setOrientation(LinearLayoutManager.VERTICAL);
allImageRecycleView.setLayoutManager(layoutManager);
allImageRecycleView.setAdapter(allImageRecyclerAdapter);
Thank you so much for reading this.
Try with this
layoutManager.scrollToPosition(index);
Use
layoutManager.scrollToPositionWithOffset(int position, int offset)
scrollToPositionWithOffset(int position, int offset) Scroll to the
specified adapter position with the given offset from resolved layout
start.
src : https://developer.android.com/reference/android/support/v7/widget/LinearLayoutManager.html
May not be relevant to your case but in general:
I find that the problem why scrollToPosition , and others API, does not work is because the dataset is not yet ready. We should call scrollToPosition when the recyclerView has received dataset. To do that, we can use AdapterDataObserver.
val adapter = binding.recyclerView.adapter!!
adapter.registerAdapterDataObserver(object : RecyclerView.AdapterDataObserver() {
override fun onItemRangeInserted(positionStart: Int, itemCount: Int) {
binding.recyclerView.scrollToPosition(positionStart)
adapter.unregisterAdapterDataObserver(this)
}
Similarly, you can override other methods of Class AdapterDataObserver, if needed.

Recyclerview fully scroll to position

My recyclerview should open the first item closed. To this end, I wrote these lines.
LinearLayoutManager llm = new LinearLayoutManager(getActivity());
llm.scrollToPosition(1);
paletteRecyclerView.setLayoutManager(llm);
I want to check if it is possible to scroll to position 1 fully. If my recyclerview is small I must open it without closing the first item. How can I check it?
I assume that you want to know if there are items to scroll in RecyclerView. So you can check as below after setting the adapter to the recyclerView.
recyclerView.post(new Runnable() {
#Override
public void run() {
int recyclerViewheight = recyclerView.getHeight();
int totalChildViewsHeight = recyclerView.computeVerticalScrollRange();
if (recyclerViewheight > totalChildViewsHeight) {
//there is no scroll (there are no more items to scroll)
} else {
//there is scroll (there are items to scroll)
}
}
});
Try with below code...
LinearLayoutManager llm = new LinearLayoutManager(getActivity());
recyclerView.setLayoutManager(llm);
RecyclerView.Adapter adapter = new YourAdapter();
recyclerView.setAdapter(adapter);
recyclerView.scrollToPosition(position);

RecyclerView for messenger application

I have converted the chat layout from listView to RecyclerView.
Now m facing some problem, When new messages arrive then it not showing automatically, I have to manually scroll to that messages.
Earlier in ListView it working.
I have used set setStackFormEnd in recylerView.
I want feature like whatsapp has like
if new message arrives and you are able to view last message then it will show that messages but
if you scrolling the messages then it doesn't change the position.
layoutManager.setStackFromEnd(true);
messagesView.setLayoutManager(layoutManager);
messagesView.setItemAnimator(new DefaultItemAnimator());
messagesView.addOnScrollListener(mOnScrollListener);
Sample on Create Code Code
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final RecyclerView recyclerView = (RecyclerView) findViewById(R.id.recyclerview);
final Button addButton = (Button) findViewById(R.id.button_is_reverse);
LinearLayoutManager layoutManager = new LinearLayoutManager(this);
layoutManager.setStackFromEnd(true);
layoutManager.setReverseLayout(true);
recyclerView.setLayoutManager(layoutManager);
recyclerView.addItemDecoration(new DividerDecoration(this));
adapter = new SampleAdapter(items);
recyclerView.setAdapter(adapter);
addButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
items.add("Items" +items.size());
adapter.notifyItemInserted(items.size());
}
});
LinearLayoutManager layoutManager = new LinearLayoutManager(getActivity());
layoutManager.setStackFromEnd(true);
layoutManager.setReverseLayout(true)
yourRecyclerView.setLayoutManager(layoutManager);
Scroll the recyclerview to last position
messagesView.scrollToPosition(messages.size()-1);
Where messages is your list of messages.
Use these line after you send/receive message event.
mRecyclerView.postDelayed(new Runnable() {
#Override
public void run() {
mRecyclerView.smoothScrollToPosition(mRecyclerView.getAdapter().getItemCount());
}
}, 100);

Categories

Resources