androidlistviewandroid-fragmentsonitemclicklistenerratingbar

onItemClickListener of ListView not working with RatingBar


I have a list item click listener (onItemClickListener) inside the overridden onViewCreated method of a fragment. The method body is given below:

@Override
public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
    final String uname = getActivity().getIntent().getExtras().getString("uname");

    list = (ListView) getActivity().findViewById(R.id.drlistforhome);
    list.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
            Doctors doc = rh.drList.get(position);
            imgURL = doc.getDrProPic();
            docName = doc.getDrName();
            jobTitle = doc.getDrJobTitle();
            docSpeciality = doc.getDrSpeciality();
            docUname = doc.getDrUname();
            startActivity(new Intent(getActivity().getApplicationContext(), DoctorsProfileH.class).putExtra("img", imgURL)
                    .putExtra("name", docName)
                    .putExtra("title", jobTitle)
                    .putExtra("speciality", docSpeciality)
                    .putExtra("uname", uname)
                    .putExtra("druname", docUname));
        }
    });

}

The custom list view layout consists of six views: 4 TextViews, an ImageView and a RatingBar. The onItemClickListener was listening to list item clicks until the RatingBar was not added. But now that I have added a RatingBar in my custom layout, the listener wouldn't listen the clicks anymore. I am using a custom adapter to set all the views. Here is my custom adapter:

class TDsAdapter extends ArrayAdapter<Doctors> {
        public TDsAdapter(){
            super(getActivity().getApplicationContext(),R.layout.custom_list_view,rh.drList);
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            View itemView = convertView;
            if (itemView == null) {
                itemView = inflater.inflate(R.layout.custom_list_view, parent, false);
            }
            Doctors currentItem = rh.drList.get(position);
            TextView drName = (TextView) itemView.findViewById(R.id.drName);
            drName.setText(currentItem.getDrName());

            TextView drSpeciality = (TextView) itemView.findViewById(R.id.drSpeciality);
            drSpeciality.setText(currentItem.getDrSpeciality());

            TextView drJobTitle = (TextView) itemView.findViewById(R.id.drJobTitle);
            drJobTitle.setText(currentItem.getDrJobTitle());

            ImageView drProPic = (ImageView) itemView.findViewById(R.id.drPropic);
            Picasso.with(getContext()).load(currentItem.getDrProPic()).into(drProPic);

            TextView distence = (TextView) itemView.findViewById(R.id.dis);
            distence.setText(currentItem.getDis());

            RatingBar rb = (RatingBar) itemView.findViewById(R.id.ratingBar);
            rb.setRating((float) 2.5);

            return itemView;
        }
    }

After running the application the list view appears fine with the RatingBar as expected, but the list items are no longer clickable. When I debug the app with a break point inside the listener, that break point never gets executed. What I understand from my very little knowledge about fragments, this is probably because the onViewCreated is expecting all the views to be created first. But as we can see, all the views have successfully been created. How can I fix my problem? Please do not hesitate to ask for more specific information.


Solution

  • you can try with

    android:descendantFocusability="blocksDescendants"
    

    in root layout of your custom row item.