Skip to content

Instantly share code, notes, and snippets.

@ajit123jain
Last active December 21, 2017 04:44
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ajit123jain/2fa1c80f9f3972c168654c62b67f9868 to your computer and use it in GitHub Desktop.
Save ajit123jain/2fa1c80f9f3972c168654c62b67f9868 to your computer and use it in GitHub Desktop.
package com.ajit123jain.recyclerview;
import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
import java.util.List;
/**
* Created by aditya on 20/12/17.
*/
public class CityAdapter extends RecyclerView.Adapter<CityAdapter.ViewHolder>{
private List<City> cities;
private int rowLayout;
private Context mContext;
private OnItemClickListener clickListener;
public CityAdapter(List<City> cities, int rowLayout, Context context) {
this.cities = cities;
this.rowLayout = rowLayout;
this.mContext = context;
}
@Override
public ViewHolder onCreateViewHolder(ViewGroup viewGroup, int position) {
View v = LayoutInflater.from(viewGroup.getContext()).inflate(rowLayout, viewGroup, false);
return new ViewHolder(v);
}
@Override
public void onBindViewHolder(ViewHolder viewHolder, int position) {
viewHolder.setIsRecyclable(false);
final City city = cities.get(position);
viewHolder.cityName.setText(city.getName());
viewHolder.cityDesc.setText(city.getDescription());
viewHolder.cityImage.setText(city.imageName);
}
@Override
public int getItemCount() {
return cities == null ? 0 : cities.size();
}
public void setClickListener(OnItemClickListener itemClickListener) {
this.clickListener = itemClickListener;
}
public class ViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
public TextView cityName;
public TextView cityDesc;
public TextView cityImage;
public ViewHolder(View itemView) {
super(itemView);
cityName = (TextView) itemView.findViewById(R.id.city);
cityDesc = (TextView)itemView.findViewById(R.id.desc);
cityImage = (TextView)itemView.findViewById(R.id.image);
itemView.setTag(itemView);
itemView.setOnClickListener(this);
}
@Override
public void onClick(View view) {
if (clickListener != null)
clickListener.onClick(view, getAdapterPosition());
}
}
}
package in.healthgraph.healthgraphehrtablet.ipd;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Context;
import android.graphics.Color;
import android.graphics.Typeface;
import android.support.v4.content.ContextCompat;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;
import java.util.List;
import in.healthgraph.healthgraphehrtablet.R;
import in.healthgraph.healthgraphehrtablet.model.IpdAppointment;
import in.healthgraph.healthgraphehrtablet.model.ListItem;
import in.healthgraph.healthgraphehrtablet.opd.MainActivity;
/**
* Created by ajit on 10/7/17.
*/
public class IpdListAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
List<ListItem> list;
VHItem VHitem;
static class VHItem extends RecyclerView.ViewHolder {
LinearLayout row_index;
TextView patientNameTextView;
TextView patientAdmissionTime;
TextView patientAdmittingDoctor;
TextView patientAdmissionReason;
TextView patientAdmissionWardNo;
View itemView;
VHItem(View itemView) {
super(itemView);
row_index=(LinearLayout)itemView.findViewById(R.id.layout_ipd_list_item);
patientNameTextView = (TextView) itemView.findViewById(R.id.patient_name_text_view);
patientAdmissionTime =(TextView)itemView.findViewById(R.id.admission_time);
patientAdmittingDoctor = (TextView)itemView.findViewById(R.id.admitting_doctor);
patientAdmissionReason = (TextView)itemView.findViewById(R.id.reason_for_admission);
patientAdmissionWardNo = (TextView)itemView.findViewById(R.id.ward_code);
this.itemView = itemView;
}
}
private List<IpdAppointment> mAppointments;
private Context mContext;
private IpdContract.IpdListPresenterContract mPresenter;
private Activity mActivity;
private View lastClickedView;
private int mSelectionIndex = -1;
IpdListAdapter(Context context, List<ListItem> appointments,
IpdContract.IpdListPresenterContract presenter, Activity activity) {
list = appointments;
Log.d("Constructor","List type of listitem");
mContext = context;
mPresenter = presenter;
mActivity = activity;
}
private Context getContext() {
return mContext;
}
@Override
public RecyclerView.ViewHolder onCreateViewHolder(final ViewGroup parent, final int viewType) {
final View appointmentItemView = LayoutInflater.from(parent.getContext()).inflate(R.layout.ipd_list_custom_item, parent, false);
final RecyclerView.ViewHolder vh = new VHItem(appointmentItemView);
// set the onclick listener on the recycler view item
/* appointmentItemView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (v != lastClickedView){
v.setBackgroundColor(ContextCompat.getColor(v.getContext(), R.color.active_selection));
new VHItem(v).patientNameTextView.setTextColor(Color.parseColor("#ffffff"));
new VHItem(v).patientAdmissionTime.setTextColor(Color.parseColor("#ffffff"));
new VHItem(v).patientAdmittingDoctor.setTextColor(Color.parseColor("#ffffff"));
new VHItem(v).patientAdmissionReason.setTextColor(Color.parseColor("#ffffff"));
new VHItem(v).patientAdmissionWardNo.setTextColor(Color.parseColor("#ffffff"));
Log.d("name",appointmentItemView.getVerticalScrollbarPosition()+"");
if (lastClickedView != null){
// set the color back to white for previous clicked item
lastClickedView.setBackgroundColor(ContextCompat.getColor(v.getContext(),
R.color.colorPrimary));
TextView ta=(TextView)lastClickedView.findViewById(R.id.patient_name_text_view);
TextView tb=(TextView)lastClickedView.findViewById(R.id.admission_time);
TextView tc=(TextView)lastClickedView.findViewById(R.id.admitting_doctor);
TextView td=(TextView)lastClickedView.findViewById(R.id.reason_for_admission);
TextView te=(TextView)lastClickedView.findViewById(R.id.ward_code);
ta.setTextColor(Color.parseColor("#636e7b"));
tb.setTextColor(Color.parseColor("#636e7b"));
tc.setTextColor(Color.parseColor("#636e7b"));
td.setTextColor(Color.parseColor("#636e7b"));
te.setTextColor(Color.parseColor("#636e7b"));
}
lastClickedView = v;
}
((MainActivity)mActivity).onIpdListItemClick((IpdAppointment) list.get(vh.getAdapterPosition()));
}
}); */
vh.itemView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Log.d("position",+"");
}
});
return new VHItem(appointmentItemView);
}
@SuppressLint("ResourceAsColor")
@Override
public void onBindViewHolder(final RecyclerView.ViewHolder holder, final int position) {
IpdAppointment appointment = (IpdAppointment) list.get(holder.getAdapterPosition());
VHitem = (VHItem)holder;
VHitem.patientNameTextView.setText(appointment.getPatientName());
VHitem.patientAdmissionTime.setText(appointment.getAdmissionTime());
VHitem.patientAdmittingDoctor.setText(appointment.getAdmittingDoctor());
String s = "Reason : ";
VHitem.patientAdmissionReason.setText(s+appointment.getReasonForAdmission());
final Typeface fontFamily = Typeface.createFromAsset(getContext().getAssets(),"fonts/font_awesome.ttf");
VHitem.patientAdmissionWardNo.setTypeface(fontFamily);
String s1 = "\uf236";
VHitem.patientAdmissionWardNo.setText(s1+" "+appointment.getWardCode());
// set the selected background color of item at selected index
if (mSelectionIndex != -1 && position==mSelectionIndex ){
// select this view
VHitem.itemView.setBackgroundColor(ContextCompat.getColor(VHitem.itemView.getContext(), R.color.active_selection));
// VHitem.patientNameTextView.setTextColor(Color.parseColor("#ffffff"));
((VHItem) holder).patientNameTextView.setTextColor(Color.parseColor("#ffffff"));
((VHItem) holder).patientAdmissionTime.setTextColor(Color.parseColor("#ffffff"));
((VHItem) holder).patientAdmittingDoctor.setTextColor(Color.parseColor("#ffffff"));
((VHItem) holder).patientAdmissionReason.setTextColor(Color.parseColor("#ffffff"));
((VHItem) holder).patientAdmissionWardNo.setTextColor(Color.parseColor("#ffffff"));
lastClickedView = VHitem.itemView;
}
// ((VHItem) holder).patientNameTextView.setTextColor(ContextCompat.getColor(lastClickedView.getContext(),R.color.button_default));
}
@Override
public int getItemCount() {
return list.size();
}
void clearSelection(){
if (lastClickedView != null){
// set the color back to white for previous clicked item
lastClickedView.setBackgroundColor(ContextCompat.getColor(lastClickedView.getContext(),
R.color.colorPrimary));
TextView ta=(TextView)lastClickedView.findViewById(R.id.patient_name_text_view);
TextView tb=(TextView)lastClickedView.findViewById(R.id.admission_time);
TextView tc=(TextView)lastClickedView.findViewById(R.id.admitting_doctor);
TextView td=(TextView)lastClickedView.findViewById(R.id.reason_for_admission);
TextView te=(TextView)lastClickedView.findViewById(R.id.ward_code);
ta.setTextColor(Color.parseColor("#636e7b"));
tb.setTextColor(Color.parseColor("#636e7b"));
tc.setTextColor(Color.parseColor("#636e7b"));
td.setTextColor(Color.parseColor("#636e7b"));
te.setTextColor(Color.parseColor("#636e7b"));
lastClickedView = null;
}
}
void setSelectedIndex(int index){
this.mSelectionIndex = index;
Log.d("Selection Index",index+"");
}
}
import android.annotation.SuppressLint;
4 import android.app.Activity;
5 import android.content.Context;
6 import android.graphics.Color;
7 import android.graphics.Typeface;
8 import android.support.v4.content.ContextCompat;
9 import android.support.v7.widget.RecyclerView;
10 import android.util.Log;
11 import android.view.LayoutInflater;
12 import android.view.View;
13 import android.view.ViewGroup;
14 import android.widget.LinearLayout;
15 import android.widget.TextView;
16
17 import java.util.List;
18
19 import in.healthgraph.healthgraphehrtablet.R;
20 import in.healthgraph.healthgraphehrtablet.model.IpdAppointment;
21 import in.healthgraph.healthgraphehrtablet.model.ListItem;
22 import in.healthgraph.healthgraphehrtablet.opd.MainActivity;
23
24 /**
25 * Created by ajit on 10/7/17.
26 */
27
28 public class IpdListAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
29 List<ListItem> list;
30 VHItem VHitem;
31
32 static class VHItem extends RecyclerView.ViewHolder {
33 LinearLayout row_index;
34 TextView patientNameTextView;
35 TextView patientAdmissionTime;
36 TextView patientAdmittingDoctor;
37 TextView patientAdmissionReason;
38 TextView patientAdmissionWardNo;
39 View itemView;
40
41 VHItem(View itemView) {
42 super(itemView);
43 row_index=(LinearLayout)itemView.findViewById(R.id.layout_ipd_list_item);
44 patientNameTextView = (TextView) itemView.findViewById(R.id.patient_name_text_view);
45 patientAdmissionTime =(TextView)itemView.findViewById(R.id.admission_time);
46 patientAdmittingDoctor = (TextView)itemView.findViewById(R.id.admitting_doctor);
47 patientAdmissionReason = (TextView)itemView.findViewById(R.id.reason_for_admission);
48 patientAdmissionWardNo = (TextView)itemView.findViewById(R.id.ward_code);
49 this.itemView = itemView;
50 }
51
52
53 }
54
55 private List<IpdAppointment> mAppointments;
56 private Context mContext;
57 private IpdContract.IpdListPresenterContract mPresenter;
58 private Activity mActivity;
59 private View lastClickedView;
60 private int mSelectionIndex = -1;
61
62 IpdListAdapter(Context context, List<ListItem> appointments,
63 IpdContract.IpdListPresenterContract presenter, Activity activity) {
64 list = appointments;
65 Log.d("Constructor","List type of listitem");
66 mContext = context;
67 mPresenter = presenter;
68 mActivity = activity;
69 }
70
71
72 private Context getContext() {
73 return mContext;
74 }
75 @Override
76 public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
77
78 View appointmentItemView = LayoutInflater.from(parent.getContext()).inflate(R.layout.ipd_list_custom_item, parent, false);
79 return new VHItem(appointmentItemView);
80
81 }
82
83 @SuppressLint("ResourceAsColor")
84 @Override
85 public void onBindViewHolder(final RecyclerView.ViewHolder holder, final int position) {
86 holder.setIsRecyclable(false);
87
88 IpdAppointment appointment = (IpdAppointment) list.get(holder.getAdapterPosition());
89 VHitem = (VHItem)holder;
90 VHitem.patientNameTextView.setText(appointment.getPatientName());
91 VHitem.patientAdmissionTime.setText(appointment.getAdmissionTime());
92 VHitem.patientAdmittingDoctor.setText(appointment.getAdmittingDoctor());
93 String s = "Reason : ";
94 VHitem.patientAdmissionReason.setText(s+appointment.getReasonForAdmission());
95 final Typeface fontFamily = Typeface.createFromAsset(getContext().getAssets(),"fonts/font_awesome.ttf");
96 VHitem.patientAdmissionWardNo.setTypeface(fontFamily);
97 String s1 = "\uf236";
98 VHitem.patientAdmissionWardNo.setText(s1+" "+appointment.getWardCode());
99 // set the selected background color of item at selected index
100 if (mSelectionIndex != -1 && position==mSelectionIndex ){
101
102 // select this view
103 VHitem.itemView.setBackgroundColor(ContextCompat.getColor(VHitem.itemView.getContext(), R.color.active_selection));
104 // VHitem.patientNameTextView.setTextColor(Color.parseColor("#ffffff"));
105 ((VHItem) holder).patientNameTextView.setTextColor(Color.parseColor("#ffffff"));
106 ((VHItem) holder).patientAdmissionTime.setTextColor(Color.parseColor("#ffffff"));
107 ((VHItem) holder).patientAdmittingDoctor.setTextColor(Color.parseColor("#ffffff"));
108 ((VHItem) holder).patientAdmissionReason.setTextColor(Color.parseColor("#ffffff"));
109 ((VHItem) holder).patientAdmissionWardNo.setTextColor(Color.parseColor("#ffffff"));
110 lastClickedView = VHitem.itemView;
111
112
113
114 }
115 // set the onclick listener on the recycler view item
116 holder.itemView.setOnClickListener(new View.OnClickListener() {
117 @Override
118 public void onClick(View v) {
119
120
121 if (v != lastClickedView){
122 v.setBackgroundColor(ContextCompat.getColor(v.getContext(), R.color.active_selection));
123 ((VHItem) holder).patientNameTextView.setTextColor(Color.parseColor("#ffffff"));
124 ((VHItem) holder).patientAdmissionTime.setTextColor(Color.parseColor("#ffffff"));
125 ((VHItem) holder).patientAdmittingDoctor.setTextColor(Color.parseColor("#ffffff"));
126 ((VHItem) holder).patientAdmissionReason.setTextColor(Color.parseColor("#ffffff"));
127 ((VHItem) holder).patientAdmissionWardNo.setTextColor(Color.parseColor("#ffffff"));
128 Log.d("name",((VHItem) holder).patientNameTextView.getText().toString());
129
130
131
132 if (lastClickedView != null){
133 // set the color back to white for previous clicked item
134 lastClickedView.setBackgroundColor(ContextCompat.getColor(v.getContext(),
135 R.color.colorPrimary));
136 TextView ta=(TextView)lastClickedView.findViewById(R.id.patient_name_text_view);
137 TextView tb=(TextView)lastClickedView.findViewById(R.id.admission_time);
138 TextView tc=(TextView)lastClickedView.findViewById(R.id.admitting_doctor);
139 TextView td=(TextView)lastClickedView.findViewById(R.id.reason_for_admission);
140 TextView te=(TextView)lastClickedView.findViewById(R.id.ward_code);
141 ta.setTextColor(Color.parseColor("#636e7b"));
142 tb.setTextColor(Color.parseColor("#636e7b"));
143 tc.setTextColor(Color.parseColor("#636e7b"));
144 td.setTextColor(Color.parseColor("#636e7b"));
145 te.setTextColor(Color.parseColor("#636e7b"));
146
147
148 }
149 lastClickedView = v;
150 }
151 ((MainActivity)mActivity).onIpdListItemClick((IpdAppointment) list.get(holder.getAdapterPosition()));
152
153 }
154
155
156 });
157
158 // ((VHItem) holder).patientNameTextView.setTextColor(ContextCompat.getColor(lastClickedView.getContext(),R.color.button_default));
159
160 }
161
162
163 @Override
164 public int getItemCount() {
165
166 return list.size();
167 }
168 void clearSelection(){
169 if (lastClickedView != null){
170 // set the color back to white for previous clicked item
171 lastClickedView.setBackgroundColor(ContextCompat.getColor(lastClickedView.getContext(),
172 R.color.colorPrimary));
173 TextView ta=(TextView)lastClickedView.findViewById(R.id.patient_name_text_view);
174 TextView tb=(TextView)lastClickedView.findViewById(R.id.admission_time);
175 TextView tc=(TextView)lastClickedView.findViewById(R.id.admitting_doctor);
176 TextView td=(TextView)lastClickedView.findViewById(R.id.reason_for_admission);
177 TextView te=(TextView)lastClickedView.findViewById(R.id.ward_code);
178 ta.setTextColor(Color.parseColor("#636e7b"));
179 tb.setTextColor(Color.parseColor("#636e7b"));
180 tc.setTextColor(Color.parseColor("#636e7b"));
181 td.setTextColor(Color.parseColor("#636e7b"));
182 te.setTextColor(Color.parseColor("#636e7b"));
183 lastClickedView = null;
184 }
185 }
186
187 void setSelectedIndex(int index){
188 this.mSelectionIndex = index;
189 Log.d("Selection Index",index+"");
190 }
191
192 }
package in.healthgraph.healthgraphehrtablet.ipd;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.RelativeLayout;
import android.widget.TextView;
import java.util.ArrayList;
import java.util.Date;
import butterknife.BindView;
import butterknife.ButterKnife;
import in.healthgraph.healthgraphehrtablet.R;
import in.healthgraph.healthgraphehrtablet.datasource.DataSource;
import in.healthgraph.healthgraphehrtablet.datasource.LocalDataSource;
import in.healthgraph.healthgraphehrtablet.datasource.RemoteDataSource;
import in.healthgraph.healthgraphehrtablet.model.Header;
import in.healthgraph.healthgraphehrtablet.model.IpdAppointment;
import in.healthgraph.healthgraphehrtablet.model.ListItem;
import in.healthgraph.healthgraphehrtablet.opd.MainActivity;
import in.healthgraph.healthgraphehrtablet.opd.OpdContract;
/**
* Created by ajit on 10/7/17.
*/
public class IpdListFragment extends Fragment implements IpdContract.IpdListViewContract {
@BindView(R.id.tabs) TabLayout tabLayout;
@BindView(R.id.layoutFailure) RelativeLayout layoutFailure;
@BindView(R.id.recycler_view_1) RecyclerView recyclerView1;
private IpdContract.IpdListPresenterContract mPresenter;
private IpdContract.IpdListViewContract mView;
private MainActivity mActivity;
private IpdListAdapter mIpdListAdapter1;
// Using one instance for holding changing items so that we can use
// notifyDataSetChanged() method on the adapter
private ArrayList<ListItem> mCurrentTabAppointments1;
TextView notarrived ,completed;
private Date mCurrentDate;
private static String data;
private static int flag ;
private DataSource dataSource;
private static int position = 0;
public IpdListFragment(){
}
public static IpdListFragment getInstance(){
return new IpdListFragment();
}
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_ipd_list, container, false);
ButterKnife.bind(this, rootView);
recyclerView1.setAdapter(null);
// get the today's date by default when the view is created
data =" ";
Bundle bundle = getArguments();
if(bundle!=null)
{
data = bundle.getString("active_user");
}
LocalDataSource localDataSource = LocalDataSource.getInstance();
RemoteDataSource remoteDataSource = RemoteDataSource.getInstance();
dataSource = DataSource.getInstance(remoteDataSource, localDataSource);
mCurrentDate = new Date();
mCurrentDate = mActivity.currentDate();
mPresenter.loadData(mCurrentDate,data);
mPresenter.createTabs(tabLayout);
setUpTabLayout();
mCurrentTabAppointments1 = new ArrayList<>();
return rootView;
}
private void setUpTabLayout(){
tabLayout.addOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
@Override
public void onTabSelected(TabLayout.Tab tab) {
mPresenter.onAppointmentTabClicked(tab.getPosition());
}
@Override
public void onTabUnselected(TabLayout.Tab tab) {
}
@Override
public void onTabReselected(TabLayout.Tab tab) {
}
});
}
@Override
public void setLoadingIndicator(boolean active) {
mActivity.setLoadingIndicator(active);
}
@Override
public void setPresenter(IpdContract.IpdListPresenterContract presenter) {
mPresenter = presenter;
}
@Override
public void setActivity(MainActivity activity) {
mActivity = activity;
}
@Override
public void showAppointments(ArrayList<IpdAppointment> allAppointments) {
// if (layoutFailure.getVisibility() == View.VISIBLE){
layoutFailure.setVisibility(View.INVISIBLE);
tabLayout.setVisibility(View.VISIBLE);
recyclerView1.setVisibility(View.VISIBLE);
// }
mCurrentTabAppointments1.clear();
mCurrentTabAppointments1.addAll(allAppointments);
if (mIpdListAdapter1 == null){
mIpdListAdapter1 = new IpdListAdapter(getContext(), mCurrentTabAppointments1, mPresenter, mActivity);
recyclerView1.setAdapter(mIpdListAdapter1);
recyclerView1.setLayoutManager(new LinearLayoutManager(getContext()));
}
mIpdListAdapter1.notifyDataSetChanged();
mIpdListAdapter1.clearSelection();
// highlight the first item of the list only if the device is tablet
// set landscape mode for table and portrait mode for mobile
if(!mActivity.getResources().getBoolean(R.bool.portrait_only)){
mIpdListAdapter1.setSelectedIndex(0);
if(allAppointments.size()>0){
loadAppointmentDetails(allAppointments.get(0));
}
}
}
/**
* Hide the tab layout and recycler view and show the failure UI
* @param message
*/
@Override
public void onDataLoadFailed(String message) {
tabLayout.setVisibility(View.GONE);
recyclerView1.setVisibility(View.INVISIBLE);
layoutFailure.setVisibility(View.VISIBLE);
}
@Override
public void refresh() {
mPresenter.loadData(mCurrentDate,data);
}
/**
* Called by the activity to selected the appointment that was previously selected
* i.e before the call to the change state was made
* @param appointmentServerId server id of the appointment on which state change was called
*/
@Override
public void refresh(String appointmentServerId) {
mPresenter.loadData(mCurrentDate, appointmentServerId,data);
}
/**
* Update the date for the OPD lists. And ask the presenter to call data for the
* new date.
* Called by the activity when date is changed
* @param date new date
*/
@Override
public void setDate(Date date) {
mCurrentDate = date;
mPresenter.loadData(mCurrentDate,data);
}
@Override
public TabLayout getTabLayout() {
return tabLayout;
}
/**
* Call the method in the activity that loads the appointment details
* with the first appointment of the list
* @param appointment first appointment in the list of current tab
*/
@Override
public void loadAppointmentDetails(IpdAppointment appointment) {
// load appointment details automatically only when the device is a tablet
// set landscape mode for table and portrait mode for mobile
if(!mActivity.getResources().getBoolean(R.bool.portrait_only)){
mActivity.onIpdListItemClick(appointment);
}
}
public int tabPosition(){
tabLayout.addOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
@Override
public void onTabSelected(TabLayout.Tab tab) {
position = tab.getPosition();
}
@Override
public void onTabUnselected(TabLayout.Tab tab) {
}
@Override
public void onTabReselected(TabLayout.Tab tab) {
}
});
return position;
}
@Override
public void selectAppointmentAtIndex(int index) {
mIpdListAdapter1.setSelectedIndex(index);
}
}
package in.healthgraph.healthgraphehrtablet.ipd;
import android.content.Context;
import android.support.annotation.NonNull;
import android.support.design.widget.TabLayout;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.TextView;
import java.util.ArrayList;
import java.util.Date;
import in.healthgraph.healthgraphehrtablet.R;
import in.healthgraph.healthgraphehrtablet.datasource.DataSource;
import in.healthgraph.healthgraphehrtablet.datasource.DataSourceContract;
import in.healthgraph.healthgraphehrtablet.model.IpdAppointment;
import in.healthgraph.healthgraphehrtablet.util.SharedPreferencesHelper;
/**
* Created by ajit on 10/7/17.
*/
public class IpdListPresenter implements IpdContract.IpdListPresenterContract,DataSourceContract.IpdListDataLoadCallbacks {
private IpdContract.IpdListViewContract mView;
private DataSource mDataSource;
private ArrayList<IpdAppointment> mAllPatients;
private ArrayList<IpdAppointment> mScheduled;
private ArrayList<IpdAppointment> mAdmittedToday;
private ArrayList<IpdAppointment> mDischargedToday;
// Keeps track of the previously selected appointment
private String mSelectedAppointmentServerId;
Context context;
public IpdListPresenter(@NonNull DataSource dataSource, @NonNull IpdListFragment view, String List,Context context1){
mView = view;
mDataSource = dataSource;
mView.setPresenter(this);
context = context1;
}
@Override
public void onStart() {
// general purpose lifecycle methods
}
/**
* Creates tabs to be displayed on the TabLayout and sets tab selected listener.
* @param tabLayout TabLayout present in the view.
*/
@Override
public void createTabs(TabLayout tabLayout) {
tabLayout.addTab(tabLayout.newTab());
tabLayout.addTab(tabLayout.newTab());
tabLayout.addTab(tabLayout.newTab());
tabLayout.addTab(tabLayout.newTab());
for (int i = 0; i < tabLayout.getTabCount(); i++){
TabLayout.Tab tab = tabLayout.getTabAt(i);
assert tab != null;
tab.setCustomView(getTabView(i));
}
}
/**
* Requests the API to load the OPD appointments for the given date.
* @param date Date for which appointments has to be requested
*/
@Override
public void loadData(Date date, String active_user) {
mView.setLoadingIndicator(true);
mDataSource.loadIpdPatientsForGivenDate(this,date);
this.mSelectedAppointmentServerId = null;
}
@Override
public void loadData(Date date, String appointmentServerId,String active_user) {
mView.setLoadingIndicator(true);
mDataSource.loadIpdPatientsForGivenDate(this,date);
this.mSelectedAppointmentServerId = appointmentServerId;
Log.d("today date is ",""+date);
}
@Override
public void onAppointmentTabClicked(int position) {
switch (position){
case 0 :
mView.showAppointments(mAllPatients);
askToLoadAppointmentDetails(mAllPatients,0);
break;
case 1 :
mView.showAppointments(mScheduled);
askToLoadAppointmentDetails(mScheduled,1);
break;
case 2 :
mView.showAppointments(mAdmittedToday);
askToLoadAppointmentDetails(mAdmittedToday,2);
break;
case 3 :
mView.showAppointments(mDischargedToday);
askToLoadAppointmentDetails(mDischargedToday,3);
break;
}
}
/**
* Helper method to get the custom tab view
* @param index position of the tab
* @return view of tab for the given position
*/
private View getTabView(int index){
View tabOneView = LayoutInflater.from(mView.getContext()).inflate(R.layout.custom_tab, null);
TextView countView = (TextView) tabOneView.findViewById(R.id.count);
TextView titleView = (TextView) tabOneView.findViewById(R.id.list_title);
String count = "";
String title = "";
switch (index){
case 0 : count = "-";
title = "ADMITTED PATIENTS";
break;
case 1 : count = "-";
title = "SCHEDULED PATIENTS";
break;
case 2 : count = "-";
title = "ADMITTED TODAY";
break;
case 3 : count = "-";
title = "DISCHARGED TODAY";
break;
}
countView.setText(count);
titleView.setText(title);
return tabOneView;
}
/**
* Callback from the model that the data has been successfully loaded.
* Now show that data in the view.
* @param = All appointments list etc
*/
@Override
public void onIpdAppointments4ForGivenDateLoaded(ArrayList<IpdAppointment> allPatients,
ArrayList<IpdAppointment> scheduledToday,
ArrayList<IpdAppointment> admittedToday,
ArrayList<IpdAppointment> dischargedToday
) {
mAllPatients = allPatients;
mScheduled = scheduledToday;
mAdmittedToday = admittedToday;
mDischargedToday = dischargedToday;
// mView.showAppointments(mAllPatients);
updateTabs(mView.getTabLayout());
mView.setLoadingIndicator(false);
if (mSelectedAppointmentServerId != null){
// select this appointment
selectAppointment(mSelectedAppointmentServerId);
}
else{
if (SharedPreferencesHelper.getCurrentTabPosition(context)!=10){
Log.d("AppointmentID", String.valueOf(SharedPreferencesHelper.getCurrentTabPosition(context)));
int x = SharedPreferencesHelper.getCurrentTabPosition(context);
if(x>=4){
x=0;}
mView.getTabLayout().getTabAt(x).select();
switch (x){
case 0:
mView.showAppointments(mAllPatients);
break;
case 1:
mView.showAppointments(mScheduled);
break;
case 2:
mView.showAppointments(mAdmittedToday);
break;
case 3:
mView.showAppointments(mDischargedToday);
break;
}
}
}
}
/**
* Called by the model when data loading failed.
* Inform the view about this.
*/
@Override
public void onAppointmentsDataLoadFailed() {
mView.onDataLoadFailed("Failed to load data");
mView.setLoadingIndicator(false);
}
@Override
public void updateTabs(TabLayout tablayout) {
View v = tablayout.getTabAt(0).getCustomView();
TextView tv = (TextView) v.findViewById(R.id.count);
tv.setText(String.valueOf(mAllPatients.size()));
v = tablayout.getTabAt(1).getCustomView();
tv = (TextView) v.findViewById(R.id.count);
tv.setText(String.valueOf(mScheduled.size()));
v = tablayout.getTabAt(2).getCustomView();
tv = (TextView) v.findViewById(R.id.count);
tv.setText(String.valueOf(mAdmittedToday.size()));
v = tablayout.getTabAt(3).getCustomView();
tv = (TextView) v.findViewById(R.id.count);
tv.setText(String.valueOf(mDischargedToday.size()));
int x = SharedPreferencesHelper.getCurrentTabPosition(context);
if(x!=4)
tablayout.getTabAt(x).select();
else
tablayout.getTabAt(0).select();
}
private void askToLoadAppointmentDetails(ArrayList<IpdAppointment> appointments,int tabPosition){
if (appointments.size() != 0 && tabPosition == SharedPreferencesHelper.getCurrentTabPosition(context)){
Log.d("Size"+appointments.size(),"Toast.LENGTH_LONG).show()");
mView.loadAppointmentDetails(appointments.get(0));
}
else{
mView.loadAppointmentDetails(null);
}
}
private void selectAppointment(String appointmentServerId){
int index = -1, list = -1;
IpdAppointment opdAppointment = null;
// search the my queue appointments
for (int i = 0; i < mAllPatients.size(); i++){
IpdAppointment temp = mAllPatients.get(i);
if (temp.getAdmissionId().equals(appointmentServerId)){
index = i;
list = 0;
opdAppointment = mAllPatients.get(index);
break;
}
}
if (index == -1){
for (int i = 0; i < mScheduled.size(); i++){
IpdAppointment temp = mScheduled.get(i);
if (temp.getAdmissionId().equals(appointmentServerId)){
index = i;
list = 1;
opdAppointment = mScheduled.get(index);
break;
}
}
}
if (index == -1){
for (int i = 0; i < mAdmittedToday.size(); i++){
IpdAppointment temp = mAdmittedToday.get(i);
if (temp.getAdmissionId().equals(appointmentServerId)){
index = i;
list = 2;
opdAppointment = mAdmittedToday.get(index);
break;
}
}
}
if (index == -1){
for (int i = 0; i < mDischargedToday.size(); i++){
IpdAppointment temp = mDischargedToday.get(i);
if (temp.getAdmissionId().equals(appointmentServerId)){
index = i;
list = 3;
opdAppointment = mDischargedToday.get(index);
break;
}
}
}
// here is index is -1 then appointment was not found else is contains the position
// of the appointment in the list
// if (list != -1 && index != -1){
// // select the tab at list
// mView.getTabLayout().getTabAt(list).select();
// // select the appointment in the list
// mView.selectAppointmentAtIndex(index);
// // load the details for the selected appointment
// mView.loadAppointmentDetails(opdAppointment);
// }
}
private void selectTab(int tabposition){
mView.getTabLayout().getTabAt(tabposition).select();
Log.d("Tab Position",String.valueOf(tabposition));
}
}
package com.ajit123jain.recyclerview;
import android.content.Intent;
import android.graphics.Color;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.DefaultItemAnimator;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.View;
import java.util.ArrayList;
import java.util.List;
public class MainActivity extends AppCompatActivity implements OnItemClickListener {
private RecyclerView mRecyclerView;
private CityAdapter mAdapter;
private List<City> cities;
private static View lastClickedView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
cities = data();
mRecyclerView = (RecyclerView)findViewById(R.id.list);
mRecyclerView.setLayoutManager(new LinearLayoutManager(this));
mRecyclerView.setItemAnimator(new DefaultItemAnimator());
mAdapter = new CityAdapter(cities, R.layout.row_city, this);
mRecyclerView.setAdapter(mAdapter);
mAdapter.setClickListener(this); // Bind the listener
}
@Override
public void onClick(View view, int position) {
if(lastClickedView!=view){
view.setBackgroundColor(Color.parseColor("#000000"));
Log.d("position",position+"");
if(lastClickedView!=null)
lastClickedView.setBackgroundColor(Color.BLUE);
lastClickedView=view;
}
mAdapter.
// The onClick implementation of the RecyclerView item click
final City city = cities.get(position);
Intent i = new Intent(this, CityviewActivity.class);
i.putExtra("city", city.name);
i.putExtra("desc", city.description);
i.putExtra("image", city.imageName);
Log.i("hello", city.name);
startActivity(i);
}
public ArrayList<City> data(){
ArrayList<City> cities = new ArrayList<>();
cities.add(new City("kota","Sinagpur","Education"));
cities.add(new City("Anwa","Rajasthan","Temple"));
cities.add(new City("Jaipur","Rajasthan","Girls"));
cities.add(new City("Varanashi","UttarPardesh","Temple"));
cities.add(new City("Bengluru","Karnataka","It world"));
cities.add(new City("Udaipur","Rajasthan","Sun City"));
cities.add(new City("Ahmedabad","Gujrat","Business"));
cities.add(new City("Gaya","Bihar","Beer"));
cities.add(new City("Gova","Goca","Romance"));
cities.add(new City("kota","Sinagpur","Education"));
cities.add(new City("Anwa","Rajasthan","Temple"));
cities.add(new City("Jaipur","Rajasthan","Girls"));
cities.add(new City("Varanashi","UttarPardesh","Temple"));
cities.add(new City("Bengluru","Karnataka","It world"));
cities.add(new City("Udaipur","Rajasthan","Sun City"));
cities.add(new City("Ahmedabad","Gujrat","Business"));
cities.add(new City("Gaya","Bihar","Beer"));
cities.add(new City("Gova","Goca","Romance"));
cities.add(new City("kota","Sinagpur","Education"));
cities.add(new City("Anwa","Rajasthan","Temple"));
cities.add(new City("Jaipur","Rajasthan","Girls"));
cities.add(new City("Varanashi","UttarPardesh","Temple"));
cities.add(new City("Bengluru","Karnataka","It world"));
cities.add(new City("Udaipur","Rajasthan","Sun City"));
cities.add(new City("Ahmedabad","Gujrat","Business"));
cities.add(new City("Gaya","Bihar","Beer"));
cities.add(new City("Gova","Goca","Romance"));
return cities;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment