所以我关注这个tutorial,我有 RecyclerView 和 GridLayoutManager 实现。 GridLayoutManager 有2列。我想要两种视图类型,所以我使用了两个扩展 RecyclerView.ViewHolder 的 ViewHolders ,我正在重写这个方法: getItemViewType(int position)< / em>,但我采取了Cast异常。 Logcat 显示:
java.lang.ClassCastException:adapter.AdapterListItemHome $ CommentViewHolder0无法强制转换为adapter.AdapterListItemHome $ CommentViewHolder1 在adapter.AdapterListItemHome.onBindViewHolder(AdapterListItemHome.java:200) 在adapter.AdapterListItemHome.onBindViewHolder(AdapterListItemHome.java:25)
但是我没有使用CommentViewHolder0对CommentViewHolder1使用强制转换,例如Line问题是这样的:
Remote Address:127.0.0.1:80
Request URL:http://localhost/project_dir/public/user/check
Request Method:POST
Status Code:200 OK
Response Headers
view source
Access-Control-Allow-Origin:http://localhost:9000
Cache-Control:no-cache
Connection:Keep-Alive
Content-Length:4
Content-Type:text/html; charset=UTF-8
Date:Wed, 29 Jul 2015 13:41:51 GMT
Keep-Alive:timeout=5, max=100
Server:Apache/2.4.9 (Win64) PHP/5.5.12
Vary:Origin
X-Powered-By:PHP/5.5.12
Request Headers
view source
Accept:application/json, text/plain, */*
Accept-Encoding:gzip, deflate
Accept-Language:en-US,en;q=0.8
Cache-Control:max-age=0
Connection:keep-alive
Content-Length:0
Host:localhost
Origin:http://localhost:9000
Referer:http://localhost:9000/
User-Agent:Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/44.0.2403.107 Safari/537.36
这是来自适配器的代码(AdapterListItemHome.java):
final CommentViewHolder0 vh0 = (CommentViewHolder0)cvh;
这是 Fragment , RecyclerView 正在实施
public class AdapterListItemHome extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
ArrayList<GadgetItem> mListItem;
Context mContext;
private int choice;
public static OnItemClickListener listener;// Define listener member variable
public static final int FIRST_ITEM = 0;
public static final int REST_ITEMS = 1;
/**
* Defines the listener interface
*/
public interface OnItemClickListener {
void onItemClick(View itemView, int position);
}
// Define the method that allows the parent activity or fragment to define the listener
public void setOnItemClickListener(OnItemClickListener listener) {
this.listener = listener;
}
/**
* view holder - holds the widgets - classical
*/
public class CommentViewHolder1 extends RecyclerView.ViewHolder {
public ProgressBar pgLoading;
public TextView tv_title;
public ImageView imgView;
//constructor
public CommentViewHolder1(final View itemView) {
super(itemView);
// Find the TextView in the LinearLayout
tv_title = ((TextView) itemView.findViewById(R.id.namgeItem));
pgLoading = (ProgressBar) itemView.findViewById(R.id.pbLoadingImage);
imgView = (ImageView) itemView.findViewById(R.id.imgItem);
itemView.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View v) {
// Triggers click upwards to the adapter on click
if (listener != null)
listener.onItemClick(itemView, getPosition());
}
});
}
}
/**
* second viewHolder
*/
public class CommentViewHolder0 extends RecyclerView.ViewHolder {
public ProgressBar pgLoading;
public TextView tv_title;
public ResizableImageView imgView;
public TextView tv_date;
//constructor
public CommentViewHolder0(final View itemView) {
super(itemView);
// Find the TextView in the LinearLayout
tv_title = ((TextView) itemView.findViewById(R.id.namgeItem));
tv_date = ((TextView) itemView.findViewById(R.id.tv_date));
pgLoading = (ProgressBar) itemView.findViewById(R.id.pbLoadingImage);
imgView = (ResizableImageView) itemView.findViewById(R.id.resizable_image);
imgView.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View v) {
// Triggers click upwards to the adapter on click
if (listener != null)
listener.onItemClick(imgView, getPosition());
}
});
}
}
/*
* VF: Constructor
*/
public AdapterListItemHome(Context pContext, ArrayList<GadgetItem> pListGadget) {
this.mContext = pContext;
this.mListItem = pListGadget;
}
/**
* Replace the contents of a view (invoked by the layout manager)
* @param viewGroup
* @param ViewType
* @return
*/
@Override
public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup viewGroup, int ViewType){
View v;
//Log.d("bill", "viewtype = " + String.valueOf(ViewType));
switch(ViewType){
case REST_ITEMS:
v = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.row_item, viewGroup, false);
CommentViewHolder1 cvh1 = new CommentViewHolder1(v);
return cvh1;
case FIRST_ITEM:
v = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.row_item_home, viewGroup, false);
CommentViewHolder0 cvh0 = new CommentViewHolder0(v);
return cvh0;
default:
v = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.row_item, viewGroup, false);
CommentViewHolder1 cvh2 = new CommentViewHolder1(v);
return cvh2;
}
}
@Override
public void onBindViewHolder(RecyclerView.ViewHolder cvh, int position) {
Transformation transformation;
GadgetItem item;
switch ( cvh.getItemViewType () ){
case REST_ITEMS:
final CommentViewHolder1 vh1 = (CommentViewHolder1)cvh;
item = mListItem.get(position);
vh1.tv_title.setText(item.gadget_title);
vh1.pgLoading.setVisibility(View.VISIBLE);
vh1.imgView.setImageDrawable(null);
/**
* clicking on the image view
*/
transformation = new Transformation() {
@Override
public Bitmap transform(Bitmap source) {
int targetWidth = vh1.imgView.getWidth();
double aspectRatio = (double) source.getHeight() / (double) source.getWidth();
int targetHeight = (int) (targetWidth * aspectRatio);
// Bitmap result = Bitmap.createScaledBitmap(source,
// targetWidth,
// targetHeight, false);
Bitmap result = scaleAndCropImage(source, 300);
if (result != source) {
// Same bitmap is returned if sizes are the same
source.recycle();
}
return result;
}
@Override
public String key() {
return "transformation" + " desiredWidth";
}
};
Picasso.with(mContext).load(item.gadget_image).into(vh1.imgView, new Callback() {
@Override
public void onError() {
}
@Override
public void onSuccess() {
vh1.pgLoading.setVisibility(View.GONE);
}
});
case FIRST_ITEM:
final CommentViewHolder0 vh0 = (CommentViewHolder0)cvh;
item = mListItem.get(position);
vh0.tv_title.setText(item.gadget_title);
vh0.pgLoading.setVisibility(View.VISIBLE);
vh0.imgView.setImageDrawable(null);
vh0.tv_date.setText(item.gadget_pubDate);
/**
* clicking on the image view
*/
transformation = new Transformation() {
@Override
public Bitmap transform(Bitmap source) {
int targetWidth = vh0.imgView.getWidth();
double aspectRatio = (double) source.getHeight() / (double) source.getWidth();
int targetHeight = (int) (targetWidth * aspectRatio);
// Bitmap result = Bitmap.createScaledBitmap(source,
// targetWidth,
// targetHeight, false);
Bitmap result = scaleAndCropImage(source, 300);
if (result != source) {
// Same bitmap is returned if sizes are the same
source.recycle();
}
return result;
}
@Override
public String key() {
return "transformation" + " desiredWidth";
}
};
Picasso.with(mContext).load(item.gadget_image).into(vh0.imgView, new Callback() {
@Override
public void onError() {
}
@Override
public void onSuccess() {
vh0.pgLoading.setVisibility(View.GONE);
}
});
}
}
// Return the size of dataset (invoked by the layout manager)
@Override
public int getItemCount() {
return mListItem.size();
}
/* (non-Javadoc)
* VF: creating a item to show
*/
Bitmap scaleAndCropImage(Bitmap image, int height) {
float ratio = (float) (height) / (float) image.getHeight();
int width = (int) ((float) image.getWidth() * ratio);
return ScalingUtilities.createScaledBitmap(image, width, height, ScalingUtilities.ScalingLogic.CROP);
}
@Override
public int getItemViewType(int position) {
int viewType;
if (position == 1)
viewType = FIRST_ITEM;
else
viewType = REST_ITEMS;
return viewType;
}
}
我一直在各地搜索,实施方法是一样的,我无法弄清问题在哪里。谢谢你的帮助
答案 0 :(得分:0)
这段代码对我来说很好用:
public class AdapterListItemHome extends RecyclerView.Adapter<AdapterListItemHome.MainHolder> {
ArrayList<GadgetItem> mListItem;
Context mContext;
public static OnItemClickListener listener;// Define listener member variable
public static final int FIRST_ITEM = 0;
public static final int REST_ITEMS = 1;
/**
* Defines the listener interface
*/
public interface OnItemClickListener {
void onItemClick(View itemView, int position);
}
// Define the method that allows the parent activity or fragment to define the listener
public void setOnItemClickListener(OnItemClickListener listener) {
this.listener = listener;
}
public class MainHolder extends RecyclerView.ViewHolder {
public MainHolder(View itemView){
super(itemView);
}
}
/**
* view holder - holds the widgets
*/
public class CommentViewHolder1 extends MainHolder {
public ProgressBar pgLoading;
public TextView tv_title;
public ImageView imgView;
//constructor
public CommentViewHolder1(final View itemView) {
super(itemView);
// Find the TextView in the LinearLayout
tv_title = ((TextView) itemView.findViewById(R.id.namgeItem));
pgLoading = (ProgressBar) itemView.findViewById(R.id.pbLoadingImage);
imgView = (ImageView) itemView.findViewById(R.id.imgItem);
itemView.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View v) {
// Triggers click upwards to the adapter on click
if (listener != null)
listener.onItemClick(itemView, getPosition());
}
});
}
}
/**
* second viewHolder
*/
public class CommentViewHolder0 extends MainHolder {
public ProgressBar pgLoading;
public TextView tv_title;
public ResizableImageView imgView;
public TextView tv_date;
//constructor
public CommentViewHolder0(final View itemView) {
super(itemView);
// Find the TextView in the LinearLayout
tv_title = ((TextView) itemView.findViewById(R.id.namgeItem_home));
tv_date = ((TextView) itemView.findViewById(R.id.tv_date));
pgLoading = (ProgressBar) itemView.findViewById(R.id.pbLoadingImage_home);
imgView = (ResizableImageView) itemView.findViewById(R.id.resizable_image);
imgView.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View v) {
// Triggers click upwards to the adapter on click
if (listener != null)
listener.onItemClick(imgView, getPosition());
}
});
}
}
public AdapterListItemHome(Context pContext, ArrayList<GadgetItem> pListGadget,ArrayList<String> pDateList) {
this.mContext = pContext;
this.mListItem = pListGadget;
this.mDateList = pDateList;
p = new Picasso.Builder(mContext)
.memoryCache(new LruCache(cacheSize))
.build();
}
/**
* Replace the contents of a view (invoked by the layout manager)
*/
@Override
public MainHolder onCreateViewHolder(ViewGroup viewGroup, int ViewType){
View v
switch(ViewType){
case REST_ITEMS:
v = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.row_item, viewGroup, false);
CommentViewHolder1 cvh1 = new CommentViewHolder1(v);
return cvh1;
case FIRST_ITEM:
v = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.row_item_home, viewGroup, false);
CommentViewHolder0 cvh0 = new CommentViewHolder0(v);
return cvh0;
default:
v = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.row_item, viewGroup, false);
CommentViewHolder1 cvh2 = new CommentViewHolder1(v);
return cvh2;
}
}
@Override
public void onBindViewHolder(MainHolder cvh, int position) {
switch ( cvh.getItemViewType () ){
case REST_ITEMS:
final CommentViewHolder1 vh1 = (CommentViewHolder1)cvh;
GadgetItem item1 = mListItem.get(position);
vh1.tv_title.setText(fixTitleText(20, item1.gadget_title) + item1.gadget_price);
vh1.pgLoading.setVisibility(View.VISIBLE);
vh1.imgView.setImageDrawable(null);
final int y = 200;
final int x = 200;
//picasso
p.with(mContext).load(item1.gadget_image).resize(x, y).centerCrop().into(vh1.imgView, new Callback() {
@Override
public void onError() {
}
@Override
public void onSuccess() {
vh1.pgLoading.setVisibility(View.GONE);
}
});
break;
case FIRST_ITEM:
final CommentViewHolder0 vh0 = (CommentViewHolder0)cvh;
GadgetItem item = mListItem.get(position);
vh0.tv_title.setText(fixTitleText(63, item.gadget_title) + item.gadget_price);
vh0.pgLoading.setVisibility(View.VISIBLE);
vh0.imgView.setImageDrawable(null);
String dateTime = fixdateTime(item.gadget_pubDate);
String date = null;
try {
date = calculateDate(dateTime);
} catch (ParseException e) {
e.printStackTrace();
}
vh0.tv_date.setText(date + " " + dateTime);
final int y1 = 335;
final int x1 = 400;
//picasso
p.with(mContext).
load(item.gadget_image).resize(x1, y1).centerCrop().into(vh0.imgView, new Callback() {
@Override
public void onError() {
}
@Override
public void onSuccess() {
vh0.pgLoading.setVisibility(View.GONE);
}
});
break;
}
}
// Return the size of dataset (invoked by the layout manager)
@Override
public int getItemCount() {
return mListItem.size();
}
/* (non-Javadoc)
* VF: creating a item to show
*/
@Override
public int getItemViewType(int position) {
int viewType;
if (position == 0 || (!(mDateList.get(position).equals(mDateList.get(position -1 )))))
viewType = FIRST_ITEM;
else
viewType = REST_ITEMS;
return viewType;
}
}