Two RecyclerView inside the same Scrollview, one does not show content - android

I'm facing a problem with two RecyclerView inside the same ScrollView (I also tried the same with a NestestScrollView). Inside the ScrollView, I have also some other View objects that form a kind of "header section" of the fragment. Then, I would like to show a horizontal list of RecyclerView, and finally, above the horizontal list, a vertical list of other RecyclerView. However, only the horizontal one is correctly visualized. Even though the Adapter of the vertical one is correctly initialized with some objects, when I run the application, the vertical list is empty. I think it is a problem related to my layout.
This is my .xml file:
<?xml version="1.0" encoding="utf-8"?>
<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"
android:id="#+id/profile_coordinator"
xmlns:app="http://schemas.android.com/apk/res-auto"
tools:context=".HomeFragment">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingBottom="20dp">
<!-- Here I have some other views (ImageView, TextView, etc.) -->
</RelativeLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:padding="10dp">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="#string/active_promos"
android:textStyle="bold"
android:textColor="#color/colorPrimary"
android:textSize="18sp"
android:layout_marginBottom="10sp"/>
<!-- Horizontal List of RecyclerView -->
<androidx.recyclerview.widget.RecyclerView
android:id="#+id/promo_recyclerview"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
app:layoutManager="androidx.recyclerview.widget.LinearLayoutManager"
android:layout_marginBottom="20sp"/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="#string/news"
android:textStyle="bold"
android:textColor="#color/colorPrimary"
android:textSize="18sp"
android:layout_marginBottom="10sp"/>
<!-- Vertical List of RecyclerView -->
<androidx.recyclerview.widget.RecyclerView
android:id="#+id/news_recyclerview"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</LinearLayout>
</LinearLayout>
</ScrollView>
Since the objects to show are the same (but the semantics is different), I'm using the same Adapter class that loads two different xml layout according to the type of object to show.
This is the Adapter.java:
public class NewsAdapter extends RecyclerView.Adapter {
private List<NewsPromotion> newsPromotions;
private boolean promos;
public NewsAdapter(List<NewsPromotion> newsPromotions, boolean promos) {
this.newsPromotions = newsPromotions;
this.promos = promos;
}
public void setData(List<NewsPromotion> newsPromotions){
this.newsPromotions = newsPromotions;
}
#NonNull
#Override
public RecyclerView.ViewHolder onCreateViewHolder(#NonNull ViewGroup parent, int viewType) {
RecyclerView.ViewHolder viewHolder;
if(promos) {
View mView = LayoutInflater.from(parent.getContext()).inflate(R.layout.promo_recyclerview_item,
parent, false);
viewHolder = new PromoViewHolder(mView);
}else {
View mView = LayoutInflater.from(parent.getContext()).inflate(R.layout.recyclerview_item_row,
parent, false);
viewHolder = new NewsViewHolder(mView);
}
return viewHolder;
}
#Override
public void onBindViewHolder(#NonNull RecyclerView.ViewHolder holder, int position) {
Log.d("Adapter", newsPromotions.get(position).title);
if(promos){
PromoViewHolder viewHolder = (PromoViewHolder) holder;
Picasso.get().load(RestClient.BASE_IMAGE_URL + newsPromotions.get(position).image).into(viewHolder.mImage);
}else {
NewsViewHolder viewHolder = (NewsViewHolder) holder;
Picasso.get().load(RestClient.BASE_IMAGE_URL + newsPromotions.get(position).image).into(viewHolder.mImage);
viewHolder.mTitle.setText(newsPromotions.get(position).title);
viewHolder.mDescription.setText(newsPromotions.get(position).content);
}
}
#Override
public int getItemCount() {
return newsPromotions.size();
}
public class NewsViewHolder extends RecyclerView.ViewHolder {
ImageView mImage;
TextView mTitle;
TextView mDescription;
private NewsViewHolder(View itemView) {
super(itemView);
mImage = itemView.findViewById(R.id.ivImage);
mTitle = itemView.findViewById(R.id.tvTitle);
mDescription = itemView.findViewById(R.id.tvDescription);
}
}
public class PromoViewHolder extends RecyclerView.ViewHolder {
ImageView mImage;
private PromoViewHolder(View itemView) {
super(itemView);
mImage = itemView.findViewById(R.id.ivImage);
}
}
}
And the following is how I initialize the two adapters in the Fragment:
RecyclerView promoRecycleView = activity.findViewById(R.id.promo_recyclerview);
promoAdapter = new NewsAdapter(new ArrayList<NewsPromotion>(), true);
promoRecycleView.setAdapter(promoAdapter);
RecyclerView newsRecycleView = activity.findViewById(R.id.news_recyclerview);
newsAdapter = new NewsAdapter(new ArrayList<NewsPromotion>(), false);
newsRecycleView.setAdapter(newsAdapter);
Finally, this is how I send the object to the adapters:
promoAdapter.setData(promos);
promoAdapter.notifyDataSetChanged();
newsAdapter.setData(news);
newsAdapter.notifyDataSetChanged();

Please post your adapter and item_layout. Because your given layout is working perfectly for me.

No issue with your layout i have run in myAppliaction, some issue in Adapter, please post/paste your activity and adapter code.

If you would check documentation on the RecyclerView, you will see that you need to specify size of the list. There is no wrap content for RecyclerView, because it's dynamic widget. So you would need to align your implementation for something like next.
<?xml version="1.0" encoding="utf-8"?>
<NestedScrollView
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"
android:id="#+id/profile_coordinator"
xmlns:app="http://schemas.android.com/apk/res-auto"
tools:context=".HomeFragment">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingBottom="20dp">
<!-- Here I have some other views (ImageView, TextView, etc.) -->
</RelativeLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:padding="10dp">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="#string/active_promos"
android:textStyle="bold"
android:textColor="#color/colorPrimary"
android:textSize="18sp"
android:layout_marginBottom="10sp"/>
<!-- Horizontal List of RecyclerView -->
<androidx.recyclerview.widget.RecyclerView
android:id="#+id/promo_recyclerview"
android:layout_width="match_parent"
android:layout_height="200dp"
android:orientation="horizontal"
app:layoutManager="androidx.recyclerview.widget.LinearLayoutManager"
android:layout_marginBottom="20sp"/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="#string/news"
android:textStyle="bold"
android:textColor="#color/colorPrimary"
android:textSize="18sp"
android:layout_marginBottom="10sp"/>
<!-- Vertical List of RecyclerView -->
<androidx.recyclerview.widget.RecyclerView
android:id="#+id/news_recyclerview"
android:layout_width="match_parent"
android:layout_height="500dp" />
</LinearLayout>
</LinearLayout>
</ScrollView>

Related

How to cover complete Background Color in recycler View?

Image Description is here
enter image description here
I did Match Parent for the Layout but then it is divided into several
scrollable sections. How do i fill the remaining bottom background
color of this Recycler View ?
Here is my code for the layout :
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:gravity="center_horizontal"
android:layout_height="wrap_content"
android:id="#+id/teacher_relative">
<LinearLayout
android:id="#+id/linr"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_margin="6dp"
android:background="#drawable/selector_item_main_menu"
android:orientation="vertical">
<ImageView
android:id="#+id/imageView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:adjustViewBounds="true"
android:src="#drawable/home_fund_raising" />
<TextView
android:id="#+id/textView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:maxLines="1"
android:shadowColor="#393939"
android:shadowDx="2.0"
android:shadowDy="1.0"
android:shadowRadius="2.0"
android:text="ZED"
android:textAlignment="center"
android:textColor="#android:color/white"
android:textSize="12sp" />
</LinearLayout>
<TextView
android:id="#+id/txt_notify"
android:layout_width="25dp"
android:layout_height="25dp"
android:background="#drawable/bg_white_circle"
android:gravity="center"
android:layout_marginLeft="77dp"
android:text="0"
android:visibility="gone"
android:textSize="13sp"
android:textColor="#color/red"
android:textStyle="bold" />
Java Code :
recyclerView = findViewById(R.id.recyclerView);
// set a GridLayoutManager with default vertical orientation and 3 number of columns
GridLayoutManager gridLayoutManager = new GridLayoutManager(getApplicationContext(),3);
recyclerView.setLayoutManager(gridLayoutManager);
I am using Grid Layout
Code for Adapter Class :
#Override
public void onBindViewHolder(ViewHolder holder, int position) {
MyTeacherModel myTeacherModel = myTeacherModelsList.get(position);
holder.imageView.setImageDrawable(mContext.getResources().getDrawable(myTeacherModel.getImages()));
holder.textView.setText(myTeacherModel.getText());
holder.teacher_relative.setBackgroundColor(Functions.getBackColor());
}
#Override
public int getItemCount() {
return myTeacherModelsList.size();
}
class ViewHolder extends RecyclerView.ViewHolder{
public TextView textView, txt_notify;
public ImageView imageView;
DataModel item;
public LinearLayout linr;
RelativeLayout teacher_relative;
public ViewHolder(View itemView) {
super(itemView);
textView = (TextView) itemView.findViewById(R.id.textView);
imageView = (ImageView) itemView.findViewById(R.id.imageView);
linr = (LinearLayout) itemView.findViewById(R.id.linr);
txt_notify = (TextView) itemView.findViewById(R.id.txt_notify);
teacher_relative = itemView.findViewById(R.id.teacher_relative);
}
}
You should give background to the container view that holds the recyclerview.
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#drawable/selector_item_main_menu">
<androidx.recyclerview.widget.RecyclerView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="#+id/menubar"
android:id="#+id/myRcv"/>
</RelativeLayout>

Huge gap between cards of recyclerView

I have a recylerView to show the images fetched from firebase cloud, However there is a large gap between some items and these gaps arise after i start scrolling, before scrolling, everything is placed perfectly, I have read a few articles, however not proved to be correct in my case.
The code for my MainActivity is given below
RecyclerView.LayoutManager layoutManager = new StaggeredGridLayoutManager(2, StaggeredGridLayoutManager.VERTICAL);
recyclerView = (RecyclerView) findViewById(R.id.recyclerView);
recyclerView.setHasFixedSize(true);
recyclerView.setLayoutManager(layoutManager);
recyclerView.setItemAnimator(new DefaultItemAnimator());
Query query = firebaseDatabase.getReference().child("Products").child(Uid).orderByKey();
FirebaseRecyclerOptions options = new FirebaseRecyclerOptions.Builder<MainConstructor>().setQuery(query, MainConstructor.class).build();
mFirebaseAdapter = new FirebaseRecyclerAdapter<MainConstructor, ShowDataViewHolder>(options) {
#Override
public ShowDataViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.list_view, parent, false);
return new ShowDataViewHolder(view);
}
#Override
protected void onBindViewHolder(#NonNull ShowDataViewHolder holder, int position, #NonNull MainConstructor model) {
holder.setImg(getApplicationContext(),model.getImageUrl());
holder.setImageText(model.getImageUrl());
holder.setCode(model.getProductCode());
progressDialog.dismiss();
}
};
recyclerView.setAdapter(mFirebaseAdapter);
}
#Override
protected void onStart() {
super.onStart();
mFirebaseAdapter.startListening();
recyclerView.setAdapter(mFirebaseAdapter);
}
#Override
protected void onStop() {
super.onStop();
mFirebaseAdapter.stopListening();
}
The code for ViewHolder class is given as
public class ShowDataViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
ImageView img;
TextView imageText, codeText;
public ShowDataViewHolder(final View itemView) {
super(itemView);
itemView.setOnClickListener(this);
}
private void setImg(Context ctx, String img1) {
img = (ImageView) itemView.findViewById(R.id.List_ImageView);
Picasso.with(ctx).load(img1).placeholder(R.drawable.notification).into(img);
// progressDialog.dismiss();
}
private void setImageText(String text){
imageText = (TextView)itemView.findViewById(R.id.textView);
imageText.setText(text);
}
private void setCode(String code){
codeText = (TextView)itemView.findViewById(R.id.Code);
codeText.setText(code);
}
The large unwanted gaps can be clearly seen here:
The list_view layout code is given as:
<?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.support.v7.widget.CardView
android:layout_width="match_parent"
android:layout_height="150dp"
android:layout_margin="2dp">
<LinearLayout
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:id="#+id/List_ImageView"
android:padding="2dp"/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/textView"
android:visibility="invisible"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/Code"
android:visibility="invisible"/>
</LinearLayout>
</android.support.v7.widget.CardView>
I have read someWhere that this problem arises because recyclerView continuously keeps on updating the items, so to correct that we need a ViewHolder class, however i have a viewHolder in my case then also this problem is there,
Can anyone help me with the solution and also with the exact problem why is it happening?
Thanks in advance
Your code is perfect just remove your parent node RelativeLayout which is actually not needed. That is creating issue with match_parent height.
<android.support.v7.widget.CardView
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="150dp"
android:layout_margin="2dp">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<ImageView
android:id="#+id/List_ImageView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="2dp" />
<TextView
android:id="#+id/textView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:visibility="invisible" />
<TextView
android:id="#+id/Code"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:visibility="invisible" />
</LinearLayout>
</android.support.v7.widget.CardView>
Change this:
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
to this:
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content">

Separating different Views in Android Studio using RecyclerView and CardView

I am using FirebaseRecyclerViewAdapter with CardView in my application and want to load video on one card and images on another, but I dont know how to separate them. Everytime I add a videoView in CardView layout it gets added on the top of ImageView in same card and also gets repeated in all the RecyclerView. Its like same thing is coming again and again with number of posts I have. I want that VideoView should play video on different post and ImageView shows images on different, I have searched many options but did not find anything useful, please help.
I am using Android Studio
CardView xml file:
<?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:tools="http://schemas.android.com/tools"
android:id="#+id/CardView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
card_view:cardCornerRadius="10dp">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<com.google.android.youtube.player.YouTubePlayerView
android:id="#+id/youtube"
android:layout_width="match_parent"
android:layout_height="200dp"/>
<ImageView
android:id="#+id/po_image"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:adjustViewBounds="true"
android:scaleType="centerCrop" />
<TextView
android:id="#+id/ti_cardView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="10dp"
android:textSize="18sp"
android:textStyle="bold"/>
<TextView
android:id="#+id/de_cardView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingLeft="10dp"
android:paddingRight="10dp" />
<TextView
android:id="#+id/so_cardView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingLeft="10dp"
android:paddingRight="10dp"
android:textColor="#color/SoColor"/>
</LinearLayout>
</android.support.v7.widget.CardView>
FirebaseRecyclerView Adapter class:
FirebaseRecyclerAdapter <post, postViewHolder> firebaseRecyclerAdapter = new FirebaseRecyclerAdapter<post, postViewHolder>(
post.class,
R.layout.post_row_recycle_home,
postViewHolder.class,
mDatabaseReference
) {
#Override
protected void populateViewHolder(postViewHolder viewHolder, post model, int position) {
viewHolder.setTi(model.getTi());
viewHolder.setde(model.getDe());
}
};
mrecyclerView.setAdapter(firebaseRecyclerAdapter);
}
public static class postViewHolder extends RecyclerViewPager.ViewHolder{
View mView;
public postViewHolder(View itemView) {
super(itemView);
mView = itemView;
}
public void setTi(String ti){
TextView post_ti = (TextView)mView.findViewById(R.id.ti_cardView);
post_ti.setText(ti);
}
public void setde(String de){
TextView post_de = (TextView)mView.findViewById(R.id.de_cardView);
post_de.setText(de);
}
set visibility of both gone at first and then depending on its video url or image url. manipulate Visibility of each 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:tools="http://schemas.android.com/tools"
android:id="#+id/CardView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
card_view:cardCornerRadius="10dp">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<com.google.android.youtube.player.YouTubePlayerView
android:visibility="gone"
android:id="#+id/youtube"
android:layout_width="match_parent"
android:layout_height="200dp"/>
<ImageView
android:visibility="gone"
android:id="#+id/po_image"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:adjustViewBounds="true"
android:scaleType="centerCrop" />
<TextView
android:id="#+id/ti_cardView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="10dp"
android:textSize="18sp"
android:textStyle="bold"/>
<TextView
android:id="#+id/de_cardView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingLeft="10dp"
android:paddingRight="10dp" />
<TextView
android:id="#+id/so_cardView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingLeft="10dp"
android:paddingRight="10dp"
android:textColor="#color/SoColor"/>
</LinearLayout>
</android.support.v7.widget.CardView>
As you are using RecyclerView, don't put the VideoPlayer and the ImageView in the same xml layout, you can create two different layouts and use the getItemViewType() to choose the right layout to inflate.
The questions on how to use differente layouts on the same RecyclerView has been answered here
Sample code:
image_item_layout.xml
<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:tools="http://schemas.android.com/tools"
android:id="#+id/CardView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
card_view:cardCornerRadius="10dp">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<ImageView
android:visibility="gone"
android:id="#+id/po_image"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:adjustViewBounds="true"
android:scaleType="centerCrop" />
<...>
</LinearLayout>
</android.support.v7.widget.CardView>
video_item_layout.xml
<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:tools="http://schemas.android.com/tools"
android:id="#+id/CardView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
card_view:cardCornerRadius="10dp">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<com.google.android.youtube.player.YouTubePlayerView
android:visibility="gone"
android:id="#+id/youtube"
android:layout_width="match_parent"
android:layout_height="
<...>
</LinearLayout>
</android.support.v7.widget.CardView>
On the ImageOrVideoAdapter.java you can use something like:
public class ImageOrVideoAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
private List<ImageOrVideoItem> dataSource;
private final int VIDEO_VIEW = 0;
private final int IMAGE_VIEW = 1;
class VideoItemViewHolder extends RecyclerView.ViewHolder {
// Do your stuff with the video here
public VideoItemViewHolder(View itemView) {
}
}
class ImageItemViewHolder extends RecyclerView.ViewHolder {
// Do your stuff with the image here
public ImageItemViewHolder(View itemView) {
}
}
#Override
public int getItemViewType(int position) {
return dataSource.get(position).isVideoItem() ? VIDEO_VIEW : IMAGE_VIEW;
}
#Override
public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
LayoutInflater inflater = LayoutInflater.from(parent.getContext());
switch (viewType) {
case VIDEO_VIEW:
return new VideoItemViewHolder(inflater.inflate(R.layout.video_item_layout, parent, false));
case IMAGE_VIEW:
return new ImageItemViewHolder(inflater.inflate(R.layout.image_item_layout, parent, false));
}
}
#Override
public void onBindViewHolder(final RecyclerView.ViewHolder holder, final int position) {
switch (holder.getItemViewType()) {
case VIDEO_VIEW:
VideoItemViewHolder viewHolder0 = (VideoItemViewHolder) holder;
break;
case IMAGE_VIEW:
ImageItemViewHolder viewHolder2 = (ImageItemViewHolder) holder;
break;
}
}
}
There are many posts all around StackOverflow about handling diferent layouts for the same RecyclerView, look around, you will find them.
EDIT
Take a look on this example using FireBase and see if it can adapted to fit your needs: Here

Incorrect layout alignment in list Item of RecyclerView

I'm having a problem with setting the layout in RecyclerView.
here is an layout xml code:
<?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"
android:weightSum="1">
<TextView
android:id="#+id/dummy_text_view"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_weight="0.5"
android:background="#ffffff" />
<Button
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="0.3"
android:text="Button"
android:textAllCaps="false" />
</LinearLayout>
here is my MainActivity
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
RecyclerView recyclerView = (RecyclerView) findViewById(R.id.recycler_view);
LinearLayoutManager layoutManager = new LinearLayoutManager(MainActivity.this);
layoutManager.setOrientation(LinearLayoutManager.VERTICAL);
recyclerView.setLayoutManager(layoutManager);
List<String> list = new ArrayList<>();
for (int i = 0; i <= 25; i++) {
list.add("item " + i);
}
DummyRVAdapter adapter = new DummyRVAdapter(MainActivity.this, list);
recyclerView.setAdapter(adapter);
}
Code for Adapter :
public class DummyRVAdapter extends RecyclerView.Adapter<DummyRVAdapter.Holder> {
private Context mContext;
private List<String> mList;
public DummyRVAdapter(Context context, List<String> list) {
this.mContext = context;
this.mList = list;
}
#Override
public DummyRVAdapter.Holder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = View.inflate(mContext, R.layout.item_dummy_layout, null);
//View view =LayoutInflater.from(mContext).inflate(R.layout.item_dummy_layout, null);
return new Holder(view);
}
#Override
public void onBindViewHolder(DummyRVAdapter.Holder holder, int position) {
holder.textView.setText(mList.get(position));
}
#Override
public int getItemCount() {
return mList.size();
}
public class Holder extends RecyclerView.ViewHolder {
TextView textView;
public Holder(View itemView) {
super(itemView);
textView = (TextView) itemView.findViewById(R.id.dummy_text_view);
}
}
}
Screenshot of my output.
In my layout XML file, I declare the size for textView as 50% and Button as 30%
,but still, my recyclerView not showing in a correct manner it is not showing in a correct manner.
This layout works fine in Listview.
I don't know where is the problem in my code. I set the Layout manager and adapter to my RecyclerView in the correct manner.
Edit:
Please see the ScreenShot it shows that TextView is not allocating 50% of the screen, that's the problem.
It I create complex Layout for Recyclerview its alignment is not in mentioned format.
but that works in Listview
Can anyone help me on this?
Thanks in advance.
Instead of
View view = View.inflate(mContext, R.layout.item_dummy_layout, null);
use:
View view = LayoutInflater.from(mContext).(R.layout.item_dummy_layout, parent, false);
Its not just weight that is having problem its the view textview may be occupying its size but it's text are samller than button size
one important thing Even after weight or without it use minsize and max size in button to fit it according to need like in your layout do this:
<?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"
android:weightSum="1">
<TextView
android:id="#+id/dummy_text_view"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_weight="0.5"
android:background="#ffffff" />
<Button
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="0.3"
android:text="Button"
android:max_height="30dp"
android:textAllCaps="false" />
As android:weightSum="1" but sum of all android:layout_weight was not equal to 1 so this was not showing properly
you have declare the size for textView as 50% and Button as 30% but rest remaining is 20% due to this reason it is not showing in proper way. so can do as either increase textview to 70% and Button to 30% so android:weightSum and android:layout_weight sum will be equal.
Change your layout to
<?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"
android:weightSum="1" >
<TextView
android:id="#+id/dummy_text_view"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="0.7"
android:background="#ffffff" />
<Button
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="0.3"
android:text="Button"
android:textAllCaps="false" />
</LinearLayout >

CardView not showing content eclipse

I am using android.support.v7.widget.CardView to show ImageView with two TextView's in RecyclerView but card view not showing the content(showing only blank white cards) when the screen orientation is vertical, on the other hand it is showing content when orientation is landscape.
When I run the same project from AndroidStudio there is no problem everything works fine.
I don't understand what is problem out there, is there problem with eclipse?
Please see the code below,
PostListFragment
public class PostListFragment extends Fragment implements AppConfig {
private ArrayList<Post> mPostArrayList;
private PostRecyclerAdapter mPostRecyclerAdapter;
private RecyclerView mRecyclerView;
//other declarations
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
//removed
}
private void init() {
// Set up RecyclerView
mRecyclerView = (RecyclerView) mRootView
.findViewById(R.id.mPostListRecyclerView);
// Setup layout manager for mPostArrayList and column count
final LinearLayoutManager mLayoutManager = new LinearLayoutManager(
getActivity());
// Control orientation of the mPostArrayList
mLayoutManager.setOrientation(LinearLayoutManager.VERTICAL);
mLayoutManager.scrollToPosition(0);
// Attach layout manager
mRecyclerView.setLayoutManager(mLayoutManager);
// Listen to the item touching
mRecyclerView.addOnItemTouchListener(new RecyclerItemClickListener(
getActivity(),
new RecyclerItemClickListener.OnItemClickListener() {
#Override
public void onItemClick(View itemView, int position) {
//some action
}
}));
mPostArrayList = new ArrayList<>();
// Bind adapter to recycler
mPostRecyclerAdapter = new PostRecyclerAdapter(
getActivity(), mPostArrayList);
mRecyclerView.setAdapter(mPostRecyclerAdapter);
}
private void getPosts() {
// Execute async task
new AsyncPosts().execute(mPostURL);
}
public class AsyncPosts extends AsyncTask<Object, String, JSONObject> {
//no problem with this
}
}
Layout used for PostListFragment
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:ads="http://schemas.android.com/apk/res-auto"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="#+id/mParentLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<FrameLayout
android:id="#+id/mPostListContainer"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1" >
<android.support.v4.widget.SwipeRefreshLayout
android:id="#+id/mPostListSwipeRefreshLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<android.support.v7.widget.RecyclerView
android:id="#+id/mPostListRecyclerView"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</android.support.v4.widget.SwipeRefreshLayout>
</FrameLayout>
<!-- removed -->
</LinearLayout>
Here is adapter to set the values,
PostRecyclerAdapter
public class PostRecyclerAdapter extends
RecyclerView.Adapter<PostRecyclerAdapter.SimpleItemViewHolder> {
private Context mContext;
private List<Post> post;
// Provide a suitable constructor (depends on the kind of data store)
public PostRecyclerAdapter(Context context, List<Post> items) {
this.mContext = context;
this.post = items;
}
// Return the size of your data set (invoked by the layout manager)
#Override
public int getItemCount() {
return this.post.size();
}
// Create new items (invoked by the layout manager)
// Usually involves inflating a layout from XML and returning the holder
#Override
public SimpleItemViewHolder onCreateViewHolder(ViewGroup viewGroup,
int viewType) {
View itemView = LayoutInflater.from(viewGroup.getContext()).inflate(
R.layout.post_list_item, viewGroup, false);
return new SimpleItemViewHolder(itemView);
}
// Replace the contents of a view (invoked by the layout manager)
// Involves populating data into the item through holder
#Override
public void onBindViewHolder(SimpleItemViewHolder viewHolder, int position) {
Glide.with(mContext).load(post.get(position).IMG_URL)
.placeholder(R.drawable.ic_placeholder).crossFade(1000)
.centerCrop().into(viewHolder.mPostListSmallThumbnail);
viewHolder.mPostListSmallTitle.setText(post.get(position).POST_TITLE);
viewHolder.mPostListSmallContent
.setText(post.get(position).POST_CONTENT);
}
// Provide a reference to the views for each data item
// Provide access to all the views for a data item in a view holder
public final static class SimpleItemViewHolder extends
RecyclerView.ViewHolder {
ImageView mPostListSmallThumbnail;
TextView mPostListSmallTitle, mPostListSmallContent;
public SimpleItemViewHolder(View itemView) {
super(itemView);
mPostListSmallThumbnail = (ImageView) itemView
.findViewById(R.id.mPostListSmallThumbnail);
mPostListSmallTitle = (TextView) itemView
.findViewById(R.id.mPostListSmallTitle);
mPostListSmallContent = (TextView) itemView
.findViewById(R.id.mPostListSmallContent);
}
}
}
post_list_item
<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="#+id/mPostListSmallCard"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:foreground="#drawable/card_background"
android:clickable="true"
app:cardCornerRadius="#dimen/blog_card_radius"
app:cardUseCompatPadding="true"
app:contentPadding="#dimen/blog_card_radius">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView
android:id="#+id/mPostListSmallThumbnail"
android:layout_width="#dimen/blog_image_thumb_dim"
android:layout_height="#dimen/blog_image_thumb_dim"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_alignParentTop="true"
android:background="#drawable/ic_placeholder"
android:adjustViewBounds="true"
android:contentDescription="#string/image_thumbnail_placeholder" />
<TextView
android:id="#+id/mPostListSmallTitle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentEnd="true"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:layout_marginLeft="#dimen/post_list_margin_left"
android:layout_marginStart="#dimen/post_list_margin_right"
android:layout_toEndOf="#+id/mPostListSmallThumbnail"
android:layout_toRightOf="#+id/mPostListSmallThumbnail"
android:ellipsize="end"
android:lines="2"
android:singleLine="false"
android:textAppearance="?android:attr/textAppearanceMedium" />
<TextView
android:id="#+id/mPostListSmallContent"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="#+id/mPostListSmallThumbnail"
android:layout_alignEnd="#+id/mPostListSmallTitle"
android:layout_alignLeft="#+id/mPostListSmallTitle"
android:layout_alignRight="#+id/mPostListSmallTitle"
android:layout_alignStart="#+id/mPostListSmallTitle"
android:layout_below="#+id/mPostListSmallTitle"
android:ellipsize="end"
android:lines="3"
android:textAppearance="?android:attr/textAppearanceSmall" />
</RelativeLayout>
</android.support.v7.widget.CardView>
Here is post_list_item for landscape layout
land/post_list_item
<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="#+id/mPostListSmallCard"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginRight="#dimen/blog_card_margin_landscape"
android:layout_marginLeft="#dimen/blog_card_margin_landscape"
app:cardCornerRadius="#dimen/blog_card_radius"
app:cardUseCompatPadding="true">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView
android:id="#+id/mPostListSmallThumbnail"
android:layout_width="#dimen/blog_image_thumb_dim"
android:layout_height="#dimen/blog_image_thumb_dim"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_alignParentTop="true"
android:adjustViewBounds="true"
android:contentDescription="#string/image_thumbnail_placeholder" />
<TextView
android:id="#+id/mPostListSmallTitle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentEnd="true"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:layout_marginLeft="#dimen/post_list_margin_left"
android:layout_marginStart="#dimen/post_list_margin_right"
android:layout_toEndOf="#+id/mPostListSmallThumbnail"
android:layout_toRightOf="#+id/mPostListSmallThumbnail"
android:ellipsize="end"
android:lines="2"
android:singleLine="false"
android:textAppearance="?android:attr/textAppearanceMedium" />
<TextView
android:id="#+id/mPostListSmallContent"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="#+id/mPostListSmallThumbnail"
android:layout_alignEnd="#+id/mPostListSmallTitle"
android:layout_alignLeft="#+id/mPostListSmallTitle"
android:layout_alignRight="#+id/mPostListSmallTitle"
android:layout_alignStart="#+id/mPostListSmallTitle"
android:layout_below="#+id/mPostListSmallTitle"
android:ellipsize="end"
android:lines="3"
android:textAppearance="?android:attr/textAppearanceSmall" />
</RelativeLayout>
</android.support.v7.widget.CardView>

Categories

Resources