Android Save previous scroll position of list

I have a problem with my list. There is a custom adapter on my list. It fetches records from the server. I am using AsyncTask

to fetch records and display in a list.

But when I click on the listview item to display the details and return to the listview activity, it scrolls to the top and updates the list.

This is my code:

 protected void onPostExecute(String result) {

            progressBar.setVisibility(View.GONE);

            if (isSwiped) {
                // reset
                isSwiped = false;
            }

            if (result != null && tag == 1) {
                adapter = new SharedBibleVersesAdapter(SharedBibleVersesActivity.this,
                        SharedBibleVersesActivity.this);
                lv.setAdapter(adapter);
                adapter.notifyDataSetChanged();


    }


 @Override
 public void onResume() {
    super.onResume();
    displayRecords();
 }

public void displayRecords() {

    if(ConnectionDetector.hasNetworkConnection(this)) {
        DisplayAsyncTask displayTask = new DisplayAsyncTask();
        displayTask.execute();
    } else {
        progressBar.setVisibility(View.GONE);
        tvHeader.setVisibility(View.VISIBLE);
            tvHeader.setText(
                    getResources().getString(R.string.no_internet_connection));

    }

}


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    tvTitle.setText(getResources().getString(R.string.shared_bible_verses));
    tvHeader = (MyTextView) findViewById(R.id.tvHeader);
    lv = (ListView) findViewById(R.id.listView);
    progressBar = (ProgressWheel) findViewById(R.id.progressBar);
    displayRecords();

}

      

Any ideas why they are scrolling to the top? I will gladly take care of any help. Thank.

+3


source to share


4 answers


It scrolls to the top because you are reinstalling the adapter ...

Using:



if (adapter == null) {
    adapter = new SharedBibleVersesAdapter(SharedBibleVersesActivity.this, SharedBibleVersesActivity.this, itemList, userAPIkey, userId);
    lv.setAdapter(adapter);
} else {
     adapter.notifyDataSetChanged();
}

      

0


source


I used the following in one of my old projects, added this code in onSaveInstanceState

and it worked:

// save index and top position
int index = listView.getFirstVisiblePosition();
View view = listView.getChildAt(0);
int top = (view == null) ? 0 : view.getTop();

      

Then save it in SharedPreferences

to return to the same position:

SharedPreferences sp = PreferenceManager.getDefaultSharedPreferences(context);
        Editor edit = sp.edit();
        edit.putString(key1, index);
        edit.putString(key2, top);
        edit.commit();

      

You can set the previous state ListView

by fetching data from SharedPreferences

, for example:



SharedPreferences sp =PreferenceManager.getDefaultSharedPreferences(getActivity());     
String _index = sp.getString("INDEX1", saved_index);
String _top = sp.getString("TOP1", saved_top);

      

Then use this after setAdapter

:

listView.setSelectionFromTop(Integer.parseInt(_index), Integer.parseInt(_top));

      

Note. onSaveInstanceState()

is a method used to store data before suspending activity.

0


source


Please add the property to the element <ListView>

in the AndroidManifest.xml

file

android:transcriptMode 

      

Values:

  • disabled - 0: scrolling disabled
  • normal - 1: If the last item in the list is visible to the user, then when a new item is added from the bottom through a call notifyDataSetChanged()

    , the list will scroll to display that item
  • alwaysScroll - 2: The list will scroll down, regardless of whether the last item is visible to the user or not.

Your case normal

is the correct value and it will solve your problem.

0


source


save your click index and use it later.

lv.setSelection(index);

      

0


source







All Articles