How to disable pulltorefresh functionality for first time? - android

I'm using chrisbanes's Android-PullToRefresh in my app.
I need to disable pulltorefresh functionality for first time fragment launch - when list is empty and items are downloading in background.
In this case (list is empty) user can swipe down and progressbar with "Release to refresh" will shown.
After loading all items I want to enable pulltorefresh functionality..
How?

I had same problem.
According to source, if view is disabled, it'll not eat touch event.
https://github.com/chrisbanes/ActionBar-PullToRefresh/blob/master/library/src/uk/co/senab/actionbarpulltorefresh/library/PullToRefreshLayout.java#L137
simply, you can do
mPullToRefreshLayout.setEnabled(false);

By default you disable pull to refresh and enable in asyncTask of post execute when to fill adapter of list.

You could put a count and call the method to refresh only when this count is bigger than 0(zero) and set a listener on scroll event to set count to 0(zero), so everytime that the user scroll you list, the count will be set to 0(zero) and when the list arrive on the top and scroll up again you refresh method will be called.
mSwipeRefreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
#Override
public void onRefresh() {
if (countDelay > 0) {
countDelay = 0;
refresh();
} else {
mSwipeRefreshLayout.setRefreshing(false);
countDelay++;
}
}
});
mSwipeRefreshLayout.getViewTreeObserver().addOnScrollChangedListener(new ViewTreeObserver.OnScrollChangedListener() {
#Override
public void onScrollChanged() {
countDelay = 0;
}
});

hope this help someone with the same issue:
mRefreshableListView.setMode(PullToRefreshBase.Mode.DISABLED);//to disable the pull functionality
mRefreshableListView.setMode(PullToRefreshBase.Mode.PULL_FROM_END);//or whatever you want

Related

Programmatically make a click or touch an item in recyclerview

How to force a click or touch an item on a recyclerview?
I found people talking to use a command recyclerView.findViewHolderForAdapterPosition(0).itemView.performClick() but recyclerView.findViewHolderForAdapterPosition(0) always returns null!
I want initially that the first recyclerview item is clicked/ touched.
new Handler().postDelayed(new Runnable() {
#Override
public void run() {
recyclerView.findViewHolderForAdapterPosition(position).itemView.performClick();
}
},100);

Android scroll up

I have two buttons Button 1, Button 2 placed horizontally like tabs. On Click of these button I am adding Linear Layout in Scroll View.
Now suppose I am clicking Button 1 and scroll down and immediately switch or click to Button 2 that scroll applies on newly added layout. I want every time on click of button content should scroll to top.
I have tried scrollview.scrollT0(0,0) and scrollview.scrollTo(0,scrollview.getTop()) but none of them is working.
I've ran into these types of issues before.
I suggest trying the following:
scrollview.post(new Runnable() {
#Override
public void run() {
scrollview.scrollTo(0, 0);
}
});
The reason this works is forces the action in the Main Thread which is where all UI Activity happens (safely, of course).
Try this . Just it is sample
private void scrollMyListViewToBottom() {
your_list.post(new Runnable() {
public void run() {
if (!running) {
// Select the last row so it will scroll into view...
your_list.setSelection(your_list.getCount() - 1);
running = true;
}
}
});
Note :
Before call this method just change Boolean running = false;

Adding items to ListView, maintaining scroll position and NOT seeing a scroll jump

I'm building an interface similar to the Google Hangouts chat interface. New messages are added to the bottom of the list. Scrolling up to the top of the list will trigger a load of previous message history. When the history comes in from the network, those messages are added to the top of the list and should not trigger any kind of scroll from the position the user had stopped when the load was triggered. In other words, a "loading indicator" is shown at the top of the list:
Which is then replaced in-situ with any loaded history.
I have all of this working... except one thing that I've had to resort to reflection to accomplish. There are plenty of questions and answers involving merely saving and restoring a scroll position when adding items to the adapter attached to a ListView. My problem is that when I do something like the following (simplified but should be self-explanatory):
public void addNewItems(List<Item> items) {
final int positionToSave = listView.getFirstVisiblePosition();
adapter.addAll(items);
listView.post(new Runnable() {
#Override
public void run() {
listView.setSelection(positionToSave);
}
});
}
Then what the user will see is a quick flash to the top of the ListView, then a quick flash back to the right location. The problem is fairly obvious and discovered by many people: setSelection() is unhappy until after notifyDataSetChanged() and a redraw of ListView. So we have to post() to the view to give it a chance to draw. But that looks terrible.
I've "fixed" it by using reflection. I hate it. At its core, what I want to accomplish is reset the first position of the ListView without going through the rigamarole of the draw cycle until after I've set the position. To do that, there's a helpful field of ListView: mFirstPosition. By gawd, that's exactly what I need to adjust! Unfortunately, it's package-private. Also unfortunately, there doesn't appear to be any way to set it programmatically or influence it in any way that doesn't involve an invalidate cycle... yielding the ugly behavior.
So, reflection with a fallback on failure:
try {
Field field = AdapterView.class.getDeclaredField("mFirstPosition");
field.setAccessible(true);
field.setInt(listView, positionToSave);
}
catch (Exception e) { // CATCH ALL THE EXCEPTIONS </meme>
e.printStackTrace();
listView.post(new Runnable() {
#Override
public void run() {
listView.setSelection(positionToSave);
}
});
}
}
Does it work? Yes. Is it hideous? Yes. Will it work in the future? Who knows? Is there a better way? That's my question.
How do I accomplish this without reflection?
An answer might be "write your own ListView that can handle this." I'll merely ask whether you've seen the code for ListView.
EDIT: Working solution with no reflection based on Luksprog's comment/answer.
Luksprog recommended an OnPreDrawListener(). Fascinating! I've messed with ViewTreeObservers before, but never one of these. After some messing around, the following type of thing appears to work quite perfectly.
public void addNewItems(List<Item> items) {
final int positionToSave = listView.getFirstVisiblePosition();
adapter.addAll(items);
listView.post(new Runnable() {
#Override
public void run() {
listView.setSelection(positionToSave);
}
});
listView.getViewTreeObserver().addOnPreDrawListener(new OnPreDrawListener() {
#Override
public boolean onPreDraw() {
if(listView.getFirstVisiblePosition() == positionToSave) {
listView.getViewTreeObserver().removeOnPreDrawListener(this);
return true;
}
else {
return false;
}
}
});
}
Very cool.
As I said in my comment, a OnPreDrawlistener could be another option to solve the problem. The idea of using the listener is to skip showing the ListView between the two states(after adding the data and after setting the selection to the right position). In the OnPreDrawListener(set with listViewReference.getViewTreeObserver().addOnPreDrawListener(listener);) you'll check the current visible position of the ListView and test it against the position which the ListView should show. If those don't match then make the listener's method return false to skip the frame and set the selection on the ListView to the right position. Setting the proper selection will trigger the draw listener again, this time the positions will match, in which case you'd unregister the OnPreDrawlistener and return true.
I was breaking up my head until I found a solution similar to this.
Before adding a set of items you have to save top distance of the firstVisible item and after adding the items do setSelectionFromTop().
Here is the code:
// save index and top position
int index = mList.getFirstVisiblePosition();
View v = mList.getChildAt(0);
int top = (v == null) ? 0 : v.getTop();
// for (Item item : items){
mListAdapter.add(item);
}
// restore index and top position
mList.setSelectionFromTop(index, top);
It works without any jump for me with a list of about 500 items :)
I took this code from this SO post: Retaining position in ListView after calling notifyDataSetChanged
The code suggested by the question author works, but it's dangerous.
For instance, this condition:
listView.getFirstVisiblePosition() == positionToSave
may always be true if no items were changed.
I had some problems with this aproach in a situation where any number of elements were added both above and below the current element. So I came up with a sligtly improved version:
/* This listener will block any listView redraws utils unlock() is called */
private class ListViewPredrawListener implements OnPreDrawListener {
private View view;
private boolean locked;
private ListViewPredrawListener(View view) {
this.view = view;
}
public void lock() {
if (!locked) {
locked = true;
view.getViewTreeObserver().addOnPreDrawListener(this);
}
}
public void unlock() {
if (locked) {
locked = false;
view.getViewTreeObserver().removeOnPreDrawListener(this);
}
}
#Override
public boolean onPreDraw() {
return false;
}
}
/* Method inside our BaseAdapter */
private updateList(List<Item> newItems) {
int pos = listView.getFirstVisiblePosition();
View cell = listView.getChildAt(pos);
String savedId = adapter.getItemId(pos); // item the user is currently looking at
savedPositionOffset = cell == null ? 0 : cell.getTop(); // current item top offset
// Now we block listView drawing until after setSelectionFromTop() is called
final ListViewPredrawListener predrawListener = new ListViewPredrawListener(listView);
predrawListener.lock();
// We have no idea what changed between items and newItems, the only assumption
// that we make is that item with savedId is still in the newItems list
items = newItems;
notifyDataSetChanged();
// or for ArrayAdapter:
//clear();
//addAll(newItems);
listView.post(new Runnable() {
#Override
public void run() {
// Now we can finally unlock listView drawing
// Note that this code will always be executed
predrawListener.unlock();
int newPosition = ...; // Calculate new position based on the savedId
listView.setSelectionFromTop(newPosition, savedPositionOffset);
}
});
}

Go to a item in Listview without using smoothScrollToPosition

I'd like to go to (display) a specific item in my listview but without scrolling. I don't want any animation but I'd like to be instantaneously transported to the desired item.
I'm using a checkable listview : mylistview.setChoiceMode(1) .
I understood that mylistview.setSelection(position) is the solution, but when I use it, nothing happens (maybe because it's a checkable listview ?).
When I use mylistview.smoothScrollToPosition(position), it works well but I have obviously this scroll animation which I don't want.
What could I do ?
Thanks.
Try this out:
myListView.post(new Runnable()
{
#Override
public void run()
{
myListView.setSelection(pos);
View v = myListView.getChildAt(pos);
if (v != null)
{
v.requestFocus();
}
}
});
From this Google Developer list answer by Romain Guy Link

Continuously Auto-Scrolling ListView

I have a Listview in Android. I want that the Listview continuously scrolls from top to bottom by itself. It should happen infinitely
And obviously I want to capture the click on any of the items of the Listview, post that the scroll will continue
Anybody having experience with such an implementation. Please help !!
http://groups.google.com/group/android-developers/msg/753a317a8a0adf03
To scroll automatically, you can use this: listView.smoothScrollToPosition(position);
private void scrollMyListViewToBottom() {
myListView.post(new Runnable() {
#Override
public void run() {
// Select the last row so it will scroll into view...
listView.smoothScrollToPosition(myListAdapter.getCount() - 1);
// Just add something to scroll to the top ;-)
}
});
}

Categories

Resources