ScreenShot of the screen from Tab(Marshmallow)
The recycler view with cardview items is showing in black. Also I cannot change background colour. But when running in android emulator it seems fine, the background is default in white color. What could i had done wrong
public class MainActivity extends AppCompatActivity {
private RecyclerView mRecyclerView;
private RecyclerView.Adapter mRecycleAdapter;
private RecyclerView.LayoutManager mLayoutManager;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mRecyclerView = (RecyclerView)findViewById(R.id.recyclerviewss);
mRecyclerView.setHasFixedSize(true);
mLayoutManager = new LinearLayoutManager(getApplicationContext());
mRecyclerView.setLayoutManager(mLayoutManager);
mRecyclerView.setItemAnimator(new DefaultItemAnimator());
mRecycleAdapter = new RentalAdapter(getApplicationContext(), 75);
mRecyclerView.setAdapter(mRecycleAdapter);
}
}
class RentalAdapter extends RecyclerView.Adapter<RentalAdapter.RentalAdapterHolder> {
private final LayoutInflater mLayoutInflater;
private Context mContext;
private int mListCount;
int lastPosition = -1;
RentalAdapter(Context context, int listCount) {
mLayoutInflater = LayoutInflater.from(context);
mContext = context;
mListCount = listCount;
}
#Override
public RentalAdapterHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = mLayoutInflater.inflate(R.layout.sampcard, parent, false);
RentalAdapter.RentalAdapterHolder rentalHolder = new RentalAdapterHolder(view);
return rentalHolder;
}
#Override
public void onBindViewHolder(final RentalAdapterHolder holder, int position) {
holder.mTextView.setText("sample");
}
#Override
public int getItemCount() {
return 75;
}
class RentalAdapterHolder extends RecyclerView.ViewHolder {
private TextView mTextView;
private ImageView mImageView;
public RentalAdapterHolder(View itemView) {
super(itemView);
mTextView = (TextView)itemView.findViewById(R.id.info_text);
}
}
}
sampcard.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="wrap_content">
<android.support.v7.widget.CardView
xmlns:card_view="http://schemas.android.com/apk/res-auto"
android:id="#+id/card_view"
android:layout_gravity="center"
android:layout_width="match_parent"
android:layout_height="50dp"
card_view:cardCornerRadius="4dp"
android:layout_marginBottom="10dp">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="ASKDNAKJDHN"
android:id="#+id/info_text"/>
</android.support.v7.widget.CardView>
</LinearLayout>
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
>
<android.support.v7.widget.RecyclerView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/recyclerviewss"
/>
</RelativeLayout>
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_gravity="center"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<android.support.v7.widget.CardView
xmlns:card_view="http://schemas.android.com/apk/res-auto"
android:id="#+id/card_view"
android:layout_gravity="center"
card_view:background_color="#color/white"
android:layout_width="match_parent"
android:layout_height="50dp"
card_view:cardCornerRadius="4dp"
android:layout_marginBottom="10dp">
<TextView
android:layout_width="wrap_content"
android:gravity="left|center_vertical"
android:paddingLeft="20dp"
android:layout_height="match_parent"
android:text="ASKDNAKJDHN"
android:id="#+id/info_text"/>
</android.support.v7.widget.CardView>
</LinearLayout>
Try this code.
Just add a background attribute android:background="#android:color/white" to your sampcard.xml layout.
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#android:color/white">
<android.support.v7.widget.CardView
xmlns:card_view="http://schemas.android.com/apk/res-auto"
android:id="#+id/card_view"
android:layout_gravity="center"
android:layout_width="match_parent"
android:layout_height="50dp"
card_view:cardCornerRadius="4dp"
android:layout_marginBottom="10dp">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="ASKDNAKJDHN"
android:id="#+id/info_text"/>
</android.support.v7.widget.CardView>
</LinearLayout>
I encountered the same problem and got it resolved by applying Background color to the root view in XML file which in you case is Recycler view.
Add this in your Recycler View XML code :
android:background = "#ffffff"
Related
I want my cards be one to another,but they spawn with extra space,i don't know how to fix this.
I thought that my problem with recycleView.
//My RVadapter
public class RVAdapter extends RecyclerView.Adapter<RVAdapter.PersonViewHolder>{
List<Bus> abstractlistbus;
public static class PersonViewHolder extends RecyclerView.ViewHolder {
CardView cv;
TextView bus_name;
TextView controller_Status;
Context mContext;
PersonViewHolder(final View itemView, final Context context) {
super(itemView);
cv = (CardView)itemView.findViewById(R.id.cv);
mContext=context;
bus_name = (TextView)itemView.findViewById(R.id.name);
controller_Status = (TextView)itemView.findViewById(R.id.Status);
}
}
RVAdapter(List<Bus> abstractlist){
this.abstractlistbus = abstractlist;
}
#Override
public int getItemCount() {
return abstractlistbus.size();
}
#Override
public PersonViewHolder onCreateViewHolder(ViewGroup viewGroup, int i) {
View v = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.cards_layout, viewGroup, false);
PersonViewHolder pvh = new PersonViewHolder(v,v.getContext());
return pvh;
}
#Override
public void onBindViewHolder(/*final*/ PersonViewHolder personViewHolder, final int i) {
personViewHolder.bus_name.setText("Номер автобуса:"+abstractlistbus.get(i).number);
personViewHolder.controller_Status.setText(abstractlistbus.get(i).controller+"");
}
}
//My activity_main.xml
<RelativeLayout 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"
xmlns:app="http://schemas.android.com/apk/res-auto"
tools:context=".MainActivity"
>
<android.support.v7.widget.RecyclerView
android:id="#+id/my_recycler_view"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:scrollbars="vertical"
/>
</RelativeLayout>
//My cards_layout.xml
<?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"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:orientation="vertical"
android:tag="cards main container">
<android.support.v7.widget.CardView
android:id="#+id/cv"
xmlns:card_view="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="wrap_content"
card_view:cardCornerRadius="10dp"
card_view:cardElevation="5dp"
card_view:cardUseCompatPadding="true">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
>
<LinearLayout
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginTop="12dp"
android:layout_weight="2"
android:orientation="vertical"
>
<TextView
android:id="#+id/name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:layout_marginTop="10dp"
android:text="Android Name"
android:textAppearance="?android:attr/textAppearanceLarge" />
<TextView
android:id="#+id/Status"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:layout_marginTop="10dp"
android:text="Android Version"
android:textAppearance="?android:attr/textAppearanceMedium"/>
</LinearLayout>
</LinearLayout>
</android.support.v7.widget.CardView>
</LinearLayout>
//And piece of code im MainActivity.java
RecyclerView rv = (RecyclerView) findViewById(R.id.my_recycler_view);
rv.setHasFixedSize(true);
LinearLayoutManager llm = new LinearLayoutManager(this);
rv.setLayoutManager(llm);
extra space between cards
enter image description here
enter image description here
enter image description here
in your card linear layout make **android:layout_height="match_parent"** to **android:layout_height="wrap_content"**
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:orientation="vertical"
android:tag="cards main container">
In your cards_layout.xml - add these margins to your linear layout.
android:layout_marginTop="5dp"
android:layout_marginBottom="5dp"
This will solve your problem.
in you adapter view change CardView's height match_parent to wrap_content
Try it with this tag in your CardView:
app:cardUseCompatPadding="true"
i'm having problem with Videoview. i have done all the settings right but still it dosen't show anything in Videoview. finally i used a library but the problem was'nt solved. i used the videoview inside the recyclerview like the example in github: (https://github.com/Krupen/AutoplayVideos), here is my code, and single_view is the layout for each item:
single_view:
<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.CardView
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:card_view="http://schemas.android.com/apk/res-auto"
xmlns:bvp="http://schemas.android.com/tools"
android:id="#+id/lay_parent_parentView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="#dimen/parent_margin"
android:layout_marginLeft="#dimen/text_TD_padding_listItem"
android:layout_marginRight="#dimen/text_TD_padding_listItem"
android:layout_marginTop="#dimen/parent_margin"
android:backgroundTint="#color/white"
card_view:cardBackgroundColor="#color/white"
card_view:cardCornerRadius="5dp"
card_view:cardElevation="5dp">
<!--style="#style/CardViewStyle"-->
<FrameLayout
android:layout_width="300dp"
android:layout_height="150dp">
<com.allattentionhere.autoplayvideos.AAH_VideoImage
android:layout_width="match_parent"
android:layout_height="match_parent" />
</FrameLayout>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="hello"/>
this is my main layout which contains the recyclerview:
<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="#+id/parent_storyDet"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:clickable="true"
android:fitsSystemWindows="false"
android:layoutDirection="rtl"
android:orientation="vertical"
>
<android.support.v4.widget.NestedScrollView
android:id="#+id/scrollView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layoutDirection="rtl"
app:layout_behavior="#string/appbar_scrolling_view_behavior"
tools:context="com.homaplus.hashtag.hashtag.Fragments.Frag_story_detail">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<com.allattentionhere.autoplayvideos.AAH_CustomRecyclerView
android:id="#+id/recycler_storyRelatedVideos"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:layout_alignParentTop="true"
android:nestedScrollingEnabled="false" />
</RelativeLayout>
</android.support.v4.widget.NestedScrollView>
</android.support.design.widget.CoordinatorLayout>
The recyclerView Adapter:
public class MyVideosAdapter extends AAH_VideosAdapter {
private List<String> list;
public class MyViewHolder extends AAH_CustomViewHolder {
//to mute/un-mute video (optional)
boolean isMuted;
public MyViewHolder(View x) {
super(x);
}
}
public MyVideosAdapter(List<String> list_urls) {
this.list = list_urls;
}
#Override
public AAH_CustomViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View itemView = LayoutInflater.from(parent.getContext())
.inflate(R.layout.row_videos, parent, false);
return new MyViewHolder(itemView);
}
#Override
public void onBindViewHolder(AAH_CustomViewHolder holder, int position) {
holder.setVideoUrl(list.get(position));
}
#Override
public int getItemCount() {
return list.size();
}
#Override
public int getItemViewType(int position) {
return 0;
}
}
finally i set the recyclerview in my fragment as folloe:
LinearLayoutManager linearLayoutManager2 = new LinearLayoutManager(getContext());
linearLayoutManager2.setOrientation(LinearLayoutManager.HORIZONTAL);
recycler_storyRelatedVideos.setActivity(getActivity());
recycler_storyRelatedVideos.setLayoutManager(linearLayoutManager2);
adapter_videos = new MyVideosAdapter(story.getList_storyVideos());
recycler_storyRelatedVideos.setHasFixedSize(true);
recycler_storyRelatedVideos.setAdapter(adapter_videos);
recycler_storyRelatedVideos.smoothScrollBy(0,1);
recycler_storyRelatedVideos.smoothScrollBy(0,-1);
I have a HorizontalScrollView inside a VerticalScrollView. If I scroll Horizontally , the scrolling is smooth. But if I scroll Vertically , the scroll is not smooth, it is getting scrolled horizontally sometimes. How to make the vertical scroll smooth ?
Here is my xml ,
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/activity_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.sasank.calendarview.MainActivity"
android:background="#android:color/white">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello World!" />
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="500dp"
android:layout_marginLeft="200dp"
android:layout_alignParentBottom="true"
android:layout_marginBottom="20dp">
<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:scrollbars="vertical">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.v7.widget.RecyclerView
android:id="#+id/chapter_list"
android:layout_width="150dp"
android:layout_height="match_parent">
</android.support.v7.widget.RecyclerView>
<HorizontalScrollView
android:id="#+id/horizontal_scroll"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_toRightOf="#id/chapter_list"
android:scrollbars="horizontal">
<android.support.v7.widget.RecyclerView
android:id="#+id/calendar"
android:layout_width="wrap_content"
android:layout_height="match_parent">
</android.support.v7.widget.RecyclerView>
</HorizontalScrollView>
</RelativeLayout>
</ScrollView>
</RelativeLayout>
</RelativeLayout>
Im using a Vertical LinearLayoutManager for Chapter_list Recyclerview and GridLayoutManager for Calendar RecyclerView
android:nestedScrollingEnabled="true"
You can try this, but still problems connected to the nested scrolling and so on are actually common. People have had the same problems here on StackOverflow, take a look, i hope it can help you: Problems with Nested Scrolling
I have vertical scrollview and horizontal recycleview photo gallery in one of my projects and it works fine. Here's the code:
fragment/activity layout:
<ScrollView 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"
xmlns:app="http://schemas.android.com/apk/res-auto"
>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
...
<android.support.v7.widget.RecyclerView
android:id="#+id/horizontal_recycler_view"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="15dp"
android:layout_marginBottom="15dp"
android:layout_marginLeft="5dp"
android:layout_marginRight="5dp"
android:horizontalSpacing="10dp"
android:isScrollContainer="false"
android:stretchMode="columnWidth"
android:verticalSpacing="10dp"
/>
...
</LinearLayout>
</ScrollView>
item:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView
android:id="#+id/horizontal_item_view_image"
android:layout_marginRight="10dp"
android:layout_width="109dp"
android:layout_height="109dp" />
</LinearLayout>
adapter:
public class HorizontalPhotosAdapter extends RecyclerView.Adapter<HorizontalPhotosAdapter.MyViewHolder> {
private Context context;
private LayoutInflater inflater;
private ArrayList<Bitmap> bitmapList;
public class MyViewHolder extends RecyclerView.ViewHolder {
private ImageView riv;
public MyViewHolder(View view) {
super(view);
riv = (ImageView) view.findViewById(R.id.horizontal_item_view_image);
}
}
public HorizontalPhotosAdapter(Context context, ArrayList<Bitmap> bitmapList, String[] imageUrls) {
this.context = context;
this.bitmapList = bitmapList;
}
#Override
public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View itemView = LayoutInflater.from(parent.getContext()).inflate(R.layout.horizontal_item_view, parent, false);
if (itemView.getLayoutParams ().width == RecyclerView.LayoutParams.MATCH_PARENT)
itemView.getLayoutParams ().width = RecyclerView.LayoutParams.WRAP_CONTENT;
return new MyViewHolder(itemView);
}
#Override
public void onBindViewHolder(final MyViewHolder holder, final int position) {
holder.riv.setImageBitmap(bitmapList.get(position));
}
#Override
public int getItemCount() {
return bitmapList.size();
}
}
implementation in fragment/activity:
horizontalAdapter=new HorizontalPhotosAdapter(getContext(), bitmapList);
horizontal_recycler_view.setAdapter(horizontalAdapter);
horizontalAdapter.notifyDataSetChanged();
LinearLayoutManager horizontalLayoutManagaer = new LinearLayoutManager(getContext(), LinearLayoutManager.HORIZONTAL, false);
horizontal_recycler_view.setLayoutManager(horizontalLayoutManagaer);
horizontal_recycler_view.setAdapter(horizontalAdapter);
I am trying a list of items using RecyclerView , everything works fine but the last item is not being displayed! The length of the list is 2 and onBindView is called twice and the when I used Log to print the items it aslo prints the last item. So the list is fine but the RecyclerView is not displaying it.
This is my code RecyclerView Adapter:
public class ModuleList extends RecyclerView.Adapter<ModuleList.ViewHolder> {
private List<Module> moduleList;
public ModuleList(List<Module> moduleList){
this.moduleList = moduleList;
}
public ModuleList(){
this.moduleList = new ArrayList<>();
}
public List<Module> getModuleList() {
return moduleList;
}
public void setModuleList(List<Module> moduleList) {
this.moduleList = moduleList;
}
#Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.list_modules,parent,false);
return new ViewHolder(view);
}
#Override
public void onBindViewHolder(ViewHolder holder, int position) {
Module module = moduleList.get(position);
// function is called twice and the all items are logged works fine
Log.d("ALL",module.getModuleName());
Log.d("ALL", String.valueOf(module.getNumberOfLevels()));
holder.moduleName.setText(String.valueOf(module.getModuleName()));
holder.numberOfLevels.setText(String.valueOf(module.getNumberOfLevels()));
}
#Override
public int getItemCount() {
//size is 2
return moduleList.size();
}
public class ViewHolder extends RecyclerView.ViewHolder{
public TextView moduleName;
public TextView numberOfLevels;
public ViewHolder(View itemView) {
super(itemView);
moduleName = (TextView) itemView.findViewById(R.id.list_module_name);
numberOfLevels = (TextView) itemView.findViewById(R.id.list_no_of_levels);
}
}
}
This is my xml where recyclerview is:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.v7.widget.RecyclerView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/all_modules_list"
android:scrollbars="vertical"
/>
</LinearLayout>
This is my xml where RecyclerView Fragment is displayed:
<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.DrawerLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="#+id/main_screen"
android:layout_height="match_parent"
android:layout_width="match_parent"
android:fitsSystemWindows="true"
>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/main_screen_container"
android:orientation="vertical"
>
<android.support.v7.widget.Toolbar
android:id="#+id/main_screen_toolbar"
android:layout_height="wrap_content"
android:layout_width="match_parent"
android:minHeight="?attr/actionBarSize"
android:background="?attr/colorPrimary"
app:theme="#style/ThemeOverlay.AppCompat.Dark.ActionBar"
app:popupTheme="#style/ThemeOverlay.AppCompat.Light"
/>
<FrameLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/fragment_container">
</FrameLayout>
</LinearLayout>
<ListView
android:layout_width="240dp"
android:layout_height="match_parent"
android:id="#+id/navigation_items"
android:layout_gravity="start"
android:background="#android:color/white"
>
</ListView>
</android.support.v4.widget.DrawerLayout>
Edit:list_module.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/list_module_name"
/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/list_no_of_levels"
/>
</LinearLayout>
Hey can you try the solution below?
Change this in the list_module.xml
<?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="wrap_content"
android:orientation="vertical">
<TextView
android:id="#+id/list_module_name"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<TextView
android:id="#+id/list_no_of_levels"
android:layout_width="match_parent"
android:layout_height="wrap_content"
/>
</LinearLayout>
I have replaced match_parent to wrap_content to parent layout.
I am new to Android. I have moved heaven and earth to get my app to show RecyclerView with Cards but am not able to,no matter what. Tried a lot of searching on Google and StackOverflow and also on a lot of recommended sites.
My code is as follows:
HomeActivity.java
public class HomeActivity extends AppCompatActivity {
...
private RecyclerView mRecyclerView;
protected void onCreate(Bundle savedInstanceState) {
...
mRecyclerView = (RecyclerView) findViewById(R.id.my_recycler_view);
TransactionRecyclerViewAdapter adapter=new TransactionRecyclerViewAdapter(this, getDataSet());
mRecyclerView.setAdapter(adapter);
mRecyclerView.setHasFixedSize(true);
mRecyclerView.setLayoutManager(new LinearLayoutManager(this));
...
}
private ArrayList<FavoriteTransaction> getDataSet() {
ArrayList results = new ArrayList<FavoriteTransaction>();
FavoriteTransaction favoriteTransaction1 = new FavoriteTransaction();
favoriteTransaction1.setAmount("11");
favoriteTransaction1.setBody("11 ka recharge");
results.add(favoriteTransaction1);
FavoriteTransaction favoriteTransaction2 = new FavoriteTransaction();
favoriteTransaction2.setAmount("12");
favoriteTransaction2.setBody("12 ka recharge");
results.add(favoriteTransaction2);
FavoriteTransaction favoriteTransaction3 = new FavoriteTransaction();
favoriteTransaction3.setAmount("13");
favoriteTransaction3.setBody("13 ka recharge");
results.add(favoriteTransaction3);
FavoriteTransaction favoriteTransaction4 = new FavoriteTransaction();
favoriteTransaction4.setAmount("14");
favoriteTransaction4.setBody("14 ka recharge");
results.add(favoriteTransaction4);
FavoriteTransaction favoriteTransaction5 = new FavoriteTransaction();
favoriteTransaction5.setAmount("15");
favoriteTransaction5.setBody("15 ka recharge");
results.add(favoriteTransaction5);
return results;
}
}
activity_home.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:background="#color/colorgray">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<LinearLayout
android:id="#+id/container_toolbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<include
android:id="#+id/toolbar"
layout="#layout/toolbar" />
</LinearLayout>
<include layout="#layout/content_home" />
</LinearLayout>
<android.support.design.widget.NavigationView
android:id="#+id/nvView"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="start"
android:background="#android:color/white"
app:menu="#menu/activity_home_drawer"
app:headerLayout="#layout/nav_header"/>
</android.support.v4.widget.DrawerLayout>
content_home.xml
<?xml version="1.0" encoding="utf-8"?>
<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"
xmlns:fab="http://schemas.android.com/apk/res-auto"
android:id="#+id/container_body"
android:layout_width="fill_parent"
android:layout_height="0dp"
android:layout_weight="1" >
<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/scrollView"
android:paddingBottom="55dp">
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="15dp">
<include
android:id="#+id/card_list_fav_transact"
layout="#layout/card_list_fav_transact"/>
</LinearLayout>
</ScrollView>
</FrameLayout>
card_list_fav_transact.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dp">
<TextView
android:id="#+id/info_text_sp"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="#string/hdr_lbl_fav"
android:layout_marginBottom="5dp"/>
<android.support.v7.widget.RecyclerView
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/my_recycler_view"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:clipToPadding="false"
android:paddingBottom="16dp"
android:paddingTop="16dp"
android:scrollbars="vertical"
/>
</LinearLayout>
card_transaction_row.xml
<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.CardView
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:card_view="http://schemas.android.com/apk/res-auto"
android:id="#+id/card_view"
android:layout_gravity="center"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#color/colorwhite">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#color/colorwhite">
<TextView
android:layout_width="50dp"
android:layout_height="50dp"
android:gravity="center"
android:text="190"
android:textColor="#color/colorPrimary"
android:id="#+id/tv_fav_amt"
android:background="#drawable/stl_tv_blue_circle"
android:textAppearance="?android:attr/textAppearanceMedium"/>
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:gravity="center_vertical"
android:layout_toRightOf="#+id/tv_fav_amt"
android:layout_toEndOf="#+id/tv_fav_amt">
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="Body"
android:id="#+id/fav_body"
android:clickable="true"
android:background="#drawable/stl_btn_underline"/>
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="SubText"
android:id="#+id/fav_subtext"
android:clickable="true"
android:background="#drawable/stl_btn_underline"/>
</LinearLayout>
<Button
style="#style/Base.Widget.AppCompat.Button.Borderless"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Buy"
android:id="#+id/btn_tr_buy"
android:layout_centerVertical="true"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"
android:layout_marginRight="10dp"
android:background="#drawable/stl_btn_blue"
android:textColor="#android:color/white"/>
</RelativeLayout>
</android.support.v7.widget.CardView>
TransactionRecyclerViewAdapter.java
public class TransactionRecyclerViewAdapter extends RecyclerView.Adapter<TransactionViewHolder> {
private ArrayList<FavoriteTransaction> transactionList;
private Context context;
private LayoutInflater inflater;
public TransactionRecyclerViewAdapter(Context context, ArrayList<FavoriteTransaction> myDataset) {
this.transactionList = new ArrayList<>(myDataset);
this.context = context;
this.inflater = LayoutInflater.from(context);
}
#Override
public int getItemCount() {
return this.transactionList.size();
}
#Override
public void onBindViewHolder(TransactionViewHolder contactViewHolder, int i) {
FavoriteTransaction favTransaction = transactionList.get(i);
contactViewHolder.tvAmount.setText(favTransaction.getAmount());
}
#Override
public TransactionViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View v=inflater.inflate(R.layout.card_transaction_row, parent, false);
TransactionViewHolder viewHolder=new TransactionViewHolder(v);
return viewHolder;
}
}
TransactionViewHolder.java
public class TransactionViewHolder extends RecyclerView.ViewHolder {
TextView tvAmount, tvBody, tvSubText;
public TransactionViewHolder(View itemView) {
super(itemView);
tvAmount = (TextView) itemView.findViewById(R.id.tv_fav_amt);
tvBody = (TextView) itemView.findViewById(R.id.fav_body);
tvSubText = (TextView) itemView.findViewById(R.id.fav_subtext);
}
}
FavoriteTransaction.java
public class FavoriteTransaction {
private int transactionId;
private String amount;
private String subText, body;
public int getTransactionId() {
return transactionId;
}
public void setTransactionId(int transactionId) {
this.transactionId = transactionId;
}
public String getAmount() {
return amount;
}
public void setAmount(String amount) {
this.amount = amount;
}
public String getBody() {
return body;
}
public void setBody(String body) {
this.body = body;
}
public String getSubText() {
return subText;
}
public void setSubText(String subText) {
this.subText = subText;
}
}
When I execute my app, I am not able to see the recycler with the cards. Can someone help me where I am going wrong. Just point me in the right direction.
Thanks in advance.
You wrote findViewById to the Activity class. However RecyclerView is not in your activity_home.xml, it is in nested layout.
Try defining the included layout and apply findViewById on that view, or simply define RecyclerView in your activity_home.xml
Why So?
Activity.findViewById(int id) method finds the id inside that particular Activity's layout. If you have nested layout, for example, like this activity_home.xml
<android.support.v4.widget.DrawerLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
...
<include
android:id="#+id/view_card_list_fav_transact"
layout="#layout/card_list_fav_transact"/>
</android.support.v4.widget.DrawerLayout>
and your card_list_fav_transact.xml
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dp">
<android.support.v7.widget.RecyclerView
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/my_recycler_view"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:clipToPadding="false"
android:paddingBottom="16dp"
android:paddingTop="16dp"
android:scrollbars="vertical"/>
</LinearLayout>
Code inside your Activity will be
View includedView = findViewById(R.id.view_card_list_favt_transact);
RecyclerView rv = includedView.findViewById(R.id.my_recycler_view);
And you are all set!