Using RecyclerView Adapter - Error E/RecyclerView: No adapter attached; skipping layout - android

I am using a custom recycler adapter to display a text_view.xmlinto a fragment_list.xml. However, when I customise my fragment_list.xmladding a RelativeLayout the text_view is not displayed anymore.
I am getting this error message: E/RecyclerView: No adapter attached; skipping layout
This is my_text_view.xml
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="18sp"
android:textStyle="normal|bold"
android:textColor="#color/black"
android:textAlignment="center"
android:text="This is my #string/cast_intro_overlay_button_text">
</TextView>
My fragment_item_list.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#color/background_color">
<Button
android:text="Back"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/buttonBack"
android:layout_weight="0.08"
android:textSize="24sp"
style="#style/Widget.AppCompat.Button.Colored"
android:layout_marginBottom="14dp"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="30sp"
android:textColor="#color/black"
android:textAlignment="center"
android:id="#+id/myTextView"
android:text="Best Scores"
android:textStyle="normal|bold"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="19dp">
</TextView>
<android.support.v7.widget.RecyclerView android:id="#+id/list"
android:name="com.example.lucadigiammarino.biogame.ItemFragment"
android:layout_width="match_parent"
android:layout_height="250dp"
app:layoutManager="LinearLayoutManager"
tools:context="com.example.lucadigiammarino.biogame.ItemFragment"
tools:listitem="#layout/my_text_view"
android:isScrollContainer="false"
android:addStatesFromChildren="true"
android:layout_below="#+id/myTextView"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_marginTop="37dp">
</android.support.v7.widget.RecyclerView>
</RelativeLayout>
This is my MyPlayerAdapter class
class MyPlayerAdapter extends RecyclerView.Adapter{
ArrayList<Player> players;
/**
* Constructor for MyPlayerAdapter class
* #param players
*/
public MyPlayerAdapter(ArrayList<Player> players) {
this.players = players;
}
#Override
public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.my_text_view, parent, false);
MyViewHolder vh = new MyViewHolder((TextView) v);
return vh;
}
#Override
public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
MyViewHolder vh = (MyViewHolder)holder;
vh.mTextView.setText(players.get(position).name+", "+players.get(position).score);
}
#Override
public int getItemCount() {
return players.size();
}
public static class MyViewHolder extends RecyclerView.ViewHolder{
public TextView mTextView;
public MyViewHolder(TextView v) {
super(v);
mTextView = v;
}
}
}
And this is my OnCreateView() of my FragmentItem class
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_item_list, container, false);
// Set the adapter
if (view instanceof RecyclerView) {
Context context = view.getContext();
RecyclerView recyclerView = (RecyclerView) view;
if (mColumnCount <= 1) {
recyclerView.setLayoutManager(new LinearLayoutManager(context));
} else {
recyclerView.setLayoutManager(new GridLayoutManager(context, mColumnCount));
}
recyclerView.setAdapter(new MyPlayerAdapter(Score.getInstance().getPlayers()));
}
return view;
}
I really don't know where to put my hands anymore. Appreciate help guys.
Thank you in advance

It's because your view object is not instanceof RecyclerView, but instance of RelativeLayout. So code inside your if-statement is never executed. Replace your code with this:
View view = inflater.inflate(R.layout.fragment_item_list, container, false);
RecyclerView recyclerView = (RecyclerView) view.findViewById(R.id.list);
Context context = view.getContext();
if (mColumnCount <= 1) {
recyclerView.setLayoutManager(new LinearLayoutManager(context));
} else {
recyclerView.setLayoutManager(new GridLayoutManager(context, mColumnCount));
}
recyclerView.setAdapter(new MyPlayerAdapter(Score.getInstance().getPlayers()));
Here you get your recyclerView instance from the layout you inflated into view instance.

Related

Fragment with multiple Recycler Views freezes UI

I am creating a Fragment that has 5 horizontal recycler views under a vertical Scroll View but each time this fragment needs to be loaded, the UI freezes for about a second.
I have created an Adapter that sets the Data from 2 different POJOs into the different Recycler Views. I have found out that the line from my code which seems to be causing the issue is the setLayoutManager() because once commented, the fragment loading does not freeze the UI. Apart from that I haven't been able to find out why is that happening when assigning the LayoutManger to the recycler view and what could I do to improve the fragment's performance.
Here is the fragment_home.xml
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.entertainment.logicaldays.planetmovie.views.fragments.HomeFragment"
android:id="#+id/layout"
android:background="#color/colorPrimary">
<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/scroll_view">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<android.support.v4.view.ViewPager
android:layout_width="match_parent"
android:layout_height="200dp"
android:id="#+id/view_pager"
android:layout_marginBottom="5dp">
</android.support.v4.view.ViewPager>
<android.support.v7.widget.RecyclerView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/recycler_popular_movies"
android:orientation="horizontal">
</android.support.v7.widget.RecyclerView>
<android.support.v7.widget.RecyclerView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/recycler_upcoming_movies"
android:orientation="horizontal">
</android.support.v7.widget.RecyclerView>
<android.support.v7.widget.RecyclerView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/recycler_top_rated_movies"
android:orientation="horizontal">
</android.support.v7.widget.RecyclerView>
<android.support.v7.widget.RecyclerView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/recycler_popular_tv_shows"
android:orientation="horizontal">
</android.support.v7.widget.RecyclerView>
<android.support.v7.widget.RecyclerView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/recycler_top_rated_tv_shows"
android:orientation="horizontal">
</android.support.v7.widget.RecyclerView>
</LinearLayout>
</ScrollView>
</FrameLayout>
Then I have the recycler_item.xml to bind to the Recycler Views:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:padding="5dp">
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
android:layout_gravity="center">
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/hover_values">
<ImageView
android:layout_width="wrap_content"
android:layout_height="150dp"
android:src="#drawable/ic_launcher_background"
android:layout_gravity="center|top"
android:id="#+id/recycler_image" />
<ImageView
android:layout_width="20dp"
android:layout_height="20dp"
android:layout_alignParentTop="true"
android:layout_marginTop="20dp"
android:alpha="0"
android:id="#+id/poster_hover_image"
android:layout_centerHorizontal="true"
android:src="#drawable/star"/>
<TextView
android:layout_marginTop="5dp"
android:alpha="0"
android:id="#+id/poster_hover_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#id/poster_hover_image"
android:fontFamily="#font/theboldfont"
android:layout_centerHorizontal="true" />
</RelativeLayout>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textAlignment="center"
android:scrollHorizontally="true"
android:minLines="2"
android:maxLines="2"
android:ellipsize="end"
android:id="#+id/recycler_title"/>
</LinearLayout>
</LinearLayout>
The Adapter which can bind 2 different POJOs into the Recycler Views:
public class RecyclerViewAdapter extends RecyclerView.Adapter<RecyclerViewAdapter.ViewHolder>{
public Object pojo;
public Context context;
public RecyclerViewAdapter(Context ctx, Object movies) {
this.pojo = movies;
this.context = ctx;
}
#Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.recycler_item, parent, false);
final ValueAnimator animatorPosterTint = ValueAnimator.ofInt(0, 180);
final ValueAnimator animatorHoverOpacity = ValueAnimator.ofFloat(0, 1);
//When long-pressing the Movie
view.setOnLongClickListener(new LongClickOnPosterItemListerner(view, animatorPosterTint, animatorHoverOpacity));
//When releasing the touch on the movie
view.setOnTouchListener(new ReleasePosterItemListener(view, animatorPosterTint, animatorHoverOpacity));
return new ViewHolder(view);
}
#Override
public void onBindViewHolder(ViewHolder holder, int position) {
if(pojo instanceof MovieList) {
holder.title.setText(((MovieList)pojo).results.get(position).title);
Glide.with(context)
.asBitmap()
.load(Utils.BASE_TMDB_POSTER_URL + ((MovieList)pojo).results.get(position).posterPath)
.into(holder.image);
holder.rating.setText(Float.toString(((MovieList)pojo).results.get(position).voteAverage));
}
else{
holder.title.setText(((TVList)pojo).results.get(position).name);
Glide.with(context)
.asBitmap()
.load(Utils.BASE_TMDB_POSTER_URL + ((TVList)pojo).results.get(position).posterPath)
.into(holder.image);
holder.rating.setText(Float.toString(((TVList)pojo).results.get(position).voteAverage));
}
}
#Override
public int getItemCount() {
int count = 0;
if(pojo instanceof MovieList && ((MovieList)pojo).results != null) {
if (((MovieList)pojo).results != null) {
count = ((MovieList)pojo).results.size();
}
}
else{
if (((TVList)pojo).results != null) {
count = ((TVList)pojo).results.size();
}
}
return count;
}
public class ViewHolder extends RecyclerView.ViewHolder{
ImageView image;
TextView title;
TextView rating;
public ViewHolder(View itemView) {
super(itemView);
image = itemView.findViewById(R.id.recycler_image);
title = itemView.findViewById(R.id.recycler_title);
rating = itemView.findViewById(R.id.poster_hover_text);
}
}
}
And finally we have the HomeFragment.java
public class HomeFragment extends Fragment {
public MainActivity mainActivity;
#BindView(R.id.recycler_popular_movies)
RecyclerView recyclerPopularMovies;
#BindView(R.id.recycler_upcoming_movies)
RecyclerView recyclerUpcomingMovies;
#BindView(R.id.recycler_top_rated_movies)
RecyclerView recyclerTopRatedMovies;
#BindView(R.id.recycler_popular_tv_shows)
RecyclerView recyclerPopularTVShows;
#BindView(R.id.recycler_top_rated_tv_shows)
RecyclerView recyclerTopRatedTVShows;
#BindView(R.id.view_pager)
ViewPager viewPager;
LinkedHashMap<Utils.RequestsToLoad, Boolean> requestsToLoad;
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
mainActivity = (MainActivity) getActivity();
// Inflate the layout into the Fragment
View v = inflater.inflate(R.layout.fragment_home, container, false);
ButterKnife.bind(this, v);
startViewPager();
startRecyclerViews();
return v;
}
#Override
public void onViewCreated(View view, #Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
}
private void startRecyclerViews(){
displayRecyclerView(recyclerPopularMovies, mainActivity.popularMovies);
displayRecyclerView(recyclerTopRatedMovies, mainActivity.upcomingMovies);
displayRecyclerView(recyclerUpcomingMovies, mainActivity.topRatedMovies);
displayRecyclerView(recyclerPopularTVShows, mainActivity.popularTVShow);
displayRecyclerView(recyclerTopRatedTVShows, mainActivity.topRatedTVShow);
}
private void displayRecyclerView(RecyclerView recyclerView, Object object){
LinearLayoutManager layoutManager = new LinearLayoutManager(mainActivity, LinearLayoutManager.HORIZONTAL, false);
final RecyclerViewAdapter adapter = new RecyclerViewAdapter(getActivity(), object);
recyclerView.setAdapter(adapter);
recyclerView.setLayoutManager(layoutManager);
}
private void startViewPager(){
SwipeAdapter swipeAdapter = new SwipeAdapter(getActivity(), mainActivity.upcomingMovies);
viewPager.setAdapter(swipeAdapter);
}
}
As said before, is there anything that could be causing that delay to load the fragment so that I can improve it?
Thanks in advance

Recycler view not rendering view

I am not getting any view from the recycler, I did some debug and the constructor of the adapter is called with items on its list, also the method
getItemCount()
but none of the rest seem to be executed.
I´ve been wandering around stack looking for this problem and i found different approaches to a solution, problem is none worked out for me, maybe I am just missing something i can´t see.
I read somewhere that it could be because the recycler view is inside an scroll view, but the solution given did not worked for me
my custom adapter:
public class CircumstancesAdapter extends RecyclerView.Adapter<CircumstancesAdapter.CircumstancesViewHolder> {
List<CatalogRespModel> circumList;
Context mContext;
public CircumstancesAdapter(List<CatalogRespModel> list, Context context) {
this.circumList = list;
this.mContext = context;
}
#Override
public CircumstancesViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = LayoutInflater.from(mContext).inflate(R.layout.row_item_circumstances, parent, false);
return new CircumstancesViewHolder(view);
}
#Override
public void onBindViewHolder(CircumstancesViewHolder holder, int position) {
holder.tvCircum.setText(circumList.get(position).get_Descripcion());
}
#Override
public int getItemCount() {
if(circumList != null) return circumList.size();
else return 0;
}
public class CircumstancesViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener{
RadioButton rbCircum;
TextView tvCircum;
private Context context;
public CircumstancesViewHolder(View itemView) {
super(itemView);
rbCircum = (RadioButton) itemView.findViewById(R.id.rb_circ);
rbCircum.setOnClickListener(this);
tvCircum = (TextView) itemView.findViewById(R.id.tv_circ);
this.context = itemView.getContext();
}
#Override
public void onClick(View v) {
}
}
}
my container layout:
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent">
<!-- TODO: Update blank fragment layout -->
<android.support.v4.widget.NestedScrollView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="15dp">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingTop="20dp"
android:paddingLeft="20dp"
android:paddingRight="20dp"
android:orientation="vertical">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingTop="8dp"
android:id="#+id/text2"
android:text="#string/text_circ"
android:textSize="20sp"
android:textColor="#color/colorPrimaryshadow"/>
<RelativeLayout
android:id="#+id/placa"
android:layout_width="180dp"
android:layout_height="120dp"
android:layout_gravity="center"
android:background="#drawable/placa">
<TextView
android:id="#+id/tv_plate"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center_vertical|center_horizontal"
android:layout_centerInParent="true"
android:text="#string/dummy"
android:textSize="40sp"
android:textColor="#android:color/black"/>
</RelativeLayout>
<ImageView
android:id="#+id/iv_indicator"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:clickable="true"
android:src="#drawable/conductora" />
<android.support.v7.widget.RecyclerView
android:id="#+id/rview_circumstances"
android:layout_width="match_parent"
android:background="#fff"
android:orientation="vertical"
android:layout_height="wrap_content"/>
<Button
android:id="#+id/boton_continuar_circustanciasa"
android:layout_marginTop="15dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#drawable/continuar"
android:layout_marginBottom="10dp"
android:elevation="30dp"
android:layout_gravity="center"/>
</LinearLayout>
</android.support.v4.widget.NestedScrollView>
</FrameLayout>
my row layout:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="match_parent"
android:background="#fff"
android:paddingTop="2dp"
android:paddingBottom="2dp"
android:layout_height="wrap_content">
<RadioButton
android:id="#+id/rb_circ"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:buttonTint="#color/colorAccentDark"
android:gravity="center_vertical"
android:textSize="14sp" />
<TextView
android:layout_gravity="center_vertical"
android:gravity="center_vertical"
android:id="#+id/tv_circ"
android:textSize="14sp"
android:text="texto de prueba"
android:textColor="#color/colorAccentDark"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</LinearLayout>
and in my onCreateView inside the fragment:
recyclerView = (RecyclerView) rootView.findViewById(R.id.rview_circumstances);
recyclerView.setNestedScrollingEnabled(false);
recyclerView.setHasFixedSize(true);
if(Catalogs.circumstancesArray.size() > 0){
adapter = new CircumstancesAdapter(Catalogs.circumstancesArray, getContext());
recyclerView.setLayoutManager(new LinearLayoutManager(getContext()));
recyclerView.setAdapter(adapter);
}
Note: I checked the List size and is greater than 0
Fragment code as requested:
public class CircumstancesFragment extends Fragment {
Button btnContinue;
TextView tvPlate;
private ImageView ivIndicator;
private static int iNumDevices;
IFragmentListener iFragmentListener;
private RecyclerView recyclerView;
RecyclerView.Adapter adapter;
private final static String TAGMap = CircumstancesFragment.class.getSimpleName();
public static final String TAG = "CircumstancesFragment.java";
public static CircumstancesFragment getInstance(Bundle bundle){
if(bundle != null){
iNumDevices = bundle.getInt("numDevices");
}
return new CircumstancesFragment();
}
#Override
public void onAttach(Context context) {
super.onAttach(context);
if(context != null){
if(context instanceof IFragmentListener){
iFragmentListener = (IFragmentListener) context;
} else
throw new RuntimeException("el contexto no esta implementando la interfaz");
}
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
final View rootView = inflater.inflate(R.layout.fragment_circustancias_, container, false);
btnContinue = (Button) rootView.findViewById(R.id.boton_continuar_circustanciasa);
tvPlate = (TextView) rootView.findViewById(R.id.tv_plate);
recyclerView = (RecyclerView) rootView.findViewById(R.id.rview_circumstances);
ivIndicator = (ImageView) rootView.findViewById(R.id.iv_indicator);
recyclerView.setNestedScrollingEnabled(false);
recyclerView.setHasFixedSize(true);
if(Catalogs.circumstancesArray.size() > 0){
adapter = new CircumstancesAdapter(Catalogs.circumstancesArray, getContext());
recyclerView.setLayoutManager(new LinearLayoutManager(getContext()));
recyclerView.setAdapter(adapter);
adapter.notifyDataSetChanged();
}
btnContinue.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view)
{
try
{
iFragmentListener.notify(null, TAG);
} catch (Exception ex)
{
Log.d("error", ex.getMessage());
}
}
});
setLayout(iNumDevices);
return rootView;
}
private void setLayout(int i){
tvPlate.setText(FdccoreConstants.insuredArray.get(0).get_Placa_Vehiculo());
if(i== FdccoreConstants.ONE_DEVICE){
ivIndicator.setImageResource(R.drawable.conductora);
}else if(i== FdccoreConstants.TWO_DEVICE){
ivIndicator.setImageResource(R.drawable.only_conductor);
}
}
private void resetView(){
}
}
Please provide more code because Your layout and adapter is working fine.
I only changed Your model list to String.
Maybe in place when You checked list size and list.size() > 0 add
adapter.notifyDataSetChanged();
=====
RecyclerView recyclerView = (RecyclerView) findViewById(R.id.rview_circumstances);
recyclerView.setNestedScrollingEnabled(false);
recyclerView.setHasFixedSize(true);
list = new ArrayList<>();
list.add("asd");
list.add("sasf");
list.add("asasfasf");
list.add("asgagsad");
list.add("asgas");
list.add("asagasgd");
list.add("asreyeryd");
list.add("asgsheyd");
CircumstancesAdapter adapter = new CircumstancesAdapter(list, this);
recyclerView.setLayoutManager(new LinearLayoutManager(this));
recyclerView.setAdapter(adapter);
solution: somehow in order to work with the scroll view i must add this line
compile 'com.android.support:recyclerview-v7:25.3.1'
compiling only appcompat-v7 wont work

error RecyclerView: No adapter attached; skipping layout when running program

I have tried answer at stackoverflow that relate to my problem. I wanna to show a data from firebase in imageview and textview. But there's error RecyclerView: No adapter attached; skipping layout. This is my coding :
MenuManajemenKendaraanFragment.java
public class MenuManajemenKendaraanFragment extends Fragment {
private RecyclerView recyclerView;
private MenuManajemenKendaraanAdapter adapter;
private DatabaseReference mDatabase;
//private ProgressDialog progressDialog;
private List<DataKendaraan> dataKendaraan;
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
getActivity().setTitle("Manajemen Kendaraan");
View v = inflater.inflate(R.layout.fragment_menu_manajemen_kendaraan, container, false);
FloatingActionButton fab_tambah_kendaraan = (FloatingActionButton) v.findViewById(R.id.fab);
mDatabase = FirebaseDatabase.getInstance().getReference();
recyclerView = (RecyclerView) v.findViewById(R.id.listViewKendaraan);
recyclerView.setHasFixedSize(true);
final FragmentActivity c = getActivity();
LinearLayoutManager layoutManager = new LinearLayoutManager(c);
recyclerView.setLayoutManager(layoutManager);
dataKendaraan = new ArrayList<>();
mDatabase = FirebaseDatabase.getInstance().getReference(Constants.DATABASE_PATH_UPLOADS);
mDatabase.addValueEventListener(new ValueEventListener() {
#Override
public void onDataChange(DataSnapshot dataSnapshot) {
//dismissing the progress dialog
//iterating through all the values in database
for (DataSnapshot postSnapshot : dataSnapshot.getChildren()) {
DataKendaraan upload = postSnapshot.getValue(DataKendaraan.class);
dataKendaraan.add(upload);
}
adapter = new MenuManajemenKendaraanAdapter(getActivity(), dataKendaraan);
//adding adapter to recyclerview
recyclerView.setAdapter(adapter);
}
fragment_menu_manajemen_kendaraan.xml
<FrameLayout 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: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:layout_alignParentTop="true"
android:clipToPadding="false"
android:padding="5dp"
android:scrollbars="vertical"
android:id="#+id/listViewKendaraan">
</android.support.v7.widget.RecyclerView>
<android.support.design.widget.FloatingActionButton
android:id="#+id/fab"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="16dp"
android:clickable="true"
app:fabSize="normal"
android:src="#drawable/ic_add_fab"
android:layout_gravity="end|bottom"/>
</FrameLayout>
MenuManajemenKendraanAdapter.java
public class MenuManajemenKendaraanAdapter extends RecyclerView.Adapter<MenuManajemenKendaraanAdapter.ViewHolder> {
private Context context;
private List<DataKendaraan> dataKendaraan;
public MenuManajemenKendaraanAdapter(Context context, List<DataKendaraan> dataKendaraan) {
this.dataKendaraan = dataKendaraan;
this.context = context;
}
#Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View v = LayoutInflater.from(parent.getContext())
.inflate(R.layout.adapter_menu_manajemen_kendaraan, parent, false);
ViewHolder viewHolder = new ViewHolder(v);
return viewHolder;
}
#Override
public void onBindViewHolder(ViewHolder holder, int position) {
DataKendaraan data = dataKendaraan.get(position);
holder.tipeKendaraan.setText(data.getTipeKendaraan());
Glide.with(context).load(data.getUriFotoKendaraan()).into(holder.fotoKendaraan);
}
#Override
public int getItemCount() {
return dataKendaraan.size();
}
class ViewHolder extends RecyclerView.ViewHolder {
public TextView tipeKendaraan;
public ImageView fotoKendaraan;
public ViewHolder(View itemView) {
super(itemView);
tipeKendaraan = (TextView)itemView.findViewById(R.id.tipe_kendaraan);
}
}
adapter_menu_manajemen_kendaraan.xml
<android.support.v7.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="90dp"
android:layout_margin="5dp">
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/fragment_frame"
android:layout_width="match_parent"
android:layout_height="match_parent">
</FrameLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="10dp">
<ImageView
android:id="#+id/imageViewFotoKendaraan"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="2"
android:layout_marginRight="10dp"/>
<LinearLayout
android:layout_width="0dp"
android:layout_height="match_parent"
android:orientation="vertical"
android:layout_weight="5">
<TextView
android:text="Tipe Kendaraan"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:fontFamily="sans-serif-black"
android:textColor="#color/colorAccent"
android:paddingBottom="2dp"
android:id="#+id/tipe_kendaraan"
android:textSize="16sp"/>
</LinearLayout>
</LinearLayout>
Thank you so much for your help.
View v = LayoutInflater.from(context) .inflate(R.layout.adapter_menu_manajemen_kendaraan, parent, false);
ViewHolder viewHolder = new ViewHolder(v);
return viewHolder;

Only one item displaying in recycler view

I am having problems displaying all the items in my recyclerview. My code only shows one item (Home. I have 3 items: Home, Browse Photos and My Travels)
fragment_navigation_drawer.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/fragment_navigation_drawer"
android:background="#EEE"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.traveldiaries.ztir.traveldiaries.NavigationDrawerFragment">
<!-- TODO: Update blank fragment layout -->
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="220dp"
android:background="#16a085"
android:id="#+id/linearLayout">
</LinearLayout>
<android.support.v7.widget.RecyclerView
android:id="#+id/drawerList"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
</android.support.v7.widget.RecyclerView>
</RelativeLayout>
RecyclerAdapter.java
public class RecyclerAdapter extends RecyclerView.Adapter<RecyclerAdapter.MyViewHolder> {
private LayoutInflater inflater;
List<MenuList> data = Collections.emptyList();
public RecyclerAdapter(Context context, List<MenuList> data){
inflater= LayoutInflater.from(context);
this.data = data;
}
#Override
public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view=inflater.inflate(R.layout.itemlist,parent,false);
MyViewHolder holder= new MyViewHolder(view);
return holder;
}
#Override
public void onBindViewHolder(MyViewHolder holder, int position) {
MenuList current=data.get(position);
holder.title.setText(current.title);
holder.image.setImageResource(current.icon);
}
#Override
public int getItemCount() {
return data.size();
}
class MyViewHolder extends RecyclerView.ViewHolder{
TextView title;
ImageView image;
public MyViewHolder(View itemView) {
super(itemView);
title = (TextView) itemView.findViewById(R.id.item_list);
image = (ImageView) itemView.findViewById(R.id.icons);
}
}
}
NavigationDrawerFragment.java (posted code only related to recyclerview)
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View layout = inflater.inflate(R.layout.fragment_navigation_drawer, container, false);
recyclerView = (RecyclerView) layout.findViewById(R.id.drawerList);
adapter = new RecyclerAdapter(getActivity(),getData());
recyclerView.setAdapter(adapter);
recyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));
return layout;
}
public static List<MenuList> getData(){
List<MenuList> data = new ArrayList<>();
String[] titles={"Home","My Travels","Browse Photos"};
int[] icons ={R.drawable.home,R.drawable.sunglasses,R.drawable.tooltip_image};
for(int i=0;i<3;i++){
MenuList current = new MenuList();
current.title=titles[i];
current.icon=icons[i];
Log.d("Data",titles[i]);
Log.d("Data",""+icons[i]);
data.add(current);
}
return data;
}
itemlist.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView
android:padding="8dp"
android:id="#+id/icons"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/ic_menu_camera"
android:layout_gravity="center_vertical"
/>
<TextView
android:id="#+id/item_list"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Dummy Text"
android:layout_gravity="center_vertical"
android:padding="8dp"
/>
</LinearLayout>
What I was able to do is to check all the passed data (all seems to be fine). Also tried to change width to wrap content (fragment_navigation_drawer.xml) in both my relative and linear layout but I just lose my background color instead and still only one item appear.
Change height of LinearLayout to wrap_content in itemlist.xml
android:layout_height="wrap_content"
Just change the height of your itemlist from "match_parent" to "wrap_content".

Android | RecyclerView doesn't work

I want to create a RecyclerView with a few CardViews in it. For this I start a Thread in the onCreate of my Activity. There I get the data from my server and put this in a list. Then I create the Adapter for the RecyclerView, but it doesn't work. Here is my code:
Here I create the adapter and it should fill all CardViews in:
ListAdapter adapter = new ListAdapter(posts);
RecyclerView rv = (RecyclerView)findViewById(R.id.post_list);
rv.setAdapter(adapter);
Here is my adapter class
public class ListAdapter extends RecyclerView.Adapter<ListAdapter.PostViewHolder> {
List<Post> posts;
public ListAdapter(List<Post> posts) {
Log.d("ListAdapter", "");
this.posts = posts;
}
#Override
public int getItemCount() {
Log.d("getItemCount", "");
return posts.size();
}
#Override
public PostViewHolder onCreateViewHolder(ViewGroup viewGroup, int i) {
Log.d("onCreateView", "");
View v = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.insert_layout, viewGroup, false);
PostViewHolder pvh = new PostViewHolder(v);
return pvh;
}
#Override
public void onBindViewHolder(PostViewHolder postViewHolder, int i) {
Log.d("onBindView", "");
postViewHolder.username.setText(posts.get(i).getUsername());
postViewHolder.text.setText(posts.get(i).getText());
postViewHolder.time.setText(Long.toString(posts.get(i).getTime()));
postViewHolder.postPhoto.setImageResource(posts.get(i).returnIMG());
}
#Override
public void onAttachedToRecyclerView(RecyclerView recyclerView) {
Log.d("onAttached", "");
super.onAttachedToRecyclerView(recyclerView);
}
public static class PostViewHolder extends RecyclerView.ViewHolder {
CardView cv;
TextView username;
TextView time;
TextView text;
ImageView postPhoto;
PostViewHolder(View itemView) {
super(itemView);
Log.d("PostViewHolder", "");
cv = (CardView) itemView.findViewById(R.id.cv);
username = (TextView) itemView.findViewById(R.id.usernameText);
time = (TextView) itemView.findViewById(R.id.timeText);
text = (TextView) itemView.findViewById(R.id.textText);
postPhoto = (ImageView) itemView.findViewById(R.id.postPhoto);
}
}
Here is my recyclerview:
<android.support.v7.widget.RecyclerView
android:id="#+id/post_list"
android:layout_width="match_parent"
android:layout_height="match_parent">
</android.support.v7.widget.RecyclerView>
and my cardview:
<android.support.v7.widget.CardView android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/cv"
xmlns:android="http://schemas.android.com/apk/res/android">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
>
<ImageView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/postPhoto"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/textText"
android:layout_alignParentTop="true"
android:textSize="30sp"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/usernameText"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/timeText"/>
</RelativeLayout>
</android.support.v7.widget.CardView>
And here the 2 errors:
E/RecyclerView: No adapter attached; skipping layout E/RecyclerView:
No layout manager attached; skipping layout
But I do both, dont't I?
set the LayoutManager first.
From your code :
ListAdapter adapter = new ListAdapter(posts);
RecyclerView rv = (RecyclerView)findViewById(R.id.post_list);
rv.setHasFixedSize(true);
LinearLayoutManager layoutManager = new LinearLayoutManager(this);
layoutManager.setOrientation(LinearLayoutManager.VERTICAL);
rv.setLayoutManager(layoutManager);
rv.setAdapter(adapter);
Hope this will help you!!..

Categories

Resources