Introduction
This article explains ListFragment in Android.
Before beginning the coding, first, we learn the basics of ListFragment. To create a ListView in your class you need to extend the ListFragment class. Create an array of String that you want to show in a ListView and ArrayAdapter object. Inside the onCreateView() method you will create the ArrayAdapter object and the array to the constructor of ArrayAdapter. Now after creating the object, you will this to the setListAdapter() method to set the list in a ListView.
In the activity_main XML file, you will use a fragment that you want to show on the MainActivity screen. In the fragment name property, you will the second class as an attribute.
Step 1
Create a project like this:
Step 2
Create an activity_main XML file and write the following.
In this XML file you will use the fragment inside Relative Layout.
- <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
- xmlns:tools="http://schemas.android.com/tools"
- android:layout_width="match_parent"
- android:layout_height="match_parent"
- android:paddingLeft="@dimen/activity_horizontal_margin"
- android:paddingRight="@dimen/activity_horizontal_margin"
- android:paddingTop="@dimen/activity_vertical_margin"
- android:paddingBottom="@dimen/activity_vertical_margin"
- tools:context=".MainActivity">
-
-
- <fragment
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:name="com.listfragment.Second"
- android:id="@+id/fragment"
- android:layout_alignParentTop="true"
- android:layout_alignParentLeft="true"/>
- </RelativeLayout>
Step 3
Create another XML file and write the following:
- <?xml version="1.0" encoding="utf-8"?>
- <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
- android:orientation="vertical"
- android:layout_width="match_parent"
- android:layout_height="match_parent"
- android:background="#123456">
- </LinearLayout>
Step 4
Create a Java file and write the following:
- package com.listfragment;
-
- import android.os.Bundle;
- import android.app.Activity;
- import android.view.Menu;
- public class MainActivity extends Activity {
-
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_main);
- }
- @Override
- public boolean onCreateOptionsMenu(Menu menu) {
-
- getMenuInflater().inflate(R.menu.main, menu);
- return true;
- }
- }
Step 5
Create another Java class and write the following.
For creating a ListView in your class you need to extend the ListFragment class. Create an array of String that you want to show in a ListView and ArrayAdapter object. Inside the onCreateView() method you will create the ArrayAdapter object and the array to the constructor of ArrayAdapter. Now after creating the object, you will this to the setListAdapter() method to set the list in a ListView.
After that, you will get the ListView by calling the getListView() method. Now set the ListView on its item click listener to get the item when you perform the click event.
- package com.listfragment;
-
- import android.*;
- import android.R;
- import android.annotation.TargetApi;
- import android.app.ListFragment;
- import android.graphics.AvoidXfermode;
- import android.os.Build;
- import android.os.Bundle;
- import android.view.LayoutInflater;
- import android.view.View;
- import android.view.ViewGroup;
- import android.widget.AdapterView;
- import android.widget.ArrayAdapter;
- import android.widget.ListView;
- import android.widget.TextView;
- import android.widget.Toast;
-
-
-
-
- @TargetApi(Build.VERSION_CODES.HONEYCOMB)
- public class Second extends ListFragment {
- ListView listview;
- View view;
- String[] array=new String[]{"Android","BlackBerry","Windows","Iphone"};
-
- public View onCreateView(LayoutInflater i,ViewGroup container,Bundle savedInstanceState)
- {
-
- ArrayAdapter<String> adapter=new ArrayAdapter<String>(getActivity(), R.layout.simple_list_item_1,R.id.text1,array);
-
- setListAdapter(adapter);
- return super.onCreateView(i,container,savedInstanceState);
- }
- public void onActivityCreated(Bundle savedInstanceState){
- super.onActivityCreated(savedInstanceState);
-
- listview=getListView();
- listview.setOnItemClickListener(new AdapterView.OnItemClickListener() {
- @Override
- public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
-
- Toast.makeText(getActivity(), (CharSequence) listview.getItemAtPosition(i),Toast.LENGTH_LONG).show();
- }
- });
- }
- }