2013-05-11 32 views
3

按鈕我有一個應用程序,幾乎是這樣的:ListFragment按每行在

enter image description here

我試圖按照此http://developer.android.com/guide/components/fragments.html(創建事件回調到活動),但現在看來,這不工作。我的意思是,我想選擇每行查看詳細信息,並且我希望能夠點擊我想​​要的任何行的按鈕a,b或c。我不確定如何將按鈕與其行相關聯。

所以這是我rowLayout.xml:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
xmlns:tools="http://schemas.android.com/tools" 
android:layout_width="match_parent" 
android:layout_height="wrap_content" 
android:orientation="horizontal" > 

<TableLayout 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:stretchColumns="0,1"> 

    <TableRow 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" > 

     <LinearLayout 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:gravity="left"> 

      <ImageView 
       android:id="@+id/imageViewTypeClient" 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       tools:ignore="ContentDescription" /> 

      <TextView 
       android:id="@+id/textView1" 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" /> 

      <TextView 
       android:id="@+id/textView2" 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:layout_marginLeft="5dp"/> 
     </LinearLayout> 

     <LinearLayout 
      android:gravity="right" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content"> 

      <ImageButton 
       android:id="@+id/imageButtonSync" 
       android:layout_width="wrap_content" 
       android:layout_height="match_parent" 
       android:src="@android:drawable/stat_notify_sync"/> 

      <ImageButton 
       android:id="@+id/imageButtonDelete" 
       android:layout_width="wrap_content" 
       android:layout_height="match_parent" 
       android:src="@android:drawable/ic_menu_delete" 
       tools:ignore="ContentDescription" /> 

      <ImageButton 
       android:id="@+id/imageButtonEdit" 
       android:layout_width="wrap_content" 
       android:layout_height="match_parent" 
       android:src="@android:drawable/ic_menu_edit" /> 

     </LinearLayout> 
    </TableRow> 

</TableLayout> 

我做了這個方式來獲得我想要的順序。

這是我ListFragment:

public class AplicacionActivity extends FragmentActivity implements OnEmployeeSelectedListener { 


public void onEmployeeSelected(int id) { 
    Log.e("activity aplicacion", "entre"); 
    Bundle arguments = new Bundle(); 
    arguments.putLong("id", id); 
    DataClientActivity fragment = new DataClientActivity(); 
    fragment.setArguments(arguments); 
    getSupportFragmentManager().beginTransaction() 
      .replace(android.R.id.tabcontent,fragment, AplicacionActivity.tagFragmentDataClient) 
      .commit(); 
} 
... 

} 

這是我ListFragment:

public class ClientsActivity extends ListFragment { 


OnEmployeeSelectedListener mEmployeeListener; 

public interface OnEmployeeSelectedListener { 
     public void onEmployeeSelected(int id); 
} 

@Override 
public void onAttach(Activity activity) { 
    super.onAttach(activity); 
    try { 
     mEmployeeListener = (OnEmployeeSelectedListener) activity; 
    } catch (ClassCastException e) { 
     throw new ClassCastException(activity.toString() + " you must implement OnEmployeeSelectedListener"); 
    } 
} 

@Override 
public void onListItemClick(ListView l, View v, int position, long id) { 

    int idEmployee = position; 
    // Send the event and Uri to the host activity 
    mEmployeeListener.onEmployeeSelected(idEmployee); 
} 

我只是想運行這個,但是當我按我的列表中的任一行的onListItemClick不叫。 儘管如此,我不知道我是否必須爲按鈕做類似的事情。

如果任何人都可以提供給我一個教程或讓我在正確的方向,我真的會讚賞它。

回答

4

對於選擇行的問題,我讓它工作。我之前提供的代碼沒有任何問題。大量的研究後,我發現這個How to fire onListItemClick in Listactivity with buttons in list?

,幫助我的是一個寫着註釋:

「有更優雅的解決方案嘗試添加機器人:descendantFocusability =」列表中的根佈局blocksDescendants」這將使點擊onListItem成爲可能,並且可以單獨處理Button或ImageButton點擊「。

我希望這可以幫助任何人。

0

我認爲ListView.setItemsCanFocus(boolean)是你在找什麼。

+0

我試過了,但沒有任何反應:(。其他的建議?我試着用true和with false。它應該是什麼布爾值? – kiduxa 2013-05-11 13:01:24

+0

從文檔:'true如果項目可以獲得焦點,否則返回false'。使它們可以聚焦,所以你可以點擊它們,你必須添加一些onClickListener給他們,讓他們實際上做點什麼。 – Karakuri 2013-05-12 22:31:13