Translate

Wednesday, 7 September 2016

Android Fragment Tutorial


A Fragment speaks to a conduct or a bit of UI in an Activity. You can consolidate numerous parts in a solitary action to assemble a multi-sheet UI and reuse a section in different exercises. You can think about a piece as a secluded area of an action, which has its own particular lifecycle, gets its own info occasions, and which you can include or evacuate while the movement is running.
Here in this showing, we make two sections in a solitary window.One part shows ListView of names of individuals whose points of interest can be seen by selecting or tapping on them. For showing these points of interest, the Second piece is utilized.

So lets begin…

Requirements:- Android Studio

Step1:- Create an android studio venture with an unfilled action.

Step2:- Here is a substance of how we are going to continue with this venture

In the first place we are going to make

fragment_list.xml->This document just contains a ListView in LinearLayout which shows rundown of alternatives or rundown of names in our case.This record would be in res->layout organizer.

With this we make its controller document called UsersListFragment.java, so make another java record with this name

Second we are going to make

fragment_details.xml->This document contains basic TextView inserted again in a LnearLayout which is utilized for showing the subtle elements of chose thing.

With this we make its controller document called DetailFragment.java,so make another java record with this name.

Finally,we are going to make

complete_layout.xml->This record contains both the fragments.So now rather than activity_main.xml, MainActivity.java document calls upon complete_layout.xml,So erase or rename activity_main.xml.

With this we are going to code into MainActivity.java which is its controller record.
Step3:- In fragment_list.xml,a ListView is announced .

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >


    <ListView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:id="@+id/userslist"
        >
    </ListView>

</LinearLayout>

Step4:- make another java in the java->package_name called UsersListFragment.java which is the controller document of fragment_list.xml.

import android.app.Fragment;
    import android.content.Context;
    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 java.util.ArrayList;
 
    public class UsersListFragment extends Fragment {
 
        private OnUserSelectedListener listener;
        ListView usersList;
        @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container,
                                 Bundle savedInstanceState) {
            View view = inflater.inflate(R.layout.fragment_list,
                    container, false);
 
            usersList = (ListView)view.findViewById(R.id.userslist);
            ArrayList<String> users = new ArrayList<String>();
            users.add("Vishal  Detake");
            users.add("Ashish Shriwastav");
            users.add("Sambhji Jatar");
            users.add("Tushar Handore");
            users.add("Ritesh Singh");
            ArrayAdapter<String> usersAdapter = new ArrayAdapter<String>(getActivity(),android.R.layout.simple_list_item_1,users);
            usersList.setAdapter(usersAdapter);
 
            usersList.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                @Override
                public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                    getDetail(usersList.getItemAtPosition(position).toString());
                }
            });
            return view;
        }
 
        public interface OnUserSelectedListener {
            public void onUserSelected(String link);
        }
 
        @Override
        public void onAttach(Context context) {
            super.onAttach(context);
            if (context instanceof OnUserSelectedListener) {
                listener = (OnUserSelectedListener) context;
            } else {
                throw new ClassCastException(context.toString()
                        + "Activity Does not Implements Listener");
            }
        }
        @Override
        public void onDetach() {
            super.onDetach();
            listener = null;
        }
        public void getDetail(String name) {
            listener.onUserSelected(name);
        }
    }


Step5:- In fragment_details.xml,a TextView is utilized to announce the subtle elements.

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >
 
    <TextView
        android:id="@+id/detailsText"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:layout_gravity="center_horizontal|center_vertical"
        android:layout_marginTop="20dip"
        android:text="Nothing Selected"
        android:textAppearance="?android:attr/textAppearanceLarge"
        android:textSize="30dip" />
 
</LinearLayout>

Step6:- So now moving to its java record, make another java in the java->package_name called DetailFragment.java which is the controller document of fragment_details.xml.

import android.app.Fragment;
        import android.os.Bundle;
        import android.view.LayoutInflater;
        import android.view.View;
        import android.view.ViewGroup;
        import android.widget.TextView;
 
public class DetailFragment extends Fragment {
 
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_details,
                container, false);
        return view;
    }
 
    public void setText(String name) {
        TextView view = (TextView) getView().findViewById(R.id.detailsText);
        view.setText(name);
    }
}

Step7:- In complete_layout.xml, 2 sections are pronounced implanted in LinearLayout.

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:baselineAligned="false"
    android:orientation="vertical" >
 
    <fragment
        android:id="@+id/listFragment"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        class="com.example.admin.fragmentexample.UsersListFragment" >
    </fragment>
 
    <fragment
        android:id="@+id/detailFragment"
        android:layout_width="match_parent"
 
        android:layout_height="wrap_content"
        class="com.example.admin.fragmentexample.DetailFragment" >
    </fragment>
 
</LinearLayout>

Step8:- So now we at last code into MainActivity.java.

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
 
public class MainActivity extends AppCompatActivity implements UsersListFragment.OnUserSelectedListener {
 
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.complete_layout);
    }
 
    @Override
    public void onUserSelected(String name) {
        DetailFragment fragment = (DetailFragment) getFragmentManager()
                .findFragmentById(R.id.detailFragment);
        fragment.setText(name);
    }
}


No comments:

Post a Comment