1

我有一個列表項單擊監聽器(onItemClickListener)內部重寫的onViewCreated片段的方法。方法體在下面給出:ListView的onItemClickListener不能與RatingBar

@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)); 
     } 
    }); 

} 

自定義列表視圖佈局包括六個觀點:4個TextView S,一個ImageViewRatingBaronItemClickListener正在監聽列表項目點擊,直到未添加RatingBar。但現在,我在自定義佈局中添加了一個RatingBar,該監聽器不再聽取點擊。我正在使用自定義適配器來設置所有視圖。這裏是我的自定義適配器:

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; 
     } 
    } 

運行的應用程序後,列表視圖中出現預期細跟RatingBar,但列表中的項目都不再點擊。當我在偵聽器中用斷點調試應用程序時,該斷點永遠不會被執行。根據我對片段知之甚少的理解,這可能是因爲onViewCreated期望首先創建所有視圖。但是,正如我們所看到的,所有的觀點都已經成功創建。我該如何解決我的問題?請不要猶豫,要求提供更具體的信息。

+0

嘗試在您的評分欄屬性中添加focuable =「false」 –

+0

嘗試過,沒有運氣:( –

回答

2

您可以在自定義行項目的根佈局

android:descendantFocusability="blocksDescendants" 

嘗試。

+0

謝謝。它幫助:) –

2

寫下面的代碼在你的活動/片段

listview.setItemsCanFocus(true); 

還請加在適配器中排佈置根,

android:descendantFocusability="blocksDescendants" 

希望這會幫助你。

+0

感謝您的回答,當我有15個代表點時,我將upvote您的答案:) –

相關問題