3

我有一个应用程序,差不多是这样的:

在此处输入图像描述

我试图遵循这个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

2 回答 2

4

对于选择行的问题,我让它工作。我之前放的代码没有问题。经过大量研究后,我发现如何使用列表中的按钮在 Listactivity 中触发 onListItemClick?

帮助我的评论是这样说的:

“还有更优雅的解决方案尝试在列表元素的根布局中添加 android:descendantFocusability="blocksDescendants"。这将使单击 onListItem 成为可能,并且您可以单独处理 Button 或 ImageButton 单击”。

我希望这对任何人都有帮助。

于 2013-05-11T15:49:09.890 回答
0

我想ListView.setItemsCanFocus(boolean)这就是你要找的。

于 2013-05-11T06:01:37.227 回答