android - 快速适配器位置

标签 android adapter fastadapter

我正在使用 mike penz 的快速适配器,我试图从适配器中删除该项目。就像当我按下接受按钮时,必须从列表中删除该项目,但要删除类似的内容,我需要在其中传递位置变量,但我无法从适配器获取位置变量>.

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);
    }


}

适配器类

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,如 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 时向 View 添加一个监听器。并为您返回项目、位置及其 View 。

关于android - 快速适配器位置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39832543/

相关文章:

android - MikePenz FastAdapter 用法

android - 阻止通知 PendingIntent 启动 Activity 已经启动?

android - 组合 3 个字符串数组列表

android - 隐藏的 IFrame 禁止在 Android 浏览器上滚动

android - 从 Adapter + ListView 打开时出现对话框错误

android - GridView在android中的可扩展列表中

java - 显示 fragment 对话框时如何使应用程序全屏显示

android - 如何创建用户界面,如所附照片

android - 如何将两个适配器设置为一个 RecyclerView?

java - 无法编写基于每个项目保存的长期过滤掉 RecyclerView 中的项目的算法