RecyclerView to be Scrolled horizontally with the width - android

I have more items to show in the cardView horizontally inside a recyclerView and I have more cards veritically.
I tried to place cardView inside a HorizontalScrollView, It worked to scroll the idividual card. I wanted to scroll the entire RecyclerView to be scrolled to see right end items.
I tried with the RecyclerView inside a HorizontalScroolView not worked. RecyclerView inside a NestedScrollView not worked.
The RecyclerView is in a fragment. Inside the viewPager tabLayout, this is one of the fragment
fragment 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:orientation="horizontal">
<android.support.v7.widget.RecyclerView
android:id="#+id/record_recycler"
android:layout_width="550dp"
android:scrollbars="horizontal"
android:layout_height="0dp"
android:layout_weight="1" />
</LinearLayout>
Adapter:
public class TestAdapter extends RecyclerView.Adapter{
private List<Model> items;
Context context;
public TestAdapter(Context con, List<Model> itemslist) {
context=con;
this.items = itemslist;
}
#Override
public int getItemViewType(int position) {
return items.get(position).getUnique();
}
#Override
public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
if(viewType==0)
return new MyViewHolder(LayoutInflater.from(parent.getContext()).inflate(R.layout.cricketrank_record_valuecard, parent, false));
else
return new MyViewHolder1(LayoutInflater.from(parent.getContext()).inflate(R.layout.cricketrank_record_titlecard, parent, false));
}
#Override
public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
if (holder instanceof MyViewHolder)
try{
((MyViewHolder)holder).bindViewHolder(position);
}catch (Exception e){
Log.e(Constant.Tag,e.toString());
}
else if(holder instanceof MyViewHolder1)
try{
((MyViewHolder1)holder).bindViewHolder(position);
}catch (Exception e){
Log.e(Constant.Tag,e.toString());
}
}
#Override
public int getItemCount() {
return items.size();
}
public class MyViewHolder extends RecyclerView.ViewHolder{
public TextView text,text1,text2,text3,text4,text5;
public View mCardView;
public MyViewHolder(View view) {
super(view);
text1 = (TextView) view.findViewById(R.id.text1);
text2 = (TextView) view.findViewById(R.id.text2);
text3 = (TextView) view.findViewById(R.id.text3);
text4 = (TextView) view.findViewById(R.id.text4);
}
public void bindViewHolder(int position) {
text1.setText(items.get(position).getTeam());
text2.setText(items.get(position).getRank());
text3.setText(items.get(position).getMatches());
text4.setText(items.get(position).getPoints());
}
}
}
public class MyViewHolder1 extends RecyclerView.ViewHolder{
public TextView text,text1,text2,text3,text4,text5;
public RelativeLayout rl;
public MyViewHolder1(View view) {
super(view);
text = (TextView) view.findViewById(R.id.text);
text1 = (TextView) view.findViewById(R.id.text1);
text2 = (TextView) view.findViewById(R.id.text2);
text3 = (TextView) view.findViewById(R.id.text3);
text4 = (TextView) view.findViewById(R.id.text4);
rl = (RelativeLayout) view.findViewById(R.id.rl);
}
public void bindViewHolder(int position) {
text1.setText(items.get(position).getTeam());
text2.setText(items.get(position).getRank());
text3.setText(items.get(position).getMatches());
text4.setText(items.get(position).getPoints());
}
}
}
MainActivity:
RecyclerView record_recycler= (RecyclerView) view.findViewById(R.id.record_recycler);
RecyclerView.LayoutManager mLayoutManager = new LinearLayoutManager(getActivity());
record_recycler.setLayoutManager(mLayoutManager);
record_recycler.setItemAnimator(new DefaultItemAnimator());
TestAdapter adapter = new TestAdapter(getActivity(), list);
record_recycler.setAdapter(adapter);

I think you are trying to scroll the recyclerview in two direction. Vertical is usual scroll of recylerview and in horizontally, the scrolling view if it is not fit with the mentioned width. Right?
If you are trying this, then I think it is not possible to scroll like you think.
You can scroll the recycler view in both the directions with different views holding on them.
As you said the scrolling of cardview is possible, by default it won't have any scrolling properly normally. So it will scroll to show the card content completely to the user if they are not seeing completely and you set the scrolling view to scroll.
The possible way is, keep the views vertically with their field name and value. If you want to create more, then add one more row to accommodate the values down.
Have a look below:
<android.support.v7.widget.CardView
android:id="#+id/card"
android:layout_marginTop="2dp"
android:layout_marginBottom="4dp"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<LinearLayout
android:id="#+id/layout2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#+id/layout1"
android:orientation="horizontal">
<TextView
android:text="One"
android:textSize="#dimen/font_itemsrow_13dp"
android:layout_weight=".11"
android:paddingLeft="3dp"
android:paddingRight="3dp"
android:layout_width="0dp"
android:layout_height="wrap_content" />
<TextView
android:text="Two"
android:textSize="#dimen/font_itemsrow_13dp"
android:layout_weight=".11"
android:paddingRight="3dp"
android:layout_width="0dp"
android:layout_height="wrap_content" />
<TextView
android:text="Three"
android:textSize="#dimen/font_itemsrow_13dp"
android:layout_weight=".24"
android:paddingRight="3dp"
android:layout_width="0dp"
android:layout_height="wrap_content" />
<TextView
android:text="Four"
android:textSize="#dimen/font_itemsrow_13dp"
android:layout_weight=".11"
android:paddingRight="3dp"
android:layout_width="0dp"
android:layout_height="wrap_content" />
<TextView
android:text="Five"
android:textSize="#dimen/font_itemsrow_13dp"
android:layout_weight=".17"
android:paddingRight="3dp"
android:layout_width="0dp"
android:layout_height="wrap_content" />
<TextView
android:text="Six"
android:textSize="#dimen/font_itemsrow_13dp"
android:layout_weight=".26"
android:paddingRight="3dp"
android:layout_width="0dp"
android:layout_height="wrap_content" />
</LinearLayout>
<LinearLayout
android:id="#+id/layout3"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="#dimen/padding_3dp"
android:layout_below="#+id/layout2"
android:orientation="horizontal">
<TextView
android:id="#+id/one"
android:textSize="#dimen/font_itemsrow_14dp"
android:textColor="#color/black"
android:layout_weight=".11"
android:paddingLeft="3dp"
android:paddingRight="3dp"
android:layout_width="0dp"
android:layout_height="wrap_content" />
<TextView
android:id="#+id/two"
android:textSize="#dimen/font_itemsrow_14dp"
android:textColor="#color/black"
android:layout_weight=".11"
android:paddingRight="3dp"
android:layout_width="0dp"
android:layout_height="wrap_content" />
<TextView
android:id="#+id/three"
android:textSize="#dimen/font_itemsrow_14dp"
android:textColor="#color/black"
android:layout_weight=".24"
android:paddingRight="3dp"
android:layout_width="0dp"
android:layout_height="wrap_content" />
<TextView
android:id="#+id/four"
android:textSize="#dimen/font_itemsrow_14dp"
android:textColor="#color/black"
android:layout_weight=".11"
android:paddingRight="3dp"
android:layout_width="0dp"
android:layout_height="wrap_content" />
<TextView
android:id="#+id/five
android:textSize="#dimen/font_itemsrow_14dp"
android:textColor="#color/black"
android:layout_weight=".17"
android:paddingRight="3dp"
android:layout_width="0dp"
android:layout_height="wrap_content" />
<TextView
android:id="#+id/six"
android:textSize="#dimen/font_itemsrow_14dp"
android:textColor="#color/black"
android:layout_weight=".26"
android:paddingRight="3dp"
android:layout_width="0dp"
android:layout_height="wrap_content" />
</LinearLayout>
<LinearLayout
....// Add extra title Row
</LinearLayout>
<LinearLayout
....// Add extra Values Row
</LinearLayout>
</RelativeLayout>
</android.support.v7.widget.CardView>

You can archive it with LayoutManager refrence, with 3 arguments, in your case, change MainActivity a little bit, it will look this:
RecyclerView.LayoutManager mLayoutManager
= new LinearLayoutManager(getActivity(), LinearLayoutManager.HORIZONTAL, false);
Like documentation says, second argument define orientation:
/**
* #param context Current context, will be used to access resources.
* #param orientation Layout orientation. Should be {#link #HORIZONTAL} or {#link
* #VERTICAL}.
* #param reverseLayout When set to true, layouts from end to start.
*/

Related

How to implement EmojiTextView in fragment?

I have an emoji in one of my data in cloud firestore. How do I can make the value readable in my fragment,recycleview? When adding old emoji such as 😭, it shows in my recycler view but when ading new emoji such as "🥺". Is it because of my android keyboard or language didn't update? I hope anyone can solve my problem.
ForumAdapter.java
public ForumHolder onCreateViewHolder(#NonNull ViewGroup parent, int viewType) {
android.view.View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.cardviewforumtitle,parent,false);
EmojiCompat.init(new BundledEmojiCompatConfig(v.getContext()));
return new ForumHolder(v);
}
class ForumHolder extends RecyclerView.ViewHolder{
EmojiTextView textViewTitle;
EmojiTextView textViewDescription;
EmojiTextView timeStamp;
/*
TextView textViewTitle;
TextView textViewDescription;
TextView timeStamp;*/
public ForumHolder(View itemView) {
super(itemView);
textViewTitle = itemView.findViewById(R.id.tvTitle);
textViewDescription = itemView.findViewById(R.id.tvDescription);
timeStamp = itemView.findViewById(R.id.tvTimestamp);
textViewTitle.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
int position = getAdapterPosition();
// NO_POSITION to prevent app crash when click -1 index
if(position != RecyclerView.NO_POSITION && listener !=null ){
listener.onItemClick(getSnapshots().getSnapshot(position),position);
}
}
});
}
}
Because I got this error
java.lang.ClassCastException: androidx.appcompat.widget.AppCompatTextView cannot be cast to androidx.emoji.widget.EmojiTextView
cardviewforumtitle.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:weightSum="2">
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:orientation="vertical">
<TextView
android:id="#+id/tvTitle"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="name"
android:textSize="15sp"
android:textStyle="bold" />
<TextView
android:id="#+id/tvDescription"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="description"
android:textSize="15sp" />
<TextView
android:id="#+id/tvTimestamp"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="timestamp"
android:textSize="15sp" />
</LinearLayout>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_weight="1">
<ImageView
android:id="#+id/profilePic"
android:layout_width="match_parent"
android:layout_height="match_parent"></ImageView>
</LinearLayout>
</LinearLayout>
Try replacing the TextViews with
<androidx.emoji.widget.EmojiTextView
..../>
in the layout.

How to make Horizontal line attach textview in recyclerview?

Am having a doubt regarding how to make the horizontal view to touch the textview while populating in recyclerview , Now let me explain briefly i having a horizontal recyclerview in that adapter view holder i have horizontal view at the end of the textview which is center vertical to that textview, so when items are added each view must touch the textview like vertical dotted timeline but the view is not touching the textview as shown in the below image.
Now let me post what i have tried so far
This is the layout that holding recyclerview:
<RelativeLayout android:layout_width="wrap_content"
android:layout_height="wrap_content"
xmlns:android="http://schemas.android.com/apk/res/android" >
<android.support.v7.widget.RecyclerView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/recyclerview"/>
</RelativeLayout>
Here am using that recyclerview in activity:
Recyclerview recyclerView = (RecyclerView) findViewById(R.id.recyclerview);
recyclerView.setLayoutManager(new LinearLayoutManager(this, LinearLayoutManager.HORIZONTAL, false));
recyclerView.setHasFixedSize(true);
TrackingAdapter mAdapter = new TrackingAdapter(this.taskLogModelList,mTaskModel.getTaskID(),fm,String.valueOf(mLastLocation.getLatitude()),String.valueOf(mLastLocation.getLongitude()));
recyclerView.setAdapter(mAdapter);
mAdapter.notifyDataSetChanged();
Here is the adapter am using :
public class TrackingAdapter extends RecyclerView.Adapter<TrackingAdapter.ViewHolder> {
private List<TaskLogModel> taskStatusFlowModels;
private D2DKnocks appclass;
private String Lat,Long;
private FragmentManager fragmentManager;
private String TaskID;
DateFormat date,msimpleDateformat;
public TrackingAdapter(List<TaskLogModel> taskStatusFlowModels,String TaskID,FragmentManager context,String Lat,String Long) {
this.taskStatusFlowModels = taskStatusFlowModels;
this.fragmentManager=context;
date=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
msimpleDateformat = new SimpleDateFormat("hh.mm a");
this.Lat=Lat;
this.TaskID=TaskID;
this.Long=Long;
appclass=((D2DKnocks)D2DKnocks.getContext());
}
public class ViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener{
private AvertaTextview txt_time;
private Rubik txt_delivered;
// private AvertaButton btn_start;
private RelativeLayout layout;
private View view_right;
public ViewHolder(View itemView) {
super(itemView);
txt_time = itemView.findViewById(R.id.txt_time);
// btn_start=itemView.findViewById(R.id.start);
layout=itemView.findViewById(R.id.layout);
txt_delivered = itemView.findViewById(R.id.txt_delivered);
view_right = (View) itemView.findViewById(R.id.right_view_line);
layout.setOnClickListener(this);
}
#Override
public void onClick(View view) {
TaskLogModel taskLogModel=taskStatusFlowModels.get(getAdapterPosition());
if(taskLogModel.getTaskStatusID()==0){
TaskStatusDialog taskStatusDialog=new TaskStatusDialog();
Bundle bundle=new Bundle();
bundle.putInt("Size",taskStatusFlowModels.size());
bundle.putString("Lat",Lat);
bundle.putString("Long",Long);
bundle.putString("TaskID",TaskID);
taskStatusDialog.setArguments(bundle);
taskStatusDialog.show(fragmentManager,"show");
}
}
}
#Override
public TrackingAdapter.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View itemView = LayoutInflater.from(parent.getContext()).inflate(R.layout.row_view_assignment_status, parent, false);
return new ViewHolder(itemView);
}
#Override
public void onBindViewHolder(TrackingAdapter.ViewHolder holder, int position) {
if (taskStatusFlowModels.get(position).getTaskStatusID() > 0) {
StatusModel statusFlowModel = getStatusName(taskStatusFlowModels.get(position).getTaskStatusID());
holder.txt_delivered.setText(statusFlowModel.getStatus());
holder.txt_delivered.setBackgroundResource(R.drawable.btn_arrived_bg);
try {
Date uppdateDate=date.parse(taskStatusFlowModels.get(position).getUpdateDateTime());
String updatedDate=msimpleDateformat.format(uppdateDate);
String[] each = updatedDate.split(" ");
String str = each[1].replace("AM", "am").replace("PM","pm");
// SpannableString ss1= new SpannableString(each[0]);
// SpannableString ss2=new SpannableString(each[1]);
// ss1.setSpan(new AbsoluteSizeSpan(20), 0, each[0].length(), SPAN_INCLUSIVE_INCLUSIVE);
// ss2.setSpan(new AbsoluteSizeSpan(15), 0, each[1].length(), SPAN_INCLUSIVE_INCLUSIVE);
// CharSequence finalText = TextUtils.concat(ss1, " ", ss2);
holder.txt_time.setText(each[0]+" "+str);
} catch (ParseException e) {
e.printStackTrace();
}
GradientDrawable gradientDrawable = (GradientDrawable) holder.txt_delivered.getBackground();
gradientDrawable.setColor(Color.parseColor(statusFlowModel.getStatusColor()));
} else {
holder.txt_delivered.setText("Choose");
holder.txt_delivered.setBackgroundResource(R.drawable.btn_choose_bg);
holder.txt_delivered.setTextColor(Color.parseColor("#b1b1b1"));
}
if(taskStatusFlowModels.size()-1==position){
holder.view_right.setVisibility(View.GONE);
}
else {
holder.view_right.setVisibility(View.VISIBLE);
}
}
public StatusModel getStatusName(Integer statusID){
for (int i=0;i<appclass.getStatusModelist().size();i++){
if (appclass.getStatusModelist().get(i).getStatusID().equals(statusID)){
return appclass.getStatusModelist().get(i);
}
}
return null;
}
#Override
public int getItemCount() {
return taskStatusFlowModels.size();
}
}
And below is the view holder layout:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:orientation="vertical">
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/layout">
<com.trident.Hawkersky.service.CustomFonts.AvertaTextview
android:id="#+id/txt_time"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="5dp"
android:drawableLeft="#drawable/ic_checked"
android:drawablePadding="10dp"
android:layout_centerHorizontal="true"
android:gravity="center"
android:text="10.00 am"
android:textColor="#444444"
android:textSize="12sp"
/>
<View
android:id="#+id/vertical_line"
android:layout_width="30dp"
android:layout_height="20dp"
android:layout_centerHorizontal="true"
android:layout_below="#+id/txt_time"
android:background="#drawable/vertical_dotted_line"
android:layerType="software"
android:visibility="visible" />
<RelativeLayout
android:layout_width="wrap_content"
android:layout_below="#+id/vertical_line"
android:layout_height="wrap_content">
<com.trident.Hawkersky.service.CustomFonts.Rubik
android:id="#+id/txt_delivered"
android:layout_centerHorizontal="true"
android:layout_width="wrap_content"
android:layout_height="#dimen/thirty"
android:layout_marginBottom="5dp"
android:singleLine="true"
android:padding="#dimen/seven"
android:background="#drawable/btn_delivered_bg"
android:gravity="center"
android:text="Delivered"
android:textAllCaps="true"
android:textColor="#FFF"
android:textSize="13sp"
android:textStyle="bold" />
<View
android:id="#+id/right_view_line" <--- this is the view
android:layout_width="#dimen/hundred"
android:layout_height="5dp"
android:layout_alignBottom="#+id/txt_delivered"
android:layout_gravity="center_vertical"
android:layout_marginBottom="12dp"
android:layout_toEndOf="#+id/txt_delivered"
android:layout_toRightOf="#+id/txt_delivered"
android:background="#drawable/dotted"
android:layerType="software"
android:visibility="visible" />
</RelativeLayout>
</RelativeLayout>
</RelativeLayout>
Now what i need is i need that view to attach to the textview , but am getting like image above how to solve it.
Please try this and let know if you need further modifications.
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="#+id/layout"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:orientation="vertical">
<com.trident.Hawkersky.service.CustomFonts.AvertaTextview
android:id="#+id/txt_time"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_marginLeft="5dp"
android:layout_marginStart="5dp"
android:drawableLeft="#drawable/ic_checked"
android:drawablePadding="10dp"
android:gravity="center"
android:text="10.00 am"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintLeft_toLeftOf="#+id/txt_delivered"
app:layout_constraintRight_toRightOf="#+id/txt_delivered"
android:textColor="#444444"
android:textSize="12sp"
/>
<View
android:id="#+id/vertical_line"
android:layout_width="30dp"
android:layout_height="20dp"
app:layout_constraintLeft_toLeftOf="#+id/txt_delivered"
app:layout_constraintRight_toRightOf="#+id/txt_delivered"
android:layout_below="#+id/txt_time"
android:layout_centerHorizontal="true"
app:layout_constraintTop_toBottomOf="#+id/txt_time"
android:background="#color/colorPrimaryDark"
android:layerType="software"
android:visibility="visible"/>
<com.trident.Hawkersky.service.CustomFonts.Rubik
android:id="#+id/txt_delivered"
android:layout_width="wrap_content"
android:layout_height="#dimen/thirty"
android:layout_marginBottom="5dp"
android:background="#drawable/btn_delivered_bg"
android:gravity="center"
app:layout_constraintTop_toBottomOf="#+id/vertical_line"
android:padding="#dimen/seven"
android:singleLine="true"
android:text="Delivered"
android:textAllCaps="true"
android:textColor="#FFF"
android:textSize="13sp"
android:textStyle="bold"/>
<View
android:id="#+id/right_view_line"
android:layout_width="100dp"
android:layout_height="5dp"
app:layout_constraintTop_toTopOf="#+id/txt_delivered"
app:layout_constraintBottom_toBottomOf="#+id/txt_delivered"
app:layout_constraintLeft_toRightOf="#+id/txt_delivered"
android:background="#drawable/dotted"
android:layerType="software"
android:visibility="visible"/>
</android.support.constraint.ConstraintLayout>
Sorry, I had to use ConstraintLayout. If you haven't been using it before, please add this implementation 'com.android.support.constraint:constraint-layout:1.0.2'1 to your module level build.gradle.

RecyclerView extra empty gap between items

1.i just implemented some recyclerviews, some of their items have extra empty space. this space sometimes disappears after some scrolling. i have tried every way you could think! i have changed heights to wrap content but still issue exists!
here is the screenshot:
Recyclerview empty space
here is item xml:
<android.support.v7.widget.CardView
android:id="#+id/btn"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#ffffff"
android:gravity="center"
android:orientation="horizontal"
android:adjustViewBounds="true"
android:padding="8dp"
android:layout_margin="6dp"
>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<ImageView
android:id="#+id/banner_image"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:adjustViewBounds="true"
android:src="#drawable/no_image_banner" />
<TextView
android:id="#+id/txtHey"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="4dp"
android:gravity="center"
android:maxLength="20"
android:text="sadas"
android:textColor="#4d4a46"
android:textSize="11dp"
android:textStyle="bold" />
<TextView
android:id="#+id/txtHi"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="2dp"
android:gravity="center"
android:text="sadas"
android:textColor="#999999"
android:textSize="9dp" />
<FrameLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:gravity="center_vertical">
<TextView
android:id="#+id/txtPrice"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:text="sadas"
android:textColor="#888888"
android:textSize="9dp" />
<LinearLayout
android:id="#+id/price_cancel"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="6dp"
android:background="#888888"
android:orientation="horizontal" />
</FrameLayout>
<TextView
android:id="#+id/txtFinalPrice"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:text="sadas"
android:textColor="#3dc24d"
android:textSize="9dp" />
<TextView
android:id="#+id/txtHello"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center_horizontal"
android:text="hey"
android:textSize="9dp"
android:visibility="gone" />
</LinearLayout>
</android.support.v7.widget.CardView>
main page xml:
<android.support.design.widget.AppBarLayout
android:id="#+id/topBar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:theme="#style/AppTheme.AppBarOverlay">
<include
layout="#layout/toolbar_main"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</android.support.design.widget.AppBarLayout>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.v7.widget.RecyclerView
android:id="#+id/product_list"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="6dp"
>
</android.support.v7.widget.RecyclerView>
<TextView
android:id="#+id/request_results"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:textSize="21dp"
android:textStyle="bold" />
</RelativeLayout>
second question is i used some recyclerviews inside a nested scroll view to prevent scrolling issues. everything is good but in lower versions of android like kitkat i have problem with scrolling(cause nested wont work on sdk<21 ) . i could use a normal scrollview that works well on both versions but normal scroll view doesnt work with coordinator layout and so appbar layout hiding behaviour wont work!
can you help me with these problems please?
here is the adapter class:
private ArrayList<Struct> structs;
OnItemListener onItemListener;
private boolean isGrid;
private int Tab;
public Adapter_Recycler(ArrayList<Struct> structs, OnItemListener onItemListener, int Tab, boolean isGrid) {
this.structs = structs;
this.onItemListener = onItemListener;
this.Tab = Tab;
this.isGrid = isGrid;
}
#Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
LayoutInflater inflater = (LayoutInflater) parent.getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View view = null;
if (Tab == 2) {
view = inflater.inflate(R.layout.item_product_color, parent, false);
}
if (Tab == 1 && isGrid == false) {
view = inflater.inflate(R.layout.item_product_list, parent, false);
}
if (Tab == 1 && isGrid) {
view = inflater.inflate(R.layout.item_product_list, parent, false);
}
ViewHolder viewHolder = new ViewHolder(view);
return viewHolder;
}
#Override
public void onBindViewHolder(ViewHolder holder, final int position) {
if (Tab == 2) {
holder.txtHey.setText(structs.get(position).hey);
holder.txtPrice.setText(structs.get(position).hi);
holder.txtHi.setVisibility(View.GONE);
holder.PriceCancel.setVisibility(View.GONE);
holder.txtFinalPrice.setVisibility(View.GONE);
holder.txtHey.setTypeface(Activity_Main.SANS);
holder.txtPrice.setTypeface(Activity_Main.SANS);
Glide
.with(Activity_Main.CONTEXT)
.load(structs.get(position).image)
.placeholder(R.drawable.background_card)
.fitCenter()
.into(holder.banner_image);
}
if (Tab == 1) {
holder.txtHey.setText(structs.get(position).hey);
holder.txtHi.setText(structs.get(position).hi);
holder.txtPrice.setText(structs.get(position).price);
holder.txtFinalPrice.setText(structs.get(position).final_price);
if (holder.txtFinalPrice.getText().toString() == "") {
holder.PriceCancel.setVisibility(View.GONE);
} else {
holder.PriceCancel.setVisibility(View.VISIBLE);
}
holder.txtHey.setTypeface(Activity_Main.SANS);
holder.txtHi.setTypeface(Activity_Main.SANS);
holder.txtPrice.setTypeface(Activity_Main.SANS);
holder.txtFinalPrice.setTypeface(Activity_Main.SANS);
Glide
.with(Activity_Main.CONTEXT)
.load(structs.get(position).image)
.placeholder(R.drawable.background_card)
.fitCenter()
.into(holder.banner_image);
}
holder.linearLayout.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if (onItemListener != null) {
onItemListener.onItemSelect(position);
}
}
});
holder.txtHello.setText(structs.get(position).hello);
holder.txtHello.setTypeface(Activity_Main.SANS);
}
#Override
public int getItemCount() {
return structs.size();
}
public static class ViewHolder extends RecyclerView.ViewHolder {
CardView linearLayout;
ImageView banner_image;
TextView txtHey;
TextView txtHi;
TextView txtHello;
TextView txtPrice;
TextView txtFinalPrice;
LinearLayout PriceCancel;
public ViewHolder(View itemView) {
super(itemView);
linearLayout = (CardView) itemView.findViewById(R.id.btn);
banner_image = (ImageView) itemView.findViewById(R.id.banner_image);
txtHey = (TextView) itemView.findViewById(R.id.txtHey);
txtHi = (TextView) itemView.findViewById(R.id.txtHi);
txtHello = (TextView) itemView.findViewById(R.id.txtHello);
txtPrice = (TextView) itemView.findViewById(R.id.txtPrice);
txtFinalPrice = (TextView) itemView.findViewById(R.id.txtFinalPrice);
PriceCancel = (LinearLayout) itemView.findViewById(R.id.price_cancel);
}
}
}
UPDATE:
the place holder aspect ratio must not be much different from the images aspect ratio. they should be the same.
If possible then provide adapter class because may be some issue with onCreateViewHolder with LayoutInflater.
I finally figured out what the problem was!
that extra space was because of
" .placeholder(R.drawable.background_card) " !
problem solved after removing the placeholder.
ofcourse issue is with the image! not placeholder.

How to set up this row in XML?

I am trying to display a RecyclerView inside a DialogFragment and I can't get the row-item XML to work right.
I am trying to display something like this:
TEXTVIEW1 EDITTEXT TEXTVIEW2
where TEXTVIEW1 takes up 1/2th of the width, EDITTEXT takes up 1/4th of the width by default, and TEXTVIEW2 takes up 1/4th of the width (the rest of it).
I stuck these three Views inside a horizontal LinearLayout. I gave each View a layout_height of wrap_content and a layout_width of 0dp and then I assigned layout_weight values to 2, 1, 1 respectively.
However it doesn't display as expected. After I populate all the rows with data and text, the EditText widths seem to wrap to their values instead of taking up the quarter portion I designated.
How do I fix this?
XML
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
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:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content"
>
<TextView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:singleLine="true"
android:ellipsize="end"
android:text="starttext"
android:textSize="16sp"
android:layout_weight="2"
/>
<EditText
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:inputType="numberDecimal"
android:text="14.0"
/>
<TextView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:singleLine="true"
android:ellipsize="end"
android:text="endingtext"
android:layout_weight="1"
/>
</LinearLayout>
My adapter:
public class SpecialobjectHistoryRecyclerViewAdapter extends RecyclerView.Adapter<SpecialobjectHistoryRecyclerViewAdapter.RecyclerViewHolder> {
public static final String TAG = SpecialobjectRecyclerViewAdapter.class.getSimpleName();
private List<SpecialobjectHistory> mSpecialobjectHistoryList;
private Context mContext;
public SpecialobjectHistoryRecyclerViewAdapter(Context context, List<SpecialobjectHistory> specialobjectHistoryList) {
mContext = context;
mSpecialobjectHistoryList = specialobjectHistoryList;
}
#Override
public RecyclerViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
return new RecyclerViewHolder(LayoutInflater.from(parent.getContext())
.inflate(R.layout.item_specialobject_history_row, parent, false));
}
#Override
public void onBindViewHolder(RecyclerViewHolder holder, int position) {
holder.bindRow(mSpecialobjectHistoryList.get(position));
}
#Override
public int getItemCount() {
return mSpecialobjectHistoryList.size();
}
public class RecyclerViewHolder extends RecyclerView.ViewHolder {
public SpecialobjectHistory thisSpecialobjectHistory;
public TextView specialobjectTimestampTextView;
public EditText specialobjectValueEditText;
public RecyclerViewHolder(View itemView) {
super(itemView);
specialobjectTimestampTextView = (TextView) itemView.findViewById(R.id.item_specialobject_history_row_textview_timestamp);
specialobjectValueEditText = (EditText) itemView.findViewById(R.id.item_specialobject_history_row_edittext_value);
}
public void bindRow(final SpecialobjectHistory specialobjectHistory) {
thisSpecialobjectHistory = specialobjectHistory;
specialobjectTimestampTextView.setText(specialobjectHistory.getTimestamp());
specialobjectValueEditText.setText(specialobjectHistory.getValue() + "");
}
}
}
you should set the width of first Textview to 0dp
android:layout_width="0dp"
<LinearLayout
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:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:singleLine="true"
android:ellipsize="end"
android:text="starttext"
android:textSize="16sp"
android:layout_weight="2" />
<EditText
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:inputType="numberDecimal" />
<TextView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:singleLine="true"
android:ellipsize="end"
android:text="endingtext"
android:layout_weight="1" />
</LinearLayout>
not tested but should work

List Item aligninment issue with Recycler View

One of my project i used ListView to show the list of items to the user and it is working like a charm. Now as a try i replaced the Listview with the RecyclerView and succeed to show the items to the user.
Well the problem is when ever i used a customized Listview item it is working with the Listview. But when i try to load the same with RecyclerView it is not aligning/ fitting for the screen.
Here is my expected result.. I achieved this by using ListView.. Here is the Result what i am expecting
but when i used RecyclerView it is showing as below.
But i am using the same listview_item xml for both views to load..
Here is my listview_item.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/listrow"
android:layout_width="match_parent"
android:layout_height="55dp"
android:weightSum="9"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:orientation="horizontal" >
<ImageView
android:id="#+id/imageView2"
android:layout_width="0dp"
android:layout_height="30dp"
android:layout_gravity="center"
android:layout_weight="0.5"
android:src="#drawable/green" />
<TextView
android:id="#+id/textView1"
android:layout_width="0dp"
android:layout_height="50dp"
android:layout_weight="1"
android:gravity="center"
android:textColor="#000000"
android:textSize="20sp" />
<TextView
android:id="#+id/textView2"
android:layout_width="0dp"
android:layout_height="50dp"
android:layout_weight="1.5"
android:gravity="center"
android:textColor="#000000"
android:textSize="20sp" />
<TextView
android:id="#+id/textView3"
android:layout_width="0dp"
android:layout_height="50dp"
android:layout_weight="1"
android:gravity="center"
android:textColor="#000000"
android:textSize="20sp" />
<TextView
android:id="#+id/textView4"
android:layout_width="0dp"
android:layout_height="50dp"
android:layout_weight="1"
android:gravity="center"
android:textColor="#000000"
android:textSize="20sp" />
<TextView
android:id="#+id/textView5"
android:layout_width="0dp"
android:layout_height="50dp"
android:layout_weight="1"
android:gravity="center"
android:textColor="#000000"
android:textSize="20sp" />
<ImageView
android:id="#+id/imageView_followupone"
android:layout_width="0dp"
android:layout_height="22dp"
android:layout_gravity="center"
android:layout_weight="1"
android:src="#drawable/nored" />
<ImageView
android:id="#+id/imageView_followuptwo"
android:layout_width="0dp"
android:layout_height="22dp"
android:layout_gravity="center"
android:layout_weight="1"
android:src="#drawable/yesgreen" />
<ImageView
android:id="#+id/imageView1"
android:layout_width="0dp"
android:layout_height="55dp"
android:layout_weight="1"
android:gravity="center"
android:src="#drawable/finalise" />
</LinearLayout>
Adapter Class:
public class MyAdapter extends RecyclerView.Adapter<MyAdapter.ViewHolder> {
private static ArrayList<SearchParms> itemData;
private static Context abc;
private int durationOfPatientRecord;
View itemLayoutView;
public MyAdapter( ArrayList<SearchParms> items, Context context) {
this.itemData = items;
this.abc=context;
}
// Create new views (invoked by the layout manager)
public MyAdapter.ViewHolder onCreateViewHolder(ViewGroup parent,
int viewType) {
// create a new view
itemLayoutView = LayoutInflater.from(parent.getContext())
.inflate(R.layout.list_row_serch, null);
// create ViewHolder
ViewHolder viewHolder = new ViewHolder(itemLayoutView);
return viewHolder;
}
// Replace the contents of a view (invoked by the layout manager)
public void onBindViewHolder(ViewHolder viewHolder, final int position) {
// - get data from your itemsData at this position
// - replace the contents of the view with that itemsData
SearchParms item = itemData.get( position );
viewHolder.textView1.setText( item.getPartcipantId());
viewHolder.textView2.setText( item.getParticipantName());
viewHolder.textView3.setText( "" + item.getAge());
viewHolder.textView4.setText( item.getCreateDate());
viewHolder.imgView1.setImageResource( R.drawable.finalise );
viewHolder.imgView2.setImageResource( R.drawable.yesgreen );
viewHolder.imgView3.setImageResource( R.drawable.yesgreen );
itemLayoutView.setBackgroundColor( Color.parseColor( "#15ffffff" ) );
}
}
// inner class to hold a reference to each item of RecyclerView
public static class ViewHolder extends RecyclerView.ViewHolder implements OnClickListener {
public TextView textView1, textView2,textView3, textView4, textView5;
public ImageView imgView1, imgView2, imgView3, imgView4;
public ViewHolder(View itemLayoutView) {
super(itemLayoutView);
textView1 = (TextView) itemLayoutView.findViewById(R.id.textView1);
textView2 = (TextView) itemLayoutView.findViewById(R.id.textView2);
textView3 = (TextView) itemLayoutView.findViewById(R.id.textView3);
textView4 = (TextView) itemLayoutView.findViewById(R.id.textView4);
textView5 = (TextView) itemLayoutView.findViewById(R.id.textView5);
imgView1 = (ImageView) itemLayoutView.findViewById(R.id.imageView1);
imgView2 = (ImageView) itemLayoutView.findViewById(R.id.imageView_followupone);
imgView3 = (ImageView) itemLayoutView.findViewById(R.id.imageView_followuptwo);
imgView4 = (ImageView) itemLayoutView.findViewById(R.id.imageView2);
itemLayoutView.setOnClickListener(this);
}
#Override
public void onClick(View v) {
int i=getAdapterPosition();
SearchParms item = itemData.get( i );
Toast.makeText(abc, "You Selected:: " +item.getParticipantName(), 3000).show();
}
}
// Return the size of your itemsData (invoked by the layout manager)
#Override
public int getItemCount() {
return itemData.size();
}
}
where it is making problem. is there any type of limitation for using RecyclerView? Curious to know about this. Thanks
Instead of setting your view like this:
itemLayoutView = LayoutInflater.from(parent.getContext()).inflate(
R.layout.list_row_serch, null);
Do the following:
itemLayoutView = LayoutInflater.from(parent.getContext()).inflate(
R.layout.list_row_serch, parent, false);

Categories

Resources