2013-05-11 41 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