3

I have seen all the related links, but didn't found out any solutions yet. I will explain the problem now. I have recyclerview (Grid Layout), i set an on Click listener as i usually do. But this one is behaving abnormally. after so many clicks on different items. OnClick runs for one or two time randomly.Any help will be much appreciated.I will post my Adapter Class code here.

public class CurrentLoadNumberListAdapter extends RecyclerView.Adapter<CurrentLoadNumberListAdapter.ListViewHolder> { private Context context; private List<CurrentLoadNumberDetail> currentLoadNumberDetailList; private NIOClient nioClient; private FragmentManager fragmentManager; private FragmentActivity myContext; private Activity activity; private android.support.v4.app.Fragment fragment; private SweetAlertDialog sweetAlertDialogProgressBar; public static int clickedPosition; public CurrentLoadNumberListAdapter(Context context, List<CurrentLoadNumberDetail> currentLoadNumberDetailList , Activity activity, NIOClient nioClient) { this.context = context; this.currentLoadNumberDetailList = currentLoadNumberDetailList; this.activity = activity; this.nioClient = nioClient; } @NonNull @Override public ListViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) { LayoutInflater inflater = LayoutInflater.from(context); View view = inflater.inflate(R.layout.company_current_load_number_listlayout,null); //Specifying Activity for Fragment Transaction activity = activity; myContext = (FragmentActivity) activity; //Iniallizing Fragment Manager fragmentManager = myContext.getSupportFragmentManager(); //Making Sweet Alert progress Bar sweetAlertDialogProgressBar = new SweetAlertDialog(context,SweetAlertDialog.PROGRESS_TYPE); sweetAlertDialogProgressBar.getProgressHelper().setBarColor(Color.parseColor("#9ccc65")); return new CurrentLoadNumberListAdapter.ListViewHolder(view); } @Override public void onBindViewHolder(@NonNull final ListViewHolder holder, final int position) { holder.loadNumber.setText(currentLoadNumberDetailList.get(position).getLoadNumber()); } @Override public int getItemCount() { return currentLoadNumberDetailList.size(); } class ListViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener { private Button loadNumber; public ListViewHolder(View itemView){ super(itemView); loadNumber = itemView.findViewById(R.id.current_load_number); itemView.setOnClickListener(this); } @Override public void onClick(View view) { int ItemPosition = getLayoutPosition(); //nioClient.sendMsg(getMinLoad(currentLoadNumberDetailList.get(position).getIdDeviceLoadRecord())+" \r\n\r\n"); Toast.makeText(context,"On click " + ItemPosition ,Toast.LENGTH_SHORT).show(); } } public void updateData(){ currentLoadNumberDetailList.clear(); notifyDataSetChanged(); } private String getMinLoad(String id){ Gson gson = new Gson(); GetMinLoadRequestModel minLoadRequestModel = new GetMinLoadRequestModel(); minLoadRequestModel.setIdDeviceLoadRecord(id); minLoadRequestModel.setMessageType("get_min_load_info"); String json = gson.toJson(minLoadRequestModel,GetMinLoadRequestModel.class); return json; } 

}

Anyone please do have a look. Thank you.

3
  • 1
    Have you tried taping outside of the button to see if that works. The button is a clickable view and may be consuming the events before they reach your view holder's itemview. Commented Sep 13, 2018 at 8:20
  • I just set the xml attribute clickable attribute false. so when i click the button. It takes a handsome amount of time to run the toast inside the onClick. Commented Sep 13, 2018 at 8:31
  • By clicking outside the buttons. It does nothing, i even made a new clean adapter. but nothing chnages.@VeselinTodorov Commented Sep 13, 2018 at 8:32

2 Answers 2

2

So i found the problem. As @Veselin Todorov suggested. Button is clickable view by default , so it was consuming the onClick before it was reaching my viewHolder. Solution Just Make the button xml attribute

android:clickable="false". 

It Will Work fine. Hope it helps someone. Thank you guys for answering.

Sign up to request clarification or add additional context in comments.

Comments

0

I had the same issue, what was happening is there were many ui events happening in my case those ui events were the on success of a non-necessary network call that was happening continuously and the onSuccess of this call was updating the list so I wan not capable of clicking any thing neither a button in the list not a whole list item(I know the reason might be something else but this could be the reason either) SOLUTION: removed the UI mutator (the unnecessary network call) and it worked fine.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.