RecyclerView for messenger application - android

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

Related

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

Divider of items in RecyclerView's vertical list isn't showing up

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

How do I place new items at the top of a recyclerView?

I'm adding content to my database (Firebase) which are suppose to be loaded into my recyclerview. But the new content always appear at the bottom of the recyclerview. Is there any way to make them appear at the top rather than appear at the bottom
If you want to appear new Items on the top just Add this code in your Activity.
you need to reverse your RecyclerView, then you totally need this code segment. It does not require any modification or any special library. It’s so simple to work on LinearLayoutManager .
LinearLayoutManager mLayoutManager = new LinearLayoutManager(this);
mLayoutManager.setReverseLayout(true);
mLayoutManager.setStackFromEnd(true);
recyclerView.setLayoutManager(mLayoutManager);
First you need to create a LinearLayoutManager object and use “setReverseLayout(true)” and “setStackFromEnd(true)” methods to reverse and add item at the end.
I finally solved the issue all i needed to do was to initialize an integer variable as 0,add it along side with the object of the model class that is to be gotten from the Firebase database and finally notifying an item inserted to the adapter.The code will explain better
int position = 0;
ArrayList<Places> data = new ArrayList<>();
LinearLayoutManager lg = new LinearLayoutManager(this);
then the final code was this
valueEventListener = new ValueEventListener() {
#Override
public void onDataChange(DataSnapshot dataSnapshot) {
for (DataSnapshot ds : dataSnapshot.getChildren()) {
Places content = ds.getValue(Places.class);
data.add(position,content);
adapter = new YourAdapter(data, getApplicationContext);
recyclerview.setLayoutManager(new Line);
recyclerview.setAdapter(adapter);
adapter.notifyItemInserted(position);
adapter.notifyDataSetChanged();
}
#Override
public void onCancelled(DatabaseError databaseError) {
// Handle error code here
}
});

How can I change the layout manager for Recycler View?

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

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

Categories

Resources