【问题标题】:Best Practice for API Updates using Retrofit with CustomAdapters for RecyclerView使用 RecyclerView 的 CustomAdapters 进行改造的 API 更新最佳实践
【发布时间】:2015-05-26 12:20:23
【问题描述】:

我正在使用改造从 API 获取 RecyclerView 的数据,方法是将其传递给片段中的自定义适配器。在适配器中,我有一个视图的内部类,并为 RecyclerView 项目实现了单击事件。

我想知道的是,使用改进 API 以更新列表中的项目等进行进一步调用的正确方法是什么?是在自定义适配器中进行调用还是进行调用是“最佳实践”从其所在的 Fragment 或 Activity 中。

以下是我的自定义适配器的基础知识,请记住,其中并没有我认为对问题没有必要的所有内容。

public class MyAdapter extends RecyclerView.Adapter<MyAdapter.MyViewHolder> {
    private static List<ListItem> list = Collections.emptyList();
    private LayoutInflater mInflater;

    public MyAdapter(Context context, List<ListItem> list) {
        mInflater = LayoutInflater.from(context);
        this.list = list;
    }

    // common stuff omitted for brevity

    // bind view holder
    public void onBindViewHolder(MyViewHolder holder, int position) {
        ListItem list = this.list.get(position);

        // setup the click handlers
        holder.setClickListener(new MyViewHolder.ClickListener() {
            @Override
            public void onClick(View v, final int pos, boolean isLongClick) {

                if(v == v.findViewById(R.id.dream_button_suggestion)) {
                    if (isLongClick) {
                        // View v at position pos is long-clicked.
                        Toast.makeText(context, "Long clicked at position: " + pos, Toast.LENGTH_SHORT).show();
                    } else {
                        // View v at position pos is clicked.
                        Toast.makeText(context, "clicked at position: " + pos, Toast.LENGTH_SHORT).show();

                        /**
                        *
                        *    HERE is where I would make call to api
                        *
                        */

                    }
                }
            }
        }
    }

    public static class MyViewHolder extends RecyclerView.ViewHolder
        implements View.OnClickListener,
        View.OnLongClickListener {

        // initialize variables here
        TextView text;

        public MyViewHolder(View itemView) {
            super(itemView);

            // set variables to view items
            text = (TextView) itemView.findViewById(R.id.text);
            text.setOnClickListener(this);
        }

        /* Interface for handling clicks - both normal and long ones. */
        public interface ClickListener {
            /**
             * Called when the view is clicked.
             *
             * @param v view that is clicked
             * @param position of the clicked item
             * @param isLongClick true if long click, false otherwise
             */
            public void onClick(View v, int position, boolean isLongClick);
       }

        /* Setter for listener. */
        public void setClickListener(ClickListener clickListener) {
            this.clickListener = clickListener;
        }

        @Override
        public void onClick(View v) {
            // If not long clicked, pass last variable as false.
            clickListener.onClick(v, getPosition(), false);
        }

        @Override
        public boolean onLongClick(View v) {
            // If long clicked, passed last variable as true.
            clickListener.onClick(v, getPosition(), true);
            return true;
        }
    }
}

【问题讨论】:

    标签: android retrofit android-recyclerview


    【解决方案1】:

    适配器可能不是实现这种逻辑的最佳位置,相反,您应该有一种机制,允许您通知您的 Activity/Fragment 列表中的项目已被单击。您可以使用自定义接口(由 Activity/Fragment 实现)或使用事件总线实现来实现 - 有多种方法可以实现。

    【讨论】:

    • 对此是否有最佳实践,或者只是偏好问题?您将如何从片段中更新列表项对象?
    猜你喜欢
    • 2019-12-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-02-24
    • 1970-01-01
    • 2017-08-07
    相关资源
    最近更新 更多