[英]How to not load all ListView items again?
我正在使用Parse.com框架,我最近在我的代碼中添加了一個load-more(onScroll)ListView。 但我發現每當加載更多進程開始時,即使之前的項目也會再次加載。 當我添加query.setSkip(someCustomSkip);
,在加載更多的過程之后,它只丟棄沒有(someCustomSkip)數量的項目。
你有什么想法,我怎么能用query.setSkip(); 方法,也保留了有益的方法?
1)ListView的第一個位置,在滾動到第5個位置之前query.setLimit(mListView.getCount + 5)
2)滾動到極限位置后的ListView。 它把我扔到這個位置並設置skip - 隱藏前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());
}
});
}
}
}
提前感謝任何建議!
我不知道你現在是否在使用它,但是Parse提供了一個名為ParseQueryAdapter
的類來處理所有這些。 其構造函數之一采用Context
和QueryFactory
。 QueryFactory
要求您覆蓋create()
並使您返回ParseQuery
。
ParseQuery
有一個描述其緩存策略的枚舉。 如果您不想每次都ping服務器,可以將緩存策略設置為先檢查緩存,然后在本地chache中找不到ParseObject
命中服務器。
ParseQueryAdapter
默認啟用分頁; 這一切都得到了照顧(即使是“加載更多數據”的觀點)。
以下是文檔中可能對您有所幫助的一小段代碼段:
ParseQueryAdapter<ParseObject> adapter =
new ParseQueryAdapter<ParseObject>(this, new ParseQueryAdapter.QueryFactory<ParseObject>() {
public ParseQuery<ParseObject> create() {
// Here we can configure a ParseQuery to our heart's desire.
ParseQuery query = new ParseQuery("Band");
query.whereContainedIn("genre", Arrays.asList({ "Punk", "Metal" }));
query.whereGreaterThanOrEqualTo("memberCount", 4);
query.orderByDescending("albumsSoldCount");
query.setCachePolicy(ParseQuery.CachePolicy.CACHE_ELSE_NETWORK)
return query;
}
});
然后,您可以像往常一樣在ListView
設置適配器。
對於那些將來在這個問題上苦苦掙扎的人,您可以簡單地將條目數分配給一個Integer(在我的下面的情況下是麻木),然后在運行Async后台函數以在列表視圖中加載更多結果之前確保列表視圖中顯示的條目數小於查詢條目。 如果沒有,您可以再次運行Async任務以附加更多結果。
L3.setOnScrollListener(new OnScrollListener() {
@Override
public void onScrollStateChanged(AbsListView view,
int scrollState) { // TODO Auto-generated method stub
int threshold = 1;
int count = L3.getCount();
if (scrollState == SCROLL_STATE_IDLE) {
if (L3.getLastVisiblePosition() >= count
- threshold) {
// Execute LoadMoreDataTask AsyncTask
// Toast.makeText(getApplicationContext(), count- threshold+"", 300).show();
if(L3.getLastVisiblePosition()+ threshold <numb)
{
//Toast.makeText(getApplicationContext(), L3.getLastVisiblePosition()+"", 300).show();
new LoadMoreDataTask().execute();
}
else
{
Toast.makeText(getApplicationContext(), "لا توجد مزيد من النتائج حاليآ...", 300).show();
}
}
}
}
@Override
public void onScroll(AbsListView view, int firstVisibleItem,
int visibleItemCount, int totalItemCount) {
// TODO Auto-generated method stub
}
});
希望能幫助到你!!
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.