如何使用recylerview适配器为每个活动选择不同的视图

时间:2019-07-16 10:14:23

标签: android-studio

如何将1个Recyclerview适配器用于具有不同视图的不同活动?

我正在使用android studio 3.1

@Override
    public RecyclerViewHolder onCreateViewHolder( ViewGroup parent, int viewType) {
        LayoutInflater inflater = LayoutInflater.from(parent.getContext());
        if(viewType == 0) {
            View itemView = inflater.inflate(R.layout.item_program, parent, false);

            return new RecyclerViewHolder(itemView);
        }else{
            View itemView = inflater.inflate(R.layout.item_exercise, parent, false);

            return new RecyclerViewHolder(itemView);
        }

    }

    @Override
    public void onBindViewHolder(final RecyclerViewHolder holder, int position) {

        holder.image.setImageResource(exerciseList.get(position).getImage_id());
        holder.text.setText(exerciseList.get(position).getName());

        holder.setItemClickListener(new ItemClickListener() {
            @Override
            public void onClick(View view, int position) {
                //Call to new activity
                if(true) {

                    Intent intent = new Intent(context, ViewExercise.class);
                    intent.setFlags(intent.FLAG_ACTIVITY_NEW_TASK);
                    intent.putExtra("image_id", exerciseList.get(position).getImage_id());
                    intent.putExtra("name", exerciseList.get(position).getName());
                    context.startActivity(intent);
                }else
                {
                    Intent intent = new Intent(context, listOfExercises.class);
                    intent.setFlags(intent.FLAG_ACTIVITY_NEW_TASK);
                    intent.putExtra("image_id", exerciseList.get(position).getImage_id());
                    intent.putExtra("name", exerciseList.get(position).getName());
                    context.startActivity(intent);
                }
                    }
        });
    }

当我单击视图持有人上的项目时,针对不同活动仅显示一种视图类型。请看附件图片 Programs.class listOfExercises.class

1 个答案:

答案 0 :(得分:0)

您是否使用了下面的Override方法。

fmin = 0.
fmax = 4000.  # since your original signal is sampled at 8 kHz
mel = librosa.feature.melspectrogram(j, n_mels=128,
                                     n_fft=input_nfft,
                                     hop_length=input_stride,
                                     sr=sr,
                                     fmin=fmin,
                                     fmax=fmax)