Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- MainActivity.java
- ===================
- package com.example.zeevm.myfragment;
- import android.app.FragmentManager;
- import android.app.FragmentTransaction;
- import android.support.v7.app.AppCompatActivity;
- import android.os.Bundle;
- import android.view.View;
- public class MainActivity extends AppCompatActivity {
- FragmentManager fm;
- FragmentTransaction ft;
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_main);
- //create fragment
- fm = getFragmentManager();
- //create fragment transaction
- ft = fm.beginTransaction();
- //add to our container fragment 1
- ft.add(R.id.fContainer, new fg1());
- //commit the changes
- ft.commit();
- }
- public void onClick1(View view)
- {
- //load fragment 1
- ft = fm.beginTransaction();
- //create new instance of our fragment
- fg1 myFrag = new fg1();
- //set transtion animation
- ft.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN);
- //replace the view
- ft.replace(R.id.fContainer,myFrag);
- //commit the change
- ft.commit();
- }
- public void onClick2(View view)
- {
- //load fragment 2
- ft = fm.beginTransaction();
- //create new instance of our fragment
- fg2 myFrag = new fg2();
- //set transtion animation
- ft.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_CLOSE);
- //replace the view
- ft.replace(R.id.fContainer,myFrag);
- //commit the change
- ft.commit();
- }
- public void onClick3(View view)
- {
- //load fragment 3
- ft = fm.beginTransaction();
- //create new instance of our fragment
- fg3 myFrag = new fg3();
- //set transtion animation
- ft.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_FADE);
- //replace the view
- ft.replace(R.id.fContainer,myFrag);
- //commit the change
- ft.commit();
- }
- }
- MyListAdapter.java
- ========================
- package com.example.zeevm.myfragment;
- import android.content.Context;
- import android.view.View;
- import android.view.ViewGroup;
- import android.widget.BaseAdapter;
- import android.widget.TextView;
- import org.w3c.dom.Text;
- import java.util.List;
- /**
- * Created by zeevm on 31/10/2016.
- */
- public class MyListAdapter extends BaseAdapter {
- List<String> myList;
- Context context;
- public MyListAdapter(List<String> myList, Context context) {
- this.myList = myList;
- this.context = context;
- }
- @Override
- public int getCount() {
- return myList.size();
- }
- @Override
- public Object getItem(int i) {
- return null;
- }
- @Override
- public long getItemId(int i) {
- return 0;
- }
- @Override
- public View getView(int i, View view, ViewGroup viewGroup) {
- TextView myText = new TextView(context);
- myText.setText(myList.get(i));
- myText.setTextSize(30);
- return myText;
- }
- }
- fg1.java
- ====================
- package com.example.zeevm.myfragment;
- import android.app.Fragment;
- import android.os.Bundle;
- import android.support.annotation.Nullable;
- import android.view.LayoutInflater;
- import android.view.View;
- import android.view.ViewGroup;
- import android.widget.ListView;
- import java.util.ArrayList;
- import java.util.List;
- /**
- * Created by zeevm on 31/10/2016.
- */
- public class fg1 extends Fragment {
- ListView myList;
- List<String> students;
- public fg1(){} //required empty public constractur
- @Nullable
- @Override
- public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
- //infalte the layout for this fragment
- View rootView = inflater.inflate(R.layout.fg1,container,false);
- //set pointer to myList
- myList = (ListView)rootView.findViewById(R.id.fmyList);
- //create our adapter to the list view
- MyListAdapter adapter= new MyListAdapter(createList(),getActivity());
- myList.setAdapter(adapter);
- return rootView;
- }
- private List<String> createList()
- {
- students = new ArrayList<>();
- students.add("Adam Tuki");
- students.add("Lorien");
- students.add("Michal");
- students.add("Noa Apchi");
- students.add("Itzik Safug");
- students.add("Elinor sheela");
- students.add("Elad i late");
- students.add("Dudi computer");
- students.add("Faruk ad 10");
- students.add("Caram no hebrew");
- students.add("Pavel i want mac");
- return students;
- }
- }
- fg2.java
- =======================
- package com.example.zeevm.myfragment;
- import android.app.Fragment;
- import android.os.Bundle;
- import android.support.annotation.Nullable;
- import android.view.LayoutInflater;
- import android.view.View;
- import android.view.ViewGroup;
- import android.widget.Button;
- import android.widget.Toast;
- /**
- * Created by zeevm on 31/10/2016.
- */
- public class fg2 extends Fragment {
- Button btnTest;
- @Nullable
- @Override
- public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
- //Inflate the layout for this fragment
- View rootView = inflater.inflate(R.layout.fg2,container,false);
- //set pointer
- btnTest=(Button)rootView.findViewById(R.id.myBtnTest);
- //set on click listner
- btnTest.setOnClickListener(new View.OnClickListener() {
- @Override
- public void onClick(View view) {
- Toast.makeText(getActivity(), "Adam Showarma/humus/chips/salat", Toast.LENGTH_SHORT).show();
- }
- });
- return rootView;
- }
- public fg2(){}
- }
- fg3.java
- =====================
- package com.example.zeevm.myfragment;
- import android.app.Fragment;
- import android.os.Bundle;
- import android.support.annotation.Nullable;
- import android.view.LayoutInflater;
- import android.view.View;
- import android.view.ViewGroup;
- import android.widget.TextView;
- /**
- * Created by zeevm on 31/10/2016.
- */
- public class fg3 extends Fragment {
- @Nullable
- @Override
- public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
- //inflate the view
- View rootView = inflater.inflate(R.layout.fg3,container,false);
- TextView myTxt=(TextView)rootView.findViewById(R.id.fg3Text);
- myTxt.setText("Elinor Sheela!!!");
- return rootView;
- }
- public fg3(){}
- }
- activity_main.xml
- =========================
- <?xml version="1.0" encoding="utf-8"?>
- <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
- xmlns:tools="http://schemas.android.com/tools"
- android:id="@+id/activity_main"
- android:layout_width="match_parent"
- android:layout_height="match_parent"
- 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="com.example.zeevm.myfragment.MainActivity">
- <LinearLayout
- android:layout_width="match_parent"
- android:layout_height="wrap_content"
- android:orientation="horizontal"
- android:id="@+id/fButton">
- <Button
- android:layout_width="0dp"
- android:layout_height="wrap_content"
- android:layout_weight="1"
- android:text="Fragment 1"
- android:onClick="onClick1"/>
- <Button
- android:layout_width="0dp"
- android:layout_height="wrap_content"
- android:layout_weight="1"
- android:text="Fragment 2"
- android:onClick="onClick2"/>
- <Button
- android:layout_width="0dp"
- android:layout_height="wrap_content"
- android:layout_weight="1"
- android:text="Fragment 3"
- android:onClick="onClick3"/>
- </LinearLayout>
- <LinearLayout
- android:layout_width="match_parent"
- android:layout_height="match_parent"
- android:id="@+id/fContainer"
- android:orientation="vertical"
- android:layout_below="@id/fButton"
- android:layout_margin="20dp">
- </LinearLayout>
- </RelativeLayout>
- fg1.xml
- =====================
- <?xml version="1.0" encoding="utf-8"?>
- <FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
- android:layout_width="match_parent"
- android:layout_height="match_parent">
- <ListView
- android:layout_width="match_parent"
- android:layout_height="match_parent"
- android:id="@+id/fmyList"/>
- </FrameLayout>
- fg2.xml
- ====================
- <?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">
- <Button
- android:layout_width="match_parent"
- android:layout_height="wrap_content"
- android:text="PRESS ME"
- android:background="#009fff"
- android:textColor="#fff"
- android:layout_marginTop="100dp"
- android:id="@+id/myBtnTest"/>
- </LinearLayout>
- fg3.xml
- =================
- <?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">
- <TextView
- android:layout_width="match_parent"
- android:layout_height="match_parent"
- android:text="I am Adam !!!!!"
- android:id="@+id/fg3Text"
- android:gravity="center"
- android:textSize="50sp"/>
- </LinearLayout>
Add Comment
Please, Sign In to add comment