Android: RecycleView не заполняется - PullRequest
0 голосов
/ 26 сентября 2018

Я знаю, что об этом спрашивали несколько раз, но я не могу найти решение для этого.В основном My RecycleView не заполняется.Метод адаптера не вызывается (onBindViewHolder, onCreateViewHolder, getItemCount).пожалуйста помоги.единственное отличие от канонической ситуации состоит в том, что фрагменты находятся внутри ViewPager.Обратите внимание, что они правильно работали только с текстовым представлением внутри.

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>

gment_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 голосов
/ 26 сентября 2018

Похоже, вы не установили адаптер на RecyclerView.Попробуйте это:

private void initList() {
    adapter = new VenuesAdapter();
    RecyclerView.LayoutManager layoutManager = new LinearLayoutManager(getActivity().getApplicationContext());
    venueList.setLayoutManager(layoutManager);
    venueList.setAdapter(adapter);
}
0 голосов
/ 26 сентября 2018

Я думаю, что ваша схема ограничений испорчена.Попробуйте это:

<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" />

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...