Android: Setting ListView fast scroll based on visible positions - android

I am trying to set the fast scroll enabled and visible if there are more items in the list than are currently visible. After setting the adapter, the ListView's count is properly increased but the last visible position is not. As a result I am currently posting a Runnable to wait until the ListView has figured out what items are / aren't visible. The code is as follows:
listView.post(new Runnable(){
#Override
public void run() {
if(listView.getLastVisiblePosition() < (listView.getCount() - 1)){
listView.setFastScrollAlwaysVisible(true);
listView.setFastScrollEnabled(true);
}
}
});
This solution works fine, but feels a bit hackish. Is there a way to know when the last visible position is updated without posting the runnable? Thanks in advance!

Related

RecyclerView - Any way to get how many items are visible on screen before user starts scrolling?

One of my activities contain a RecyclerView.
On launching the activity, I want to know how many times onBindViewHolder() would run before user does any scrolling. Using logging, I checked that it runs almost as many times as the number of items visible on the screen. So essentially I want to know how many items are visible on the screen.
getItemCount() gives the total number of items. This is not what I want.
Is there any other way or method to get only the count of visible items?
I checked this post, but it did not help my case - Get visible items in RecyclerView
To get RecyclerView visible count you can use layoutManager.getChildCount().
But you have to use like this :
layoutManager.postOnAnimation(new Runnable() {
#Override
public void run() {
System.out.println("Visible count " + layoutManager.getChildCount());
}
});

How to programmatically initiate a swipe to dismiss with RecyclerView

When I delete an item from a RecyclerView I want to show a "swipe" animation, that is that the item is moved sideways off the screen. I believe there is support for "swipe to dismiss" using ItemTouchHelper, but this is touch-initiated whereas I want to be able to initiate the swipe programmatically.
I have also tried setting the RecyclerView item animator by extending DefaultItemAnimator. Using this method I can get items to swipe left and right but unfortunately the gap in the list closes quickly such that the swipe does not finish before the list item gap has closed.
Anyone know how to do this?
You can use ItemAnimators offered by this library. More specifically, your would use their SlideInLeftAnimator or SlideInRightAnimator.
Although it's already answered with external library; probably someone wants to do it with raw java.. I have done it with the solution in here
The idea is that, you take off the list item view of the RecyclerView to be deleted and animate it; and normally remove it from the adapter
private void removeListItem(View rowView, final int position) {
Animation anim = AnimationUtils.loadAnimation(this,
android.R.anim.slide_out_right);
anim.setDuration(500);
rowView.startAnimation(anim);
new Handler().postDelayed(new Runnable() {
public void run() {
values.remove(position); //Remove the current content from the array
adapter.notifyDataSetChanged(); //Refresh list
}
}, anim.getDuration());
}

How can I make a scroll view scroll to the bottom?

I have tried this using methods I could find on internet.
My program is a listview containing 3000 webviews (webview inside a cardview).
(I dont think this issue is because webview being a scrollable one)
When I start the activity it is scrolled to bottom, but not the very bottom, just few webviews (around 10) out of 3000 stays below the screen.
I suspect the issue is because, the webview resizes just in milliseconds after it is loadData with a local String.
I have tried,
mainListView.post(new Runnable() {
#Override
public void run() {
// Select the last row so it will scroll into view...
mainListView.setSelection(getCount() - 1);
}
});
android:transcriptMode="alwaysScroll"
android:stackFromBottom="true"
both resulting same.
Try:
myListView.setSelection(myListAdapter.getCount() - 1);
First you get the total count of the item available in your listview, the set the selection to the last item by minus 1, since the setSelection() start from 0.

Scrolling android gridview blocking userinteraction

I have a gridview placed inside scrollview.
When app is launched the 12 items are displayed(page 1) in gridview.
WHen user scrolls and reaches the end of the scroll, next 12 items of next page is fetched via webservice, added to the initial array of items.
the notifydatasetchanged of gridview adapter is called and also the height of gridview is set dynamically.
But the issue is in point #3. This is blocking the user interaction for 1-2 seconds.
Please help.
Have you tried performing the update in a thread?
runOnUiThread(new Runnable() {
public void run() {
//Update the grid view and set height.

Android: scroll to the bottom of the view

I have a ListView containing some custom Views, and at the end of my list, a FooterView containing a Button.
When I click the button, it adds a view at the end of my ListView. But the cursor of the scrollbar stays at its position.
Now, what I want is to auto-scroll to the end of the list (so the user can see the new item).
Do you have any idea on how I can do that?
i think you will find you happyness here
ListView has a method just for this: smoothScrollToPosition
Once you've added the new item, just calculate the length of the list and pass that (remembering zero-indexing) to the above method
Try this one.. it will solve your problem, i tried it and it works great.
listView.post(new Runnable(){
public void run() {
listView.setSelection(listView.getCount() - 1);
}});

Categories

Resources