Android 创建自定义 RecyclerView.Adapter 并从中创建其他 Class
Android Create a Custom RecyclerView.Adapter and Create Other Class from it
各位,我有一个从 WebService 填充的 RecyclerView,还有一个管理异步请求的 DataProvider class。因此,当获取数据时,我需要此 DataProvider 通知 RecyclerView.Adapter 有新数据。为此,我需要向 RecyclerView.Adapter 添加一个允许此通信的方法。但是当我创建一个基础class(扩展RecyclerView.Adapter)并在其中创建自定义适配器时,它不会让我重写RecyclerView.Adapter 方法。我做错了什么?
这是基础class
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
的要点:
当我第一次开始使用 RecyclerView
而不是 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());
}
}
谢谢 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 中您喜欢的任何方法并实现特殊方法
各位,我有一个从 WebService 填充的 RecyclerView,还有一个管理异步请求的 DataProvider class。因此,当获取数据时,我需要此 DataProvider 通知 RecyclerView.Adapter 有新数据。为此,我需要向 RecyclerView.Adapter 添加一个允许此通信的方法。但是当我创建一个基础class(扩展RecyclerView.Adapter)并在其中创建自定义适配器时,它不会让我重写RecyclerView.Adapter 方法。我做错了什么?
这是基础class
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
的要点:
当我第一次开始使用 RecyclerView
而不是 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());
}
}
谢谢 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 中您喜欢的任何方法并实现特殊方法