【问题标题】:ListFragment with buttons for each row每行带有按钮的 ListFragment
【发布时间】:2013-05-05 18:12:56
【问题描述】:

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

我曾尝试关注此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。 尽管如此,我不知道我是否必须为按钮做类似的事情。

如果有人可以为我提供教程或将我引向正确的方向,我将不胜感激。

【问题讨论】:

    标签: android android-listfragment


    【解决方案1】:

    对于选择行的问题,我让它工作。我之前放的代码没有问题。经过大量研究,我发现了这个How to fire onListItemClick in Listactivity with buttons in list?

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

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

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

    【讨论】:

      【解决方案2】:

      我认为ListView.setItemsCanFocus(boolean) 是您要找的。​​p>

      【讨论】:

      • 我试过但没有任何反应:(。还有其他建议吗?我尝试了 true 和 false。应该是哪个布尔值?
      • 来自文档:true if items can get focus, false otherwise。这只是使它们具有焦点,因此您可以单击它们。您必须向它们添加某种 onClickListener 以使它们在您单击时实际执行某些操作。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-03-23
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多