Reputation: 1117
I have a listview which I am forcefully scrolling to the bottom of the list, whenever the list is updated. However, after scrolling to the bottom of the list, for some reason, the listview becomes fixated at that point and the user cannot scroll back and forth on the list to see the other data.
Question: After setSelection(list.getBottom())
, what should I have in order to allow scrolling through the entire list once again?
@Override
public View getView(int position, View convertView, ViewGroup parent) {
/* the list is being populated here - the code has been omitted */
list.populateFrom(date, name, class);
list.clearFocus();
list.post(new Runnable() {
@Override
public void run() {
list.setSelection(list.getBottom());
}
});
return list;
}
}
Upvotes: 1
Views: 2365
Reputation:
As suggested by A--C, it is problem with the
list.setSelection(list.getBottom());
Every time your getView() is called you are moving/scrolling the view to the bottom, there fore when you are trying to scroll the view again then your getView() is called and again you again move to bottom. So instead of setting the selection in the listview to the bottom in the getView() you should do it in your activity/fragment. In activity you observe the change and set selection to the bottom.
If this was helpful explanation for your problem, do upvote
Upvotes: 0
Reputation: 133570
cus= new Customlistadapter(paramets for customer adapter));//Custom list adapter
lv1 = (ListView) findViewById(R.id.list);
lv1.setTranscriptMode(AbsListView.TRANSCRIPT_MODE_ALWAYS_SCROLL);
lv1.setStackFromBottom(true);
cus.notifyDataSetChanged();// for refreshing listview with updates
lv1.setAdapter(cus);
cus.registerDataSetObserver(new DataSetObserver() {
@Override
public void onChanged() {
super.onChanged();
lv1.setSelection(cus.getCount() - 1);
}
});
Use the above in your activity class. A--C has pointed to the problem.
If you are using AysncTask in your activity class try the below code.
private class DownloadFilesTask extends AsyncTask<Void, Void, VOid> {
protected void onPreExecute()
{
//display progress dialog
}
protected Long doInBackground(Void... params) {
// get data from server
return null;
}
protected void onPostExecute(Void params) {
// dismiss dialog
//set data
// paste the above listview code here.
}
}
Upvotes: 0
Reputation: 36449
The problem lies with the fact that you are calling
list.setSelection(list.getBottom());
in getView()
, which is called every single time the ListView needs more Views drawn. This means you constantly scroll down.
Instead, update the data set you have from outside the Adapter
class (such as from an Activity/Fragment
), then either reset the ListView
's adapter or clear the adapter and readd the items, then call setSelection()
.
One way to do this:
Activity
starts an AsyncTask
AsyncTask
updates a List
of data via doInBackground()
.
In onPostExecute()
, instantiate the Adapter
with the List
passed from doInBackground()
. Or clear the data set of the Adapter
you currently have, then loop through the List and re-add the items to the Adapter
.
If reinstantiating the Adapter
, set the ListView
's Adapter
to what you have created.
call ListView#setSelection()
Upvotes: 3