Я знаю, что его спрашивали несколько раз, но я не могу найти решение. В основном My 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 () в адаптере вызывается правильно с правильными данными, но тогда ничего не происходит. Любая идея?

0
jack_the_beast 26 Сен 2018 в 03:21

2 ответа

Лучший ответ

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

private void initList() {
    adapter = new VenuesAdapter();
    RecyclerView.LayoutManager layoutManager = new LinearLayoutManager(getActivity().getApplicationContext());
    venueList.setLayoutManager(layoutManager);
    venueList.setAdapter(adapter);
}
1
Barns 26 Сен 2018 в 00:43

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

<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" />
0
nupadhyaya 26 Сен 2018 в 00:39