gpt4 book ai didi

java - 如何不再加载所有 ListView 项目?

转载 作者:太空狗 更新时间:2023-10-29 14:16:35 26 4
gpt4 key购买 nike

我正在使用 Parse.com 框架,并且最近在我的代码中添加了一个 load-more (onScroll) ListView。但我发现,每当加载更多过程开始时,甚至以前的项目也会再次加载。当我添加 query.setSkip(someCustomSkip); 时,在加载更多过程之后它只会丢弃没有 (someCustomSkip) 数量的项目。

你知道吗,我该如何使用 query.setSkip();方法并保存provoive的?

1) ListView的第一个位置,滚动到第5个位置之前 query.setLimit(mListView.getCount + 5)

ListView's first position, before scrolling to 5th position (<code>query.setLimit(mListView.getCount + 5</code>)

2) 滚动到限制位置后的 ListView。它把我带到这个位置并设置跳过 - 隐藏前 5 个项目

ListView after scrolling to limit-position. It throws me to this position and sets skip - hides first 5 items

还有我的部分代码,这对这个问题很重要:

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 的类来处理所有这些。它的构造函数之一采用 ContextQueryFactoryQueryFactory 要求您覆盖 create() 并让您返回一个 ParseQuery

ParseQuery 有一个描述其缓存策略的枚举。如果你不想每次都ping服务器,你可以设置缓存策略先检查缓存,如果在本地缓存中没有找到ParseObject则再命中服务器。

ParseQueryAdapter 默认启用分页;一切都为您服务(即使是显示“加载更多数据”的 View )。

下面是文档中的一小段代码,可能会对您有所帮助:

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 中设置适配器。

关于java - 如何不再加载所有 ListView 项目?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21352807/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com