FastAdapter 位置 [英] FastAdapter Position

查看:14
本文介绍了FastAdapter 位置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 mike penz 的快速 adapter,我试图从 adapter 中删除该项目.就像当我按下接受按钮时,必须从列表中删除该项目,但要删除它,我需要在其中传递位置变量,但我无法从 adapter.

I am using fast adapter of the mike penz where I am trying to remove the item from the adapter. Its like when I press the accept button then the item must be removed from the list but to remove the like I need to pass the position variable in it but I am not able to get the position variable from the adapter.

MainActivity.class

MainActivity.class

public class Doc_Appointment_Req extends BaseActivity {
    @BindView(R.id.doc_app_req_recyclerview)
    RecyclerView doc_app_req_recyclerview;
    FastItemAdapter<Doc_Appointment_Req_Adapter> doctorreqappadapter;



    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.doc_appointment_req);
        ButterKnife.bind(this);
        assert getSupportActionBar() != null;
        getSupportActionBar().setTitle("Appointments Requests");
        getSupportActionBar().setDisplayShowHomeEnabled(true);
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);
        doctorreqappadapter = new FastItemAdapter<>();
        LinearLayoutManager departmentmanager = new LinearLayoutManager(Doc_Appointment_Req.this, LinearLayoutManager.VERTICAL, false);
        doc_app_req_recyclerview.setLayoutManager(departmentmanager);
        doc_app_req_recyclerview.setAdapter(doctorreqappadapter);
        appointmentreqmethod();
    }

    private void appointmentreqmethod() {
        Display.openloader(Doc_Appointment_Req.this);
        final String docappointmentreq = Constant.url + "getmyappointment&dr_id=" + Session.getUserID(getApplicationContext());
        Display.log(docappointmentreq);
        JsonObjectRequest docappointrequest = new JsonObjectRequest(Request.Method.GET, docappointmentreq, null, new Response.Listener<JSONObject>() {
            @Override
            public void onResponse(JSONObject response) {
                try {
                    JSONArray docarray = response.getJSONArray("data");
                    for (int i = 0; i < docarray.length(); i++) {
                        JSONObject docreqappobj = docarray.getJSONObject(i);
                        Doc_Appointment_Req_Adapter docapplistreq = new Doc_Appointment_Req_Adapter(getApplicationContext());
                        docapplistreq.setPatient_name(docreqappobj.getString("patient_name"));
                        docapplistreq.setApp_date(docreqappobj.getString("app_date"));
                        docapplistreq.setTime(docreqappobj.getString("time").replace("$", " "));
                        docapplistreq.setAge(docreqappobj.getString("age"));
                        docapplistreq.setDescription(docreqappobj.getString("description"));
                        docapplistreq.setGender(docreqappobj.getString("gender"));
                        docapplistreq.setType(docreqappobj.getString("type"));
                        docapplistreq.setId(docreqappobj.getString("id"));
                        docapplistreq.setUser_id(docreqappobj.getString("user_id"));
                        doctorreqappadapter.add(docapplistreq);
                    }
                } catch (JSONException e) {
                    e.printStackTrace();
                }
                doctorreqappadapter.notifyDataSetChanged();
                Display.dismissloader();

            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {
                Display.log(error.toString());
            }
        });
        docappointrequest.setRetryPolicy(new DefaultRetryPolicy(500000, DefaultRetryPolicy.DEFAULT_MAX_RETRIES,
                DefaultRetryPolicy.DEFAULT_BACKOFF_MULT));
        AppController.getInstance().addToRequestQueue(docappointrequest);
    }


}

适配器.class

public class Doc_Appointment_Req_Adapter extends AbstractItem<Doc_Appointment_Req_Adapter, Doc_Appointment_Req_Adapter.Doc_App_req_Vh> {

        private String patient_name;
        private String time;
        private String app_date;
        private String gender;
        private String age;
        private String description;
        private String type;
        private String user_id;
        private String id;
        Context context;
        FastItemAdapter<Doc_Appointment_Req_Adapter> docfastitemadapter;

        public String getUser_id() {
            return user_id;
        }

        public void setUser_id(String user_id) {
            this.user_id = user_id;
        }

        public String getPatient_name() {
            return patient_name;
        }

        public void setPatient_name(String patient_name) {
            this.patient_name = patient_name;
        }

        public String getTime() {
            return time;
        }

        public void setTime(String time) {
            this.time = time;
        }

        public String getApp_date() {
            return app_date;
        }

        public void setApp_date(String app_date) {
            this.app_date = app_date;
        }

        public String getGender() {
            return gender;
        }

        public void setGender(String gender) {
            this.gender = gender;
        }

        public String getAge() {
            return age;
        }

        public void setAge(String age) {
            this.age = age;
        }

        public String getDescription() {
            return description;
        }

        public void setDescription(String description) {
            this.description = description;
        }

        public void setType(String type) {
            this.type = type;
        }

        public Doc_Appointment_Req_Adapter(Context context) {
            this.context = context;
        }

        public String getId() {
            return id;
        }

        public void setId(String id) {
            this.id = id;
        }

        @Override
        public int getType() {
            return R.id.doc_app_req_recyclerview;
        }

        @Override
        public int getLayoutRes() {
            return R.layout.doc_appointment_req_row;
        }

        @Override
        public void bindView(Doc_App_req_Vh holder, List payloads) {
            super.bindView(holder, payloads);
            holder.pat_name.setText(patient_name);
            holder.pat_problem.setText(description);
            holder.pat_age.setText(age);
            holder.pat_gender.setText(gender);
            holder.pat_you.setText(type);
            holder.date.setText(app_date);
            holder.timeslot.setText(time);
            holder.accept.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
    payloads.remove(here i need the position);
                }
            });
            holder.reject.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
    payloads.remove(here i need the position);

                }
            });
            holder.reschedule.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
    payloads.remove(here i need the position);
                }
            });

        }


        public static class Doc_App_req_Vh extends RecyclerView.ViewHolder {
            @BindView(R.id.pat_name)
            TextView pat_name;
            @BindView(R.id.pat_problem)
            TextView pat_problem;
            @BindView(R.id.pat_age)
            TextView pat_age;
            @BindView(R.id.pat_gender)
            TextView pat_gender;
            @BindView(R.id.pat_you)
            TextView pat_you;
            @BindView(R.id.date)
            TextView date;
            @BindView(R.id.timeslot)
            TextView timeslot;
            @BindView(R.id.accept)
            Button accept;
            @BindView(R.id.reject)
            Button reject;
            @BindView(R.id.reschedule)
            Button reschedule;

            public Doc_App_req_Vh(View itemView) {
                super(itemView);
                ButterKnife.bind(this, itemView);
            }
        }

        private void appointmentaccept(String actionpass) {
            String accepturl = Constant.url + actionpass + "&user_id=" + user_id + "&dr_id=" + Session.getUserID(context) + "&app_id=" + id;
            Display.log(accepturl);
            JsonObjectRequest acceptrequest = new JsonObjectRequest(Request.Method.GET, accepturl, null, new Response.Listener<JSONObject>() {
                @Override
                public void onResponse(JSONObject response) {
                    try {
                        if (response.getString("responce").equals("success")) {
                            Display.toast(context, response.getString("data"));
                        }
                    } catch (JSONException e) {
                        e.printStackTrace();
                    }

                }
            }, new Response.ErrorListener() {
                @Override
                public void onErrorResponse(VolleyError error) {
                    Display.log(error.toString());
                }
            });
            acceptrequest.setRetryPolicy(new DefaultRetryPolicy(500000, DefaultRetryPolicy.DEFAULT_MAX_RETRIES,
                    DefaultRetryPolicy.DEFAULT_BACKOFF_MULT));
            AppController.getInstance().addToRequestQueue(acceptrequest);


        }
    }

推荐答案

您不应在 ViewHolder 中设置点击侦听器,因为您将无法访问位置或任何内容.请使用 ClickListenerHelper示例.

You should not set click listeners within the ViewHolder as you would not be able to access position or anything. Please use the ClickListenerHelper as shown in the sample.

//init the ClickListenerHelper which simplifies custom click listeners on views of the Adapter
mClickListenerHelper = new ClickListenerHelper<>(fastItemAdapter);

fastItemAdapter.withOnCreateViewHolderListener(new FastAdapter.OnCreateViewHolderListener() {
    @Override
    public RecyclerView.ViewHolder onPreCreateViewHolder(ViewGroup parent, int viewType) {
        return fastItemAdapter.getTypeInstance(viewType).getViewHolder(parent);
    }

    @Override
    public RecyclerView.ViewHolder onPostCreateViewHolder(final RecyclerView.ViewHolder viewHolder) {
        mClickListenerHelper.listen(viewHolder, ((Doc_Appointment_Req_Adapter.Doc_App_req_Vh) viewHolder). accept, new ClickListenerHelper.OnClickListener<Doc_Appointment_Req_Adapter>() {
            @Override
            public void onClick(View v, int position, Doc_Appointment_Req_Adapter item) {
                ...
            }
        });
        return viewHolder;
    }
});

基本上,这会在创建 ViewHolder 时向视图添加一个 listener.并为您返回项目、位置及其视图.

Basically, this will add a listener to the view when the ViewHolder is created. And returns the item, the position, and its view for you.

这篇关于FastAdapter 位置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆