Android从中创建自定义RecyclerView.Adapter并创建其他类 [英] Android Create a Custom RecyclerView.Adapter and Create Other Class from it

查看:162
本文介绍了Android从中创建自定义RecyclerView.Adapter并创建其他类的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

好的人我有一个我从WebService填充的RecyclerView,我有一个DataProvider类来管理异步请求。因此,当获取数据时,我需要此DataProvider通知RecyclerView.Adapter有新数据。为此,我需要向RecyclerView.Adapter添加一个允许此通信的方法。但是当我用这个新方法创建一个基类(扩展了RecyclerView.Adapter),然后创建自定义适配器时,它不会让我覆盖RecyclerView.Adapter方法。我做错了什么?

Well people I have a RecyclerView which I populate from a WebService, and I have a DataProvider class that manage the async requests. So when the data is fetched I need this DataProvider to inform the RecyclerView.Adapter that there is new data. For this I need to add a method to RecyclerView.Adapter that allow this communication. But When I create a base class (which extends RecyclerView.Adapter) with this new method in it, and then create the custom adapter it won't let me Override RecyclerView.Adapter methods. What am I doing wrong?

这是基类

public abstract class BaseRecyclerAdapter extends RecyclerView.Adapter<BaseRecyclerAdapter.ViewHolder>{

public static class ViewHolder extends RecyclerView.ViewHolder{

    public ViewHolder (View v){
        super(v);
    }
}

public BaseRecyclerAdapter(RecyclerView rv){

}

public void setDataSet( String data) {
   //This is the method i need to add
}

}

这是扩展BaseRecyclerAdapter的自定义适配器

And this is the custom adapter which extends BaseRecyclerAdapter

public class PlacesAdapter extends BaseRecyclerAdapter<PlacesAdapter.ViewHolder> {

public static class ViewHolder extends RecyclerView.ViewHolder{
    public TextView mTextView;
    public ViewHolder(View v){
        super(v);
        mTextView= (TextView) v.findViewById(R.id.info_text);
    }
}

public PlacesAdapter(RecyclerView recyclerView){
    super(recyclerView);
}
@Override
public int getItemCount() {
    return 0;
}

@Override
public void setDataSet( String data) {

}

@Override
public void onBindViewHolder(ViewHolder holder, int position) {
    //This says Method does not override method from its suplerclass
}

}

我想这与类型参数有关,但我无法弄清楚发生了什么

I guess it's something to do with type parameters, but I can't figure out what is going on

推荐答案

看看这个要点Pascal Welsch



<当我第一次在 ListView 上使用 RecyclerView 时,这对我有所帮助。

Check out this Gist from Pascal Welsch:

This helped me when I first started using the RecyclerView over the ListView.

import android.support.v7.widget.RecyclerView;

import java.util.Collections;
import java.util.Comparator;
import java.util.List;

/**
 * Created by pascalwelsch on 04.07.14.
 */
public abstract class ArrayAdapter<T, VH extends RecyclerView.ViewHolder>
        extends RecyclerView.Adapter<VH> {

    private List<T> mObjects;

    public ArrayAdapter(final List<T> objects) {
        mObjects = objects;
    }

    /**
     * Adds the specified object at the end of the array.
     *
     * @param object The object to add at the end of the array.
     */
    public void add(final T object) {
        mObjects.add(object);
        notifyItemInserted(getItemCount() - 1);
    }

    /**
     * Remove all elements from the list.
     */
    public void clear() {
        final int size = getItemCount();
        mObjects.clear();
        notifyItemRangeRemoved(0, size);
    }

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

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

    public long getItemId(final int position) {
        return position;
    }

    /**
     * Returns the position of the specified item in the array.
     *
     * @param item The item to retrieve the position of.
     * @return The position of the specified item.
     */
    public int getPosition(final T item) {
        return mObjects.indexOf(item);
    }

    /**
     * Inserts the specified object at the specified index in the array.
     *
     * @param object The object to insert into the array.
     * @param index  The index at which the object must be inserted.
     */
    public void insert(final T object, int index) {
        mObjects.add(index, object);
        notifyItemInserted(index);

    }

    /**
     * Removes the specified object from the array.
     *
     * @param object The object to remove.
     */
    public void remove(T object) {
        final int position = getPosition(object);
        mObjects.remove(object);
        notifyItemRemoved(position);
    }

    /**
     * Sorts the content of this adapter using the specified comparator.
     *
     * @param comparator The comparator used to sort the objects contained in this adapter.
     */
    public void sort(Comparator<? super T> comparator) {
        Collections.sort(mObjects, comparator);
        notifyItemRangeChanged(0, getItemCount());
    }
}

来源: https://gist.github.com/passsy/f8eecc97c37e3de46176

这篇关于Android从中创建自定义RecyclerView.Adapter并创建其他类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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