How to hide recyclerview's all item in activity - android

I want to hide checkbox of all lists if I clicked one list within activity using recyclerview.
But when I implement it, only the check box in the recyclerview list of the clicked position disappears. How can I hide the checkbox of the entire list?
Of course it is possible on the reyclerview adapter, but I want this function to be implemented in activity.
In activity, the adapter was declared:
RecyclerView.LayoutManager mLayoutManager = new LinearLayoutManager(getApplicationContext());
recyclerViewAdapter = new RecyclerViewAdapter(this, patientList, selected_patientList);
recyclerView.setLayoutManager(mLayoutManager);
recyclerView.setItemAnimator(new DefaultItemAnimator());
recyclerView.setAdapter(recyclerViewAdapter);
And I wrote the code that hides the check box when click the recyclerview list in activity :
recyclerView.addOnItemTouchListener(new RecyclerItemClickListener(this, recyclerView, new RecyclerItemClickListener.OnItemClickListener() {
#Override
public void onItemClick(View view, int position) {
CheckBox checkBox = (CheckBox) view.findViewById(R.id.checkBox) ;
checkBox.setVisibility(View.GONE);
}
And my recyclerview adapter code is
public class RecyclerViewAdapter extends RecyclerView.Adapter<RecyclerViewAdapter.MyViewHolder> {
private Activity activity;
public ArrayList<PatientItem> patientList = new ArrayList<>();
public ArrayList<PatientItem> selected_patientList = new ArrayList<>();
Context mContext;
boolean checkboxIsVisible = true ;
public RecyclerViewAdapter(Context context, ArrayList<PatientItem> patientList, ArrayList<PatientItem> selected_patientList) {
this.mContext = context;
this.patientList = patientList;
this.selected_patientList = selected_patientList;
}
public class MyViewHolder extends RecyclerView.ViewHolder {
TextView patientType;
TextView clinicID;
TextView patientName;
TextView dateFirst;
TextView dateFinal;
CheckBox checkBox ;
ConstraintLayout cl_listitem;
public MyViewHolder(View itemView) {
super(itemView);
patientType = (TextView) itemView.findViewById(R.id.typeViewItem);
clinicID = (TextView) itemView.findViewById(R.id.clinicIDItem);
patientName = (TextView) itemView.findViewById(R.id.patientNameItem);
dateFirst = (TextView) itemView.findViewById(R.id.dateFirstItem);
dateFinal = (TextView) itemView.findViewById(R.id.dateFinalItem);
checkBox = (CheckBox) itemView.findViewById(R.id.checkBox) ;
cl_listitem = (ConstraintLayout) itemView.findViewById(R.id.cl_listitem);
}
}
#Override
public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View itemView = LayoutInflater.from(parent.getContext()).inflate(R.layout.patient_list_item, parent, false);
final RecyclerViewAdapter.MyViewHolder vHolder = new RecyclerViewAdapter.MyViewHolder(itemView) ;
return vHolder;
}
#Override
public void onBindViewHolder(MyViewHolder holder, int position) {
PatientItem data = patientList.get(position);
holder.patientType.setText(data.getPatientType());
holder.clinicID.setText(data.getClinicID());
holder.patientName.setText(data.getPatientName());
holder.dateFirst.setText(data.getDateFirst());
holder.dateFinal.setText(data.getDateFinal());
holder.checkBox.setChecked(data.isDeleteBox());
holder.checkBox.setVisibility(checkboxIsVisible?View.VISIBLE:View.GONE);
if(data.getPatientType() == "P"){
holder.patientType.setText("");
holder.patientType.setBackground(ContextCompat.getDrawable(mContext, R.drawable.parkinson));
}
else if(data.getPatientType() == "ET"){
holder.patientType.setText("");
holder.patientType.setBackground(ContextCompat.getDrawable(mContext, R.drawable.essential_tremor));
}
else{
holder.patientType.setText("ㅡ");
}
}
#Override
public int getItemCount() {
return patientList.size();
}
public void clear() {
int size = patientList.size() ;
patientList.clear() ;
notifyItemRangeRemoved(0, size);
}
//어댑터 정비
public void refreshAdapter() {
this.selected_patientList = selected_patientList;
this.patientList = patientList;
this.notifyDataSetChanged();
}
}

I solved this problem like this.
I declared onBindViewHolder at RecyclerViewAdapter:
holder.checkBox.setVisibility(checkboxIsVisible?View.VISIBLE:View.GONE);
And, I declared visible() method in RecyclerViewAdapter :
public void visible(){
checkboxIsVisible = false ;
this.notifyDataSetChanged();
}
Than, I use visible method in activity :
recyclerView.addOnItemTouchListener(new RecyclerItemClickListener(this, recyclerView, new RecyclerItemClickListener.OnItemClickListener() {
#Override
public void onItemClick(View view, int position) {
recyclerViewAdapter.visible();
}
When I run this, when I click on one list, all the check boxes on the list disappear.

Related

Get selected item position in Recyclerview

I am using Recyclerview. I added a menu with Recyclerview. I want to make a switch case in Recyclerview. So when I click on item 1, when I click on item 2, I click on step 3. When I click on my item .. I want to call a function. Currently I tried to read the position of the items with getAdapterPosition, but the output is always -1. For example, when I click on model, when I click on model2, I want to call a function.
Main2Activity
public class Main2Activity extends AppCompatActivity implements NavigationView.OnNavigationItemSelectedListener {
RecyclerView navRecyclerView;
LinearLayoutManager layoutManager;
ArrayList<NavigationDataModel> arrayList = new ArrayList<>();
NavigationAdapter adapter;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main2);
Toolbar toolbar = findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
navigationView.setNavigationItemSelectedListener(this);
navRecyclerView = findViewById(R.id.navRecyclerView);
navRecyclerView.setHasFixedSize(true);
layoutManager = new LinearLayoutManager(this);
navRecyclerView.setLayoutManager(layoutManager);
DividerItemDecoration divider = new DividerItemDecoration(
navRecyclerView.getContext(),
DividerItemDecoration.VERTICAL
);
divider.setDrawable(ContextCompat.getDrawable(getBaseContext(), R.drawable.diveder_recycler));
navRecyclerView.addItemDecoration(divider);
initArray();
adapter = new NavigationAdapter(this, arrayList);
navRecyclerView.setAdapter(adapter);
private void initArray() {
NavigationDataModel model = new NavigationDataModel();
model.setIcon(R.mipmap.avatar);
model.setTitle("Hesabım");
arrayList.add(model);
NavigationDataModel model2 = new NavigationDataModel();
model2.setIcon(R.mipmap.sss3);
model2.setTitle("S.S.S");
arrayList.add(model2);
}
NavigationAdapter
public class NavigationAdapter extends RecyclerView.Adapter<NavigationAdapter.ViewHolder> {
Context context;
ArrayList<NavigationDataModel> arrayList = new ArrayList<>();
public NavigationAdapter(Context context, ArrayList<NavigationDataModel> arrayList) {
this.context = context;
this.arrayList = arrayList;
}
#NonNull
#Override
public ViewHolder onCreateViewHolder(#NonNull ViewGroup parent, int viewType) {
View view = LayoutInflater.from(context).inflate(R.layout.navigationrecyclerview_adapter11, parent, false);
final ViewHolder holder = new ViewHolder(view);
holder.navTitle.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
int position = holder.getAdapterPosition();
System.out.println("asdasdadasd" + position);
}
});
return new ViewHolder(view);
}
#Override
public void onBindViewHolder(#NonNull ViewHolder holder, int position) {
holder.navIcon.setImageResource(arrayList.get(position).getIcon());
holder.navIconNew.setImageResource(arrayList.get(position).getIcon());
holder.rootView.setBackgroundColor(arrayList.get(position).getColor());
holder.navTitle.setText(arrayList.get(position).getTitle());
}
#Override
public int getItemCount() {
return arrayList.size();
}
public class ViewHolder extends RecyclerView.ViewHolder {
ImageView navIcon, navIconNew;
TextView navTitle;
LinearLayout rootView;
public ViewHolder(View itemView) {
super(itemView);
rootView = itemView.findViewById(R.id.rootView);
navIcon = itemView.findViewById(R.id.navIcon);
navIconNew = itemView.findViewById(R.id.navIconNew);
navTitle = itemView.findViewById(R.id.navTitle);
Toast.makeText(context, String.valueOf(getAdapterPosition()), Toast.LENGTH_SHORT).show();
}
}
}
#Override
public void onBindViewHolder(#NonNull ViewHolder holder, int position) {
holder.navIcon.setImageResource(arrayList.get(position).getIcon());
holder.navIconNew.setImageResource(arrayList.get(position).getIcon());
holder.rootView.setBackgroundColor(arrayList.get(position).getColor());
holder.navTitle.setText(arrayList.get(position).getTitle());
holder.navTitle.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// use position herer
}
});
}

Horizontal RecycleView Item click scroll to first item

Hello guys i am having an issue with Horizontal Recycle View in which if am scroll to right then click on any item then it automatically scroll to first item in left , basically i have to stay on current clicked position not to scroll to start left , i use multiple thing from stack but none of them worked like scrolltopostion, creating custom layout manager class !!! Please Help
Below is my code:
private void setAdapter() {
recyclerView.setHasFixedSize(true);
mLayoutManager = new LinearLayoutManager(context, LinearLayoutManager.HORIZONTAL, false);
recyclerView.setLayoutManager(mLayoutManager);
mainAdapterClass = new MainAdapterClassw(context, AuctionManager.list);
recyclerView.setAdapter(mainAdapterClass);
}
Below is adapter Class:
public class MainAdapterClassw extends RecyclerView.Adapter<MainAdapterClassw.ViewHolder> {
ArrayList<AuctionBean> list;
Context context;
public MainAdapterClassw(Context context, ArrayList<AuctionBean> list) {
super();
this.context = context;
this.list = list;
}
#Override
public ViewHolder onCreateViewHolder(ViewGroup viewGroup, int i) {
View v = LayoutInflater.from(viewGroup.getContext())
.inflate(R.layout.customelayout, viewGroup, false);
ViewHolder viewHolder = new ViewHolder(v);
return viewHolder;
}
#Override
public void onBindViewHolder(ViewHolder viewHolder, int i) {
auctionBean = list.get(i);
viewHolder.top_sr_no.setText(auctionBean.getAuction_id());
viewHolder.view2.setVisibility(View.GONE);
viewHolder.itemView.setSelected(selectedPos == i);
viewHolder.bottom_sr_no.setText(auctionBean.getReserve_price());
text_qantity.setText(qantity1);
Picasso.with(context)
.load("http://traala.com/Bidding/images/date.png")
.placeholder(R.mipmap.ic_launcher)
.into(productimage);
}
viewHolder.setClickListener(new ItemClickListener() {
#Override
public void onClick(View view, int position, boolean isLongClick) {
AuctionBean auctionBean1 = list.get(position);
Log.d("LogPosition",position+"");
if (countDownTimer != null) {
countDownTimer.cancel();
}
CSPreferences.putString(context, "auction_id", auctionBean1.getAuction_id());
ModelManager.getInstance().getAuctionManager().AuctionManager(context, Operations.getallAcuntion(context,
ActionString.getall_auctions, "101", auctionBean1.getAuction_id()));
}
});
switch (auctionBean.getAuction_status()) {
case "status_active.png":
viewHolder.imgThumbnail.setImageResource(R.drawable.status_active);
break;
case "status_pending.png":
viewHolder.imgThumbnail.setImageResource(status_pending);
break;
case "status_over.png":
viewHolder.imgThumbnail.setImageResource(R.drawable.ic_icon_auction_red);
break;
}
}
#Override
public int getItemCount() {
return list.size();
}
public class ViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener, View.OnLongClickListener {
public ImageView imgThumbnail;
public TextView top_sr_no;
public TextView bottom_sr_no;
View view2;
private ItemClickListener clickListener;
public ViewHolder(View itemView) {
super(itemView);
imgThumbnail = (ImageView) itemView.findViewById(R.id.imageview);
top_sr_no = (TextView) itemView.findViewById(R.id.textsrno);
bottom_sr_no = (TextView) itemView.findViewById(R.id.textbottom_srno);
view2 = (View) itemView.findViewById(R.id.view1);
itemView.setOnClickListener(this);
itemView.setOnLongClickListener(this);
}
public void setClickListener(ItemClickListener itemClickListener) {
this.clickListener = itemClickListener;
}
#Override
public void onClick(View view) {
clickListener.onClick(view, getAdapterPosition(), false);
adapterpostion =getAdapterPosition();
auctionstatus =auctionList.get(getAdapterPosition());
}
#Override
public boolean onLongClick(View view) {
clickListener.onClick(view, getPosition(), true);
return true;
}
}
}
}
Please add this property in your parent layout in xml file
android:descendantFocusability="blocksDescendants"
Set adapter in RecyclerView as below:
private void setAdapter() {
recyclerView.setHasFixedSize(true);
mLayoutManager = new LinearLayoutManager(context, LinearLayoutManager.HORIZONTAL, false);
recyclerView.setLayoutManager(mLayoutManager);
recyclerView.setNestedScrollingEnabled(false);
recyclerView.setHasFixedSize(true);
mainAdapterClass = new MainAdapterClassw(context, AuctionManager.list);
recyclerView.setAdapter(mainAdapterClass);
}
In my case it was occurred because of gravity property inside EditText, It was like: android:gravity="center", So removing this property resolved the issue.

RecylerView Adapter Override methods not being called

public class HashTagSearchAdapter extends RecyclerView.Adapter<HashTagSearchAdapter.HashTagViewHolder> {
Context context;
List<TagSearchObject> tagResponses;
public HashTagSearchAdapter (Context context, List<TagSearchObject> tagResponses){
this.context = context;
this.tagResponses = tagResponses;
for (int i =0;i<tagResponses.size();i++){
Log.e("tagfor",tagResponses.get(i).toString());
}
Log.e("constructor",tagResponses.size()+"");
Log.e("constructor","here");
}
#Override
public HashTagViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = LayoutInflater.from(context).inflate(R.layout.tag_search_list,parent,false);
Log.e("onCreate","here");
return new HashTagViewHolder(view);
}
#Override
public void onBindViewHolder(HashTagViewHolder holder, int position) {
TagSearchObject userTagResponse = tagResponses.get(position);
Log.e("tag",userTagResponse.toString());
holder.name.setText(userTagResponse.getName());
holder.count.setText(userTagResponse.getChallengeCount()+" public posts");
}
#Override
public int getItemCount() {
Log.e("getItemCount",this.tagResponses.size()+"");
return tagResponses.size();
}
class HashTagViewHolder extends RecyclerView.ViewHolder{
ImageView hashtag;
TextView name,count;
public HashTagViewHolder(View itemView) {
super(itemView);
name = (TextView) itemView.findViewById(R.id.name);
count = (TextView) itemView.findViewById(R.id.count);
}
}
}
here logcat is :
E/tagfor:TagSearchObject{id='1', name='hahah', challengeCount='1'}
E/constructor: 1
E/constructor: here
Because you have not added a layout manager and/or your recycler view is not visible. Since it's not visible, no attempt to draw is made and itemCount is never called because, well, there is nothing to draw.
set layout manager before setAdapter to the RecyclerView
LinearLayoutManager llm = new LinearLayoutManager(getApplicationContext());
HashTagSearchAdapter hashTagSearchAdapter = new
HashTagSearchAdapter(context,tagSearchObjects);
recyclerView.setLayoutManager(llm);
recyclerView.setAdapter(hashTagSearchAdapter);

Android - Recycler View onClick Listener not Working

I have made a RecyclerView adapter and using that adapter to inflate data in recycler view , I previously used the general Method of ListView onItemClickListner but it doesn't seems to work with RecyclerView , Plese anyone help me out with this
My RecyclerView Adapter class is :
public class TimeAdaptra extends
RecyclerView.Adapter<TimeAdaptra.RecyclerViewHolder> {
ArrayList<String> time, status;
public TimeAdaptra(ArrayList<String> title, ArrayList<String> dec) {
this.time = title;
this.status = dec;
}
#Override
public RecyclerViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.time_slice, parent, false);
RecyclerViewHolder recyclerViewHolder = new RecyclerViewHolder(view);
return recyclerViewHolder;
}
#Override
public void onBindViewHolder(RecyclerViewHolder holder, int position) {
holder.myTitle.setText(time.get(position));
holder.mydec.setText(status.get(position));
}
#Override
public int getItemCount() {
return time.size();
}
public static class RecyclerViewHolder extends RecyclerView.ViewHolder {
TextView myTitle;
TextView mydec;
public RecyclerViewHolder(View view) {
super(view);
myTitle = (TextView) view.findViewById(R.id.timee);
mydec = (TextView) view.findViewById(R.id.status);
}
}
}
And I am doing inflation like this :
public class Slots extends AppCompatActivity {
RecyclerView recyclerView;
RecyclerView.Adapter adapter;
RecyclerView.LayoutManager manager;
private ArrayList<String> Time = new ArrayList<String>();
private ArrayList<String> Status = new ArrayList<String>();
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_slots);
Time.add("18:00");
Status.add("Available");
recyclerView = (RecyclerView) this.findViewById(R.id.timeslice);
manager = new LinearLayoutManager(this);
recyclerView.setLayoutManager(manager);
recyclerView.setHasFixedSize(true);
adapter = new TimeAdaptra(Time, Status);
recyclerView.setAdapter(adapter);
adapter.notifyDataSetChanged();
Make the RecyclerViewHolder implement View.OnClickListener like this:
public static class RecyclerViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener{
TextView myTitle;
TextView mydec;
public RecyclerViewHolder(View view) {
super(view);
myTitle = (TextView) view.findViewById(R.id.timee);
mydec = (TextView) view.findViewById(R.id.status);
myTitle.setOnClickListener(this);
mydec.setOnClickListener(this);
}
#Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.timee:
// do task
break;
case R.id.status:
//do task
break;
}
}
}
If you want to click on each item of the list then you should get the id of the root layout of the xml your are inflating and setOnClickListener() to it.

How to intent fragment to activity once clicked recycler list item with in fragment and how to get the recycler item value?

I here by attached my code.
I have a fragment with in that have recycler list item list.
If i click the recycler item on fragment i can't move to new activity.
And i want to get the recycler item value which item i have clicked.
So kindly please suggest me to how to intent fragment to activity once clicked recycler list item with in fragment and how to get the recycler item value?
ContactOneFragment
public class ContactOneFragment extends Fragment {
protected RecyclerView.LayoutManager mLayoutManager;
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view= inflater.inflate(R.layout.fragment_contactone, null, false);
RecyclerView recyclerView = (RecyclerView) view.findViewById(R.id.recyclerView);
DetailModel detail[] = {
new DetailModel("Meenakshi.A","9566415797"),
new DetailModel("Pandimurugeswari.S","8940144873"),
new DetailModel("Selvam. K","7708410451"),
new DetailModel("Raja Lakshmi.V","9952754812"),
new DetailModel("Narmatha Devi.S","9894762259"),
new DetailModel("Murugan.M","9566368816"),
new DetailModel("SenthilMurugan.A","7667453595"),
};
mLayoutManager = new LinearLayoutManager(getActivity());
recyclerView.setLayoutManager(mLayoutManager);
DetailAdapter mAdapter = new DetailAdapter(detail);
recyclerView.setAdapter(mAdapter);
recyclerView.setItemAnimator(new DefaultItemAnimator());
return view;
}
}
DetailAdapter
public class DetailAdapter extends RecyclerView.Adapter<DetailAdapter.ViewHolder> {
private DetailModel[] itemsData;
private final ArrayList<Integer> selected = new ArrayList<>();
OnItemClickListener mItemClickListener;
Context context;
public DetailAdapter(DetailModel[] itemsData) {
this.itemsData = itemsData;
}
// Create new views (invoked by the layout manager)
#Override
public DetailAdapter.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
// create a new view
View itemLayoutView = LayoutInflater.from(parent.getContext())
.inflate(R.layout.contactnamenumber, null);
// create ViewHolder
ViewHolder viewHolder = new ViewHolder(itemLayoutView);
return viewHolder;
}
// Replace the contents of a view (invoked by the layout manager)
#Override
public void onBindViewHolder(ViewHolder viewHolder, int position) {
// - get data from your itemsData at this position
// - replace the contents of the view with that itemsData
viewHolder.namelist.setText(itemsData[position].getName());
viewHolder.numberlist.setText(itemsData[position].getNumber());
// viewHolder.imgViewIcon.setImageResource(itemsData[position].getImageUrl());
// viewHolder.imgViewIcon.setTag(new Integer(position));
}
// inner class to hold a reference to each item of RecyclerView
public class ViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener{
public TextView namelist;
public TextView numberlist;
String name;
String number;
public ViewHolder(View itemLayoutView) {
super(itemLayoutView);
namelist = (TextView) itemLayoutView.findViewById(R.id.name);
numberlist = (TextView) itemLayoutView.findViewById(R.id.contactnumber);
//imgViewIcon = (ImageView) itemLayoutView.findViewById(R.id.item_icon);
name=namelist.getText().toString();
number=numberlist.getText().toString();
itemLayoutView.setOnClickListener(this);
}
#Override public void onClick(View view) {
}
}
public interface OnItemClickListener {
public void onItemClick(View view , int position);
}
public void SetOnItemClickListener(final OnItemClickListener mItemClickListener) {
this.mItemClickListener = mItemClickListener;
}
// Return the size of your itemsData (invoked by the layout manager)
#Override
public int getItemCount() {
return itemsData.length;
}
} //Update - Code formatting
Modify your constructor DetailAdapter as following:
public DetailAdapter(Context context, DetailModel[] itemsData) {
this.context = context;
this.itemsData = itemsData;
}
On your onClick() method in adapter add following code:
#Override
public void onClick(View view) {
int pos = getAdapterPosition();
String tempName = itemsData[pos].getName();
String tempNumber = itemsData[pos].getNumber();
Intent intent = new Intent(context, YourActivity.class);
startActivity(intent);
}
Add this on your adapter,
View.OnClickListener mOnClickListener = new OrderClickListener();
then
#Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.contactnamenumber, parent, false);
view.setOnClickListener(mOnClickListener);
return new ViewHolder(view);
}
now in adapter,
private class OrderClickListener implements View.OnClickListener {
#Override
public void onClick(View v) {
mOrderSelect.onOrderSelect(rvOrderHistory.getChildLayoutPosition(v));
}
}
From above steps you can get the position which item is clicked. Here you can go to next activity.

Categories

Resources