mardi 28 juin 2016

Android ListView with Fragment and ListFragment class

I have a ListView displaying items based on my fragment layout. The list is visible, is populated and is using the desired fragment layout. My ListFragment class, however... does not seem te be loaded. I'm obviously overlooking something.

My adapter has the following override:

@Override public View getView( int position, View convertView, ViewGroup parent )
{
    // Get the data item for this position
    SMSConversation conversation = getItem( position );

    // Check if an existing view is being reused, otherwise inflate the view
    if( convertView == null ) {
        convertView = LayoutInflater.from( getContext() ).inflate( R.layout.fragment_main, parent, false );
    }

    return convertView;
}

My ListFragment layout is as follows:

<LinearLayout
    android:id="@+id/main"
    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"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context=".ui.MainActivity"
    >

    <ImageView
        android:id="@+id/image_contact"
        android:layout_width="45dp"
        android:layout_height="45dp"
        android:layout_gravity="center_vertical"
        android:layout_marginEnd="15dp"
        android:maxHeight="45dp"
        android:maxWidth="45dp"
        android:minHeight="45dp"
        android:minWidth="45dp"
        android:src="@drawable/image_contact"/>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical">

        <TextView
            android:id="@+id/tvNumber"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="Number"/>

        <TextView
            android:id="@+id/tvMessage"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="Message"/>
    </LinearLayout>

</LinearLayout>

My ListFragment class is as follows:

public class FragmentMain extends ListFragment
{
    @Override public View onCreateView(
        LayoutInflater inflater,
        ViewGroup container,
        Bundle savedInstanceState
    ) {
        Log.i( "Fragment Main:", "onCreateView fired" );
        super.onCreateView( inflater, container, savedInstanceState );

        return inflater.inflate(
            R.layout.fragment_main,
            container,
            false
        );
    }

    public void onCreate( Bundle savedInstanceState )
    {
        Log.i( "Fragment Main:", "onCreate fired" );
        super.onCreate( savedInstanceState );
    }

    @Override public boolean onOptionsItemSelected( MenuItem item )
    {
        Log.i( "Fragment Main:", "onOptionsItemSelected fired" );
        return super.onOptionsItemSelected( item );
    }

    @Override public void onListItemClick( ListView l, View v, int position, long id )
    {
        Log.i( "Fragment Main:", "onListItemClick fired" );
        super.onListItemClick( l, v, position, id );
    }

    @Override public boolean onContextItemSelected( MenuItem item )
    {
        Log.i( "Fragment Main:", "onContextItemSelected fired" );
        return super.onContextItemSelected( item );
    }
}

The result I'm trying to accomplish is that my class FragmentMain will handle clicking and long-clicking the list's fragments.

My best bet is that I am inflating the fragment in a wrong way in the adapter. I haven't been able to work out a solution, though.

Aucun commentaire:

Enregistrer un commentaire