所有列表项都是由RecyclerViewAdapter创建的。如何在布局中正确设置RecyclerView?

时间:2019-01-31 16:15:00

标签: android android-layout android-fragments android-recyclerview

我在片段中有RecyclerView。问题在于在显示片段期间通过RecyclerAdapter调用每个元素。 onCreateViewHolder()onBindViewHolder()方法调用列表中每个可能的元素,而不仅是可​​见元素。

我已经尝试在android:layout_heightwrap_contentmatch_parent上将0dp设置为constraintBottom_toBottomOf="parent" constraintTop_toTopOf="parent"的解决方案

我发现RecyclerView适配器仅对可见元素(fragment_learn_choose_content.xml)调用的唯一可能方法:

<?xml version="1.0" encoding="utf-8"?>
<layout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        xmlns:tools="http://schemas.android.com/tools">

    <android.support.v7.widget.RecyclerView
        android:id="@+id/recycler_view_learn_choose_content"
        android:layout_width="match_parent"
        android:layout_height="600dp" />
</layout>

但是android:layout_height="600dp"是硬编码的,我想避免这种情况。

我的片段:

public class LearnChooseContentFragment {
    private LearnChooseContentMenuRecyclerAdapter adapter;
    private List<LearnContent> listOfContents;

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        listOfContents = new ArrayList<>();
        adapter = new LearnChooseContentMenuRecyclerAdapter(listOfContents);
    }

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        super.onCreateView(inflater, container, savedInstanceState);
        FragmentLearnChooseContentBinding binding = DataBindingUtil.inflate(inflater, R.layout.fragment_learn_choose_content, container, false);
        RecyclerView list = binding.recyclerViewLearnChooseContent;
        list.setLayoutManager(new LinearLayoutManager(getContext()));
        list.addItemDecoration(new MyDividerItemDecoration(list.getContext()));
        list.setAdapter(adapter);

        EventBus.getDefault().register(this);
        return binding.getRoot();
    }

    @Override
    protected void onEvent(Service service) {
        ...
        listOfContents.clear();
        adapter.setList(someList);
    }

我的RecyclerAdapter类(LearnChooseContentMenuRecyclerAdapter):

class LearnChooseContentMenuRecyclerAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
private final List<LearnContent> list;

LearnChooseContentMenuRecyclerAdapter(List<LearnContent> list) {
    this.list = new ArrayList<>(list);
}

@NonNull
@Override
public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int viewType) {
    LayoutInflater inflater = LayoutInflater.from(viewGroup.getContext());
    ItemLearnContentBinding bindingContentItem = DataBindingUtil.inflate(inflater, R.layout.item_learn_content, viewGroup, false);
    return new ViewHolder(bindingContentItem);
}

@Override
public void onBindViewHolder(@NonNull RecyclerView.ViewHolder viewHolder, int position) {
            viewHolder.bind(list.get(position));
}

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

public void setList(List<LearnContent> newList){
    final LearnContentDiffCallback diffCallback = new LearnContentDiffCallback(list, newList);
    final DiffUtil.DiffResult diffResult = DiffUtil.calculateDiff(diffCallback);

    list.clear();
    list.addAll(newList);
    diffResult.dispatchUpdatesTo(this);
}

public List<LearnContent> getList() {
    return list;
}

public class ViewHolder extends RecyclerView.ViewHolder {
    private ItemLearnContentBinding binding;
    private LearnContent content;

    public ViewHolder(@NonNull ItemLearnContentBinding binding) {
        super(binding.getRoot());
        this.binding = binding;
    }

    public void bind(LearnContent learnContent){
        setItem(learnContent);
        binding.executePendingBindings();
    }

    public void setItem(LearnContent learnContent) {
        ...
    }
}

有什么想法吗?

0 个答案:

没有答案