首页 > 解决方案 > 以相反的顺序刷新后,我需要在 RecyclerView 顶部显示数据

问题描述

回收视图加载列表并由 mList.add(0,actor) 到达顶部,但我想反转列表。

我也像这样更改了我的代码,

   mLayoutManager.setReverseLayout(true);
   mList.add(0,actor);

这是我的 recycleview 信息,这是我的 recyclview api 代码 InfoJson():

public class GatekeeperInfoActivity  extends BaseActivity implements View.OnClickListener , MyComplaintListner  {
private RecyclerView mRecyclerView;
private GateInfoAdapter mAdapter;
private RecyclerView.LayoutManager mLayoutManager;
private List<GateInfoPojo> mList;
Context ctx;
String visitor_id,id,cid;
M_Shared_Pref m_shared_pref;
ImageView back_button, img;
private MyDialog dialog;
TextView complaint,subject,msg,from_name_txt;
ImageView attach;
String from_id,from_name,from_mobile;
String id_rwa,new_id;
public static final int DISMISS_TIMEOUT = 2000;
LinearLayout ll_no_data;

int limit =0;
int limit_refresh;
static int nums;
private appconfig.EndlessRecyclerOnScrollListener scrollListener;
LinearLayoutManager linearLayoutManager;
public int overallXScrol = 0;
Button button;


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.rwa_view_info);
    ctx = GatekeeperInfoActivity.this;

    dialog = new MyDialog(this);
    m_shared_pref = new M_Shared_Pref(GatekeeperInfoActivity.this);
    visitor_id = m_shared_pref.getPrefranceStringValue(App_Info.Visitor_ID);
    id = m_shared_pref.getPrefranceStringValue(App_Info.Flat_User_Id);
    cid = m_shared_pref.getPrefranceStringValue(App_Info.Flat_User_Id_cid);

    Bundle bundle = this.getIntent().getExtras();
    from_id = bundle.getString("id");
    from_name = bundle.getString("name");
    from_mobile = bundle.getString("mobile");



    id_rwa = getIntent().getStringExtra("id_rwa");

    if (from_id != null && !from_id.isEmpty() && !from_id.equals("null"))
    { new_id = from_id; }

    else{ new_id = id_rwa; }
    System.out.println("check:check"+from_id+":"+id_rwa);

    ///// using this   new_id

    mList = new ArrayList<GateInfoPojo>();
    mRecyclerView = findViewById(R.id.my_recycler_view);

    mRecyclerView.setHasFixedSize(true);
    mLayoutManager = new LinearLayoutManager(this);
    mRecyclerView.setLayoutManager(new LinearLayoutManager(this));
    mAdapter = new GateInfoAdapter(mList, ctx);


    ll_no_data = findViewById(R.id.ll_no_data);


    mAdapter.setMyClickListener(this);



  /*  button.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            limit_refresh = nums+limit;
            limit = limit_refresh;
            if(nums>=20){
                //  item_progress_bar.setVisibility(View.VISIBLE);
                InfoJson();
            }
        }
    });*/



    mRecyclerView.addOnScrollListener(new RecyclerView.OnScrollListener() {
        @Override
        public void onScrollStateChanged(RecyclerView recyclerView, int newState) {
            super.onScrollStateChanged(recyclerView, newState);
            if (!recyclerView.canScrollVertically(-1)) {
                //  Toast.makeText(GatekeeperInfoActivity.this, "Last", Toast.LENGTH_LONG).show();

                limit_refresh = nums+limit;
                limit = limit_refresh;
                if(nums>=20){
                    //  item_progress_bar.setVisibility(View.VISIBLE);
                    InfoJson();
                }
            }
        }
    });


    img = findViewById(R.id.pic);
    from_name_txt = findViewById(R.id.from_name_txt);

    attach = findViewById(R.id.attach);
    msg = findViewById(R.id.msg);
    complaint = findViewById(R.id.complaint);
    subject = findViewById(R.id.subject);

    back_button = findViewById(R.id.back_button);
    back_button.setOnClickListener(this);
    attach.setOnClickListener(this);







    if(NetWorkCheck.checkConnection(GatekeeperInfoActivity.this)){
        mList.clear();
        InfoJson();
    }
    else{
        TastyToast.makeText(getApplicationContext(), "Internet connection is disable", TastyToast.LENGTH_LONG, TastyToast.WARNING);
    }

}



@Override
public void onClick(View v) {
    switch (v.getId()){
        case R.id.back_button:
            finish();
            break;


        case R.id.attach:
            if(msg.getText().toString().length()>0){
                submitReply(msg.getText().toString(),new_id);
            }
            else{
                TastyToast.makeText(getApplicationContext(), "Enter Message", TastyToast.LENGTH_LONG, TastyToast.INFO);

            }
            break;
    }
}


public void InfoJson() {
    dialog.ShowProgressDialog();
    RequestQueue requestQueue = Volley.newRequestQueue(this);
    StringRequest stringRequest = new StringRequest(Request.Method.POST, Constant.Base_Url+"AllChatWithUser.php?", new Response.Listener<String>() {
        @Override
        public void onResponse(String response) {
            try {
                dialog.CancelProgressDialog();

                JSONObject obj = new JSONObject(response);
                String error = obj.getString("error");


                nums = obj.getInt("nums");
                System.out.println("limit_limit_num"+nums);
                if(nums>=1) {
                    if (error.equals("true")) {
                        from_name_txt.setText("GateKeeper : " + obj.getString("names"));
                        JSONArray tower = obj.getJSONArray("Flat");

                        for (int i = 0; i < tower.length(); i++) {
                            JSONObject jsonnew = tower.getJSONObject(i);

                            GateInfoPojo actor = new GateInfoPojo();
                            String id = jsonnew.getString("id");
                            String Reply_From = jsonnew.getString("reply_from");
                            String message = jsonnew.getString("message");
                            String reply_date = jsonnew.getString("send_time");
                            String usertype = jsonnew.getString("usertype");

                            actor.setId(id);
                            actor.setReply_from(Reply_From);
                            actor.setMessage(message);
                            actor.setSend_time(reply_date);
                            actor.setUsertype(usertype);

                            if(limit==0){
                                mList.add(actor);
                            }
                            else{
                                mList.add(0,actor);
                            }
                            mAdapter.notifyDataSetChanged();
                            mRecyclerView.setAdapter(mAdapter);
                            mRecyclerView.getLayoutManager().scrollToPosition(nums - 1);
                        }


                    }
                }
                else
                {
                    mRecyclerView.setVisibility(View.GONE);
                    ll_no_data.setVisibility(View.VISIBLE);
                    //  TastyToast.makeText(getApplicationContext(),  obj.getString("msg"), TastyToast.LENGTH_LONG, TastyToast.ERROR);
                }
            } catch (JSONException e) {
                e.printStackTrace();
            }
        }
    }, new Response.ErrorListener() {
        @Override
        public void onErrorResponse(VolleyError error) {
            VolleyLog.d(TAG, "Error: " + error.getMessage());
            dialog.CancelProgressDialog();
        }
    }) {
        @Override
        protected Map<String, String> getParams() {
            Map<String, String> params = new HashMap<>();
            params.put("empid", new_id);
            params.put("fid", id);
            params.put("usertype", "FlatUser");
            params.put("limit", String.valueOf(limit));
            Log.e("params", String.valueOf(params));
            return params;
        }
    };

    requestQueue.add(stringRequest);
}

@Override
public void onItemClick(View v, Object bean, String feed, String rating,String comp_spinner) {
    String complaint_id = ((GateInfoPojo) bean).getId();
    // submitComplaintInfo(complaint_id,feed,rating,comp_spinner);
}

@Override
public void onItemClickActivity(View v, Object bean) {
    String complaint_id = ((GateInfoPojo) bean).getId();
}

public void submitReply(final String msg1,final String new_id) {
    dialog.ShowProgressDialog();
    RequestQueue requestQueue = Volley.newRequestQueue(this);
    StringRequest stringRequest = new StringRequest(Request.Method.POST, Constant.Base_Url+"ChatWithUser.php?", new Response.Listener<String>() {
        @Override
        public void onResponse(String response) {
            try {
                dialog.CancelProgressDialog();
                JSONObject obj = new JSONObject(response);
                String error = obj.getString("error");

                if (error.equals("true"))
                {
                    //  TastyToast.makeText(getApplicationContext(),  obj.getString("msg"), TastyToast.LENGTH_LONG, TastyToast.SUCCESS);
                    msg.setText("");
                    InfoJson();
                    mList.clear();
                }
                else
                {
                    TastyToast.makeText(getApplicationContext(),  obj.getString("msg"), TastyToast.LENGTH_LONG, TastyToast.ERROR);
                }
            } catch (JSONException e) {
                e.printStackTrace();
            }
        }
    }, new Response.ErrorListener() {
        @Override
        public void onErrorResponse(VolleyError error) {
            VolleyLog.d(TAG, "Error: " + error.getMessage());
            dialog.CancelProgressDialog();
        }
    }) {
        @Override
        protected Map<String, String> getParams() {
            Map<String, String> params = new HashMap<>();
            params.put("fid", id);
            params.put("empid", new_id);
            params.put("usertype", "FlatUser");
            params.put("message", msg1);
            Log.e("params", String.valueOf(params));
            return params;
        }
    };

    requestQueue.add(stringRequest);
}

}

但这不起作用,它添加到顶部,但不能将 api 数据从底部反转到顶部。任何帮助,将不胜感激。

标签: androidandroid-recyclerview

解决方案


尝试添加以下代码,它们都需要为真。

mLayoutManager.setStackFromEnd(true);
mLayoutManager.setReverseLayout(true);

推荐阅读