Académique Documents
Professionnel Documents
Culture Documents
One Activity can have any no.of fragments. Each fragment have it's own
functionality.
All the fragmnets must be existed in one or more than one activity. Without
activity we can't show the fragment in android application.
Using fragments we can utilize the device screen for multiple functionalities
shows at the same time.
android:layout_height="match_parent"
android:orientation="horizontal"
android:baselineAligned="false">
<fragment
android:id="@+id/listOfEmployees"
android:name="com.example.fragmentcommunication.fragments.ListOfEmployeesF
ragment"
android:layout_width="0dp"
android:layout_height="fill_parent"
android:layout_weight="1"/>
<fragment
android:id="@+id/employeeDetais"
android:name="com.example.fragmentcommunication.fragments.EmployeeDetailsF
ragment"
android:layout_width="0dp"
android:layout_height="fill_parent"
android:layout_weight="2"/>
</LinearLayout>
Now crete a new layout for list of Districts. This is the fragment in your
android activity. The layout name is fragment_list_fragment.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
android:id="@+id/listOfdists"
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<ListView
android:id="@+id/list"
android:layout_width="match_parent"
android:layout_height="match_parent" >
</ListView>
</LinearLayout>
Now crete another new layout for display individual district details. This is the
fragment
in
your
android
activity.
The
layout
fragment_details_fragment.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/details"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<TextView
android:id="@+id/detailsView"
android:layout_width="fill_parent"
android:layout_height="wrap_content"/>
name
is
</LinearLayout>
Now create we need to create three two more classes for two fragment.
Copy
below
code
into
in
your
application project.
import java.util.ArrayList;
import java.util.List;
import android.app.Activity;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.ListView;
public class ListFragment extends Fragment implements OnItemClickListener
android
{
private View v;
private static ListView lvOne;
private List<String> listOfDistricts;
private ArrayAdapter<String> adapter;
ListItemSelectedListener itemListerner;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState)
{
inflater
(LayoutInflater)getActivity().getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
v = inflater.inflate(R.layout.fragment_list_fragment, null, false);
addListItemsToListView();
initializeListeners();
return v;
}
@Override
public void onAttach(Activity activity)
{
super.onAttach(activity);
itemListerner = (ListItemSelectedListener)activity;
}
private void addListItemsToListView()
{
lvOne = (ListView)v.findViewById(R.id.list);
listOfDistricts = new ArrayList<String>();
listOfDistricts.add("Srikakulam");
listOfDistricts.add("Vijayanagaram");
listOfDistricts.add("Vizag");
adapter
new
ArrayAdapter<String>(getActivity(),
android.R.layout.simple_list_item_1, listOfDistricts);
lvOne.setAdapter(adapter);
}
private void initializeListeners()
{
lvOne.setOnItemClickListener(this);
}
public interface ListItemSelectedListener
{
itemListerner.listItemSelectedListener(listOfDistricts.get(position));
This peace of code indicates the value set as a perameter to method of interface.
when implent the interface into activity. Then, you must override this method in
Activity register for this fragment.
Now create a new class with name DetailsFragment.java In this class it will
display the related content. Based on selected items in the first fragment. The
below code snippet is copy into your class.
DetailsFragment.java
package com.example.fragmentcommunication;
import android.content.Context;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
In
the
above
code
snippet
we
are
write
one
method public
void
setContentView(R.layout.activity_main);
}
catch(Exception e)
{
Log.e(TAG, e.toString());
}
}
@Override
public void listItemSelectedListener(String name)
{
DetailsFragment detailsFragment = new DetailsFragment();
detailsFragment.getItemValue(name, this);
}
}