I am a novice Java/Android studio student, just blindly following examples on the internet.
I wanted to make a CardView inside a fragment, but most of the examples out there were putting cardview inside an activity, so I searched and changed the appropriate codes. After some time tinkering, I managed to make it build without errors.
However, cardview is not showing up in RecyclerView.
I suspect it is a layout issue, but after all the changes with layout_height and layout_width, I can't get cardview to be seen.
Any help will be appreciated.
My code:
HomeFragment.java
public class HomeFragment extends Fragment implements View.OnClickListener {
final int ITEM_SIZE = 5;
public View onCreateView(#NonNull LayoutInflater inflater, #Nullable ViewGroup container, #Nullable Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.fragment_home, container, false);
Button bh = v.findViewById(R.id.bh);
bh.setOnClickListener(this);
RecyclerView rv = (RecyclerView) v.findViewById(R.id.recyclerview);
LinearLayoutManager layoutManager = new LinearLayoutManager(getActivity());
rv.setHasFixedSize(true);
rv.setLayoutManager(layoutManager);
List<Item> items = new ArrayList<>();
Item[] item = new Item[ITEM_SIZE];
item[0] = new Item(R.drawable.a, "#1");
item[1] = new Item(R.drawable.b, "#2");
item[2] = new Item(R.drawable.c, "#3");
item[3] = new Item(R.drawable.d, "#4");
item[4] = new Item(R.drawable.e, "#5");
RecyclerAdapter adaptor = new RecyclerAdapter(getActivity(), items, R.layout.fragment_home);
rv.setAdapter(adaptor);
return v;
}
#Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.bh:
((BottomNavigationView)getActivity().findViewById(R.id.nav_view)).setSelectedItemId(R.id.navigation_settings);
break;
}
}
}
RecyclerAdapter.java
public class RecyclerAdapter extends RecyclerView.Adapter<RecyclerAdapter.ViewHolder> {
Context context;
List<Item> items;
int item_layout;
public RecyclerAdapter(Context context, List<Item> items, int item_layout) {
this.context = context;
this.items = items;
this.item_layout = item_layout;
}
#Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.fragment_home, null);
return new ViewHolder(v);
}
#Override
public void onBindViewHolder(ViewHolder holder, int position) {
final Item item = items.get(position);
Drawable drawable = ContextCompat.getDrawable(context, item.getImage());
holder.image.setBackground(drawable);
holder.title2.setText(item.getTitle());
holder.cardview.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Toast.makeText(context, item.getTitle(), Toast.LENGTH_SHORT).show();
}
});
}
#Override
public int getItemCount() {
return this.items.size();
}
public class ViewHolder extends RecyclerView.ViewHolder {
ImageView image;
TextView title2;
CardView cardview;
public ViewHolder(View itemView) {
super(itemView);
image = (ImageView) itemView.findViewById(R.id.image);
title2 = (TextView) itemView.findViewById(R.id.title2);
cardview = (CardView) itemView.findViewById(R.id.cardview);
}
}
}
Item.java
public class Item {
int image;
String title;
int getImage() { return this.image; }
String getTitle() { return this.title; }
public Item(int image, String title) {
this.image = image;
this.title = title;
}
}
fragment_home.xml
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.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:id="#+id/MainLayout"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="#+id/text_home"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/title_home"
android:textSize="40sp"
android:textStyle="bold"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.952"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<androidx.recyclerview.widget.RecyclerView
android:id="#+id/recyclerview"
android:layout_width="0dp"
android:layout_height="wrap_content"
app:layout_constraintBottom_toTopOf="#+id/bh"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/text_home" />
<Button
android:id="#+id/bh"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="50dp"
android:text="#string/title_settings"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
card.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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="match_parent"
android:orientation="vertical">
<com.google.android.material.card.MaterialCardView
android:id="#+id/cardview"
android:layout_width="match_parent"
android:layout_height="200dp"
android:clickable="true"
android:focusable="true"
android:foreground="?android:attr/selectableItemBackground"
app:cardCornerRadius="20dp"
app:cardElevation="4dp">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<ImageView
android:id="#+id/image"
android:layout_width="match_parent"
android:layout_height="150dp"
android:scaleType="centerCrop" />
<TextView
android:id="#+id/title2"
android:layout_width="match_parent"
android:layout_height="50dp"
android:layout_margin="5dp"
android:text="TextView"
android:textSize="25sp" />
</LinearLayout>
</com.google.android.material.card.MaterialCardView>
Seems you are inflating the wrong layout in RecyclerAdapter.java
try this
public class RecyclerAdapter extends RecyclerView.Adapter<RecyclerAdapter.ViewHolder> {
Context context;
List<Item> items;
int item_layout;
public RecyclerAdapter(Context context, List<Item> items, int item_layout) {
this.context = context;
this.items = items;
this.item_layout = item_layout;
}
#Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.card, null);
return new ViewHolder(v);
}
#Override
public void onBindViewHolder(ViewHolder holder, int position) {
final Item item = items.get(position);
Drawable drawable = ContextCompat.getDrawable(context, item.getImage());
holder.image.setBackground(drawable);
holder.title2.setText(item.getTitle());
holder.cardview.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Toast.makeText(context, item.getTitle(), Toast.LENGTH_SHORT).show();
}
});
}
#Override
public int getItemCount() {
return this.items.size();
}
public class ViewHolder extends RecyclerView.ViewHolder {
ImageView image;
TextView title2;
CardView cardview;
public ViewHolder(View itemView) {
super(itemView);
image = (ImageView) itemView.findViewById(R.id.image);
title2 = (TextView) itemView.findViewById(R.id.title2);
cardview = (CardView) itemView.findViewById(R.id.cardview);
}
}
Related
The view inside recycler view is not aligned properly. The items inside recyclerview are having different height on adding new item.
I tried of using display metrics but could not achieve it. My item list can be changed by adding or deleting items but the view padding, width and height must h=not get disturbed.
categoryRecyclerView = findViewById(R.id.categoriesRecyclerView);
GridLayoutManager gridLayoutManager = new GridLayoutManager(this, 1,GridLayoutManager.HORIZONTAL, false);
categoryRecyclerView.setLayoutManager(gridLayoutManager);
categoryRecyclerView.addItemDecoration(new ItemDecorator(getResources().getDimensionPixelSize(R.dimen.item_spacing), 3));
CategoryItemAdapter categoryItemAdapter = new CategoryItemAdapter(this, categoryList);
categoryRecyclerView.setAdapter(categoryItemAdapter);
Code for Adapter
public class CategoryItemAdapter extends RecyclerView.Adapter<CategoryItemAdapter.MyViewHolder> {
private Context context;
private List<Category> categoryList;
private onItemClickListener mItemClickListener;
public CategoryItemAdapter(Context context, List<Category> categoryList) {
this.context = context;
this.categoryList = categoryList;
}
#NonNull
#Override
public MyViewHolder onCreateViewHolder(#NonNull ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.category_item, parent, false);
return new MyViewHolder(view);
}
#Override
public void onBindViewHolder(#NonNull CategoryItemAdapter.MyViewHolder holder, int position) {
Category category = (Category) categoryList.get(position);
holder.textView.setText(category.getCategoryName());
}
#Override
public int getItemCount() {
return categoryList.size();
}
public void setOnItemClickListener(onItemClickListener mItemClickListener) {
this.mItemClickListener = mItemClickListener;
}
public interface onItemClickListener {
void onItemClickListener(View view, int position, Category category);
}
class MyViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
CardView cardview;
TextView textView;
ImageView selectItem;
RelativeLayout selectItemLayout;
LinearLayout linearLayout;
public MyViewHolder(#NonNull View itemView) {
super(itemView);
cardview = itemView.findViewById(R.id.cardview);
textView = itemView.findViewById(R.id.categoryName);
linearLayout = itemView.findViewById(R.id.llbackground);
selectItemLayout = itemView.findViewById(R.id.selectItemLayout);
selectItem = itemView.findViewById(R.id.selectItem);
selectItemLayout.setVisibility(authenticationHelper.isAdmin() ? View.VISIBLE : View.GONE);
itemView.setOnClickListener(this);
Util.setRandomGradientColor(linearLayout, GradientDrawable.Orientation.LEFT_RIGHT);
}
#Override
public void onClick(View view) {
if (mItemClickListener != null) {
mItemClickListener.onItemClickListener(view, getAdapterPosition(), categoryList.get(getAdapterPosition()));
}
}
}
}
Code for view
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:card_view="http://schemas.android.com/apk/res-auto"
android:layout_height="wrap_content"
android:layout_width="wrap_content">
<androidx.cardview.widget.CardView
android:id="#+id/cardview"
android:layout_width="150dp"
android:layout_height="100dp"
card_view:cardCornerRadius="5dp"
card_view:cardElevation="2dp">
<LinearLayout
android:id="#+id/llbackground"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:background="#drawable/category_gradient"
android:gravity="center">
<TextView
android:id="#+id/categoryName"
android:text="Men"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="18sp"
android:textColor="#color/colorWhite"
android:textStyle="bold"/>
</LinearLayout>
<RelativeLayout
android:id="#+id/selectItemLayout"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:elevation="10dp"
android:padding="5dp">
<ImageView
android:id="#+id/selectItem"
android:layout_width="20dp"
android:layout_height="20dp"
android:clickable="false"
android:tag="#string/unchecked"
android:enabled="false"
android:layout_alignParentBottom="true"
android:layout_alignParentRight="true"
android:src="#drawable/circle"/>
</RelativeLayout>
</androidx.cardview.widget.CardView>
</RelativeLayout>
Recycler view in activity
<androidx.recyclerview.widget.RecyclerView
android:id="#+id/categoriesRecyclerView"
android:layout_below="#+id/categoriesLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_centerHorizontal="true"/>
Thanks for help!!
So, for a personal project I'm making a stream app for Android which are separated by three fragments in the main screen where each of those fragment will be a list of a type of show in our archive, like movies, TV series and animations by using a RecyclerView inside of them.
That being said, after some errors that I managed to fix, the app is not showing what was expected, by skipping the RecyclerView because it has no "adapter".
Here is the code so far:
The main activity:
public class MenuPrincipal extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.menu_principal);
AbasMenuPrincipalAdapter adapter = new AbasMenuPrincipalAdapter( getSupportFragmentManager() );
adapter.adicionar( new FilmesFragment() , "Filmes");
adapter.adicionar( new SeriesFragment() , "Series");
adapter.adicionar( new AnimacoesFragment() , "Animações");
ViewPager viewPager = (ViewPager) findViewById(R.id.abas_view_pager);
viewPager.setAdapter(adapter);
TabLayout tabLayout = (TabLayout) findViewById(R.id.abas);
tabLayout.setupWithViewPager(viewPager);
}
}
One of the fragments used on the main screen:
public class AnimacoesFragment extends Fragment {
private RecyclerView mRecyclerView;
private RecyclerView.Adapter mAdapter;
private RecyclerView.LayoutManager mLayoutManager;
ArrayList<Item> exemploItemList = new ArrayList<Item>();
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.animacoes_fragment, container, false);
TextView tv = view.findViewById(R.id.text1);
tv.setText("Você está na terceira aba");
exemploItemList.add(new Item(R.drawable.ic_android, "Line1", "line2"));
exemploItemList.add(new Item(R.drawable.ic_desktop_mac, "Line2", "line3"));
exemploItemList.add(new Item(R.drawable.ic_laptop_windows, "Line4", "line5"));
mRecyclerView = (RecyclerView) view.findViewById(R.id.AnimacaoRecyclerView);
mRecyclerView.setHasFixedSize(true);
LinearLayoutManager llm = new LinearLayoutManager(getActivity());
llm.setOrientation(LinearLayoutManager.VERTICAL);
mRecyclerView.setLayoutManager(llm);
ItemAdapter adapter = new ItemAdapter(exemploItemList);
mAdapter = adapter;
mRecyclerView.setAdapter(mAdapter);
return view;
}
}
And its layout:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal">
<TextView
android:id="#+id/text1"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<androidx.recyclerview.widget.RecyclerView
android:id="#+id/AnimacaoRecyclerView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="4dp"
android:scrollbars="vertical" />
</LinearLayout>
This is what I'm trying to show on that RecyclerView (later will be the real thing, now it's just a test):
public class Item {
private int mImageResource;
private String text1;
private String text2;
public Item(int mImageResource, String text1, String text2) {
this.mImageResource = mImageResource;
this.text1 = text1;
this.text2 = text2;
}
public int getmImageResource() {
return mImageResource;
}
public void setmImageResource(int mImageResource) {
this.mImageResource = mImageResource;
}
public String getText1() {
return text1;
}
public void setText1(String text1) {
this.text1 = text1;
}
public String getText2() {
return text2;
}
public void setText2(String text2) {
this.text2 = text2;
}
}
It's layout:
<?xml version="1.0" encoding="utf-8"?>
<androidx.cardview.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
xmlns:app="http://schemas.android.com/apk/res-auto"
app:cardCornerRadius="4dp">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_margin="4dp">
<ImageView
android:id="#+id/imageView"
android:layout_width="50dp"
android:layout_height="50dp"
android:contentDescription="#string/imagem"
android:padding="2dp" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentStart="true"
android:layout_alignParentTop="true"
android:layout_marginStart="50dp"
android:layout_marginTop="0dp"
android:text="#string/line1"
android:textColor="#000000"
android:textSize="20sp"
android:textStyle="bold"
android:id="#+id/textView"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentStart="true"
android:layout_alignParentTop="true"
android:layout_marginStart="50dp"
android:layout_marginTop="28dp"
android:text="#string/line2"
android:textSize="15sp"
android:textStyle="bold"
android:id="#+id/textView2"/>
</RelativeLayout>
</androidx.cardview.widget.CardView>
Finally, the adapter used to convert the list:
public class ItemAdapter extends RecyclerView.Adapter<ItemAdapter.ItemViewHolder> {
private ArrayList<Item> mItemList;
public static class ItemViewHolder extends RecyclerView.ViewHolder{
public ImageView mImageView;
public TextView mTextView1;
public TextView mTextView2;
public ItemViewHolder(#NonNull View itemView) {
super(itemView);
mImageView = itemView.findViewById(R.id.imageView);
mTextView1 = itemView.findViewById(R.id.textView);
mTextView2 = itemView.findViewById(R.id.textView2);
}
}
#NonNull
#Override
public ItemViewHolder onCreateViewHolder(#NonNull ViewGroup parent, int viewType) {
View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.item, parent, false);
ItemViewHolder ivh = new ItemViewHolder(v);
return ivh;
}
public ItemAdapter(ArrayList<Item> itemList){
mItemList = itemList;
}
#Override
public void onBindViewHolder(#NonNull ItemViewHolder holder, int position) {
Item currentItem = mItemList.get(position);
holder.mImageView.setImageResource(currentItem.getmImageResource());
holder.mTextView1.setText(currentItem.getText1());
holder.mTextView2.setText(currentItem.getText2());
}
#Override
public int getItemCount() {
return mItemList.size();
}
}
First of all, check your fragment layout file. The linear layout parent orientation set to 'horizontal' and width of it's child views ('textview' & 'recyclerview') is match parent. This won't work.
Try to initialize your recyclerview adapter in 'onCreate' and use 'notifyDataSetChanged()' after setting up the adapter with recyclerview.
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>
As Mr. Mike M mentioned, i have moved the code to the HomeFragment Class, and it works.
this is the HomeFragment class:
public class HomeFragment extends Fragment {
public static final String TITLE = "Accueil";
private RecyclerView myRecycler;
private LastOffersRecyclerAdapter listAdapter;
private ArrayList<OfferItem> dataList;
public static HomeFragment newInstance() {
return new HomeFragment ();
#Nullable
#Override
public View onCreateView(LayoutInflater inflater, #Nullable ViewGroup container, Bundle savedInstanceState) {
createSampleData();
View rootView = inflater.inflate(R.layout.fragment_one, container, false);
listAdapter = new LastOffersRecyclerAdapter(getContext(),dataList);
myRecycler = rootView.findViewById(R.id.my_list_recycler);
myRecycler.setLayoutManager(new LinearLayoutManager(getContext(), LinearLayoutManager.HORIZONTAL, false));
myRecycler.setItemAnimator(new DefaultItemAnimator());
myRecycler.setAdapter(listAdapter);
return rootView;
}
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);
}
}
}
Creating a tabbed fragment activity, in my home_fragment i am trying to display a horizontal and a vertical list view, but its not displaying the list, below is my code of home_fragment for onCreateView() method --
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View v=inflater.inflate(R.layout.fragment_home, container, false);
horizontalList = (RecyclerView)v.findViewById(R.id.horizontal_recycler);
verticalList = (RecyclerView)v.findViewById(R.id.recyle_view);
horizontalList.setHasFixedSize(true);
verticalList.setHasFixedSize(true);
//set horizontal LinearLayout as layout manager to creating horizontal list view
LinearLayoutManager horizontalManager = new LinearLayoutManager(getContext(), LinearLayoutManager.HORIZONTAL, false);
horizontalList.setLayoutManager(horizontalManager);
horizontalAdapter = new HorizontalListAdapter(getActivity());
horizontalList.setAdapter(horizontalAdapter);
//set vertical LinearLayout as layout manager for vertial listview
LinearLayoutManager layoutManager = new LinearLayoutManager(getContext(), LinearLayoutManager.VERTICAL, false);
verticalList.setLayoutManager(layoutManager);
verticalAdapter = new VerticalListAdapter(getActivity());
verticalList.setAdapter(verticalAdapter);
return v;
}
Is there any changes to be made here.
HorizontalListAdapter.java --
public class HorizontalListAdapter extends RecyclerView.Adapter<HorizontalListAdapter.ViewHolder>{
private Activity activity;
public HorizontalListAdapter(Activity activity) {
this.activity = activity;
}
#Override
public ViewHolder onCreateViewHolder(ViewGroup viewGroup, int viewType) {
LayoutInflater inflater = activity.getLayoutInflater();
View view = inflater.inflate(R.layout.item_horizontal_list, viewGroup, false);
return new ViewHolder(view);
}
#Override
public void onBindViewHolder(HorizontalListAdapter.ViewHolder viewHolder, final int position) {
viewHolder.linearLayout.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Toast.makeText(activity, "Position clicked: " + position, Toast.LENGTH_SHORT).show();
}
});
}
#Override
public int getItemCount() {
return 10;
}
/**
* View holder to display each RecylerView item
*/
protected class ViewHolder extends RecyclerView.ViewHolder {
private LinearLayout linearLayout;
public ViewHolder(View view) {
super(view);
linearLayout = (LinearLayout) view.findViewById(R.id.layout);
}
}
}
item_horizontal_list.xml--
<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="210dp"
android:layout_height="wrap_content"
android:layout_marginRight="5dp">
<LinearLayout
android:id="#+id/layout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<ImageView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:scaleType="fitXY"
android:src="#drawable/mountain" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="10dp"
android:text="#string/lorem_ipsum" />
</LinearLayout>
</android.support.v7.widget.CardView>
Not displaying horizontal and vertical list in fragment
in your onBindViewHolder you are no displaying any data check out your adapter class
You need to set any data inside your onBindViewHolder like this
Sample code
public class HorizontalListAdapter extends RecyclerView.Adapter<HorizontalListAdapter.ViewHolder> {
private Activity activity;
public HorizontalListAdapter(Activity activity) {
this.activity = activity;
}
#Override
public ViewHolder onCreateViewHolder(ViewGroup viewGroup, int viewType) {
LayoutInflater inflater = activity.getLayoutInflater();
View view = inflater.inflate(R.layout.item_horizontal_list, viewGroup, false);
return new ViewHolder(view);
}
#Override
public void onBindViewHolder(HorizontalListAdapter.ViewHolder viewHolder, final int position) {
viewHolder.linearLayout.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Toast.makeText(activity, "Position clicked: " + position, Toast.LENGTH_SHORT).show();
}
});
viewHolder.edtName.setText("NILU");
viewHolder.imageView.setImageResource(R.mipmap.ic_launcher);
}
#Override
public int getItemCount() {
return 10;
}
/**
* View holder to display each RecylerView item
*/
protected class ViewHolder extends RecyclerView.ViewHolder {
private LinearLayout linearLayout;
TextView edtName, edtPhone, edtPass, edtAdrress;
ImageView imageView;
public ViewHolder(View view) {
super(view);
linearLayout = (LinearLayout) view.findViewById(R.id.layout);
edtName = view.findViewById(R.id.edtUname);
imageView = view.findViewById(R.id.imageView);
}
}
}
Changes in your layout you need to set id to your imageview and textview
<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="210dp"
android:layout_height="wrap_content"
android:layout_marginRight="5dp">
<LinearLayout
android:id="#+id/layout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<ImageView
android:id="#+id/imageView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:scaleType="fitXY"
android:src="#drawable/mountain" />
<TextView
android:id="#+id/edtUname"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="10dp"
android:text="#string/lorem_ipsum" />
</LinearLayout>
</android.support.v7.widget.CardView>
here is the good tutorial of Android working with Card View and Recycler View
I have a RecyclerView that contains a bunch of CardViews. Each CardView then has its own RecyclerView in it. The problem is that only the "main" RecyclerView is scrolling. The nested one in the CardView is not getting focus, so I can't scroll it. How can i make it so that both will be scrollable?
Parent Layout
<android.support.design.widget.CoordinatorLayout android:id="#+id/coordinatorLayout_main"
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="false">
<android.support.v7.widget.RecyclerView
android:id="#+id/recyclerView_savedFiles"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:clipToPadding="false"
android:padding="8dp"/>
</android.support.design.widget.CoordinatorLayout>
Parent Adapter
public class AdapterSavedFilesCards extends RecyclerView.Adapter<AdapterSavedFilesCards.ViewHolderSavedFilesCard>{
/**
* List of cards in the recycler view
*/
private final ArrayList<CardViewSavedFiles> cards;
private final Context context;
public AdapterSavedFilesCards(final Context context){
this.context = context;
cards = new ArrayList<>(Arrays.asList(new CardViewSavedFiles[]{
new CardViewSavedFiles("Card One"),
new CardViewSavedFiles("Card Two"),
new CardViewSavedFiles("Card Three")
}));
}
#Override
public ViewHolderSavedFilesCard onCreateViewHolder(ViewGroup parent, int viewType){
final View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.card_view_saved_files, parent, false);
return new AdapterSavedFilesCards.ViewHolderSavedFilesCard(view);
}
#Override
public void onBindViewHolder(ViewHolderSavedFilesCard holder, int position){
//Get the card at this position
final CardViewSavedFiles card = cards.get(position);
holder.recyclerView.setLayoutManager(new LinearLayoutManager(context));
holder.recyclerView.setItemAnimator(null);
holder.recyclerView.setHasFixedSize(true);
holder.recyclerView.setAdapter(card.getAdapterSavedFiles());
holder.title.setText(card.getTitle());
holder.subTitle.setText("Subtitle");
((CardView)holder.itemView).setCardBackgroundColor(ContextCompat.getColor(holder.itemView.getContext(), card.getBackgroundColor()));
}
#Override
public int getItemCount(){
return cards.size();
}
public ArrayList<CardViewSavedFiles> getCards(){
return cards;
}
protected class ViewHolderSavedFilesCard extends RecyclerView.ViewHolder{
private final TextView title;
private final TextView subTitle;
private final RecyclerView recyclerView;
public ViewHolderSavedFilesCard(final View itemView){
super(itemView);
title = (TextView) itemView.findViewById(R.id.title);
subTitle = (TextView) itemView.findViewById(R.id.subTitle);
recyclerView = (RecyclerView) itemView.findViewById(R.id.recyclerView);
}
}
}
Child Layout
<android.support.v7.widget.CardView
android:id="#+id/cardView_savedFactors"
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<LinearLayout
android:id="#+id/rootView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<TextView
android:id="#+id/title"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
<TextView
android:id="#+id/subTitle"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
<android.support.v7.widget.RecyclerView
android:id="#+id/recyclerView"
android:layout_width="match_parent"
android:layout_height="128dp"
android:paddingStart="#dimen/content_padding_start"
android:paddingEnd="#dimen/content_padding_end"/>
<Button
android:id="#+id/button"
style="?android:attr/borderlessButtonStyle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
</LinearLayout>
</android.support.v7.widget.CardView>
Child Adapter
public class AdapterSavedFiles extends RecyclerView.Adapter<AdapterSavedFiles.ViewHolderSavedFiles>{
private final ArrayList<File> files = new ArrayList<>();
private final FILE_TYPE fileType;
public AdapterSavedFiles(final Context context, final FILE_TYPE fileType){
this.fileType = fileType;
switch (fileType){
case ONE:
files.addAll(Arrays.asList(FileManager.getInstance(context).getFilesDirOne().listFiles()));
break;
case TWO:
files.addAll(Arrays.asList(FileManager.getInstance(context).getFilesDirTwo().listFiles()));
break;
}
}
#Override
public ViewHolderSavedFiles onCreateViewHolder(ViewGroup parent, int viewType){
final View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.list_item_saved_file, parent, false);
return new AdapterSavedFiles.ViewHolderSavedFiles(view);
}
#Override
public void onBindViewHolder(ViewHolderSavedFiles holder, int position){
holder.fileName.setText("List item " + position);
switch (fileType){
case ONE:
holder.icon.setBackgroundTintList(ColorStateList.valueOf(ContextCompat.getColor(holder.itemView.getContext(), R.color.primary)));
holder.icon.setText("P");
break;
case TWO:
holder.icon.setBackgroundTintList(ColorStateList.valueOf(ContextCompat.getColor(holder.itemView.getContext(), R.color.green)));
holder.icon.setText("F");
break;
}
}
#Override
public int getItemCount(){
return files.size();
}
public ArrayList<File> getFiles(){
return files;
}
protected class ViewHolderSavedFiles extends RecyclerView.ViewHolder{
private final TextView fileName;
private final TextView icon;
public ViewHolderSavedFiles(final View itemView){
super(itemView);
icon = (TextView) itemView.findViewById(R.id.icon);
fileName = (TextView) itemView.findViewById(R.id.file_name);
}
}
}
Layout Screenshot
You may use ListView in Child adapter in onBindViewHolder instead of TextView.This may solve scrolls child items inside CardView.