gpt4 book ai didi

java - Android:如何检测所有ListView对象是否已加载

转载 作者:行者123 更新时间:2023-12-02 06:18:27 26 4
gpt4 key购买 nike

近一周以来,我一直在想(没有成功)如何检测所有 ListView 对象(行)是否已加载。我的 ListView 是加载更多类型(我已将限制设置为 5 行,因此每 5 行后它会加载更多 5 行)。我正在使用 Parse.com 框架。加载整个列表后,我想删除页脚 View 。有关更多信息,请参阅下面我的代码。

您有什么想法或建议吗?如何实现它?

提前致谢。

public class Fragment1 extends Fragment {
private ListView mListView;
private AnimalAdapter mAdapter;
ProgressBar mProgressBar;
EditText mEditText;
RelativeLayout mRelativeLayout;
ProgressBar oProgressBar;
LinearLayout mLinearLayout;
Context context;
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 footer = getActivity().getLayoutInflater().inflate(R.layout.footer, null);
mListView.addFooterView(footer);
footer.setVisibility(View.VISIBLE);


mProgressBar = (ProgressBar) rootView.findViewById (R.id.loading_animals);
mProgressBar.setVisibility(View.VISIBLE);

RemoteDataTask task = new RemoteDataTask();
task.execute();





return rootView;
}





public void updateData() { //Main area no.1
mListView = (ListView)getView().findViewById(R.id.animal_list);
int limit = mListView.getCount() + 5;
ParseQuery<Animal> query = ParseQuery.getQuery(Animal.class);


query.setCachePolicy(CachePolicy.CACHE_THEN_NETWORK);
query.orderByAscending("animal");
query.setLimit(limit);

Toast.makeText(getActivity(), "now", Toast.LENGTH_SHORT).show();



query.findInBackground(new FindCallback<Animal>() {


@Override
public void done(List<Animal> animals, ParseException error) {

if(animals != null){
mAdapter.clear();

mProgressBar.setVisibility(View.INVISIBLE);
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) {


updateData();




return null;
}


@Override
protected void onPostExecute(Void result) {

mListView = (ListView) getView().findViewById(R.id.animal_list);
// Pass the results into ListViewAdapter.java
mAdapter = new AnimalAdapter(getActivity(), new ArrayList<Animal>());
mListView.setAdapter(mAdapter);




mListView.setVisibility(View.VISIBLE);
mListView.setTextFilterEnabled(true);



mListView.setAdapter(mAdapter);
mAdapter = new AnimalAdapter(getActivity(), new ArrayList<Animal>());
mListView = (ListView) getView().findViewById(R.id.animal_list);
mAdapter = new AnimalAdapter(getActivity(), new ArrayList<Animal>());
mListView.setAdapter(mAdapter);


mListView.setAdapter(mAdapter);
mEditText = (EditText) getView().findViewById(R.id.search_animal);

mListView.setOnScrollListener(new OnScrollListener() {



@Override //Main area no.2
public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount,
int totalItemCount) {
final int lastItem = firstVisibleItem + visibleItemCount;



if(lastItem == totalItemCount) {
updateData();



}



mListView.setFooterDividersEnabled(false);
}

@Override
public void onScrollStateChanged(AbsListView view,
int scrollState) {
if (SCROLL_STATE_TOUCH_SCROLL == scrollState) {
View currentFocus = getActivity().getCurrentFocus();
if(currentFocus != null) {
currentFocus.clearFocus();
}

mEditText = (EditText) getActivity().findViewById(R.id.search_animal);
InputMethodManager imm = (InputMethodManager)getActivity().getSystemService(
Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(mEditText.getWindowToken(), 0);




}

}

});

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());



}
});


}

}


}

最佳答案

首先您应该检查项目总数

例如:20 件

然后最初 setLimit =5 并在“Loadmore”异步任务中,您应该跳过以前的值,例如“setSkip”。这样它就不会加载前 5 个项目并查询接下来的 5 个项目

这样,您可以检查最后一个 block 是否已加载,一旦加载了 20 个项目,请删除页脚。

我希望这有帮助。

关于java - Android:如何检测所有ListView对象是否已加载,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21245072/

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