我希望当我向上滚动到列表视图时使用volley加载更多数据,我创建了PHP webservice和结果
{
"error": false,
"status_code": 200,
"total_items": 2,
"last_page": 1,
"current_page": 1,
"per_page": 10,
"result": [
{
"id": 1,
"title": "here title one",
"city": "city",
"zone": "zone",
"type": "type",
"service": "service",
"space": "545",
"date": "2016-08-28 12:24:34",
"image": "http:\/\/localhost\/my_cms\/public\/assets\/media\/ad-pic.png"
}, {
"id": 2,
"title": "here title two",
"city": "city",
"zone": "zone",
"type": "type",
"service": "service",
"space": "545",
"date": "2016-08-28 12:24:34",
"image": "http:\/\/localhost\/my_cms\/public\/assets\/media\/ad-pic.png"
}]
}
我的RecentActivity.Java类
public class RecentActivity extends AppCompatActivity implements SwipeRefreshLayout.OnRefreshListener, AbsListView.OnScrollListener {
private SwipeRefreshLayout swipeRefreshLayout;
// Log tag
private static final String TAG = HomeActivity.class.getSimpleName();
// RealstateDataModel json url
private static final String homeAdsurl = "http://192.168.43.74/my_cms/public/api/realstate/latest?page=";
private ProgressDialog pDialog;
private List<RealstateDataModel> realstateData = new ArrayList<RealstateDataModel>();
private ListView listView;
private HomeListAdapter homaAdapter;
int current_page = 0;
boolean loading;
RecentActivity activity = null;
View loadMoreView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_recent);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
listView = (ListView) findViewById(R.id.Recent_View);
String home= getResources().getString(R.string.recient_offers);
setTitle(home);
activity = this;
//List View
setListAdapter();
listView.setOnScrollListener(this);
}
public void setListAdapter(){
swipeRefreshLayout = (SwipeRefreshLayout) findViewById(R.id.swipe_refresh_recent_layout);
homaAdapter = new HomeListAdapter(this, realstateData);
listView.setAdapter(homaAdapter);
pDialog = new ProgressDialog(this);
// Showing progress dialog before making http request
pDialog.setMessage("Loading...");
pDialog.setCancelable(false);
pDialog.show();
swipeRefreshLayout.setOnRefreshListener(this);
/**
* Showing Swipe Refresh animation on activity create
* As animation won't start on onCreate, post runnable is used
*/
swipeRefreshLayout.post(new Runnable() {
@Override
public void run() {
swipeRefreshLayout.setRefreshing(true);
fetchRealStateList();
}
}
);
}
@Override
public void onRefresh() {
// showing refresh animation before making http call
swipeRefreshLayout.setRefreshing(true);
fetchRealStateList();
}
private void fetchRealStateList() {
// Creating volley request obj
JsonObjectRequest homeListReq = new JsonObjectRequest(Request.Method.GET,
homeAdsurl, null, new Response.Listener<JSONObject>() {
@Override
public void onResponse(JSONObject response) {
Log.d(TAG, response.toString());
hidePDialog();
if (response.length() > 0) {
try {
JSONArray ResultArray = response.getJSONArray("result");
if(realstateData!=null) {
realstateData.clear();
}
// Parsing json
for (int i = 0; i < ResultArray.length(); i++) {
try {
JSONObject obj = ResultArray.getJSONObject(i);
RealstateDataModel realstateDataBeans = new RealstateDataModel();
realstateDataBeans.setTitle(obj.getString("title"));
realstateDataBeans.setCity(obj.getString("city"));
realstateDataBeans.setZone(obj.getString("zone"));
realstateDataBeans.setType(obj.getString("type"));
realstateDataBeans.setService(obj.getString("service"));
realstateDataBeans.setSpace(obj.getString("space"));
realstateDataBeans.setCreated_at(obj.getString("date"));
realstateDataBeans.setImage(obj.getString("image"));
// adding movie to movies array
realstateData.add(0,realstateDataBeans);
} catch (JSONException e) {
e.printStackTrace();
}
}
} catch (JSONException e) {
e.printStackTrace();
}
// notifying list adapter about data changes
// so that it renders the list view with updated data
homaAdapter.notifyDataSetChanged();
}
// stopping swipe refresh
swipeRefreshLayout.setRefreshing(false);
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
VolleyLog.d(TAG, "Error: " + error.getMessage());
hidePDialog();
// stopping swipe refresh
swipeRefreshLayout.setRefreshing(false);
}
});
// Adding request to request queue
AppController.getInstance().addToRequestQueue(homeListReq);
}
private void hidePDialog() {
if (pDialog != null) {
pDialog.dismiss();
pDialog = null;
}
}
@Override
public void onDestroy() {
super.onDestroy();
hidePDialog();
}
@Override
public void onScrollStateChanged(AbsListView absListView, int i) {
}
@Override
public void onScroll(AbsListView absListView, int i, int i1, int i2) {
}
}
它尝试和搜索更多,但当它尝试滚动不效果时,我做f onScrollStateChanged和onScroll为空以填写您的有用代码
答案 0 :(得分:0)
例如,首先加载10项。当用户达到底部加载10项时。你怎么能理解它的底部?
The last item's bottom == scroll position
表示列表的结尾。加载更多。