使用CustomAdapter和RecyclerView进行改造的API更新的最佳实践

时间:2015-03-23 13:26:49

标签: android retrofit android-recyclerview

我正在使用retrofit通过将其传递到片段中的自定义适配器来从RecyclerView的API获取数据。在适配器中,我有一个视图的内部类,并为RecyclerView项实现了单击事件。

我想知道的是,通过改进API以获取列表中项目的更新,进行进一步调用的正确方法是什么?是"最佳实践"在自定义适配器中进行调用,或者从片段或活动中进行调用。

以下是我的自定义适配器的基础知识,请记住,它并不包含我认为对此问题不是必需的所有内容。

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

1 个答案:

答案 0 :(得分:0)

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