So, I have a card view created as follows:
<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="5dp">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<TextView
android:id="#+id/timeline"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="5dp" />
<TextView
android:id="#+id/title"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="5dp"
android:textStyle="bold" />
<TextView
android:id="#+id/website"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="5dp" />
</LinearLayout>
And in my adapter class I have the following code to populate the card view.
public class MyViewHolder extends RecyclerView.ViewHolder {
private TextView tv_title, tv_timeline, tv_website;
public MyViewHolder(View view) {
super(view);
tv_title = (TextView) view.findViewById(R.id.title);
tv_timeline = (TextView) view.findViewById(R.id.timeline);
tv_website = (TextView) view.findViewById(R.id.website);
}
}
#Override
public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View itemView = LayoutInflater.from(parent.getContext())
.inflate(R.layout.activity_row_data, parent, false);
return new MyViewHolder(itemView);
}
#Override
public void onBindViewHolder(MyViewHolder holder, final int position) {
holder.tv_title.setText(mTitleList.get(position));
holder.tv_timeline.setText(mtimelineList.get(position));
holder.tv_website.setText(mSiteSrc.get(position));
}
#Override
public int getItemCount() {
return mTitleList.size();
}
This correctly shows me the output I am expecting. The problem is that a CardView only show me up to 3 items in my 3 TextView, and then keep creating more cards holding up to 3 items to show me the rest.
What I would like to do is to use the textview "timeline" as a header, and then dynamically add content for textview "title" only. So for example, I might have a card which has 1 "timeline" TextView, 0, 2, 5 etc "title" TextView, 1 "website" TextView
Is this something that can be done with CardView? If yes, I'd appreciate any helpful pointers to help me get started.
Convert your xml :
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<TextView
android:id="#+id/timeline"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="5dp" />
<LienarLayout
android:id="#+id/title"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="5dp"
android:orientation="vertical"/>
<TextView
android:id="#+id/website"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="5dp" />
</LinearLayout>
Now in your onBindViewHolder add dynamic TextViews to title:
public class MyViewHolder extends RecyclerView.ViewHolder {
private TextView tv_timeline, tv_website;
private LinearLayout title;
public MyViewHolder(View view) {
super(view);
title = (LinearLayout) view.findViewById(R.id.title);
tv_timeline = (TextView) view.findViewById(R.id.timeline);
tv_website = (TextView) view.findViewById(R.id.website);
}
}
#Override
public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View itemView = LayoutInflater.from(parent.getContext())
.inflate(R.layout.activity_row_data, parent, false);
return new MyViewHolder(itemView);
}
#Override
public void onBindViewHolder(MyViewHolder holder, final int position) {
holder.tv_timeline.setText(mtimelineList.get(position));
holder.tv_website.setText(mSiteSrc.get(position));
for(int i = 0; i < 5; i++) { // 5 is the count how many times you want to add textview
LayoutParams lparams = new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
TextView tv=new TextView(holder.title.getContext());
tv.setLayoutParams(lparams);
tv.setText("test = "+i);
holder.title.addView(tv);
}
}
#Override
public int getItemCount() {
return mTitleList.size();
}
If I understand right the question that you want to put dynamically only part of the data. So in bindView holder bind only dynamic part which is static you can hardcoded in xml file with string value.
Related
The view inside recycler view is not aligned properly. The items inside recyclerview are having different height on adding new item.
I tried of using display metrics but could not achieve it. My item list can be changed by adding or deleting items but the view padding, width and height must h=not get disturbed.
categoryRecyclerView = findViewById(R.id.categoriesRecyclerView);
GridLayoutManager gridLayoutManager = new GridLayoutManager(this, 1,GridLayoutManager.HORIZONTAL, false);
categoryRecyclerView.setLayoutManager(gridLayoutManager);
categoryRecyclerView.addItemDecoration(new ItemDecorator(getResources().getDimensionPixelSize(R.dimen.item_spacing), 3));
CategoryItemAdapter categoryItemAdapter = new CategoryItemAdapter(this, categoryList);
categoryRecyclerView.setAdapter(categoryItemAdapter);
Code for Adapter
public class CategoryItemAdapter extends RecyclerView.Adapter<CategoryItemAdapter.MyViewHolder> {
private Context context;
private List<Category> categoryList;
private onItemClickListener mItemClickListener;
public CategoryItemAdapter(Context context, List<Category> categoryList) {
this.context = context;
this.categoryList = categoryList;
}
#NonNull
#Override
public MyViewHolder onCreateViewHolder(#NonNull ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.category_item, parent, false);
return new MyViewHolder(view);
}
#Override
public void onBindViewHolder(#NonNull CategoryItemAdapter.MyViewHolder holder, int position) {
Category category = (Category) categoryList.get(position);
holder.textView.setText(category.getCategoryName());
}
#Override
public int getItemCount() {
return categoryList.size();
}
public void setOnItemClickListener(onItemClickListener mItemClickListener) {
this.mItemClickListener = mItemClickListener;
}
public interface onItemClickListener {
void onItemClickListener(View view, int position, Category category);
}
class MyViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
CardView cardview;
TextView textView;
ImageView selectItem;
RelativeLayout selectItemLayout;
LinearLayout linearLayout;
public MyViewHolder(#NonNull View itemView) {
super(itemView);
cardview = itemView.findViewById(R.id.cardview);
textView = itemView.findViewById(R.id.categoryName);
linearLayout = itemView.findViewById(R.id.llbackground);
selectItemLayout = itemView.findViewById(R.id.selectItemLayout);
selectItem = itemView.findViewById(R.id.selectItem);
selectItemLayout.setVisibility(authenticationHelper.isAdmin() ? View.VISIBLE : View.GONE);
itemView.setOnClickListener(this);
Util.setRandomGradientColor(linearLayout, GradientDrawable.Orientation.LEFT_RIGHT);
}
#Override
public void onClick(View view) {
if (mItemClickListener != null) {
mItemClickListener.onItemClickListener(view, getAdapterPosition(), categoryList.get(getAdapterPosition()));
}
}
}
}
Code for view
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:card_view="http://schemas.android.com/apk/res-auto"
android:layout_height="wrap_content"
android:layout_width="wrap_content">
<androidx.cardview.widget.CardView
android:id="#+id/cardview"
android:layout_width="150dp"
android:layout_height="100dp"
card_view:cardCornerRadius="5dp"
card_view:cardElevation="2dp">
<LinearLayout
android:id="#+id/llbackground"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:background="#drawable/category_gradient"
android:gravity="center">
<TextView
android:id="#+id/categoryName"
android:text="Men"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="18sp"
android:textColor="#color/colorWhite"
android:textStyle="bold"/>
</LinearLayout>
<RelativeLayout
android:id="#+id/selectItemLayout"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:elevation="10dp"
android:padding="5dp">
<ImageView
android:id="#+id/selectItem"
android:layout_width="20dp"
android:layout_height="20dp"
android:clickable="false"
android:tag="#string/unchecked"
android:enabled="false"
android:layout_alignParentBottom="true"
android:layout_alignParentRight="true"
android:src="#drawable/circle"/>
</RelativeLayout>
</androidx.cardview.widget.CardView>
</RelativeLayout>
Recycler view in activity
<androidx.recyclerview.widget.RecyclerView
android:id="#+id/categoriesRecyclerView"
android:layout_below="#+id/categoriesLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_centerHorizontal="true"/>
Thanks for help!!
class MyAdapter extends RecyclerView.Adapter<MyAdapter.ViewHolder> {
private final int listSize;
private String[] list;
public MyAdapter(int listSize, String[] list) {
this.listSize = listSize;
this.list = list;
}
#NonNull
#Override
public ViewHolder onCreateViewHolder(#NonNull ViewGroup parent, int viewType) {
Context context = parent.getContext();
LayoutInflater inflater = LayoutInflater.from(context);
View view = inflater.inflate(R.layout.rv_item,parent,false);
ViewHolder viewHolder = new ViewHolder(view);
return viewHolder;
}
#Override
public void onBindViewHolder(#NonNull ViewHolder holder, int position) {
holder.bind(position);
}
#Override
public int getItemCount() {
return listSize;
}
class ViewHolder extends RecyclerView.ViewHolder {
TextView item;
Button button;
public ViewHolder(#NonNull View itemView) {
super(itemView);
item = itemView.findViewById(R.id.item_view);
button = itemView.findViewById(R.id.button_item_view);
}
void bind(int index) {
button.setText(String.valueOf(index));
item.setText(list[index]);
}
}
}
In this adapter class I want to show a String array. The only problem is that the first element (with zero index) is missed on the screen, despite the fact that element exist beyond the screen (Sorry for such a big picture).
The buttons must be numbered with zero, so should the array be printed starting with zero element. How can I resolve this problem?
UPD: Here's the layout:
rv_item
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:id="#+id/item_view"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical|start"
android:text="TextView"
android:textSize="40sp" />
<Button
android:id="#+id/button_item_view"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical|end"
android:layout_marginEnd="48dp"
android:padding="10dp"
android:text="INFO" />
</FrameLayout>
activity_top, in which recyclerView widget is used
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".TopLevelActivity">
<androidx.recyclerview.widget.RecyclerView
android:id="#+id/listOfGoods"
android:layout_width="409dp"
android:layout_height="729dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
tools:listitem="#layout/rv_item" />
</androidx.constraintlayout.widget.ConstraintLayout>
Do not give fixed height of recyclerView
Correct Way:
<androidx.recyclerview.widget.RecyclerView
android:id="#+id/listOfGoods"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:layoutManager="androidx.recyclerview.widget.LinearLayoutManager"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
tools:listitem="#layout/rv_items" />
P.S: I have taken your example and run the app on api 29 work fine cheers!
Also Do not make it listSize final, compiler will give error as you are not initializing
I know that it could be the same, but I would like to corroborate, try to change this code:
#Override
public int getItemCount() {
return listSize;
}
For this:
#Override
public int getItemCount() {
return list.length;
}
One more thing: try to debug this object this.list = list; and check if the array contains zero index element. If the array contains that element, probably it should something about layout as mentioned #Rui Alves.
Before marking as duplicate et al, viewpager from recyclerview is kind of triccky. The code is able to retrieve data from db and display it in a recyclerview with cardview. I want to be able to scroll the images in the viewpager while in the recyclerview as shown in the wireframe below, the dots represent number of images retrieved from DB which should be swipeeable right or left. Imageview seems not to be swipeable.
IMAGE ADAPTER.JAVA
public class ImageAdapter extends RecyclerView.Adapter<ImageAdapter.ViewHolder> {
List<ImageList>imageLists;
Context context;
public ImageAdapter(List<ImageList> imageLists, Context context) {
this.imageLists = imageLists;
this.context = context;
}
#NonNull
#Override
public ViewHolder onCreateViewHolder(#NonNull ViewGroup parent, int viewType) {
View v= LayoutInflater.from(parent.getContext()).inflate(R.layout.image_list,parent,false);
return new ViewHolder(v);
}
#Override
public void onBindViewHolder(#NonNull ViewHolder holder, int position) {
ImageList imageList = imageLists.get(position);
holder.tvname.setText(imageList.getName());
holder.tv2name.setText(imageList.getName2());
String picture = imageList.getImageurl();
String picture2 = imageList.getImage2url();
String picture3 = imageList.getImage3url();
String[] imageUrls = {picture, picture2, picture3};
Picasso.get()
.load(picture)
.placeholder(R.drawable.placeholder)
.error(R.drawable.error)
.resize(0, 200)
.into(holder.img);
}
#Override
public int getItemCount() {
return imageLists.size();
}
public class ViewHolder extends RecyclerView.ViewHolder{
private TextView tvname, tv2name;
private ImageView img;
public ViewHolder(View itemView) {
super(itemView);
img=(ImageView)itemView.findViewById(R.id.image_view);
tvname=(TextView)itemView.findViewById(R.id.txt_name);
tvname=(TextView)itemView.findViewById(R.id.txt_name2);
}
}
}
XML
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center">
<androidx.cardview.widget.CardView
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:cardCornerRadius="5dp"
app:cardElevation="3dp"
app:cardUseCompatPadding="true"
android:padding="2dp"
android:layout_margin="5dp">
<ImageView
android:id="#+id/image_view"
android:layout_width="match_parent"
android:layout_height="200dp"
android:contentDescription="ServerImg"
android:scaleType="fitXY" />
</androidx.cardview.widget.CardView>
<TextView
android:textSize="25sp"
android:textColor="#color/colorPrimaryDark"
android:textStyle="bold"
android:id="#+id/txt_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<TextView
android:textSize="25sp"
android:textColor="#color/colorPrimaryDark"
android:textStyle="bold"
android:id="#+id/txt_name2"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
I want select optionally one image like radio button and put tick mark on that image. How can i do it.Here I add my adapter class and xml file
This is my adapter class.
public class StarCountAdapter extends RecyclerView.Adapter<StarCountAdapter.StarCountHolder> {
Context context;
LayoutInflater inflater;
List<StarCount> starCounts = new ArrayList<>();
public StarCountAdapter(Context context, List<StarCount> starCounts) {
this.context = context;
this.inflater = LayoutInflater.from(context);
this.starCounts = starCounts;
}
#Override
public StarCountHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = inflater.inflate(R.layout.star_count_row,parent,false);
return new StarCountHolder(view);
}
#Override
public void onBindViewHolder(StarCountHolder holder, int position) {
StarCount model = starCounts.get(position);
Picasso.with(context)
.load("http://"+model.getImagePath())
.into(holder.starImage);
holder.actorName.setText(model.getName());
holder.counts.setText(""+model.getCount());
}
#Override
public int getItemCount() {
return starCounts.size();
}
public class StarCountHolder extends RecyclerView.ViewHolder {
ImageView starImage;
TextView actorName,counts;
public StarCountHolder(View itemView) {
super(itemView);
starImage = (ImageView) itemView.findViewById(R.id.starCountIv);
actorName = (TextView) itemView.findViewById(R.id.acterName);
counts = (TextView) itemView.findViewById(R.id.counts);
}
}
}
This is my star_count_row.xml file.
<?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="wrap_content"
android:padding="5dp">
<ImageView
android:id="#+id/starCountIv"
android:layout_width="match_parent"
android:layout_height="175dp" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="120dp"
android:orientation="vertical">
<TextView
android:id="#+id/acterName"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textColor="#000" />
<TextView
android:id="#+id/counts"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textColor="#000" />
</LinearLayout>
</RelativeLayout>
Thank You.
create custom radio button followin link here you will get your answer
Adding custom radio buttons in android
I'm in the process of learning android app development however I have come to an impasse. My RecyclerView is picking up how many items are in my String array however it is not showing the actual data in text1, any help would be much appreciated.
This is my adapter
public class EventCalenderAdapter extends RecyclerView.Adapter<EventCalenderAdapter.ViewHolder> {
static String[] fakeData = new String[] {
"One","Two","Three","Four","Five","Ah..Ah..Ah"
};
static class ViewHolder extends RecyclerView.ViewHolder {
CardView cardView;
TextView titleView;
public ViewHolder(CardView card) {
super(card);
cardView = card;
titleView = (TextView) card.findViewById(R.id.text1);
}
}
#Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int i) {
CardView v = (CardView) LayoutInflater.from(parent.getContext()).inflate(R.layout.event_task, parent, false);
return new ViewHolder(v);
}
#Override
public void onBindViewHolder(ViewHolder viewHolder, int i) {
viewHolder.titleView.setText(fakeData[i]);
}
#Override
public int getItemCount() {
return fakeData.length;
}
This is the corresponding XML
<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.CardView
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:event_view="http://schemas.android.com/apk/res-auto"
android:layout_height="100dp"
android:layout_width="match_parent"
android:id="#+id/event_view"
android:layout_marginTop="#dimen/task_card_half_spacing"
android:layout_marginBottom="#dimen/task_card_half_spacing"
android:layout_marginLeft="#dimen/gutter"
android:layout_marginRight="#dimen/gutter"
android:layout_gravity="center"
android:elevation="#dimen/task_card_elevation"
android:foreground="?android:attr/selectableItemBackground"
event_view:cardCornerRadius="0dp" >
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent" >
<ImageView
android:layout_width="100dp"
android:layout_height="100dp"
android:id="#+id/image"
android:scaleType="centerCrop"
android:layout_alignParentLeft="true"/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
style="#android:style/TextAppearance.Medium.Inverse"
android:id="#+id/text1"
android:maxLines="1"
android:ellipsize="end"
android:padding="10dp"
android:layout_alignTop="#+id/image"
android:layout_toRightOf="#+id/image"/>
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
style="#android:style/TextAppearance.Inverse"
android:id="#+id/text2"
android:maxLines="3"
android:ellipsize="end"
android:padding="10dp"
android:layout_alignStart="#+id/text1"
android:layout_below="#+id/text1"/>
</RelativeLayout>
</android.support.v7.widget.CardView>
and this is my fragment
public class EventCalenderFragment extends Fragment {
RecyclerView recyclerView;
EventCalenderAdapter adapter;
public EventCalenderFragment() {
// Required empty public constructor
}
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
adapter = new EventCalenderAdapter();
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
final View v = inflater.inflate(R.layout.fragment_event_calender, container, false);
recyclerView = (RecyclerView) v.findViewById(R.id.recycler);
recyclerView.setAdapter(adapter);
recyclerView.setHasFixedSize(true);
recyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));
return v;
}
Hm... everything looks ok, can you try setting this to it:
android:textColor="#000000"