Android: Recyclerview внутри фрагмента, извлекающего данные из TMDbApi, выдает ошибку: E / RecyclerView: адаптер не подключен;пропускающий макет - PullRequest
0 голосов
/ 26 декабря 2018

Я боролся с проблемой на Android.У меня есть приложение с ящиком, в котором отображаются фрагменты в зависимости от того, какой элемент меню вы щелкаете в ящике.

Вот как работает мое приложение: Это ящик с кнопками меню:

This has 4 buttons, Movies, TvShows,Watchlist and profile

Всякий раз, когда я нажимаю кнопку, например Профиль, она загружает правильный фрагмент: Profile Fragments get loaded when I click the corresponding menu button

Теперь у меня есть этопроблема с Recyclerview из-за моего фрагмента телепередачи

Когда я нажимаю на телешоу, я получаю пустой белый макет, подобный этому:

Empty layout

И вот что я получаю в logcat: com.example.derwishe.movielist E / RecyclerView: адаптер не подключен;пропускающий макет

Вот мой код:

Класс фрагмента:

package com.example.derwishe.movielist;

import android.content.Context;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;

import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Toast;

import com.example.derwishe.movielist.Api.OnGetTvShowsCallback;
import com.example.derwishe.movielist.Api.TvShow;
import com.example.derwishe.movielist.Api.TvShowsRepository;

import java.util.List;


public class TvShowFragment extends Fragment {
 private TvShowsRepository tvShowsRepository;
 private RecyclerView tvShowList;
 private ListAdapter adapter;
 Context context;

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, 
Bundle savedInstanceState) {
    final View view = 
inflater.inflate(R.layout.fragment_tvshow,container,false);
    tvShowsRepository = TvShowsRepository.getInstance();
    tvShowList = (RecyclerView)view.findViewById(R.id.showsRecyclerView);
    RecyclerView.LayoutManager layoutManager = new 
LinearLayoutManager(getActivity());
    tvShowList.setLayoutManager(layoutManager);
    this.context = context;

    tvShowsRepository.getTvShows(new OnGetTvShowsCallback() {
        @Override
        public void onSuccess(List<TvShow> tvShows) {
            adapter = new ListAdapter(tvShows);
            tvShowList.setAdapter(adapter);
        }

        @Override
        public void onError() {
            Toast.makeText(context, "Please check your internet 
connection.", Toast.LENGTH_SHORT).show();            }
    });

    return view;
    }
}

Класс Listadapter:

package com.example.derwishe.movielist;

import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.example.derwishe.movielist.Api.TvShow;

import java.util.List;

public class ListAdapter extends RecyclerView.Adapter<ListAdapter.TvShowHolder> {
private List<TvShow> tvShows;

public ListAdapter(List<TvShow> tvShows){
    this.tvShows = tvShows;
}

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

@Override
public void onBindViewHolder(TvShowHolder holder, int i) {
    holder.bind(tvShows.get(i));

}

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

class TvShowHolder extends RecyclerView.ViewHolder{
    TextView showTitle;

    public TvShowHolder(View itemView){
        super(itemView);
        showTitle = itemView.findViewById(R.id.showTitle);
    }

    public void bind(TvShow tvShow){
        showTitle.setText(tvShow.getTitle());
    }
}
}

Класс MainActivity с выдвижным ящиком:

package com.example.derwishe.movielist;

import android.support.annotation.NonNull;
import android.support.design.widget.NavigationView;
import android.support.v4.view.GravityCompat;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.ActionBarDrawerToggle;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.Toolbar;
import android.view.MenuItem;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity implements NavigationView.OnNavigationItemSelectedListener {
private DrawerLayout drawer;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    Toolbar toolbar = findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);

    drawer = findViewById(R.id.drawer_layout);
    //Reference naar navigatieview voor click events

    NavigationView navigationView =findViewById(R.id.nav_view);
    navigationView.setNavigationItemSelectedListener(this);

    ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(this,drawer,toolbar,R.string.navigation_draw_open,R.string.navigation_draw_close);
    drawer.addDrawerListener(toggle);
    toggle.syncState();
    //initiele fragment openen bij start
    if(savedInstanceState == null) {
        getSupportFragmentManager().beginTransaction().replace(R.id.fragment_container,
                new MovieFragment()).commit();
        navigationView.setCheckedItem(R.id.nav_movies);
    }
}

@Override
public boolean onNavigationItemSelected(@NonNull MenuItem menuItem) {
    switch (menuItem.getItemId()){
        case R.id.nav_movies:
            getSupportFragmentManager().beginTransaction().replace(R.id.fragment_container,
                    new MovieFragment()).commit();
            break;
        case R.id.nav_tvshows:
            getSupportFragmentManager().beginTransaction().replace(R.id.fragment_container,
                    new TvShowFragment()).commit();
            break;
        case R.id.nav_watchlist:
            getSupportFragmentManager().beginTransaction().replace(R.id.fragment_container,
                    new WatchlistFragment()).commit();
            break;
        case R.id.nav_profile:
            getSupportFragmentManager().beginTransaction().replace(R.id.fragment_container,
                    new ProfileFragment()).commit();
            break;
        case R.id.nav_support:
            Toast.makeText(this,"Support message activated",Toast.LENGTH_SHORT).show();
            break;
    }
    drawer.closeDrawer(GravityCompat.START);
    return true;
}

@Override
public void onBackPressed() {
    if(drawer.isDrawerOpen(GravityCompat.START)){
        drawer.closeDrawer(GravityCompat.START);
    }else{
    super.onBackPressed();
    }
}
}

И, наконец, файлы макетов: Activity_main.xml

<android.support.v4.widget.DrawerLayout 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:id="@+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
tools:context=".MainActivity"
tools:openDrawer="start">

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <android.support.v7.widget.Toolbar
        android:id="@+id/toolbar"
        android:layout_width="match_parent"
        android:layout_height="?attr/actionBarSize"
        android:background="@color/colorPrimary"
        android:elevation="4dp"
        android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
        app:popupTheme="@style/ThemeOverlay.AppCompat.Light" />

    <FrameLayout
        android:id="@+id/fragment_container"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />
</LinearLayout>

<android.support.design.widget.NavigationView
    android:id="@+id/nav_view"
    android:layout_width="wrap_content"
    android:layout_height="match_parent"
    android:layout_gravity="start"
    app:headerLayout="@layout/nav_header"
    app:menu="@menu/draw_menu" />



</android.support.v4.widget.DrawerLayout>

фрагмент_tvshow.xml, в котором хранится обзор утилитарной системы

<RelativeLayout
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:layout_width="match_parent"
    android:layout_height="match_parent"
    android:id="@+id/showsRecyclerView"/>


</RelativeLayout>

И показывает_item.xml, представляющий 1предмет в списке переработчиков

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal" android:layout_width="match_parent"
android:layout_height="wrap_content">
<ImageView
    android:layout_width="0dp"
    android:layout_height="50dp"
    android:scaleType="fitCenter"
    android:layout_margin="8dp"
    android:layout_weight="1"
    android:id="@+id/showImage"/>
<TextView
    android:layout_width="0dp"
    android:layout_height="wrap_content"
    android:id="@+id/showTitle"
    android:text="Show Title"
    android:textSize="22sp"
    android:layout_weight="3"
    android:layout_margin="22dp"/>

</LinearLayout>

1 Ответ

0 голосов
/ 26 декабря 2018

Я думаю, это потому, что как только ваш фрагмент onCreate запущен в первый раз, recyclerView не назначается адаптер, только после того, как вы извлечете свои данные (телешоу), вы установите адаптер в свой recyclerView.

Одним из способов решения этой проблемы было бы иметь конструктор, который не принимает никаких аргументов для вашего адаптера, и метод внутри него, который передавал бы любые данные, которые потребуется отобразить recyclerView.Вы бы получили что-то вроде этого:

public class ListAdapter {

    public ListAdapter() {}

    public void submitList(final List<TvShow> tvShows) {
        this.tvShows = tvShows;
        notifyDataChanged();
    }
}
...