0

卡 1 的成分和布局 2 的步骤 - 图片 公共类 DetailAdapter 扩展 RecyclerView.Adapter {

    private List<Ingredient> ingredientList;
    private List<Step> stepList;
    private Context context;

    private boolean check;
    private int which_layout;


    public DetailAdapter(Context context, BakingModel bakingModel){
       // Log.d("Inside Adapter" , bakingModel.getIngredients().get(0).getIngredient());
        this.context = context;
        ingredientList = bakingModel.getIngredients();
        ingredientList.addAll(bakingModel.getIngredients());
        stepList = bakingModel.getSteps();
        stepList.addAll(bakingModel.getSteps());
        Log.d("Inside Adapter" , ingredientList.get(0).getIngredient());
        check = false;
        which_layout = 0;
    }

    public static class DetailView1 extends RecyclerView.ViewHolder {

        @Nullable@BindView(R.id.ingredient)
        TextView textView_ing;
        @Nullable@BindView(R.id.ingredient_list)
        EditText editText_inglist;


        public DetailView1(View itemView) {
            super(itemView);
            ButterKnife.bind(this,itemView);

        }
    }

    public static class DetailView2 extends RecyclerView.ViewHolder{

        @Nullable @BindView(R.id.step)
        TextView textView_step;

        public DetailView2(View itemView) {
            super(itemView);
            ButterKnife.bind(this,itemView);
        }

    }

    @Override
    public int getItemViewType(int position) {
        if(!check){
            which_layout = 1;
            check = true;
            Log.d("getItemView","Layout 1");

        }
        else {
            which_layout = 2;
        }
        return  which_layout;

    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup viewGroup, int i) {

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

        switch (which_layout){
            case 1 :
                View v1 = inflater.inflate(R.layout.fragment_detail1,viewGroup,false);
                viewHolder = new DetailView1(v1);
                Log.d("onCreateView","Create Layout 1");
                break;
            default:
                View v2 = inflater.inflate(R.layout.fragment_detail2,viewGroup,false);
                viewHolder = new DetailView2(v2);
                Log.d("onCreateView","Create Layout 2");
        }

        return viewHolder;
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder viewHolder, int i) {
        switch (viewHolder.getItemViewType()) {
            case 1 :
                DetailView1 detailView1 = (DetailView1) viewHolder;
                detailView1.textView_ing.setText(R.string.ingredients);
                for(Ingredient ingredient : ingredientList) {
                    detailView1.editText_inglist.append(ingredient.getIngredient() + "\n");
                }
                Log.d("onBindView","Bind Layout 1");
                break;
            default:
                DetailView2 detailView2= (DetailView2) viewHolder;
                detailView2.textView_step.setText(stepList.get(i).getShortDescription());
                Log.d("onBindView","Bind Layout 2");
        }

    }

    @Override
    public int getItemCount() {
        return stepList.size()+1;
    }
}

当我将 getItemView 下的哪个布局的值更改为 1 时,相同的编辑文本将填充 stepList.size() 次数。否则,如果是现在这样,则没有编辑文本。仅填充 DetailView2 的步骤描述。

DetailView1 正在被 DetailView2 替换。

编辑:基本上我想要实现的是为成分创建一张卡片。然后是多张卡片的步骤。

4

0 回答 0