作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
好吧,我是 recyclerview
的新手,我在这里迷路了。我想在一个 Activity 中有 2 个(或更多)recyclerview。我的问题出在这部分代码中
error: here inconvertable type cannot cast dataobjectholder to groupview holder
case 2:
GroupViewHolder groupviewHolder = (GroupViewHolder) holder; //here inconvertable type cannot cast dataobjectholder to groupview holder
groupviewHolder.label1.setText(mDataset.get(position).getmText1());
// holder.dateTime.setText(mDataset.get(position).getmText2());
// holder.imageView.setImageResource();
Picasso.
with(mContext).
load("http://justedhak.com/funnyimages/uploaded_images563cd66b98a20.png")
.placeholder(R.drawable.error)
.fit()
.noFade()
.into(groupviewHolder.imageView1);
break;
}
这是我的完整代码
public class MyRecyclerViewAdapter extends RecyclerView
.Adapter<MyRecyclerViewAdapter
.DataObjectHolder> {
Context mContext;
private static String LOG_TAG = "MyRecyclerViewAdapter";
private ArrayList<DataObject> mDataset;
private static MyClickListener myClickListener;
public MyRecyclerViewAdapter(Context context)
{
this.mContext =context;
}
)
public static class GroupViewHolder extends RecyclerView.ViewHolder
{
TextView label1;
ImageView imageView1;
// TextView dateTime;
public GroupViewHolder(View itemView) {
super(itemView);
label1 = (TextView) itemView.findViewById(R.id.textView);
// dateTime = (TextView) itemView.findViewById(R.id.textView2);
imageView1 = (ImageView) itemView.findViewById(R.id.image);
// itemView.setOnClickListener(this);
}
}
public static class DataObjectHolder extends RecyclerView.ViewHolder
implements View
.OnClickListener {
TextView label;
ImageView imageView;
// TextView dateTime;
public DataObjectHolder(View itemView) {
super(itemView);
label = (TextView) itemView.findViewById(R.id.textView);
// dateTime = (TextView) itemView.findViewById(R.id.textView2);
imageView = (ImageView) itemView.findViewById(R.id.image);
itemView.setOnClickListener(this);
}
@Override
public void onClick(View v) {
myClickListener.onItemClick(getPosition(), v);
}
}
public void setOnItemClickListener(MyClickListener myClickListener) {
this.myClickListener = myClickListener;
}
public MyRecyclerViewAdapter(ArrayList<DataObject> myDataset) {
mDataset = myDataset;
}
@Override
public DataObjectHolder onCreateViewHolder(ViewGroup parent,
int viewType) {
View view = LayoutInflater.from(parent.getContext())
.inflate(R.layout.recyclerview_item, parent, false);
DataObjectHolder dataObjectHolder = new DataObjectHolder(view);
return dataObjectHolder;
}
@Override
public int getItemViewType(int position)
{
return position==0? 1:2;
}
@Override
public void onBindViewHolder(DataObjectHolder holder, int position) {
switch(holder.getItemViewType())
{
case 1:
holder.label.setText(mDataset.get(position).getmText1());
// holder.dateTime.setText(mDataset.get(position).getmText2());
// holder.imageView.setImageResource();
Picasso.
with(mContext).
load("http://justedhak.com/funnyimages/uploaded_images563cd66b98a20.png")
.placeholder(R.drawable.error)
.fit()
.noFade()
.into(holder.imageView);
break;
case 2:
GroupViewHolder groupviewHolder = (GroupViewHolder) holder;
groupviewHolder.label1.setText(mDataset.get(position).getmText1());
// holder.dateTime.setText(mDataset.get(position).getmText2());
// holder.imageView.setImageResource();
Picasso.
with(mContext).
load("http://justedhak.com/funnyimages/uploaded_images563cd66b98a20.png")
.placeholder(R.drawable.error)
.fit()
.noFade()
.into(groupviewHolder.imageView1);
break;
}
}
public void addItem(DataObject dataObj, int index) {
mDataset.add(dataObj);
notifyItemInserted(index);
}
public void deleteItem(int index) {
mDataset.remove(index);
notifyItemRemoved(index);
}
@Override
public int getItemCount() {
return mDataset.size();
}
public interface MyClickListener {
public void onItemClick(int position, View v);
}
最佳答案
首先,你有两个不同的Viewholder,所以你的类extends RecyclerView.Adapter
而不是 extends RecyclerView.Adapter<MyRecyclerViewAdapter.DataObjectHolder>
现在看看你的onCreateViewHolder
功能:它只返回一种类型的Viewholder。应该是:
@Override
public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent,
int viewType) {
if (viewType == 1) {
View view = LayoutInflater.from(parent.getContext())
.inflate(R.layout.recyclerview_item, parent, false);
DataObjectHolder dataObjectHolder = new DataObjectHolder(view);
return dataObjectHolder;
} else {
//TODO: Inflate view and return GroupViewHolder
}
}
那么你的 onBindViewHolder
有错误功能:
@Override
public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
switch(getItemViewType(position)) {
// TODO...
}
}
或
@Override
public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
if (holder instanceof DataObjectHolder) {
// Do this...
} else if (holder instanceof GroupViewHolder) {
// ...do that
}
}
关于android - 添加多个回收 View 时,不可转换类型无法将 dataobjectholder 转换为 groupview 持有者,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34999025/
好吧,我是 recyclerview 的新手,我在这里迷路了。我想在一个 Activity 中有 2 个(或更多)recyclerview。我的问题出在这部分代码中 error: here i
我是一名优秀的程序员,十分优秀!