kiduxa
kiduxa

Reputation: 3351

ListFragment with buttons for each row

I have an app, pretty much as this:

enter image description here

I have tried to follow this http://developer.android.com/guide/components/fragments.html (Creating event callbacks to the activity) But it seems it doesn't work. I mean, I would like to select each row for see details and also I want to be able to click in button a, b or c of any row I want. I'm not sure how to relate a button with its row.

So this is my 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>

I made this way to get the order what I want.

This is my 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();
}
...

}

And this is my 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);
}

I was just trying to run this but the onListItemClick is not called when I press any row of my list. Despite of this, I dont know if I have to do something similar for the press buttons.

If anyone can provide me a tutorial or put me in the right direction I'll really apreciate it.

Upvotes: 3

Views: 3322

Answers (2)

kiduxa
kiduxa

Reputation: 3351

For the problem with selecting the rows, I made it work. There is nothing wrong with the code I put before. After a lot of research I found this How to fire onListItemClick in Listactivity with buttons in list?

The comment that helped me was the one that says:

"There is even more elegant solution try to add android:descendantFocusability="blocksDescendants" in root layout of list element. That will make clicks onListItem possible and separately u can handle Button or ImageButton clicks".

I hope this could help to any one.

Upvotes: 4

Karakuri
Karakuri

Reputation: 38605

I think ListView.setItemsCanFocus(boolean) is what you're looking for.

Upvotes: 0

Related Questions