I finally managed to get the card view to look just like I want it.
I use a GridLayoutManager with two columns to display cards with a thumbnail. Everything nice and working so far.
Anyway, once I scroll up, everything blows up. Large gaps (the size of a card itself) emerge between each row, now I every second row will be an empty one. I do not understand how that would possibly happen. I don't have any problem while scrolling down, but even one inch back up and everything is in pieces (so to speak).
Maybe some can see what is wrong with the layout.
actvivity_main.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"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<android.support.v7.widget.Toolbar xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="#+id/my_toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="?attr/colorPrimary"
android:elevation="4dp"
android:theme="#style/ThemeOverlay.AppCompat.ActionBar"
app:popupTheme="#style/ThemeOverlay.AppCompat.Light"
app:titleTextColor="#android:color/primary_text_dark" />
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="16dp"
android:paddingLeft="16dp"
android:paddingRight="16dp"
android:paddingTop="16dp"
tools:context=".view.MainActivity">
<android.support.v7.widget.RecyclerView
android:id="#+id/series_recycler_view"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_alignParentStart="true"
android:scrollbars="vertical"
/>
</RelativeLayout>
card.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/layout_content"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<android.support.v7.widget.CardView
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="#+id/card_view"
android:layout_width="160dp"
android:layout_height="270dp"
android:layout_gravity="center"
android:layout_margin="5dp"
android:elevation="3dp"
app:cardUseCompatPadding="true"
app:cardCornerRadius="0dp">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView
android:id="#+id/thumbnail"
android:layout_width="match_parent"
android:layout_height="200dp"
android:background="?attr/selectableItemBackgroundBorderless"
android:clickable="true"
/>
<TextView
android:id="#+id/title"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#id/thumbnail"
android:paddingLeft="10dp"
android:paddingRight="10dp"
android:paddingTop="10dp"
android:textColor="#color/colorSeriesTitle"
android:textSize="15dp" />
<TextView
android:id="#+id/rating"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#id/title"
android:paddingBottom="5dp"
android:paddingLeft="10dp"
android:paddingRight="10dp"
android:textSize="12dp" />
</RelativeLayout>
</android.support.v7.widget.CardView>
Adapter
public class MyAdapter extends RecyclerView.Adapter<MyAdapter.MyViewHolder>{
private List<Stuff> stuff;
private Callback callback;
private Context context;
public MyAdapter(Context context) {
this.context = context;
stuff = Collections.emptyList();
}
public MyAdapter(List<Stuff> stuff) {
this.stuff = stuff;
}
public void setStuff(List<Stuff> stuff) {
this.stuff = stuff;
}
public void setCallback(Callback callback) {
this.callback = callback;
}
public static class MyViewHolder extends RecyclerView.ViewHolder {
public Stuff stuff;
public View contentLayout;
public ImageView thumbnailImageView;
public TextView titleTextView;
public TextView ratingTextView;
public MyViewHolder(View itemView) {
super(itemView);
contentLayout = itemView.findViewById(R.id.layout_content);
thumbnailImageView = (ImageView) itemView.findViewById(R.id.thumbnail);
titleTextView = (TextView) itemView.findViewById(R.id.title);
ratingTextView = (TextView) itemView.findViewById(R.id.rating);
}
}
public interface Callback {
void onItemClick(Series series);
}
#Override
public SeriesViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
final View itemView = LayoutInflater.from(parent.getContext())
.inflate(R.layout.series_card, parent, false);
final MyViewHolderviewHolder = new MyViewHolder(itemView);
viewHolder.contentLayout.setOnClickListener(view -> {
if (callback != null) {
callback.onItemClick(viewHolder.series);
}
});
return viewHolder;
}
#Override
public void onBindViewHolder(MyViewHolderholder, int position) {
Stuff stuff = this.stuff.get(position);
holder.stuff = stuff;
holder.titleTextView.setText(stuff.getName())
holder.ratingTextView.setText(stuff.getStatus());
Picasso.with(context)
.load(stuff.getSomeImageURL())
.fit()
.centerCrop()
.into(holder.thumbnailImageView);
}
#Override
public int getItemCount() {
return stuff.size();
}
card.xml:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/layout_content"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
Changing the layout_height to wrap_content fixed the problem.
Related
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'm trying to animate animate the transition from a recyclerview item to an activity with details about that item. I've followed this example:
https://developer.android.com/training/material/animations.html#Transitions
The activities both use shared view elements. The transitions are working, but not as smooth as I would expect. What I mean by that is during the transition, the text shortly falls outside the cardview and the image snaps back in place. See the video:
Video exmaple
NOTE: I do use android databinding and a viewpagaer. I'm not sure if this matters or not for my issue.
In multiple awnsers I've seen people use:
android:clipChildren="false"
android:clipToPadding="false"
But even if I put that on all parents, it still happens. What am i doing wrong?
List Fragment:
#Override
public void onViewCreated(final View view, #Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
//Adapter & recyclerview init
mAdapter = new ProductListAdapter(productList, new OnProductClickListener() {
#Override
public void onProductClick(View view1, Product product) {
final View viewProductCv = view1.findViewById(R.id.list_product_cv);
Intent intent = new Intent(getActivity(), ProductDetailPage.class);
intent.putExtra(PRODUCT, product);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
ActivityOptions options = ActivityOptions.makeSceneTransitionAnimation(getActivity(),
Pair.create(viewProductCv, "productCardView")); // similar name
startActivity(intent, options.toBundle());
} else
startActivity(intent);
}
});
}
Recyclerview item layout
<layout xmlns:android="http://schemas.android.com/apk/res/android">
<data>
<variable
name="product"
type="com.icemobile.estimotemirror.model.Product" />
</data>
<android.support.v7.widget.CardView
xmlns:card_view="http://schemas.android.com/apk/res-auto"
android:id="#+id/list_product_cv"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:transitionName="productCardView" <!-- similar name -->
android:layout_margin="#dimen/card_margin"
card_view:cardCornerRadius="#dimen/card_corner_radius">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<ImageView
android:id="#+id/list_product_image"
android:layout_width="match_parent"
android:layout_height="150dp"
android:scaleType="fitCenter"
android:src="#{product.image}"/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="50dp"
android:gravity="center"
android:orientation="vertical"
android:padding="8dp">
<TextView
android:id="#+id/list_product_title"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="#{product.name}"
android:textColor="#color/colorPrimary"
android:textSize="#dimen/title_14" />
<TextView
android:id="#+id/list_product_distance"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="#{product.distance}"
android:textSize="#dimen/text_12" />
</LinearLayout>
</LinearLayout>
</android.support.v7.widget.CardView>
</layout>
Activity details layout
<layout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:card_view="http://schemas.android.com/tools">
<data>
<variable
name="product"
type="com.icemobile.estimotemirror.model.Product" />
</data>
<android.support.design.widget.CoordinatorLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.v7.widget.Toolbar
android:id="#+id/toolbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:background="?attr/colorPrimary"
android:elevation="6dp"
android:minHeight="?attr/actionBarSize"
android:theme="#style/ThemeOverlay.AppCompat.Dark.ActionBar"
app:popupTheme="#style/ThemeOverlay.AppCompat.Light" />
<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<android.support.v7.widget.CardView
android:id="#+id/details_card_view"
android:layout_width="250dp"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:transitionName="productCardView" <!-- similar name -->
android:layout_gravity="center"
android:layout_marginLeft="#dimen/card_margin"
android:layout_marginRight="#dimen/card_margin"
android:layout_marginTop="86dp"
card_view:cardCornerRadius="#dimen/card_corner_radius">
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
android:padding="#dimen/activity_horizontal_margin">
<ImageView
android:id="#+id/list_product_image"
android:layout_width="match_parent"
android:layout_height="200dp"
android:scaleType="fitCenter"
android:src="#{product.image, default=#drawable/default_thumbnail}"/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="75dp"
android:gravity="center"
android:orientation="vertical"
android:padding="8dp">
<TextView
android:id="#+id/detail_product_title"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="#{product.name}"
android:textColor="#color/colorPrimary"
android:textSize="#dimen/title_18" />
<TextView
android:id="#+id/detail_product_distance"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="#{product.distance}"
android:textSize="#dimen/text_14" />
</LinearLayout>
</LinearLayout>
</android.support.v7.widget.CardView>
<TextView
android:id="#+id/detail_product_description"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentEnd="true"
android:layout_alignParentStart="true"
android:layout_below="#+id/details_card_view"
android:layout_marginTop="#dimen/activity_vertical_margin"
android:gravity="center"
android:padding="#dimen/activity_horizontal_margin"
android:text="dedscription"
android:textColor="#color/colorPrimary"
android:textSize="#dimen/title_14" />
</RelativeLayout>
</ScrollView>
</android.support.design.widget.CoordinatorLayout>
</layout>
My recyclerview Adapter:
public class ProductListAdapter extends RecyclerView.Adapter<ProductListAdapter.ViewHolder> {
private List<Product> productList;
private final OnProductClickListener listener;
public ProductListAdapter(List<Product> productList, OnProductClickListener listener) {
this.productList = productList;
this.listener = listener;
}
#Override
public ProductListAdapter.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
LayoutInflater layoutInflater = LayoutInflater.from(parent.getContext());
ViewDataBinding binding = DataBindingUtil.inflate(layoutInflater, R.layout.list_item_product, parent, false);
return new ViewHolder(binding);
}
#Override
public void onBindViewHolder(final ProductListAdapter.ViewHolder viewHolder, final int position) {
Product product = productList.get(position);
viewHolder.bind(product, listener);
}
#Override
public int getItemCount() {
return (productList != null) ? productList.size() : 0;
}
public void clear() {
this.productList.clear();
}
public void add(Product product) {
this.productList.add(product);
}
class ViewHolder extends RecyclerView.ViewHolder {
private final ViewDataBinding binding;
private ViewHolder(ViewDataBinding binding) {
super(binding.getRoot());
this.binding = binding;
}
void bind(final Product product, final OnProductClickListener listener) {
itemView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
listener.onProductClick(v, product);
}
});
binding.setVariable(BR.product, product);
binding.executePendingBindings();
}
}
Any help would be appreciated!
This is a common issue with data binding and recyclerviews. The Data does not bind on the same frame as the viewholder, it waits a little bit. You need to make sure to add the following method when you use databinding in the recyclerview.adapter or viewholder so it binds immediately.
public void bind(Product product){
mBinding.getViewModel().setProduct(product);
mBinding.executePendingBindings();
}
In my case the childeren of the cardview that was used for shared element transition had a slightly different xml layout in the 2nd activity, that probably caused the stutter. Now I've kept the layout the same and the transition is smooth.
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"
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!
I have 2 layouts in an app that are being covered by the toolbar. In each layout I've tried using android:layout_below="#id/app_bar_layout or android:layout_below="#id/toolbar, with no luck. How can I fix this?
The recycler view is called by a touch on an menu item and, inflated by rv adpater and view holder classes. So far, I have no other problems with the app except the toolbar covering the views.
<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout
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:fitsSystemWindows="true"
tools:context="com.example.aaron.walkingtourtest09feb.MainActivity">
<android.support.design.widget.AppBarLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/app_bar_layout"
android:theme="#android:style/Theme.Material">
<android.support.v7.widget.Toolbar
android:id="#+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:elevation="4dp"
app:popupTheme="#style/AppTheme.PopupOverlay"
android:background="#607D8B"/>
</android.support.design.widget.AppBarLayout>
<include layout="#layout/content_main"/>
</android.support.design.widget.CoordinatorLayout>
content_main.xml:
<FrameLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/content_frame"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="#+id/toolbar"
tools:context="com.example.test09feb.MainActivity"
tools:showIn="#layout/app_bar_main"
app:layout_behavior="#string/appbar_scrolling_view_behavior">
<fragment
android:id="#+id/listFragment"
android:tag="unique_tag"
android:layout_width="match_parent"
android:layout_height="match_parent"
class="com.example.test09feb.MainActivity$ExampleFragment" >
</fragment>
Recyclerview_activity.xml
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#color/cardview_light_background"
android:animateLayoutChanges="true"
android:padding="6dp">
<android.support.v7.widget.RecyclerView
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:id="#+id/rv">
</android.support.v7.widget.RecyclerView>
cards.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/tools"
xmlns:card_view="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<android.support.v7.widget.CardView
android:id="#+id/cv"
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:card_view="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:elevation="4dp"
card_view:cardUseCompatPadding="true">
<RelativeLayout
android:id="#+id/card_container"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:animateLayoutChanges="true"
android:background="#EEEEEE"
android:orientation="vertical"
android:padding="0dp">
<ImageView
android:id="#+id/subject_photo"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:adjustViewBounds="true"
android:scaleType="fitCenter"/>
<TextView
android:id="#+id/subject_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#id/subject_photo"
android:paddingLeft="16dp"
android:paddingRight="16dp"
android:paddingTop="16dp"
android:textColor="#android:color/black"
android:textSize="16sp"/>
<TextView
android:id="#+id/subject_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#id/subject_name"
android:paddingBottom="16dp"
android:paddingLeft="16dp"
android:paddingRight="16dp"
android:textColor="#777777"
/>
<Button
android:id="#+id/card_button_left"
style="#style/Widget.AppCompat.Button.Borderless"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#id/subject_text"
android:layout_toLeftOf="#+id/card_button_right"
android:text="#string/watch"
/>
<Button
android:id="#id/card_button_right"
style="#style/Widget.AppCompat.Button.Borderless"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_below="#id/subject_text"
android:text="#string/read"/>
<TextView
android:id="#+id/expanded_subject_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#id/card_button_right"
android:paddingBottom="16dp"
android:paddingLeft="16dp"
android:paddingRight="16dp"
android:textColor="#777777"
/>
</RelativeLayout>
</android.support.v7.widget.CardView>
RVAdapter:
public class RVAdapter extends RecyclerView.Adapter<RVAdapter.SubjectViewHolder> {
List<Subject> subjects;
static SubjectViewHolder svh;
View v = null;
Context cxt;
private static MyListener listener;
public interface MyListener {
void onClick(View itemView, int viewPosition);
}
RVAdapter(List<Subject> subjects, Context cxt) {
this.subjects = subjects;
this.cxt = cxt;
}
public void setOnClickListener(MyListener listener) {
this.listener = listener;
}
#Override
public void onAttachedToRecyclerView(RecyclerView recyclerView) {
super.onAttachedToRecyclerView(recyclerView);
}
#Override
public SubjectViewHolder onCreateViewHolder(ViewGroup viewGroup, int i) {
v = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.cards, viewGroup, false);
svh = new SubjectViewHolder(v);
return svh;
}
String[] localLinks = {
"http://192.168.11.111:8000/pic1.png",
"http://192.168.11.111:8000/pic2.jpg",
"http://192.168.11.111:8000/pic3.png",
"http://192.168.11.111:8000/pic4.jpg",
"http://192.168.11.111:8000/pic5.png",
"http://192.168.11.111:8000/pic6.jpg",
"http://192.168.11.111:8000/pic7.png",
"http://192.168.11.111:8000/pic8.jpg",
"http://192.168.11.111:8000/pic9.png",
"http://192.168.11.111:8000/pic10.jpg",
"http://192.168.11.111:8000/pic11.png",
"http://192.168.11.111:8000/pic12.png",
"http://192.168.11.111:8000/pic13.png",
"http://192.168.11.111:8000/pic14.png",
};
#Override
public void onBindViewHolder(SubjectViewHolder subjectViewHolder, int i) {
subjectViewHolder.subjectName.setText(subjects.get(i).subjectName);
subjectViewHolder.subjectText.setText(subjects.get(i).subjectText);
Picasso.with(cxt).load(localLinks[i]).into(subjectViewHolder.subjectPhoto);
subjectViewHolder.expandedSubjectText.setText(subjects.get(i).expandedSubjectText);
subjectViewHolder.expandedSubjectText.setVisibility(View.GONE);
}
#Override
public long getItemId(int position) {
return position;
}
#Override
public int getItemCount() {
return subjects.size();
}
public static class SubjectViewHolder extends RecyclerView.ViewHolder {
CardView cv;
TextView subjectName;
TextView subjectText;
TextView expandedSubjectText;
ImageView subjectPhoto;
Button leftButton;
Button rightButton;
SubjectViewHolder(final View itemView) {
super(itemView);
cv = (CardView) itemView.findViewById(R.id.cv);
subjectName = (TextView) itemView.findViewById(R.id.subject_name);
subjectText = (TextView) itemView.findViewById(R.id.subject_text);
expandedSubjectText = (TextView) itemView.findViewById(R.id.expanded_subject_text);
subjectPhoto = (ImageView) itemView.findViewById(R.id.subject_photo);
leftButton = (Button) itemView.findViewById(R.id.card_button_left);
rightButton = (Button) itemView.findViewById(R.id.card_button_right);
rightButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if (expandedSubjectText.getVisibility() == View.GONE) {
expandedSubjectText.setVisibility(View.VISIBLE);
} else {
expandedSubjectText.setVisibility(View.GONE);
}
// Triggers click upwards to the adapter on click
if (listener != null)
v.findViewById(R.id.card_container);
listener.onClick(rightButton, svh.getAdapterPosition());
}
});
leftButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// Triggers click upwards to the adapter on click
if (listener != null)
listener.onClick(leftButton, svh.getAdapterPosition());
}
});
}
}}
result of adding appbar_scrolling_view_behavior as suggested. A blank bar covers the textview that has just animated itself visible.
You have to add following attribute to the parent view in content_main.xml
app:layout_behavior="#string/appbar_scrolling_view_behavior"
Like this:
<FrameLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/content_frame"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.test09feb.MainActivity"
tools:showIn="#layout/app_bar_main"
xmlns:app="http://schemas.android.com/apk/res-auto"
app:layout_behavior="#string/appbar_scrolling_view_behavior">
</FrameLayout>
Your Toolbars height is defined by: android:layout_height="?attr/actionBarSize"
so I added a top margin with those values to the list view:
android:layout_marginTop="?attr/actionBarSize"
Worked for me in the app I'm working on.