我正试图在我的RecyclerView
CardView
中实施类似谷歌加动画。我的onBindViewHolder
方法出错了。
错误无法解析符号lastPosition
,无法解决符号position
这两个错误。
这是我的onBindViewHolder
方法代码:
public class SimpleCardViewAdapter extends RecyclerView.Adapter<SimpleCardViewAdapter.ViewHolder> {
private List<CardViewData> mDataset;
public SimpleCardViewAdapter(List<CardViewData> dataset) {
mDataset = dataset;
}
@Override
public ViewHolder onCreateViewHolder(final ViewGroup viewGroup, int i) {
View v = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.primary_layout, viewGroup, false);
return new ViewHolder(v);
}
@Override
public void onBindViewHolder(final ViewHolder viewHolder, int i) {
final CardViewData cardViewData = mDataset.get(i);
viewHolder.mTitle.setText(cardViewData.getTitle());
viewHolder.mDescription.setText(cardViewData.getDescription());
viewHolder.mImage.setImageResource(cardViewData.getImage());
Animation animation = AnimationUtils.loadAnimation(context,
(i > lastPosition) ? R.anim.up_from_bottom
: R.anim.down_from_top);
viewHolder.itemView.startAnimation(animation);
lastPosition = i;
viewHolder.itemView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(v.getContext(), "Title: " + cardViewData.getTitle(), Toast.LENGTH_SHORT).show();
}
});
}
@Override
public int getItemCount() {
return mDataset == null ? 0 : mDataset.size();
}
public static class ViewHolder extends RecyclerView.ViewHolder {
public TextView mTitle;
public TextView mDescription;
public ImageView mImage;
public ViewHolder(View itemView) {
super(itemView);
mTitle = (TextView) itemView.findViewById(R.id.row_title);
mDescription = (TextView) itemView.findViewById(R.id.row_description);
mImage = (ImageView) itemView.findViewById(R.id.row_image);
}
}
}
答案 0 :(得分:0)
public void onBindViewHolder(final ViewHolder viewHolder, int i) {
final CardViewData cardViewData = mDataset.get(i);
viewHolder.mTitle.setText(cardViewData.getTitle());
viewHolder.mDescription.setText(cardViewData.getDescription());
viewHolder.mImage.setImageResource(cardViewData.getImage());
Animation animation = AnimationUtils.loadAnimation(context,
(i > lastPosition) ? R.anim.up_from_bottom
: R.anim.down_from_top);
viewHolder.itemView.startAnimation(animation);
lastPosition = i;
viewHolder.itemView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(v.getContext(), "Title: " + cardViewData.getTitle(), Toast.LENGTH_SHORT).show();
}
});
}
答案 1 :(得分:0)
更改构造函数以将Context
传递给适配器。它将使用context
解决您的问题。初始化适配器时,请调用getActivity()
作为Context
的参数。
我在lastPosition
中没有看到任何逻辑,默认变量应该是什么,所以我只能声明并初始化这个变量。
private List<CardViewData> mDataset;
private int lastPosition = 0;
private Context context;
public SimpleCardViewAdapter(Context context, List<CardViewData> dataset) {
mDataset = dataset;
this.context = context;
}
答案 2 :(得分:0)
如果这是您的完整代码,难怪为什么它说无法解析符号&#39; lastPosition&#39; 。
首先在类的开头初始化变量,如下面的
public class SimpleCardViewAdapter extends RecyclerView.Adapter<SimpleCardViewAdapter.ViewHolder> {
int lastPosition = 0 ; // Here is the initialization
private List<CardViewData> mDataset;
public SimpleCardViewAdapter(List<CardViewData> dataset) {
// your code here
}
}
我认为你从某个地方复制粘贴代码。如果是这样,请提供您复制的链接,以便我们可以轻松地帮助您。
您的另一个错误无法解析符号&#39; 是不可能的。你确定你收到了这个错误吗?