BottomsheetFragment中的RecyclerView问题

时间:2020-07-09 09:55:31

标签: android android-recyclerview android-adapter android-bottomsheetdialog

我正在尝试使用回收站视图创建一个底页片段。但是我在回收者视图中遇到了一个错误,adapter not set skipping layout

我很确定我为回收站视图设置了适配器。我似乎不明白问题所在。

底稿片段代码

public class QueueBottomSheetDialog extends BottomSheetDialogFragment {
    //Views
    private View fragmentView;
    private RecyclerView queueRecyclerView;
    private QueueRecyclerAdapter queueRecyclerAdapter;

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        fragmentView = inflater.inflate(R.layout.queue_bottomsheet,container,false);

        Initialize();
        populateSongs();

        return fragmentView;
    }

    private void Initialize(){
        queueRecyclerView = fragmentView.findViewById(R.id.queue_recyclerView);
        queueRecyclerView.setHasFixedSize(true);
    }

    private void populateSongs(){
        queueRecyclerAdapter = new QueueRecyclerAdapter(getContext(),MusicService.songsSet);
        queueRecyclerView.setAdapter(queueRecyclerAdapter);
        queueRecyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));
    }
}

回收站适配器的代码

public class QueueRecyclerAdapter extends RecyclerView.Adapter<QueueRecyclerAdapter.QueueViewHolder> {

    private Context context;
    private SongSet songSet;

    public QueueRecyclerAdapter(Context context,SongSet songSet){
        this.context = context;
        this.songSet = songSet;
    }

    @NonNull
    @Override
    public QueueViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        LayoutInflater layoutInflater = LayoutInflater.from(parent.getContext());
        View view = layoutInflater.inflate(R.layout.queue_cardview,parent,false);
        return new QueueViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull QueueViewHolder holder, int position) {
        holder.songTv.setText(songSet.get(position).getTitle());
        holder.artistTv.setText(songSet.get(position).getArtist());
    }

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

    public static class QueueViewHolder extends RecyclerView.ViewHolder{
        private TextView songTv;
        private TextView artistTv;

        public QueueViewHolder(@NonNull View itemView) {
            super(itemView);
            songTv = itemView.findViewById(R.id.sName);
            artistTv = itemView.findViewById(R.id.aName);
        }
    }
}

我在主要活动中打这样的电话

queueBottomSheetDialog queueBottomSheetDialog = newQueueBottomSheetDialog();
queueBottomSheetDialog.show(getSupportFragmentManager(),"QUEUE BOTTOMSHEET");

0 个答案:

没有答案