我试图向下滚动将数据加载到Recyclerview中。按升序工作正常。但是,当我按降序尝试时,它不起作用。我的服务器端代码是PHP。请参见下面的代码。
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_bank_transection);
recyclerView = findViewById(R.id.recycler_view);
progressDialog = new ProgressDialog(BankTransection.this);
progressDialog.setCancelable(false);
transactionList = new ArrayList<>();
adapter = new TransactionAdapter(BankTransection.this, transactionList);
session = new Session(BankTransection.this);
userid = session.getUserDetails().get(session.KEY_USERID);
mLayoutManager= new GridLayoutManager(BankTransection.this,1);
recyclerView.setLayoutManager(mLayoutManager);
recyclerView.addItemDecoration(new BankTransection.GridSpacingItemDecoration(1, dpToPx(10), true));
recyclerView.setItemAnimator(new DefaultItemAnimator());
recyclerView.setAdapter(adapter);
recyclerView.addOnScrollListener(new RecyclerView.OnScrollListener() {
@Override
public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
//super.onScrolled(recyclerView, dx, dy);
if(mLayoutManager.findLastCompletelyVisibleItemPosition() == transactionList.size()-1){
GetTransactionlist(transactionList.get(transactionList.size()-1).getId());
}
}
});
GetTransactionlist(0);
}
private void GetTransactionlist(int id){
progressDialog.setMessage("Loading Please Wait...");
showDialog();
AsyncTask<Integer,Void,Void> task = new AsyncTask<Integer, Void, Void>() {
@Override
protected Void doInBackground(Integer... integers) {
OkHttpClient client = new OkHttpClient();
okhttp3.Request request = new okhttp3.Request.Builder()
.url("http://api.prokart.in/gettransaction.php?userid="+userid+"&id="+integers[0])
.build();
try {
Response response = client.newCall(request).execute();
JSONArray array = new JSONArray(response.body().string());
for (int i=0; i<array.length(); i++){
JSONObject object =array.getJSONObject(i);
Log.e("Trandata", String.valueOf(object));
int id =object.has("id")?object.getInt("id"):0;
String userid = object.has("userid")?object.getString("userid"):"";
String cdate = object.has("cdate")?object.getString("cdate"):"";
int credit = object.has("credit")?object.getInt("credit"):0;
int debit = object.has("debit")?object.getInt("debit"):0;
int balance =object.has("balance")?object.getInt("balance"):0;
String tremark = object.has("tremark")?object.getString("tremark"):"";
Log.e("TranResponse", String.valueOf(object));
Transactions transactions = new Transactions(id,userid,cdate,credit,debit,balance,tremark);
transactionList.add(transactions);
}
}catch (IOException e){
e.printStackTrace();
} catch (JSONException e) {
System.out.println("End of Members");
}
return null;
}
@Override
protected void onPostExecute(Void aVoid) {
hideDialog();
adapter.notifyDataSetChanged();
}
};
task.execute(id);
}
public class GridSpacingItemDecoration extends RecyclerView.ItemDecoration {
private int spanCount;
private int spacing;
private boolean includeEdge;
public GridSpacingItemDecoration(int spanCount, int spacing, boolean includeEdge) {
this.spanCount = spanCount;
this.spacing = spacing;
this.includeEdge = includeEdge;
}
@Override
public void getItemOffsets(Rect outRect, View view, RecyclerView parent, RecyclerView.State state) {
int position = parent.getChildAdapterPosition(view); // item position
int column = position % spanCount; // item column
if (includeEdge) {
outRect.left = spacing - column * spacing / spanCount; // spacing - column * ((1f / spanCount) * spacing)
outRect.right = (column + 1) * spacing / spanCount; // (column + 1) * ((1f / spanCount) * spacing)
if (position < spanCount) { // top edge
outRect.top = spacing;
}
outRect.bottom = spacing; // item bottom
} else {
outRect.left = column * spacing / spanCount; // column * ((1f / spanCount) * spacing)
outRect.right = spacing - (column + 1) * spacing / spanCount; // spacing - (column + 1) * ((1f / spanCount) * spacing)
if (position >= spanCount) {
outRect.top = spacing; // item top
}
}
}
}
private void showDialog() {
if (!progressDialog.isShowing())
progressDialog.show();
}
private void hideDialog() {
if (progressDialog.isShowing())
progressDialog.dismiss();
}
/**
* Converting dp to pixel
*/
private int dpToPx(int dp) {
Resources r = getResources();
return Math.round(TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, dp, r.getDisplayMetrics()));
}
在我的PHP代码中
$id = $_GET["id"];
$userid = $_GET["userid"];
$arraylist =array();
$sql=$dbh->prepare("SELECT * FROM commission WHERE userid=? AND id > '$id' ORDER BY id DESC LIMIT 20");
$sql->bindValue(1,$userid);
$sql->execute();
if ($sql->rowCount() >0) {
while($row = $sql->fetch(PDO::FETCH_ASSOC)) {
$catagory["userid"] = $row["userid"];
$catagory["id"] = $row["id"];
$catagory["cdate"] = $row['cdate'];
$catagory["credit"] = $row['credit'];
$catagory["debit"] = $row['debit'];
$catagory["balance"] = $row['balance'];
$catagory["tremark"] = $row['tremark'];
$arraylist[] = $catagory;
header('Content-Type:Application/json');
$json = json_encode($arraylist);
}
} else {
echo "0 results";
}
echo $json;
我如何在向下滚动时加载更多数据,并且应按降序排列。 请建议我还有什么更好的方法。