簡體   English   中英

Android從中創建自定義RecyclerView.Adapter並創建其他類

[英]Android Create a Custom RecyclerView.Adapter and Create Other Class from it

好吧,我有一個RecyclerView,我從WebService填充,我有一個DataProvider類來管理異步請求。 因此,當獲取數據時,我需要此DataProvider通知RecyclerView.Adapter有新數據。 為此,我需要向RecyclerView.Adapter添加一個允許此通信的方法。 但是當我用這個新方法創建一個基類(它擴展了RecyclerView.Adapter),然后創建自定義適配器時,它不會讓我覆蓋RecyclerView.Adapter方法。 我究竟做錯了什么?

這是基類

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的自定義適配器

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
}

}

我想這與類型參數有關,但我無法弄清楚發生了什么

看看Pascal Welsch這個要點:

當我第一次開始在ListView使用RecyclerView時,這對我有所幫助。

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

謝謝Jared這對我幫助很大

所以最后BaseRecyclerAdapter看起來像這樣:

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

public static class VH extends RecyclerView.ViewHolder{

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

public BaseRecyclerAdapter(RecyclerView rv){

}

public void setDataSet( String data) {
   //this is the special method added
}
}

現在,您可以從RecyclerView.Adapter覆蓋您喜歡的任何方法,並實現特殊方法

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM