How to make Horizontal line attach textview in recyclerview? - android

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.

Related

RecyclerView not populating, likely a silly adapter problem

I used a working RecyclerView as a template for another, but I made some sort of mistake in its implementation and I can't find it. Here is the code that initializes the view:
mMessageThreadList = new ArrayList<>();
mRecyclerView = findViewById(R.id.chat_list_recycler_view);
private void initRecyclerView() {
Log.d(TAG, "initRecyclerView Called");
//Initializes and sets up adapter
mAdapter = new ChatListRecyclerViewAdapter(mThreadList);
mRecyclerView.setAdapter(mAdapter);
Query query = mThreadCollection;
query.addSnapshotListener(new EventListener<QuerySnapshot>() {
#Override
public void onEvent(#Nullable QuerySnapshot queryDocumentSnapshots, #Nullable FirebaseFirestoreException e) {
for (DocumentChange documentChange : queryDocumentSnapshots.getDocumentChanges()) {
switch (documentChange.getType()) {
case ADDED:
Thread thread = documentChange.getDocument().toObject(Thread.class);
Log.d(TAG, "Last Message: " + thread.getLastMessage().getMessage());
mThreadList.add(thread);
mAdapter.notifyDataSetChanged();
}
}
}
});
}
Note that the print log statement in the SnapshotListener does contain a value when printed so the information is returning from Firebase, it's just not displaying. Which makes me think my problem is with my Adapter/Viewholder:
public class ChatListRecyclerViewAdapter extends RecyclerView.Adapter<ChatListRecyclerViewAdapter.ChatListViewHolder> {
private List<Thread> threadList;
ChatListRecyclerViewAdapter(List<Thread> threadList) {
this.threadList = threadList;
}
#NonNull
#Override
public ChatListViewHolder onCreateViewHolder(#NonNull ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.chat_list_row, parent, false);
return new ChatListViewHolder(view);
}
#Override
public void onBindViewHolder(#NonNull ChatListViewHolder holder, int position) {
// This method fills fields with data for each list item
Log.d(TAG, "onBindViewholder called");
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("MMM hh:mm a");
final Thread thread = threadList.get(position);
char initial = thread.getPartnerName().charAt(0);
char uppercaseInitial = Character.toUpperCase(initial);
Log.d(TAG, thread.getLastMessage() + " " + thread.getPartnerID());
holder.partnerName.setText(thread.getPartnerName());
holder.authorInitial.setText(uppercaseInitial);
holder.lastMessageText.setText(thread.getLastMessage().getMessage());
holder.lastMessageTime.setText(simpleDateFormat.format(thread.getLastMessage().getTimestamp()));
}
#Override
public int getItemCount() {
return threadList.size();
}
//Viewholder stores the information about the layout and content of each list item, and serves as a template for each item of a RecyclerView
public class ChatListViewHolder extends RecyclerView.ViewHolder {
TextView partnerName;
TextView authorInitial;
TextView lastMessageText;
TextView lastMessageTime;
LinearLayout listTextHolder;
public ChatListViewHolder(View itemView) {
super(itemView);
partnerName = itemView.findViewById(R.id.partner_name);
authorInitial = itemView.findViewById(R.id.partner_initial);
lastMessageText = itemView.findViewById(R.id.last_message);
lastMessageTime = itemView.findViewById(R.id.last_message_time);
listTextHolder = itemView.findViewById(R.id.list_text_holder);
}
}
}
chat_list_row.xml file:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/single_thread_container"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:layout_marginTop="5dp"
android:layout_marginBottom="5dp"
android:background="#color/colorPrimaryDark">
<TextView
android:id="#+id/partner_initial"
android:background="#drawable/chat_list_circle"
android:layout_width="45dp"
android:layout_height="45dp"
android:layout_alignParentStart="true"
android:gravity="center"
android:text="A"
android:textSize="36sp"
tools:ignore="HardcodedText" />
<TextView
android:id="#+id/last_message_time"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentEnd="true"
android:padding="5dp"
android:fontFamily="#font/nunito_extralight"
android:text="#string/sample_time"
android:textColor="#android:color/darker_gray"
android:textSize="12sp"
android:typeface="normal" />
<LinearLayout
android:id="#+id/list_text_holder"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toEndOf="#id/partner_initial"
android:layout_centerVertical="true"
android:orientation="vertical">
<TextView
android:id="#+id/partner_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="start"
android:layout_marginStart="5dp"
android:fontFamily="#font/nunito"
android:text="#string/sample_name"
android:textColor="#color/white"
android:textSize="14sp"
android:textStyle="bold"
android:typeface="sans" />
<TextView
android:id="#+id/last_message"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="5dp"
android:fontFamily="#font/nunito_extralight"
android:gravity="start"
android:text="#string/sample_message"
android:textColor="#android:color/darker_gray"
android:textSize="12sp"
android:typeface="normal" />
</LinearLayout>
</RelativeLayout>
Edit: Layout of the Activity that RecyclerView appears in. I also edit above code to show where the variable is attached to the view:
<android.support.constraint.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="fill_parent"
android:layout_height="fill_parent"
android:background="#color/colorPrimaryDark"
android:paddingLeft="0dp"
android:paddingTop="#dimen/activity_vertical_margin"
android:paddingRight="0dp"
android:paddingBottom="0dp"
tools:context="org.andrewedgar.theo.ChatListActivity">
<include
android:id="#+id/chat_list_toolbar"
layout="#layout/toolbar" />
<android.support.v7.widget.RecyclerView
android:id="#+id/chat_list_recycler_view"
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_alignParentStart="true"
android:layout_centerVertical="true"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/chat_list_toolbar" />
<android.support.design.widget.FloatingActionButton
android:id="#+id/new_check_in_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="end|bottom"
android:layout_marginEnd="16dp"
android:layout_marginBottom="16dp"
android:src="#drawable/ic_person_pin_circle_black_24dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent" />
</android.support.constraint.ConstraintLayout>
Seems like you have missed to add layout manager of RecyclerView. Would you please add this and try again
RecyclerView.LayoutManager mLayoutManager = new LinearLayoutManager(getApplicationContext());
recyclerView.setLayoutManager(mLayoutManager);
Your recyclerview is not showing any data because the list in adapter is still empty. After fetching data from firebase you are adding to mThreadList, which is still with activity and not adapter. To update the list in adapter you can add a method in adapter class and pass the list to adapter and notifyDataseChanged there.
Activity:
//after firebase task...
mAdapter.updateData(mThreadList);
Adapter: add new method
void updateData(List<Thread> threadList){
this.threadList = threadList;
notifyDatasetChanged(); //notify adapter that new list is added
}

How to change the indicator color every different cardview?

I create a cardview design with indicator in the left (Red, Green) but i have problem, how can i change the indicator color depends on the status from database. If the status say "No" then the indicator turn red and if "Yes" the indicator turn green.
This is the xml file :
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<android.support.v7.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:card_view="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="wrap_content"
card_view:cardCornerRadius="6dp"
card_view:cardElevation="3dp"
card_view:cardUseCompatPadding="true">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="0.9">
<ImageView
android:layout_marginLeft="22dp"
android:layout_marginTop="14dp"
android:layout_marginBottom="14dp"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#drawable/rectangle"/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="0.1"
android:orientation="vertical">
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/layout_card_item"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="5dp"
android:orientation="vertical"
android:padding="5dp">
<TextView
android:id="#+id/txt_order_date"
android:textSize="13sp"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
<TextView
android:id="#+id/txt_customer_name"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
<TextView
android:id="#+id/txt_order_number"
android:textSize="12sp"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
<TextView
android:id="#+id/txt_product_status"
android:textSize="12sp"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
<TextView
android:id="#+id/txt_notes"
android:textSize="12sp"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:singleLine="false"
android:layout_weight="1"
android:maxLines="4"/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="30dp"
android:background="#drawable/layout_bg"
android:descendantFocusability="blocksDescendants"
android:orientation="horizontal"
android:layout_below="#+id/layout_card_item"
android:gravity="center"
android:weightSum="1">
<Button
android:id="#+id/download_pdf"
android:textSize="14sp"
android:layout_width="wrap_content"
android:layout_height="30dp"
android:background="#e1e3e8"
android:focusable="false"
android:textColor="#000307"
android:focusableInTouchMode="true"
android:drawableLeft="#drawable/ic_pictogram_download_orange"
android:text=" Download PDF"
/>
<!--<ImageButton-->
<!--android:layout_width="10dp"-->
<!--android:layout_height="10dp"-->
<!--android:src="#mipmap/ic_order_download_pdf"/>-->
</LinearLayout>
</LinearLayout>
</LinearLayout>
</LinearLayout>
</android.support.v7.widget.CardView>
</LinearLayout>
This is the adapter :
public class OrderListAdapter extends RecyclerView.Adapter<OrderListAdapter.OrderListViewHolder> {
private ArrayList<OrderListModel> itemOrderList;
public OrderListAdapter(ArrayList<OrderListModel> itemOrderList) {
this.itemOrderList = itemOrderList;
}
#Override
public OrderListViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
LayoutInflater layoutInflater = LayoutInflater.from(parent.getContext());
View view = layoutInflater.inflate(R.layout.order_list_item, parent, false);
return new OrderListViewHolder(view);
}
#Override
public void onBindViewHolder(OrderListViewHolder orderListViewHolder, int position) {
orderListViewHolder.txtDate.setText(itemOrderList.get(position).getDate());
orderListViewHolder.txtOrderNumber.setText(itemOrderList.get(position).getOrderNumber());
orderListViewHolder.txtCustomerName.setText(itemOrderList.get(position).getCustomerName());
orderListViewHolder.txtProductStatus.setText(itemOrderList.get(position).getProductStatus());
orderListViewHolder.txtNotes.setText(itemOrderList.get(position).getNotes());
}
#Override
public int getItemCount() {
return (itemOrderList != null) ? itemOrderList.size() : 0;
// return itemOrderList.size();
}
public class OrderListViewHolder extends RecyclerView.ViewHolder{
private TextView txtDate, txtOrderNumber, txtCustomerName, txtProductStatus,
txtNotes;
public OrderListViewHolder(View itemView) {
super(itemView);
txtDate = (TextView) itemView.findViewById(R.id.txt_order_date);
txtOrderNumber = (TextView) itemView.findViewById(R.id.txt_order_number);
txtCustomerName = (TextView) itemView.findViewById(R.id.txt_customer_name);
txtProductStatus = (TextView) itemView.findViewById(R.id.txt_product_status);
txtNotes = (TextView) itemView.findViewById(R.id.txt_notes);
}
}
}
This is the main :
public class OrderListFragment extends Fragment {
private RecyclerView recyclerView;
private OrderListAdapter adapter;
private ArrayList<OrderListModel> OrderListArrayList;
#Nullable
#Override
public View onCreateView(LayoutInflater inflater, #Nullable ViewGroup container, #Nullable Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_order_list, container, false);
getActivity().setTitle(R.string.title_mn_order_list);
addData();
recyclerView = (RecyclerView) view.findViewById(R.id.orderList_recycler_view);
RecyclerView.LayoutManager layoutManager = new LinearLayoutManager(this.getActivity());
recyclerView.setLayoutManager(layoutManager);
adapter = new OrderListAdapter(OrderListArrayList);
recyclerView.setAdapter(adapter);
return view;
}
void addData(){
OrderListArrayList = new ArrayList<>();
OrderListArrayList.add(new OrderListModel(
"4 Juli 2018 10:54",
"0001",
"Jopa",
"No",
"Notes"));
OrderListArrayList.add(new OrderListModel(
"4 Juli 2018 10:54",
"0001",
"Jopa",
"Yes",
"Notes"));
}
}
Do you have any idea how can i change it?
You must give an id to your ImageView say status
In OrderListViewHolder add in your declarations private ImageView status;
In OrderListViewHolder add status = (ImageView) itemView.findViewById(R.id.status);
In OrderListViewHolder add
if (itemOrderList.get(position).getStatus().equals("Yes")) {
orderListViewHolder.status.setBackgroundColor(0x00ff00);
} else {
orderListViewHolder.status.setBackgroundColor(0xff0000); }
of course you must create the getStatus() method like all the others getSomething()
Assuming, that your ImageView is your indicator bar:
<ImageView
android:id="#+id/ivIndicator" <!-- Add this id -->
android:layout_marginLeft="22dp"
android:layout_marginTop="14dp"
android:layout_marginBottom="14dp"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#drawable/rectangle"/>
Get a reference to it in your OrderListViewHolder:
ivIndicator = (ImageView) itemView.findViewById(R.id.ivIndicator);
And then tint your imageview in onBindViewHolder:
if("Yes".equals(itemOrderList.get(position).getStatus())) {
orderListViewHolder.ivIndicator.setColorFilter(ContextCompat.getColor(activity, android.R.color.green))
else {
orderListViewHolder.ivIndicator.setColorFilter(ContextCompat.getColor(activity, android.R.color.red))
}
Make two drawable files like #drawable/rectangle, set it with different color you want.
if(status.equals("No") {
yourView.setBackground(context.getResources().getDrawable(R.drawable.rectangleWithRed));
}
else if (status.equals("Yes") {
yourView.setBackground(context.getResources().getDrawable(R.drawable.rectangleWithGreen);
}
if you want to change the color according to the status, then on onBindViewHolder of your Recycler Adapter, check for the status and set to desired color.
if(status.equals("Yes")
{
rectangleImageView.setColor(Color.GREEN);
}
else
{
rectangleImageView.setColor(Color.RED);
}
public void onBindViewHolder(OrderListViewHolder orderListViewHolder, int position) {
if (OrderListModel.getProductStatus().equals("Yes"))
rectangleImageView.setColorFilter(getContext().getResources().getColor(R.color.green));
} else {
rectangleImageView.setColorFilter(getContext().getResources().getColor(R.color.red));
}
}

Horizontal View line not displaying in RecyclerVeiw

I am using Horizontal Recycler View :
My Layout file contains recyclerview as below :
<android.support.v7.widget.RecyclerView
android:id="#+id/recycler_Styles"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#color/white" />
My custom/singleview/raw file to be bind in RecyclerView is as below :
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<TextView
android:maxLines="2"
android:ellipsize="end"
android:layout_centerHorizontal="true"
android:id="#+id/txt_style"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="#dimen/dimen_10"
android:text="Style"
android:textColor="#color/black" />
<View
android:layout_centerHorizontal="true"
android:id="#+id/viewStyle"
android:layout_width="match_parent"
android:layout_height="#dimen/dimen_2"
android:layout_below="#+id/txt_style"
android:background="#color/colorAppDefault" />
<!--#455A90-->
My viewStyle is display when I am giving its width to Fix. But, Its look improper.
What might be the issue ?
I am doing as below in My Adapter.
public RecyclerViewHolders(View itemView) {
super(itemView);
txt_style = (TextView) itemView.findViewById(R.id.txt_style);
viewStyle = itemView.findViewById(R.id.viewStyle);
}
public void setData(DrinkStyleModel modelBeerStyle, final int position) {
final DrinkStyleModel modelFinal = modelBeerStyle;
if (modelFinal.is_check()) {
txt_style.setTextColor(context.getResources().getColor(R.color.black));
viewStyle.setVisibility(View.VISIBLE);
} else {
txt_style.setTextColor(context.getResources().getColor(R.color.gray));
viewStyle.setVisibility(View.GONE);
}
}

RecyclerView doesn't call onCreateViewHolder and other methods

Fragment has ViewPager, in ViewPager we are swiping between two fragments(let's name it sub-fragment). And first and second sub-fragment has got RecyclerView.
I doesn't know what's going on because I've create RecyclerView exactly in the same way in other fragment and It is workking well.
recycler_view_item: (I'm sure that this item is appriopriate and its ids are compatible with Adapter class
<?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="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#color/colorHeaderText"
android:weightSum="100"
android:layout_marginBottom="4dp"
android:layout_marginLeft="2dp"
android:layout_marginRight="2dp">
<LinearLayout
android:orientation="vertical"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="20"
android:weightSum="50">
<TextView
android:text="Client"
android:layout_width="wrap_content"
android:layout_height="0dp"
android:id="#+id/textView8"
android:layout_weight="15"
style="#style/ItemPaymentsHeaderTextView"
/>
<TextView
android:text="TextView"
android:layout_width="match_parent"
android:layout_height="0dp"
android:id="#+id/clientinfo_item_client"
android:layout_weight="35"
style="#style/ItemPaymentsInfoTextView"/>
</LinearLayout>
<LinearLayout
android:orientation="vertical"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="25"
android:weightSum="50">
<TextView
android:text="Date"
android:layout_width="wrap_content"
android:layout_height="0dp"
android:id="#+id/textView13"
android:layout_weight="15"
style="#style/ItemPaymentsHeaderTextView"
/>
<TextView
android:text="TextView"
android:layout_width="match_parent"
android:layout_height="0dp"
android:id="#+id/clientinfo_item_date"
android:layout_weight="35"
style="#style/ItemPaymentsInfoTextView"/>
</LinearLayout>
<LinearLayout
android:orientation="vertical"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="27"
android:weightSum="50">
<TextView
android:text="Payment amount"
android:layout_width="wrap_content"
android:layout_height="0dp"
android:id="#+id/textView9"
android:layout_weight="15"
style="#style/ItemPaymentsHeaderTextView"
/>
<TextView
android:text="TextView"
android:layout_width="match_parent"
android:layout_height="0dp"
android:id="#+id/clientinfo_item_totalamount"
android:layout_weight="35"
style="#style/ItemPaymentsInfoTextView"/>
</LinearLayout>
<!--TODO jak bedzie po wszystkim to wyrzucic TYP ( określony w TabLayout)-->
<LinearLayout
android:orientation="vertical"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="20"
android:weightSum="50">
<TextView
android:text="Type"
android:layout_width="wrap_content"
android:layout_height="0dp"
android:id="#+id/textView10"
android:layout_weight="15"
style="#style/ItemPaymentsHeaderTextView"
/>
<TextView
android:text="Purchase"
android:layout_width="match_parent"
android:layout_height="0dp"
android:id="#+id/clientinfo_item_type"
android:layout_weight="35"
style="#style/ItemPaymentsInfoTextView"/>
</LinearLayout>
<ImageButton
android:layout_width="0dp"
android:layout_height="wrap_content"
app:srcCompat="#drawable/arrow_expand_24"
android:id="#+id/clientinfo_item_downarrow"
android:background="#color/icon_transparent"
android:layout_weight="5"
android:scaleType="fitCenter"
android:elevation="0dp"
android:layout_gravity="center_vertical|clip_horizontal"/>
FrameLayout that contain ViewPager and TabLayout
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout
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"
tools:context="com.example.android.debtors.Fragments.FragmentSingleClientInfo">
<android.support.design.widget.TabLayout
android:id="#+id/clientsinfo_tabs"
style="#style/CategoryTab"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<android.support.v4.view.ViewPager
android:id="#+id/clientsinfo_viewpager"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
</FrameLayout>
onCreateView - method where I create RecyclerView
listOfPayments that I am passing to AdapterClientInfo is not null.
#Nullable
#Override
public View onCreateView(LayoutInflater inflater, #Nullable ViewGroup container, #Nullable Bundle savedInstanceState) {
listOfPayments = getPaymentsByClientId(clientsID);
for( Payment p : listOfPayments ){
Log.i(TAG, "onCreateView: payment : " + p.toString(true));
}
View rootView = inflater.inflate(R.layout.recycler_view_with_viewpager,container, false);
AdapterClientInfo adapterClientInfo = new AdapterClientInfo(getContext(),listOfPayments);
RecyclerView recyclerView = (RecyclerView) rootView.findViewById(R.id.recycler_view_with_viewpager);
setupRecyclerView(recyclerView);
recyclerView.setAdapter(adapterClientInfo);
recyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));
return rootView;
}
In above method I inflate layout.recycler_view_with_viewpager:
<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.RecyclerView
xmlns:android="http://schemas.android.com/apk/res/android"
android:paddingTop="55dp"
android:id="#+id/recycler_view_with_viewpager"
android:scrollbars="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
And in AdapterClientInfo only constructor is calling while creating instance and any of other method doesn't work, nothing is called.
AdapterClientInfo:
public class AdapterClientInfo extends RecyclerView.Adapter<AdapterClientInfo.MyViewHolder> {
private static final String TAG = AdapterClientInfo.class.getSimpleName();
List<Payment> listOfPayments = new ArrayList<>();
Context context;
public AdapterClientInfo(Context context, List<Payment> list) {
Log.i(TAG, "AdapterClientInfo: ");
this.context = context;
this.listOfPayments = list;
Log.i(TAG, "AdapterClientInfo: size of list : " + list.size());
}
#Override
public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
Log.i(TAG, "onCreateViewHolder: ");
View itemView = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_clients_info_payments, parent, false);
return new MyViewHolder(itemView);
}
#Override
public void onBindViewHolder(MyViewHolder holder, int position) {
Log.i(TAG, "onBindViewHolder: START");
Payment payment = listOfPayments.get(position);
Log.i(TAG, "onBindViewHolder: payment " + payment.toString());
String clientName = getClientByID(payment.getPaymentClientID()).getClientName();
String[] dateArray = payment.getPaymentDate().split(" ");
String dateString = dateArray[0];
holder.textViewClient.setText(clientName);
holder.textViewDate.setText(dateString);
holder.textViewPaymentAmount.setText(String.valueOf(payment.getPaymentAmount()));
if(payment.isPaymentGotOrGiven())//if tru
holder.textViewType.setText("Received");
else
holder.textViewType.setText("Given");
Log.i(TAG, "onBindViewHolder: END");
}
#Override
public int getItemCount() {
Log.i(TAG, "getItemCount: " + listOfPayments.size());
return listOfPayments.size();
}
public class MyViewHolder extends RecyclerView.ViewHolder {
TextView textViewClient, textViewPaymentAmount, textViewDate, textViewType;
public MyViewHolder(View itemView) {
super(itemView);
Log.i(TAG, "MyViewHolder: ");
textViewClient = (TextView) itemView.findViewById(R.id.clientinfo_item_client);
textViewPaymentAmount = (TextView) itemView.findViewById(R.id.clientinfo_item_totalamount);
textViewDate = (TextView) itemView.findViewById(R.id.clientinfo_item_date);
textViewType = (TextView) itemView.findViewById(R.id.clientinfo_item_type);
}
}
private Client getClientByID(long ID){
DatabaseClients dbClients = new DatabaseClients(context);
Client client = dbClients.getClientByID(ID);
return client;
}
}
I'm sure that listOfPayments's size is more than 0 so getItemCount return more than zero
Logs show that only Constructor is calling
I think that it is simple mistake but I can't notice that...
Problem was causing by method in ViewPagerAdapter. I don't know if it was autogenerated, I copied it accidentally or .. I don't know but deleting this method helped.
#Override
public boolean isViewFromObject(View view, Object object) {
return false;
}

RecyclerView to be Scrolled horizontally with the width

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.
*/

Categories

Resources