我已经在android中研究了各种分页,但到目前为止我还没有在我的项目中成功实现这个概念。 我有一个带有baseadapter的自定义列表视图,数据一次只有10个,我还有一个编辑文本,有助于在清除前面的元素后搜索数据并在同一个列表中加载数据。我认为问题在我的listview滚动监听器中。可能是我没有以正确的方式实现listview在加载10个元素后没有停止加载。它一次又一次地直到所有数据都没有加载。可能是由于一些逻辑。请帮忙!发现我的错误。我在这里发布了所有必要的代码。
public class Search extends Fragment {
ListView lv_search;
Context context;
static int pagenum = 1;
boolean isLoading = false;
ListViewSearchAdapter adapter;
EditText et_search;
VenueModel venueModel;
ProgressDialog pdialog;
Location latlng;
String search;
private boolean isDataLoad = true;
ImageView map;
public Search(ScreenTwo screenTwo) {
this.context = screenTwo;
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.frag_search, container, false);
Log.e("SearchFragment", "Enter Search Fragment");
lv_search = (ListView) view.findViewById(R.id.lv);
et_search = (EditText) view.findViewById(R.id.et_search);
map = (ImageView)view.findViewById(R.id.img_map);
lv_search.setOnScrollListener(new OnScrollListener() {
@Override
public void onScrollStateChanged(AbsListView view, int scrollState) {}
@Override
public void onScroll(AbsListView view, int firstVisibleItem,
int visibleItemCount, int totalItemCount) {
int lastInScreen = firstVisibleItem + visibleItemCount;
if ((totalItemCount==lastInScreen) && !isLoading) {
Log.e("Venue Search", "onScroll " + "lastInScreen "
+ lastInScreen + "firstVisibleItem "
+ firstVisibleItem + "visibleItemCount "
+ visibleItemCount);
if (isDataLoad == true) {
callVenu();
}else if(isDataLoad == false){
result();
}
else{
Log.e("Data Ended", "Data Ended");
}
}
}
});
et_search.addTextChangedListener(new TextWatcher() {
@Override
public void onTextChanged(CharSequence s, int start, int before,
int count) {
if (s.length() == 0) {
pagenum = 1;
isDataLoad = true;
venueModel.getDetails().clear();
adapter.notifyDataSetChanged();
}
}
@Override
public void beforeTextChanged(CharSequence s, int start, int count,
int after) {
// TODO Auto-generated method stub
}
@Override
public void afterTextChanged(Editable s) {
// TODO Auto-generated method stub
}
});
et_search.setOnEditorActionListener(new OnEditorActionListener() {
@Override
public boolean onEditorAction(TextView v, int actionId,
KeyEvent event) {
if (actionId == EditorInfo.IME_ACTION_SEARCH) {
Toast.makeText(getActivity(),
et_search.getText().toString(), Toast.LENGTH_SHORT)
.show();
search = et_search.getText().toString();
InputMethodManager inputManager = (InputMethodManager) getActivity()
.getSystemService(Context.INPUT_METHOD_SERVICE);
inputManager.hideSoftInputFromWindow(
et_search.getWindowToken(),
InputMethodManager.HIDE_NOT_ALWAYS);
if (!v.getText().toString().equals("")) {
pagenum = 1;
isDataLoad = false;
venueModel.getDetails().clear();
adapter.notifyDataSetChanged();
}
}
return false;
}
});
return view;
}
private void callVenu() {
LocationManager lm = (LocationManager) getActivity()
.getApplicationContext().getSystemService(
Context.LOCATION_SERVICE);
latlng = lm.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
AsyncHttpClient client = new AsyncHttpClient();
RequestParams params = new RequestParams();
params.put("lat", latlng.getLatitude());
params.put("lng", latlng.getLongitude());
params.put(
"user_id",
getActivity().getApplicationContext()
.getSharedPreferences("pref", Context.MODE_PRIVATE)
.getString("user_id", ""));
params.put("pagenum", pagenum);
client.post("http://projectsonseoxperts.com.au/checked/api/venue",
params, new AsyncHttpResponseHandler() {
@Override
public void onStart() {
// TODO Auto-generated method stub
super.onStart();
pdialog = new ProgressDialog(getActivity());
pdialog.show();
}
@Override
public void onCancel() {
// TODO Auto-generated method stub
super.onCancel();
Log.e("cANCEL pAGINATION", "cANCEL pAGINATION");
pdialog.dismiss();
}
@Override
public void onFinish() {
// TODO Auto-generated method stub
super.onFinish();
pdialog.dismiss();
}
@Override
public void onSuccess(int arg0, Header[] arg1, byte[] arg2) {
Gson gson = new Gson();
VenueModel venueModelLocal = gson.fromJson(new String(
arg2), VenueModel.class);
if (venueModelLocal != null
&& venueModelLocal.getDetails().size() == 0) {
isLoading = true;
return;
}
if (venueModelLocal.getStatus() == true) {
isDataLoad = true;
String abc = new String(arg2);
Log.e("venueModel venueModel venueModel ", abc);
if (venueModelLocal.getKey().equals(100)) {
isLoading = false;
if (adapter == null) {
venueModel = venueModelLocal;
adapter = new ListViewSearchAdapter(
getActivity(), venueModel);
lv_search.setAdapter(adapter);
} else {
venueModel.getDetails().addAll(
venueModelLocal.getDetails());
adapter.notifyDataSetChanged();
}
}
}
pagenum++;
Log.e("PAGENUM", pagenum + "");
}
@Override
public void onFailure(int arg0, Header[] arg1, byte[] arg2,
Throwable arg3) {
Log.e("onfailureLOOPJ", new String(arg2));
isLoading = false;
}
});
}
private void result() {
AsyncHttpClient client = new AsyncHttpClient();
RequestParams params = new RequestParams();
params.put("search", search);
params.put("lat", latlng.getLatitude());
params.put("lng", latlng.getLongitude());
params.put("pagenum", pagenum);
client.post(
"http://projectsonseoxperts.com.au/checked/api/search-venue",
params, new AsyncHttpResponseHandler() {
@Override
public void onStart() {
// TODO Auto-generated method stub
super.onStart();
pdialog = new ProgressDialog(getActivity());
pdialog.show();
}
@Override
public void onCancel() {
// TODO Auto-generated method stub
super.onCancel();
Log.e("cANCEL pAGINATION", "cANCEL pAGINATION");
pdialog.dismiss();
}
@Override
public void onFinish() {
// TODO Auto-generated method stub
super.onFinish();
pdialog.dismiss();
}
@Override
public void onSuccess(int arg0, Header[] arg1, byte[] arg2) {
String responseString = new String(arg2);
VenueModel modellocal = new VenueModel();
Gson gson = new Gson();
modellocal = gson.fromJson(responseString,
VenueModel.class);
if (modellocal != null
&& modellocal.getDetails().size() == 0) {
isLoading = true;
return;
}
if (modellocal.getStatus() == true) {
isDataLoad = false;
String abc = new String(arg2);
Log.e("venueModel venueModel venueModel ", abc);
if (modellocal.getKey().equals(100)) {
isLoading = false;
if (adapter == null) {
venueModel = modellocal;
adapter = new ListViewSearchAdapter(
getActivity(), venueModel);
lv_search.setAdapter(adapter);
} else {
venueModel.getDetails().addAll(
modellocal.getDetails());
adapter.notifyDataSetChanged();
}
// lv_search.invalidateViews();
}
}
pagenum++;
Log.e("SEARCHPAGE", pagenum + "");
}
@Override
public void onFailure(int arg0, Header[] arg1, byte[] arg2,
Throwable arg3) {
isLoading = false;
isDataLoad = false;
}
});
}
}
答案 0 :(得分:0)
您应该使用无限滚动侦听器。此链接可帮助您了解如何使用无限滚动侦听器。 https://github.com/codepath/android_guides/wiki/Endless-Scrolling-with-AdapterViews
第一页id [1,2,3,4,5]
将id 5发送到服务器后
第二页id [6,7,8,9,10]
答案 1 :(得分:-2)
请考虑使用CommonWare的优秀https://github.com/commonsguy/cwac-endless。