gpt4 book ai didi

java - 什么是 RecyclerView.Adapter 以及它与 Android 中的 RecyclerView.Adapter 有何不同?

转载 作者:行者123 更新时间:2023-12-02 09:04:35 27 4
gpt4 key购买 nike

我正在学习 RecyclerView 并在 developer 中的站点 Adapter 类扩展 RecyclerView.Adapter<MyAdapter.MyViewHolder> 。实现显示:

public class MyAdapter extends RecyclerView.Adapter<MyAdapter.MyViewHolder> {
private String[] mDataset;


public static class MyViewHolder extends RecyclerView.ViewHolder {

public TextView textView;
public MyViewHolder(TextView v) {
super(v);
textView = v;
}
}

public MyAdapter(String[] myDataset) {
mDataset = myDataset;
}

@Override
public MyAdapter.MyViewHolder onCreateViewHolder(ViewGroup parent,
int viewType) {

TextView v = (TextView) LayoutInflater.from(parent.getContext())
.inflate(R.layout.my_text_view, parent, false);

MyViewHolder vh = new MyViewHolder(v);
return vh;
}

@Override
public void onBindViewHolder(MyViewHolder holder, int position) {

holder.textView.setText(mDataset[position]);

}

@Override
public int getItemCount() {
return mDataset.length;
}
}

在这个tutorial ,适配器类扩展RecyclerView.Adapter 。实现是:

public class SimpleAdapter extends RecyclerView.Adapter {

private List<SimpleViewModel> models = new ArrayList<>();


public SimpleAdapter(final List<SimpleViewModel> viewModels) {
if (viewModels != null) {
this.models.addAll(viewModels);
}
}


@Override
public RecyclerView.ViewHolder onCreateViewHolder(final ViewGroup parent, final int viewType) {
final View view = LayoutInflater.from(parent.getContext()).inflate(viewType, parent, false);
return new SimpleViewHolder(view);
}


@Override
public void onBindViewHolder(final RecyclerView.ViewHolder holder, final int position) {
((SimpleViewHolder) holder).bindData(models.get(position));
}


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


@Override
public int getItemViewType(final int position) {
return R.layout.item_simple_itemview;
}
}

那么 RecyclerView.Adapter<MyAdapter.MyViewHolder> 之间有什么区别?和RecyclerView.Adapter RecyclerView.Adapter<MyAdapter.MyViewHolder> 中的语法 <> 是什么?在这种情况下意味着?我知道它代表泛型。我什么时候应该使用这个?

最佳答案

So what is the difference between RecyclerView.Adapter< MyAdapter.MyViewHolder> and RecyclerView.Adapter and what is the syntax<> in RecyclerView.Adapter means in this case? I know it stands for generics. When should I use this?

首先,我们回顾一下 Adapter 的定义类(class)。它在 RecyclerView 中定义。如下:

public abstract static class Adapter<VH extends ViewHolder>

那么“泛型”部分是什么意思呢?这意味着 Adapter 类对某个类类型 ( VH ) 进行操作,该类类型必须是类 ViewHolder 的后代。 。这被称为 Bounded Type 并确保适配器正在使用的类的类型保证是 ViewHolder 的类型.

正如您在您发布的开发人员网站上的示例中所看到的,当您创建自己的适配器并将 ViewHolder 的类型指定为 MyViewHolder 时, ,那么您必须在适配器中重写的方法在其签名中显式声明该类型。例如,onCreateViewHolder 的返回类型明确设置为 MyViewHolder .

@Override
public MyAdapter.MyViewHolder onCreateViewHolder(ViewGroup parent,
int viewType) {

TextView v = (TextView) LayoutInflater.from(parent.getContext())
.inflate(R.layout.my_text_view, parent, false);

MyViewHolder vh = new MyViewHolder(v);
return vh;
}
<小时/>

或者,您发布的教程没有指定类型。

public class SimpleAdapter extends RecyclerView.Adapter

当您扩展一个需要泛型类型但未指定泛型类型的类时,编译器默认使用最小公分母 - 即继承层次结构中最低的类类型,可以保证该类可以按设计使用。因为Adapter的泛型类型定义为VH extends ViewHolder ,编译器知道类类型必须至少 a ViewHolder并默认为该值。因此,此示例重写的相同方法返回 RecyclerView.ViewHolder而不是MyViewHolder :

@Override
public RecyclerView.ViewHolder onCreateViewHolder(final ViewGroup parent, final int viewType) {
final View view = LayoutInflater.from(parent.getContext()).inflate(viewType, parent, false);
return new SimpleViewHolder(view);
}

如果RecyclerView.Adapter被定义为Adapter<VH> (无限制类型),并且您在不指定类型的情况下扩展了它,类型将默认为 Object (所有Java类的根类):

@Override
public Object onCreateViewHolder(final ViewGroup parent, final int viewType) {
final View view = LayoutInflater.from(parent.getContext()).inflate(viewType, parent, false);
return new SimpleViewHolder(view);
}
<小时/>

那么,最后为什么要使用其中之一而不是另一个呢?一般来说:您应该始终指定泛型类的类型。这保证了类型安全。在教程示例中,您可能会返回错误类型的 View 持有者,该 View 持有者会编译但在运行时崩溃:

@Override
public RecyclerView.ViewHolder onCreateViewHolder(final ViewGroup parent, final int viewType) {
final View view = LayoutInflater.from(parent.getContext()).inflate(viewType, parent, false);

// Returning wrong view holder, but because it still extends ViewHolder and
// that is all this method requires, this compiles
return new SomeOtherViewHolder(view);
}


@Override
public void onBindViewHolder(final RecyclerView.ViewHolder holder, final int position) {
// Above we created a SomeOtherViewHolder but here we're expecting a SimpleViewHolder
// This will crash trying to cast to the wrong type
((SimpleViewHolder) holder).bindData(models.get(position));
}

在第一个示例中不可能犯这个错误,因为 ViewHolder类型被显式声明:

@Override
public MyAdapter.MyViewHolder onCreateViewHolder(ViewGroup parent,
int viewType) {

TextView v = (TextView) LayoutInflater.from(parent.getContext())
.inflate(R.layout.my_text_view, parent, false);

// Trying to return SomeOtherViewHolder when method expects MyViewHolder
// Compiler's like (*waves finger*) "nuh-uh, not on my watch" and fails
// to compile
SomeOtherViewHolder vh = new SomeOtherViewHolder(v);
return vh;
}

@Override
public void onBindViewHolder(MyViewHolder holder, int position) {
// Notice here that holder is explicitly of MyViewHolder type and casting
// is not necessary! That's TYPE SAFETY y'all.
holder.textView.setText(mDataset[position]);
}

因为您指定了您期望的显式具体类型,所以您不会意外返回错误的类型,您不需要强制转换,并且可以利用所有您自定义中定义的公共(public)方法 ViewHolder类。

希望有帮助!

关于java - 什么是 RecyclerView.Adapter<MyAdapter.MyViewHolder> 以及它与 Android 中的 RecyclerView.Adapter 有何不同?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59919366/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com