为RecyclerView实现不同的布局,但在onBindViewHolder上获得类转换错误

时间:2015-10-27 21:07:59

标签: java android android-layout android-widget android-recyclerview

我正在关注this tutorial,但我收到以下演员错误:

for key of index_of if expr ... else ...

尝试投射时,onBindViewHolder会调用错误:java.lang.ClassCastException: com.banshai.appetit.controllers.home.OpenRestaurantsViewHolder cannot be cast to com.banshai.appetit.controllers.home.FirstOpenRestaurantVHolder at com.banshai.appetit.controllers.home.RestaurantAdapter.onBindViewHolder。我错过了什么?

这是我的完整适配器:

FirstOpenRestaurantVHolder firstVH = (FirstOpenRestaurantVHolder) viewholder;

2 个答案:

答案 0 :(得分:2)

必须更改代码而不是切换以使其更清晰(这只是个人偏好),添加一些控制台日志以查看发生了什么但这应该有效:

public class RestaurantAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {

    private List<Restaurant> restaurants;
    private Context context;
    private SharedData sharedData;

    private final int FIRST_OPEN_RESTAURANT = 0;
    private final int OPEN_RESTAURANT = 1;

    public RestaurantAdapter(List<Restaurant> restaurantsList) {
        restaurants = restaurantsList;
        sharedData = SharedData.getInstance();
    }

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

    @Override
    public int getItemViewType(int position) {
        Log.i("getItemVi(position)", "position=" + position);
        //If position is 0 this means we need to use FirstOpenRestaurantVHolder
        if (position == 0) {
            return 0;

        } else if (position > 0) {
            return 1;
        }


        return 1; //TODO ojo con este default
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup viewGroup, int viewType) {
        Log.i("onCreateVewHo(viewType)", "viewType=" + viewType);

        RecyclerView.ViewHolder viewHolder;
        LayoutInflater inflater = LayoutInflater.from(viewGroup.getContext());

        if (viewType == 0) {
            View firstOpenRest = inflater.inflate(R.layout.intro_home_restaurant_first_card, viewGroup, false);
            viewHolder = new FirstOpenRestaurantVHolder(firstOpenRest);

        } else {
            View openRestaurant = inflater.inflate(R.layout.intro_home_resturant_card_row, viewGroup, false);
            viewHolder = new OpenRestaurantsViewHolder(openRestaurant);
        }

        return viewHolder;
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder viewholder, int position) {
        Log.i("onBindViewHolder", "viewholder.getItemViewType()=" + viewholder.getItemViewType());

        if (viewholder.getItemViewType() == 0) {
            FirstOpenRestaurantVHolder firstVH = (FirstOpenRestaurantVHolder) viewholder;
            firstVH.franchiseName.setText("FIRST!!!");

        } else {
            OpenRestaurantsViewHolder openVH = (OpenRestaurantsViewHolder) viewholder;
            openVH.franchiseName.setText("OPEN BUT NOT FIRST");

        }
    }

    public class OpenRestaurantsViewHolder extends RecyclerView.ViewHolder {

        protected TextView franchiseName;
        protected TextView homeCategory;
        protected ImageView restaurantPhoto;
        protected ImageView franchiseLogo;


        public OpenRestaurantsViewHolder(View view) {
            super(view);
            franchiseName = (TextView) view.findViewById(R.id.home_franchise_name);
            restaurantPhoto = (ImageView) view.findViewById(R.id.home_resturant_pic);
            franchiseLogo = (ImageView) view.findViewById(R.id.home_franchise_logo);
            homeCategory = (TextView) view.findViewById(R.id.home_category);

        }
    }

    public class FirstOpenRestaurantVHolder extends RecyclerView.ViewHolder {

        protected TextView franchiseName;
        protected TextView homeCategory;
        protected ImageView restaurantPhoto;
        protected ImageView franchiseLogo;


        public FirstOpenRestaurantVHolder(View view) {
            super(view);

            franchiseName = (TextView) view.findViewById(R.id.home_first_franchise_name);
            restaurantPhoto = (ImageView) view.findViewById(R.id.home_first_resturant_pic);
            franchiseLogo = (ImageView) view.findViewById(R.id.home_first_franchise_logo);
            homeCategory = (TextView) view.findViewById(R.id.home_first_category);
        }


    }
}

答案 1 :(得分:0)

问题在于:

case FIRST_OPEN_RESTAURANT:
  FirstOpenRestaurantVHolder firstVH = (FirstOpenRestaurantVHolder) viewholder; 
onCreateViewHolder设置OpenRestaurantsViewHolder FIRST_OPEN_RESTAURANTonBindViewHolder,但FirstOpenRestaurantVHolder将getTag值转换为case FIRST_OPEN_RESTAURANT: OpenRestaurantsViewHolder firstVH = (OpenRestaurantsViewHolder) viewholder;

init_data