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?