i have an Activity
with tabLayout
, each tab is represented by a Fragment
(i have three fragments : Home, Caegories and my account), so i want to display on the HomeFragment
an horizontal RecyclerView
,everything works well but the RecyclerView doesn't work, where i get this eroor :
E/RecyclerView: No adapter attached; skipping layout.
I havre already tested this solution but i got nothing.
this what i did:
First: ActivityMain and xml layout:
private ArrayList<OfferItem> dataList;
private RecyclerView myRecycler;
private View fragmentViewLayout;
private LastOffersRecyclerAdapter listAdapter;
//......
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
setViewPager();
setTabIcons();
mTabLayout.addOnTabSelectedListener(new TabLayout.ViewPagerOnTabSelectedListener(mViewPager));
mTabLayout.addOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
@Override
public void onTabSelected(TabLayout.Tab tab) {
int tabPoaition = tab.getPosition();
if(tabPoaition == 0){
View view = tab.getCustomView();
TextView tabTextView = (TextView)view.findViewById(R.id.tabRoot);
tabTextView.setTextColor(Color.parseColor("#ef4437"));
tabTextView.setCompoundDrawablesWithIntrinsicBounds(0, R.drawable.ic_home_red, 0, 0);
}
else if(tabPoaition ==1){
View view = tab.getCustomView();
TextView tabTextView = (TextView)view.findViewById(R.id.tabRoot);
tabTextView.setTextColor(Color.parseColor("#ef4437"));
tabTextView.setCompoundDrawablesWithIntrinsicBounds(0, R.drawable.ic_category_red, 0, 0);
}
else{
View view = tab.getCustomView();
TextView tabTextView = (TextView)view.findViewById(R.id.tabRoot);
tabTextView.setTextColor(Color.parseColor("#ef4437"));
tabTextView.setCompoundDrawablesWithIntrinsicBounds(0, R.drawable.ic_person_red, 0, 0);
}
}
@Override
public void onTabUnselected(TabLayout.Tab tab) {
int tabPoaition = tab.getPosition();
if(tabPoaition == 0){
View view = tab.getCustomView();
TextView tabTextView = (TextView)view.findViewById(R.id.tabRoot);
tabTextView.setTextColor(Color.parseColor("#bdbcbc"));
tabTextView.setCompoundDrawablesWithIntrinsicBounds(0, R.drawable.ic_home_gray, 0, 0);
}
else if(tabPoaition ==1){
View view = tab.getCustomView();
TextView tabTextView = (TextView)view.findViewById(R.id.tabRoot);
tabTextView.setTextColor(Color.parseColor("#bdbcbc"));
tabTextView.setCompoundDrawablesWithIntrinsicBounds(0, R.drawable.ic_category_gray, 0, 0);
}
else{
View view = tab.getCustomView();
TextView tabTextView = (TextView)view.findViewById(R.id.tabRoot);
tabTextView.setTextColor(Color.parseColor("#bdbcbc"));
tabTextView.setCompoundDrawablesWithIntrinsicBounds(0, R.drawable.ic_person_gray, 0, 0);
}
}
@Override
public void onTabReselected(TabLayout.Tab tab) {
}
});
fragmentViewLayout = getLayoutInflater().inflate(R.layout.fragment_one, null);
listAdapter = new LastOffersRecyclerAdapter(this,dataList);
myRecycler = (RecyclerView)fragmentViewLayout.findViewById(R.id.my_list_recycler);
myRecycler.setLayoutManager(new LinearLayoutManager(getApplicationContext(), LinearLayoutManager.HORIZONTAL, false));
myRecycler.setItemAnimator(new DefaultItemAnimator());
myRecycler.setAdapter(listAdapter);
createSampleData();
}
///...
private void createSampleData(){
dataList = new ArrayList<OfferItem>();
OfferItem itm;
for(int i =0 ;i<10 ; i++){
itm = new OfferItem(i,"business_logo.png","offer_img.png","La Piscope","Fes");
dataList.add(itm);
}
listAdapter.notifyDataSetChanged();
}
activity_main.xml
<android.support.constraint.ConstraintLayout 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"
tools:context="com.biliki.biliki.bilikiapp.MainActivity">
<android.support.design.widget.AppBarLayout
android:id="@+id/my_home_app_bar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:theme="@style/AppTheme.AppBarOverlay">
<android.support.design.widget.TabLayout
android:id="@+id/tab"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:tabGravity="fill"
app:tabMode="fixed"
app:tabBackground="?attr/selectableItemBackground"
/>
</android.support.design.widget.AppBarLayout>
<include
android:layout_height="wrap_content"
android:layout_width="match_parent"
app:layout_constraintTop_toBottomOf="@id/my_home_app_bar"
app:layout_constraintLeft_toLeftOf="parent"
layout="@layout/content_main" />
</android.support.constraint.ConstraintLayout>
Second : my AdapterClass LastOffersRecylerAdapter.java
public class LastOffersRecyclerAdapter extends RecyclerView.Adapter<LastOffersRecyclerAdapter.OffersItemHolder> {
private Context context;
private ArrayList<OfferItem> dataList;
public LastOffersRecyclerAdapter(Context context, ArrayList<OfferItem> dataList) {
this.context = context;
this.dataList = dataList;
}
@Override
public OffersItemHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.last_offers_item,parent,false);
OffersItemHolder offHolder = new OffersItemHolder(v);
return offHolder;
}
@Override
public void onBindViewHolder(OffersItemHolder holder, int position) {
OfferItem item = dataList.get(position);
int logoResourceId = context.getResources().getIdentifier(item.getLogoUrl(), "drawable", context.getPackageName());
int offerImgResourceId = context.getResources().getIdentifier(item.getOfferImgUrl(), "drawable", context.getPackageName());
holder.busniessLogo.setImageResource(logoResourceId);
holder.offerImg.setImageResource(offerImgResourceId);
holder.offer_business_city.setText(item.getBusiness_name() + "| "+item.getBusiness_city());
}
@Override
public int getItemCount() {
return dataList.size();
}
public class OffersItemHolder extends RecyclerView.ViewHolder{
public ImageView offerImg;
public ImageView busniessLogo;
public TextView offer_business_city;
public OffersItemHolder(View itemView) {
super(itemView);
this.offerImg = itemView.findViewById(R.id.offer_img);
this.busniessLogo = itemView.findViewById(R.id.business_logo);
this.offer_business_city = itemView.findViewById(R.id.business_name_city);
}
}
and this is the xml layout for HomeFragment
<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:padding="5dp">
<RelativeLayout
android:id="@+id/title_layout"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_margin="5dp"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent">
<com.biliki.biliki.bilikiapp.uitemplate.font.RobotoTextView
android:id="@+id/itemTitle"
android:paddingLeft="10dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_centerVertical="true"
android:layout_gravity="center_vertical"
android:layout_toLeftOf="@+id/btnMore"
android:text="@string/lastoffersTitle" />
<Button
android:id="@+id/btnMore"
style="@style/allButtonsstyle"
android:layout_width="65dp"
android:layout_height="42dp"
android:textAlignment="gravity"
android:layout_alignParentEnd="true"
android:layout_alignParentRight="true"
android:layout_centerVertical="true"
android:text="@string/moreLastOffers" />
</RelativeLayout>
<android.support.v7.widget.RecyclerView
android:id="@+id/my_list_recycler"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="5dp"
android:orientation="horizontal"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintTop_toBottomOf="@id/title_layout" />
</android.support.constraint.ConstraintLayout>
the TextView
and the button are displayed but the RecyclerView
not.
could you please resolve this? thnak you.
UPDATE:
the content_main
xml file contains the tabLayout ViewPager
:
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:tools="http://schemas.android.com/tools"
tools:showIn="@layout/activity_main">
<android.support.v4.view.ViewPager
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/tabs_pager"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</android.support.constraint.ConstraintLayout>