2013-05-30 40 views
0

早上好,Android的 - 的ListView setOnItemClickListener事件片段不工作

我有這是從來沒有發射片段內setOnItemClickListener事件ListView的問題。

下面是該項目在listView的代碼:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
android:layout_width="fill_parent" 
android:layout_height="80dip" > 

<ImageView 
    android:id="@+id/url_foto" 
    android:layout_width="100dip" 
    android:layout_height="100dip" 
    android:src="@drawable/stub" 
    android:scaleType="centerCrop"/> 

<LinearLayout 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    android:layout_toRightOf="@id/url_foto" 
    android:orientation="vertical" 
    android:paddingLeft="10sp" > 

    <TextView 
     android:id="@+id/nome" 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:textIsSelectable="true" /> 

    <TextView 
     android:id="@+id/cognome" 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:textIsSelectable="true" /> 

下面的列表的XML代碼(即還包括:使用一些聽衆的EditText):

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
android:id="@+id/LinearLayout01" 
android:layout_width="fill_parent" 
android:layout_height="fill_parent" 
android:orientation="vertical" > 

<EditText 
    android:id="@+id/cerca_il_prof_edit_search" 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:hint="search" /> 

<ListView 
    android:id="@+id/cerca_il_prof_list_result" 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:cacheColorHint="#00000000" 
    android:drawSelectorOnTop="true" 
    android:focusable="true" /> 

終於包含未觸發事件的片段:

public class CercaIlProfFragment extends Fragment { 
private DbAdapter dbHelper; 
private Cursor cursor; 
private Context ctx; 
List<Professore> listProf; 

private static final String TAG = "CercaIlProf - "; 

private EditText string_search; 
private ListView listViewProf; 

int textlength = 0; 

/** 
* The fragment argument representing the section number for this fragment. 
*/ 
public static final String ARG_SECTION_NUMBER = "section_number"; 

@Override 
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { 
    View rootView = inflater.inflate(R.layout.cerca_il_prof, container, false); 

    ctx = getActivity(); 
    listProf = new ArrayList<Professore>(); 

    string_search = (EditText) rootView.findViewById(R.id.cerca_il_prof_edit_search); 

    Date currDate = new Date(); 
    Calendar calendar = Calendar.getInstance(); 
    calendar.setTime(currDate); 
    calendar.add(Calendar.MONTH, 1); 

    // get all prof 
    getAllProf(); 

    listViewProf = (ListView) rootView.findViewById(R.id.cerca_il_prof_list_result); 
    ProfessoreListAdapterWithCache professoreListAdapterWithCache = new ProfessoreListAdapterWithCache(ctx, 
      R.layout.cerca_il_prof_list_result_item, listProf, this.getActivity()); 
    listViewProf.setAdapter(professoreListAdapterWithCache); 

    listViewProf.setOnItemClickListener(new OnItemClickListener() { 
     @Override 
     public void onItemClick(AdapterView<?> parent, View view, int position, long id) { 
      Toast.makeText(getActivity(), "Click ListItem Number " + position, Toast.LENGTH_LONG).show(); 
     } 
    }); 

    listViewProf.setEnabled(true); 

    string_search.addTextChangedListener(new TextWatcher() { 
     public void afterTextChanged(Editable s) { 
      Toast.makeText(getActivity(), "Click ListItem Number ", Toast.LENGTH_LONG).show(); 
     } 

     public void beforeTextChanged(CharSequence s, int start, int count, int after) { 
      //do stuff 
     } 

     public void onTextChanged(CharSequence s, int start, int before, int count) { 
      //do stuff 
    }); 

    return rootView; 

} 

有人可以幫我嗎?

在此先感謝。

問候。

朱塞佩

+0

如果你的片段從未被解僱,你怎麼會有問題? – Blackbelt

回答

2

要在@擴大阿什溫的回答,請注意的ListView的塊點擊至少包含一個可聚焦 後代項,但它不會使內容對焦可達調用 setItemsCanFocus(真),一個解決辦法是通過禁用後裔的可聚焦性,使用

android:descendantFocusability="blocksDescendants" 

在定義您的列表項的佈局中,儘管您可以僅禁用前面提到的可以關注焦點的子項。 (首先從http://cyrilmottier.com/2011/11/23/listview-tips-tricks-4-add-several-clickable-areas/瞭解到這一點,但是其他一些SO帖也指出了這一點。)

1

聲明你的ListView和itemClick在監聽器裏onViewCreated一樣,

@Override 
public void onViewCreated(View view, Bundle savedInstanceState) { 
    super.onViewCreated(view, savedInstanceState); 

    final ListView listViewProf=(ListView)getActivity().findViewById(R.id.cerca_il_prof_list_result); 
    listViewProf.setOnItemClickListener(new OnItemClickListener() { 
     public void onItemClick(AdapterView<?> parent, View view, int position, long id) { 

     } 
    }); 
} 
+0

我已經理解了這個問題:它在項目細節的xml文件中。 –

+0

曾爲subveiws點擊做過工作,但沒有用於onItemClick – fnc12

1

只要把android:focusable="false" android:clickable="false"佈局。對於所有textviews,按鈕等。 工作正常。

0

你會嘗試這樣它會幫助你 lv.setOnItemClickListener(新android.widget.AdapterView.OnItemClickListener(){

  @Override 
      public void onItemClick(android.widget.AdapterView<?> arg0, 
        View arg1, int arg2, long arg3) { 
       // TODO Auto-generated method stub 


      } 
     }); 
相關問題