Android:未填充RecycleView

时间:2018-09-26 00:21:34

标签: android android-fragments android-recyclerview adapter

我知道已经被多次询问了,但是我找不到解决方案。基本上我的RecycleView没有被填充。没有调用适配器的任何方法(onBindViewHolder,onCreateViewHolder,getItemCount)。 请帮忙。与规范情况的唯一区别是片段位于ViewPager内部。请注意,它们仅在内部使用textView才能正常工作。

MeetingsFragment:

public abstract class MeetingsFragment extends Fragment implements VenueListContract.View {

    protected static MeetingsFragment instance;
    protected View view;
    protected VenuesAdapter adapter;
    protected MeetingsPresenter presenter;

    @BindView(R.id.venue_list)
    RecyclerView venueList;


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        view = inflater.inflate(R.layout.fragment_meetings, container, false);
        ButterKnife.bind(this, view);
        presenter = new MeetingsPresenter(this, VenuesRepositoryImpl.getInstance(LocalVenuesDataSource.getInstance()));
        initList();
        return view;
    }

    @Override
    public void onStart (){
        super.onStart();
        presenter.loadItems();
    }
    private void initList() {
        adapter = new VenuesAdapter();
        RecyclerView.LayoutManager layoutManager = new LinearLayoutManager(getActivity().getApplicationContext());
        venueList.setLayoutManager(layoutManager);
    }

    @Override
    public void showItems(List<Venue> items) {
        adapter.setList(items);
    }
}

VenuesAdapter:

public class VenuesAdapter extends RecyclerView.Adapter<VenuesAdapter.VenueViewHolder> {
    private List<Venue> list;

    public static class VenueViewHolder extends RecyclerView.ViewHolder{
        @BindView(R.id.venue_item_photo)
        ImageView photo;

        @BindView(R.id.venue_item_price)
        TextView price;

        @BindView(R.id.venue_item_venue)
        TextView venue;

        @BindView(R.id.venue_item_location)
        TextView location;

        @BindView(R.id.venue_item_day)
        TextView day;

        @BindView(R.id.venue_item_time)
        TextView time;


        public VenueViewHolder(View view){
            super(view);
            ButterKnife.bind(this, view);
        }

    }

    @Override
    public void onBindViewHolder(VenueViewHolder holder, int position){
        Venue venue = list.get(position);
        holder.location.setText(venue.getLocation());
        holder.price.setText(venue.getCost());
        holder.venue.setText(venue.getVenue());
    }

    @Override
    public VenueViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View itemView = LayoutInflater.from(parent.getContext())
                .inflate(R.layout.item_venue, parent, false);

        return new VenueViewHolder(itemView);
    }

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

    public void setList(List<Venue> list){
        this.list = list;
        notifyDataSetChanged();
    }
}

item_venue.xml:

<android.support.constraint.ConstraintLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_marginHorizontal="@dimen/venue_item_margin"
    android:layout_marginTop="@dimen/venue_item_margin">

    <ImageView
        android:id="@+id/venue_item_photo"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />

    <TextView
        android:id="@+id/venue_item_price"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginStart="@dimen/venue_item_spacing"
        android:layout_marginTop="@dimen/venue_item_spacing"
        android:hint="free"
        app:layout_constraintStart_toStartOf="@id/venue_item_photo"
        app:layout_constraintTop_toTopOf="@id/venue_item_photo" />

    <TextView
        android:id="@+id/venue_item_venue"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginStart="@dimen/venue_item_spacing"
        android:layout_marginBottom="@dimen/venue_item_spacing"
        android:hint="a venue"
        app:layout_constraintBottom_toTopOf="@+id/venue_item_location"
        app:layout_constraintStart_toStartOf="@id/venue_item_photo" />

    <TextView
        android:id="@+id/venue_item_location"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginStart="@dimen/venue_item_spacing"
        android:layout_marginBottom="@dimen/venue_item_spacing"
        android:hint="a place"
        app:layout_constraintBottom_toBottomOf="@id/venue_item_photo"
        app:layout_constraintStart_toStartOf="@id/venue_item_photo" />

    <TextView
        android:id="@+id/venue_item_day"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginEnd="@dimen/venue_item_spacing"
        android:layout_marginBottom="@dimen/venue_item_spacing"
        android:hint="20/09/2018"
        app:layout_constraintBottom_toTopOf="@+id/venue_item_time"
        app:layout_constraintEnd_toEndOf="@id/venue_item_photo" />

    <TextView
        android:id="@+id/venue_item_time"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginEnd="@dimen/venue_item_spacing"
        android:layout_marginBottom="@dimen/venue_item_spacing"
        android:hint="09:00 - 11:00"
        app:layout_constraintBottom_toBottomOf="@id/venue_item_photo"
        app:layout_constraintEnd_toEndOf="@id/venue_item_photo" />

</android.support.constraint.ConstraintLayout>

fragment_meetings:

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
    xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent"
    android:layout_height="match_parent">

    <android.support.v7.widget.RecyclerView
        android:id="@+id/venue_list"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"/>

</android.support.constraint.ConstraintLayout>
使用正确的数据正确调用适配器中的

setList(),但是什么也没有发生。有想法吗?

2 个答案:

答案 0 :(得分:1)

您似乎尚未将适配器设置为RecyclerView。试试这个:

private void initList() {
    adapter = new VenuesAdapter();
    RecyclerView.LayoutManager layoutManager = new LinearLayoutManager(getActivity().getApplicationContext());
    venueList.setLayoutManager(layoutManager);
    venueList.setAdapter(adapter);
}

答案 1 :(得分:0)

我认为您的约束布局搞砸了。试试这个:

<android.support.constraint.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dp">

<ImageView
    android:id="@+id/venue_item_photo"
    android:layout_width="match_parent"
    app:layout_constraintLeft_toLeftOf="parent"
    app:layout_constraintTop_toTopOf="parent"
    android:layout_height="wrap_content" />

<TextView
    android:id="@+id/venue_item_price"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginStart="10dp"
    android:layout_marginTop="10dp"
    android:hint="free"
    app:layout_constraintStart_toStartOf="@id/venue_item_photo"
    app:layout_constraintTop_toBottomOf="@id/venue_item_photo" />

<TextView
    android:id="@+id/venue_item_venue"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginStart="10dp"
    android:layout_marginBottom="10dp"
    android:hint="a venue"
    app:layout_constraintBottom_toTopOf="@+id/venue_item_location"
    app:layout_constraintTop_toBottomOf="@+id/venue_item_price"
    app:layout_constraintStart_toStartOf="parent" />

<TextView
    android:id="@+id/venue_item_location"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginStart="10dp"
    android:layout_marginBottom="10dp"
    android:hint="a place"
    app:layout_constraintBottom_toTopOf="@+id/venue_item_day"
    app:layout_constraintTop_toBottomOf="@+id/venue_item_venue"
    app:layout_constraintStart_toStartOf="parent" />

<TextView
    android:id="@+id/venue_item_day"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginEnd="10dp"
    android:layout_marginBottom="10dp"
    android:hint="20/09/2018"
    app:layout_constraintBottom_toTopOf="@+id/venue_item_time"
    app:layout_constraintStart_toStartOf="parent" />

<TextView
    android:id="@+id/venue_item_time"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginEnd="10dp"
    android:layout_marginBottom="10dp"
    android:hint="09:00 - 11:00"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintStart_toStartOf="@id/venue_item_photo" />