我正在使用Parse.com框架,并最近在我的代码中添加了一个load-more (onScroll) ListView。但是我发现,每当load-more过程开始时,甚至之前的项目也会重新加载。当我添加
您有任何想法,如何使用query.setSkip()方法并保存以前的项目?
1)ListView的第一个位置,在滚动到第5个位置之前
以下是对此问题重要的部分代码:
感谢您提前给出的任何建议!
query.setSkip(someCustomSkip);
后,在load-more过程之后,它只会删除(someCustomSkip)数量的项。您有任何想法,如何使用query.setSkip()方法并保存以前的项目?
1)ListView的第一个位置,在滚动到第5个位置之前
query.setLimit(mListView.getCount + 5)
2)滚动到限制位置后的ListView。它将我引导到这个位置并设置跳过-隐藏前5个项目以下是对此问题重要的部分代码:
public class Fragment1 extends Fragment {
static ListView mListView;
static AnimalAdapter mAdapter;
static ProgressBar mProgressBar;
static EditText mEditText;
static LayoutInflater inflater;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
final View rootView = inflater.inflate(R.layout.animalsfrag, container, false);
mListView = (ListView)rootView.findViewById(R.id.animal_list);
View header = getActivity().getLayoutInflater().inflate(R.layout.header, null);
header.setPadding(2, 8, 4, 2);
mListView.setVisibility(View.INVISIBLE);
mListView.requestFocus();
mListView.addHeaderView(header);
View footie = getActivity().getLayoutInflater().inflate(R.layout.footer, null);
mListView.addFooterView(footie);
footie.setVisibility(View.INVISIBLE);
mProgressBar = (ProgressBar) rootView.findViewById (R.id.loading_animals);
mProgressBar.setVisibility(View.VISIBLE);
RemoteDataTask task = new RemoteDataTask();
task.execute();
return rootView;
}
public void updateData() { //method, which updates my data
mListView = (ListView)getView().findViewById(R.id.animal_list);
final ParseQuery<Animal> query = ParseQuery.getQuery(Animal.class);
query.setCachePolicy(CachePolicy.NETWORK_ONLY);
query.orderByAscending("animal");
query.setLimit(mListView.getCount() + 5);
if (mListView.getCount() > 5) {
query.setSkip(5);
}
query.findInBackground(new FindCallback<Animal>() {
@Override
public void done(List<Animal> animals, ParseException error) {
if(animals != null){
mAdapter.clear();
mProgressBar = (ProgressBar) getView().findViewById (R.id.loading_animals);
mProgressBar.setVisibility(View.INVISIBLE);
RelativeLayout footie = (RelativeLayout) getView().findViewById(R.id.footerview);
footie.setVisibility(View.VISIBLE);
for (int i = 0; i < animals.size(); i++) {
mAdapter.add(animals.get(i));
}
}
}
});
}
private class RemoteDataTask extends AsyncTask<Void, Void, Void> {
@Override
protected void onPreExecute() {
super.onPreExecute(); }
@Override
protected Void doInBackground(Void... params) {
return null;
}
@Override
protected void onPostExecute(Void result) {
mListView = (ListView) getView().findViewById(R.id.animal_list);
mEditText = (EditText) getView().findViewById(R.id.search_animal);
mAdapter = new AnimalAdapter(getActivity(), new ArrayList<Animal>());
mListView.setVisibility(View.VISIBLE);
mListView.setTextFilterEnabled(true);
mListView.setAdapter(mAdapter);
mListView.setOnScrollListener(new OnScrollListener() {
//my OnScrollListener
@Override
public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount,
int totalItemCount) {
final int lastItem = firstVisibleItem + visibleItemCount;
if(lastItem == totalItemCount) {
if (mListView.getCount() > 20) {
RelativeLayout footie = (RelativeLayout) getView().findViewById(R.id.footerview);
mListView.removeFooterView(footie);
}
else{
updateData();
}
}
}
@Override
public void onScrollStateChanged(AbsListView view,
int scrollState) {
if (SCROLL_STATE_TOUCH_SCROLL == scrollState) {
View currentFocus = getActivity().getCurrentFocus();
if(currentFocus != null) {
currentFocus.clearFocus();
}
}
}
});
mListView.setAdapter(mAdapter);
mEditText.addTextChangedListener(new TextWatcher(){
@Override
public void afterTextChanged(Editable s) {}
@Override
public void beforeTextChanged(CharSequence s,
int start, int count, int after) {}
@Override
public void onTextChanged(CharSequence s, int start,
int before, int count) {
System.out.println("Text ["+s+"]");
mAdapter.getFilter().filter(s.toString());
}
});
}
}
}
感谢您提前给出的任何建议!
MyAdapter extends ParseQueryAdapter
,对吧? - marsoncreate()
。我的只是一个例子。 - Emmanuel