我在列表视图中实现了延迟加载。当我向下滚动列表并成功添加时,我在列表视图中添加了 10 行。但是在这里,在追加之后,列表会滚动到第一个位置。我希望列表与附加之前的位置相同。您能否看看下面的代码并帮助我。谢谢你。
package com.example.abe;
import java.util.ArrayList;
import android.annotation.TargetApi;
import android.app.Activity;
import android.content.Context;
import android.os.Build;
import android.os.Bundle;
import android.support.v4.app.ListFragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AbsListView;
import android.widget.AbsListView.OnScrollListener;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.Toast;
@TargetApi(Build.VERSION_CODES.HONEYCOMB)
public class Tab1Fragment extends ListFragment {
ListView lv;
Activity act = this.getActivity();
Context ct = this.getActivity();
XMLGettersSetters data;
boolean loadingMore = false;
String abc[] = new String[50];
static int count = 0;
final int itemsPerPage = 10;
ArrayList<String> myListItems;
ArrayList<String> myListItems2;
ArrayAdapter<String> adapter;
static int size = 0;
int position;
@SuppressWarnings("unused")
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
if (container == null) {
return null;
}
myListItems2 = new ArrayList<String>();
adapter = new ArrayAdapter<String>(getActivity().getBaseContext(),
android.R.layout.simple_list_item_1, myListItems2);
View root = inflater.inflate(R.layout.tab_frag1_layout, container,
false);
lv = (ListView) root.findViewById(android.R.id.list);
lv.setAdapter(adapter);
return root;
}
@Override
public void onViewCreated(View view, Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
myListItems = new ArrayList<String>();
View footerView = ((LayoutInflater) this.getActivity()
.getSystemService(Context.LAYOUT_INFLATER_SERVICE)).inflate(
R.layout.listfooter, null, false);
this.getListView().addFooterView(footerView);
this.setListAdapter(adapter);
for (int i = 0; i < 50; i++) {
abc[i] = "ab" + i;
}
this.getListView().setOnScrollListener(new OnScrollListener() {
@Override
public void onScrollStateChanged(AbsListView view, int scrollState) {
}
@Override
public void onScroll(AbsListView view, int firstVisibleItem,
int visibleItemCount, int totalItemCount) {
System.out.println(abc.length);
System.out.println(count);
System.out.println("First" + firstVisibleItem);
System.out.println("Vis" + visibleItemCount);
int lastInScreen = firstVisibleItem + visibleItemCount;
System.out.println("Last" + lastInScreen);
System.out.println("Total" + totalItemCount);
if ((lastInScreen == totalItemCount) && !(loadingMore)) {
if (count < size) {
try {
Thread thread = new Thread(null, loadMoreListItems);
thread.start();
} catch (Exception e) {
e.printStackTrace();
}
}
else {
Toast.makeText(getActivity().getApplicationContext(),
"DATA =)", Toast.LENGTH_LONG).show();
}
}
}
});
try {
Thread thread = new Thread(null, loadMoreListItems);
thread.start();
} catch (Exception e) {
e.printStackTrace();
}
}
private Runnable loadMoreListItems = new Runnable() {
@Override
public void run() {
loadingMore = true;
myListItems = new ArrayList<String>();
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
}
size = abc.length;
for (int i = 0; i < itemsPerPage; i++) {
if (count < size) {
myListItems.add(abc[count].toString());
count = count + 1;
} else {
break;
}
}
Log.i("a", "b");
try {
getActivity().runOnUiThread(returnRes);
} catch (Exception e) {
e.printStackTrace();
}
Log.i("a", "b");
}
};
private Runnable returnRes = new Runnable() {
@Override
public void run() {
if (myListItems != null && myListItems.size() > 0) {
for (int i = 0; i < myListItems.size(); i++)
adapter.add(myListItems.get(i));
}
setListAdapter(adapter);
Log.i("a", "b");
adapter.notifyDataSetChanged();
lv.setSelection(position);
lv.setSelectionFromTop(position, 0);
Log.i("a", "b");
getActivity().setTitle(
"Neverending List with "
+ String.valueOf(adapter.getCount()) + " items");
loadingMore = false;
}
};
}