对于许多Recyclerviews,我需要许多适配器吗?

时间:2015-10-02 13:36:33

标签: android adapter android-recyclerview android-viewholder

我正在学习如何正确开发Android应用程序。现在我正在开发一个应用程序,该应用程序应显示具有不同Recyclerview的不同碎片。到目前为止,我只有一个带有单个适配器的Recyclerview。在适配器中,Recyclerview中的Textviews将通过ViewHolder进行分配。

现在我的问题:这是如何工作的?我是否需要一个新的适配器用于不同的Recyclerview,或者是否可以填充"来自一个SINGLE适配器的许多Recyclerviews的TextViews?

2 个答案:

答案 0 :(得分:4)

如果所有ListViews或RecyclerViews使用相同的Textview和ImageViews等显示相同的数据,那么您可以重复使用您的适配器。

但是,如果他们不以完全相同的方式使用数据,您应该使用自己的功能创建一个新的适配器。

由于您有多个列表显示完全相同的数据,因此很有可能,通常每个ListView或RecyclerView都有自己的专用适配器。

你可以这样做,以便适配器检查它属于什么(哪个ListView或RecyclerView)并使用取决于该检查的数据,但这是令人讨厌的并与swiftmailer冲突。

答案 1 :(得分:2)

您可以使用超类适配器,并将其扩展以用于不同的情况。这样就可以编写更少的代码并重用超类中的东西。

以下是我的项目中的一个示例(具有过滤器可能性的超级抽象RecyclerViewAdapter):

public abstract class GenericRecycleAdapter<T, K extends Holders.TextImageHolder> extends RecyclerView.Adapter implements Filterable {
    private final Context context;
    protected List<T> mList = new ArrayList<T>() {
    };
    private List<T> originalData = null;
    private Action1<List> task;
    private ItemFilter mFilter = new ItemFilter();
    private int lastPosition;

    public GenericRecycleAdapter(List<T> list, Context context) {
        this.context = context;
        if (list != null) {
            this.mList = list;
        }

        this.originalData = list;
    }


    public void refresh(List<T> list) {
        if (list != null) {
            this.mList = list;
        }
        this.originalData = list;
        notifyDataSetChanged();
    }

    public T getItem(int position) {
        return mList.get(position);
    }

    public Context getContext() {
        return context;
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View v = LayoutInflater.from(parent.getContext())
                .inflate(getLayout(), parent, false);
        return getCustomHolder(v);
    }

    public Holders.TextImageHolder getCustomHolder(View v) {
        return new Holders.TextImageHolder(v) {
            @Override
            public void onClick(View v) {
                onItem(mList.get(this.getAdapterPosition()));
                onPosition(this.getAdapterPosition());
            }
        };
    }

    protected void onPosition(int position) {

    }

    protected abstract void onItem(T t);

    public abstract int getLayout();

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
        onSet(mList.get(position), (K) holder);
        onSetPosition((K) holder, position);
        setAnimation(((K) holder).getContainer(), position);
    }

    protected void onSetPosition(K holder, int position) {

    }

    public abstract void onSet(T item, K holder);

    @Override
    public int getItemCount() {
        return mList.size();
    }

    public void addObserverToFilter(Action1<List> task) {
        this.task = task;
    }

    @Override
    public Filter getFilter() {
        return mFilter;
    }

    private class ItemFilter extends Filter {
        @Override
        protected FilterResults performFiltering(CharSequence constraint) {

            String filterString = constraint.toString().toLowerCase();

            FilterResults results = new FilterResults();

            final List<T> list = originalData;

            int count = list.size();
            final ArrayList<T> nlist = new ArrayList<>(count);

            String filterableString;

            for (int i = 0; i < count; i++) {
                filterableString = list.get(i).toString();
                if (filterImplementation(filterString, filterableString.toLowerCase())) {
                    nlist.add(list.get(i));
                }
            }

            results.values = nlist;
            results.count = nlist.size();

            return results;
        }

        @SuppressWarnings("unchecked")
        @Override
        protected void publishResults(CharSequence constraint, FilterResults results) {
            mList = (ArrayList<T>) results.values;
            notifyDataSetChanged();
            if (task != null) {
                task.call(mList);
            }

        }

    }

    public List<T> getFilteredList() {
        return mList;
    }

    public List<T> getOriginalList() {
        return originalData;
    }

    protected boolean filterImplementation(String key, String door) {
        return door.startsWith(key);
    }

    protected void setAnimation(View viewToAnimate, int position)

    {
        if (viewToAnimate == null) {
            return;
        }
        if (position > lastPosition) {
            Animation animation = AnimationUtils.loadAnimation(context, android.R.anim.slide_in_left);
            viewToAnimate.startAnimation(animation);
            lastPosition = position;
        }

    }
}

和处理程序:

public class Holders {

    public static class TextImageHolder extends RecyclerView.ViewHolder implements View.OnClickListener {


        private final View container;

        public TextImageHolder(View itemView) {
            super(itemView);
            container = itemView.findViewById(R.id.container); 
            //add other view
            container.setOnClickListener(this);

        }



        @Override
        public void onClick(View v) {

        }
    }


}

另一个视图的用法示例:

public class ChildAdapter extends GenericRecycleAdapter<String,Holders.TextImageHolder> {


    public ChildAdapter (List<String> list, Context context) {
        super(list, context);
    }

    @Override
    protected void onItem(String s) {
//triggers when container is clicked
    }

    @Override
    public int getLayout() {
        return R.layout.some_row; //the id's here should be the same as in the Handler
    }

    @Override
    public void onSet(String item, Holders.TextImageHolder holder) {
        holder.text.setText(item);
    }

}