2

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 (check the log commands, none of them are triggered):

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.

EDIT

I have done some more reading and came across this post: Can a ListView contain Fragments

In the comments, some users are explaining why using Fragments as list items may be a bad idea. Since I could just inflate any view and not use a fragment I would probably not to use the my ListFragment class and could just register and handle the click or long-click event in my adapter class. I would like to separate the code responsible for for handling the clicking or long-clicking from the actual adapter which is why I started using the fragment as list-items. Is there any other way I can separate the logic?

Community
  • 1
  • 1
halfpastfour.am
  • 5,764
  • 3
  • 44
  • 61
  • 1
    Why do you inflate **fragment_main** both in **getView()** and in **FragmentMain** class? Also what is the result you want to accomplish? – Todor Kostov Jun 26 '16 at 21:14
  • Ah, the double inflation is from me experimenting and desperately trying to get the fragment class to do anything. I will edit my question to further explain my desired result. – halfpastfour.am Jun 26 '16 at 21:16
  • 1
    Did you overwrite getCount in your adapter? – cark Jun 26 '16 at 21:16
  • @cark I have not. Would you suggest I do? What can I achieve by doing that? The list itself is working perfectly fine. – halfpastfour.am Jun 26 '16 at 21:20
  • 1
    @BobKruithof sorry, i missed a part of your question. If the list is visible, the problem isn't the getCount. – cark Jun 26 '16 at 21:32
  • Can you post the code where you add FragmentMain? However, why do you want to extend ListFragment? If you just need to handle clicks on the items, you can just use setOnItemClickListener on the standard ListView, and you can get the listView calling getListView on your ListFragment. – cark Jun 26 '16 at 21:45

0 Answers0