如何使用相同的回收适配器充气不同的布局?

时间:2018-09-19 09:10:33

标签: android android-layout

我正在使用wordAdapter类来回收列表视图,我希望在给定条件下膨胀不同的布局,例如,如果标志等于膨胀activity_all布局等于1,并且如果标志等于膨胀activity_food布局等于2,但是当我尝试使用时以下代码使我的应用崩溃了

 CountingActivity countingActivity;
    FoodActivity foodActivity;
    //Making constructor of the class wordAdapter which takes Activity and ArrayList as arguments
    public wordAdapter(Activity context, ArrayList<word> words,int flag){
        super(context,0,words);
    }

    @NonNull

    public View getView(int position,@NonNull View convertView,@NonNull ViewGroup parent){

        View listItemView = convertView;

        //Checking if recycle view is available or not
         if(listItemView == null)
        {
            if (countingActivity.flag == 1) {
                listItemView = LayoutInflater.from(getContext()).inflate(R.layout.activity_all,parent,false);

            }
            else if(foodActivity.flag == 2)
            {
                listItemView = LayoutInflater.from(getContext()).inflate(R.layout.activity_food,parent,false);
            }
        }

2 个答案:

答案 0 :(得分:2)

您可以在onCreateViewHolder部分

中添加它
    @NonNull
    @Override
    public YouAdapter.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {

        if (viewType == 0)
        {
            View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.your_xml_one, parent, false);
            return new v;

        }else
        {
            View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.your_xml_two, parent, false);
            return new v;
        }

    }

答案 1 :(得分:0)

在适配器类中,您需要像上面那样声明类型

private static final int VIEW_ITEM = 1;
private static final int LOADING = 0;
private static final int VIEW_TYPE_EMPTY = 2;

onCreateViewHolder中,您需要这样声明ViewHolders

@Override
public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent,
                                                      int viewType) {

        RecyclerView.ViewHolder vh = null;

        switch (viewType) {

            case VIEW_TYPE_EMPTY:
                View emptyView = LayoutInflater.from(parent.getContext()).inflate(
                        R.layout.item_view_no_data, parent, false);

                vh = new EmptyViewHolder(emptyView);
                break;

            case VIEW_ITEM:
                View itemView = LayoutInflater.from(parent.getContext()).inflate(
                        R.layout.item_view_game_details, parent, false);

                vh = new GameViewHolder(itemView);
                break;

            case LOADING:
                View v = LayoutInflater.from(parent.getContext()).inflate(
                        R.layout.layout_progress_bar, parent, false);

                vh = new ProgressViewHolder(v);
                break;

        }

        return vh;
    }

onBindViewHolder中,您需要先检查viewholder实例和根据实例进行的类型转换

@Override
public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, final int position) {

        if (holder instanceof GameViewHolder) {
           /** You code for you ViewHlder**/

        } else if (holder instanceof ProgressViewHolder) {
            /** You code for you ViewHlder
            ((ProgressViewHolder) holder).progressBar.setIndeterminate(true);
            **/
        }
    }