RecyclerView多行布局包括GridView

时间:2015-12-01 13:44:37

标签: android android-layout android-recyclerview

我想在RecyclerView中有2个布局。但我得到了NPE。

public class RecyclerViewAdapter  extends RecyclerView.Adapter<RecyclerViewAdapter.ViewHolder>{
    private static final int TYPE_ADS = 1;
    private static final int TYPE_GRID = 2;
    private List<App> applist;
    private Activity activity;
    public final static String TYPE_APP = "apps";
    private List<AnetworkAds> adsList;

    public RecyclerViewAdapter(List<App> appList,Activity activity,List<AnetworkAds>adsList) {
        this.applist = appList;
        this.activity=activity;
        this.adsList=adsList;
    }
    public static class ViewHolder extends RecyclerView.ViewHolder {
        public View view;
        public ViewHolder(View v) {
            super(v);
 view = v;
        }
    }
    @Override
    public RecyclerViewAdapter.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        switch (viewType) {
            case TYPE_ADS:
                View view1 = LayoutInflater.from(parent.getContext())
                        .inflate(R.layout.advertisement_layout, parent, false);
                ViewHolder viewHolder1 = new ViewHolder(view1);
                return viewHolder1;
            case TYPE_GRID:
                View view2 = LayoutInflater.from(parent.getContext())
                        .inflate(R.layout.grid_app_item, parent, false);
                ViewHolder viewHolder2 = new ViewHolder(view2);
                return viewHolder2;

            default:
                return null;
        }
    }

    @Override
    public void onBindViewHolder(RecyclerViewAdapter.ViewHolder holder, final int position) {
        if(holder.getItemViewType()==TYPE_GRID) {
            ImageView imageViewImage = (ImageView) holder.view.findViewById(R.id.imageViewImage);
            TextView textViewTitle = (TextView) holder.view.findViewById(R.id.textViewTitle);
            RatingBar ratingBar = (RatingBar) holder.view.findViewById(R.id.appItemRateBar);
            TextView text_view_price = (TextView) holder.view.findViewById(R.id.text_view_price);
            final ImageView popup = (ImageView) holder.view.findViewById(R.id.popup);
            ImageView tik = (ImageView) holder.view.findViewById(R.id.tik);

            textViewTitle.setText(applist.get(position).getTitle());
            text_view_price.setText(applist.get(position).getPrice()+"");
            ratingBar.setRating(applist.get(position).getRate());
            popup.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Utility.popupClick(applist.get(position), popup, activity, TYPE_APP);
                }
            });
            Glide.with(activity.getApplicationContext())
                    .load(applist.get(position).getImage())
                    .fitCenter()
                    .crossFade()
                    .into(imageViewImage);

            if (applist.get(position).isInstalled()) {
                tik.setVisibility(View.VISIBLE);
                tik.setImageResource(R.drawable.tikblue);
                text_view_price.setVisibility(View.GONE);
            } else {
                text_view_price.setVisibility(View.VISIBLE);
               tik.setVisibility(View.GONE);
                if (!applist.get(position).isFree()) {
                    text_view_price.setText(applist.get(position).getPrice() + activity.getResources().getString(R.string.t));
                } else {
                    text_view_price.setText(activity.getResources().getString(R.string.free));
                }

            }

        }else if (holder.getItemViewType()==TYPE_ADS){
            ImageView advertise=(ImageView)holder.view.findViewById(R.id.advertise);
//            Glide.with(activity.getApplicationContext())
//                    .load(adsList.get(position).getBanner())
//                    .fitCenter()
//                    .crossFade()
//                    .into(advertise);
        }

    }

    @Override
    public int getItemCount() {
        return 6;
    }
    @Override
    public int getItemViewType(int position) {
        switch (position) {
            case 0:
                return TYPE_ADS;

            case 1:
                return TYPE_GRID;


            default:
                return 0;
        }
    }
}

片段oncreateView()中的我的代码:

 mRecyclerView=(RecyclerView)rootView.findViewById(R.id.popular_recycler_view);
        layoutManager= new GridLayoutManager(mActivity,Utility.calculateColumnCount(mActivity));
        mRecyclerView.setLayoutManager(layoutManager);
 mAdapter = new RecyclerViewAdapter(appList,mActivity,null);
                    mRecyclerView.setAdapter(mAdapter);

错误:

java.lang.NullPointerException
          at android.support.v7.widget.RecyclerView$Adapter.createViewHolder(RecyclerView.java:5229)
          at android.support.v7.widget.RecyclerView$Recycler.getViewForPosition(RecyclerView.java:4453)
          at android.support.v7.widget.RecyclerView$Recycler.getViewForPosition(RecyclerView.java:4363)
          at android.support.v7.widget.LinearLayoutManager$LayoutState.next(LinearLayoutManager.java:1961)
          at android.support.v7.widget.GridLayoutManager.layoutChunk(GridLayoutManager.java:458)
          at android.support.v7.widget.LinearLayoutManager.fill(LinearLayoutManager.java:1333)
          at android.support.v7.widget.LinearLayoutManager.onLayoutChildren(LinearLayoutManager.java:562)....

3 个答案:

答案 0 :(得分:0)

您的ViewHolder为空。做那样的事情:

public static class ViewHolder extends RecyclerView.ViewHolder {

        public ImageView imageViewImage ;
            super(v);
            imageViewImage = (ImageView) v.findViewById(R.id.imageViewImage );
            ..itc
        }

答案 1 :(得分:0)

中定义您的参考资料
public ViewHolder(View v) {
            super(v);
ImageView imageViewImage = (ImageView) view.findViewById(R.id.imageViewImage);
                TextView textViewTitle = (TextView) view.findViewById(R.id.textViewTitle);
                RatingBar ratingBar = (RatingBar) view.findViewById(R.id.appItemRateBar);
                TextView text_view_price = (TextView) view.findViewById(R.id.text_view_price);
                final ImageView popup = (ImageView) holder.view.findViewById(R.id.popup);
                ImageView tik = (ImageView) holder.view.findViewById(R.id.tik);
// view = v;
        }

答案 2 :(得分:0)

问题在于此代码:

@Override
public int getItemCount() {
    return 6;
}
@Override
public int getItemViewType(int position) {
    switch (position) {
        case 0:
            return TYPE_ADS;

        case 1:
            return TYPE_GRID;


        default:
            return 0;
    }
}

对于0和1,您将返回正确的类型,对于您返回ZERO的所有其他4个项目。那么当它调用onCreateViewHolder

@Override
public RecyclerViewAdapter.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
    switch (viewType) {
        case TYPE_ADS:
            View view1 = LayoutInflater.from(parent.getContext())
                    .inflate(R.layout.advertisement_layout, parent, false);
            ViewHolder viewHolder1 = new ViewHolder(view1);
            return viewHolder1;
        case TYPE_GRID:
            View view2 = LayoutInflater.from(parent.getContext())
                    .inflate(R.layout.grid_app_item, parent, false);
            ViewHolder viewHolder2 = new ViewHolder(view2);
            return viewHolder2;
        default:
            return null;
    }
}

您返回null,因为该类型不是ADS且不是GRID。 你必须在所有可能的位置返回一个合适的持有人。