m_naish

Fragments02

Nov 17th, 2017
39
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. MainActivity.java
  2. ===================
  3. package com.example.zeevm.myfragment;
  4.  
  5. import android.app.FragmentManager;
  6. import android.app.FragmentTransaction;
  7. import android.support.v7.app.AppCompatActivity;
  8. import android.os.Bundle;
  9. import android.view.View;
  10.  
  11. public class MainActivity extends AppCompatActivity {
  12.  
  13.     FragmentManager fm;
  14.     FragmentTransaction ft;
  15.  
  16.     @Override
  17.     protected void onCreate(Bundle savedInstanceState) {
  18.         super.onCreate(savedInstanceState);
  19.         setContentView(R.layout.activity_main);
  20.         //create fragment
  21.         fm = getFragmentManager();
  22.         //create fragment transaction
  23.         ft = fm.beginTransaction();
  24.         //add to our container fragment 1
  25.         ft.add(R.id.fContainer, new fg1());
  26.         //commit the changes
  27.         ft.commit();
  28.     }
  29.  
  30.     public void onClick1(View view)
  31.     {
  32.         //load fragment 1
  33.         ft = fm.beginTransaction();
  34.         //create new instance of our fragment
  35.         fg1 myFrag = new fg1();
  36.         //set transtion animation
  37.         ft.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN);
  38.         //replace the view
  39.         ft.replace(R.id.fContainer,myFrag);
  40.         //commit the change
  41.         ft.commit();
  42.     }
  43.  
  44.     public void onClick2(View view)
  45.     {
  46.         //load fragment 2
  47.         ft = fm.beginTransaction();
  48.         //create new instance of our fragment
  49.         fg2 myFrag = new fg2();
  50.         //set transtion animation
  51.         ft.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_CLOSE);
  52.         //replace the view
  53.         ft.replace(R.id.fContainer,myFrag);
  54.         //commit the change
  55.         ft.commit();
  56.     }
  57.  
  58.     public void onClick3(View view)
  59.     {
  60.         //load fragment 3
  61.         ft = fm.beginTransaction();
  62.         //create new instance of our fragment
  63.         fg3 myFrag = new fg3();
  64.         //set transtion animation
  65.         ft.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_FADE);
  66.         //replace the view
  67.         ft.replace(R.id.fContainer,myFrag);
  68.         //commit the change
  69.         ft.commit();
  70.     }
  71. }
  72.  
  73.  
  74. MyListAdapter.java
  75. ========================
  76. package com.example.zeevm.myfragment;
  77.  
  78. import android.content.Context;
  79. import android.view.View;
  80. import android.view.ViewGroup;
  81. import android.widget.BaseAdapter;
  82. import android.widget.TextView;
  83.  
  84. import org.w3c.dom.Text;
  85.  
  86. import java.util.List;
  87.  
  88. /**
  89.  * Created by zeevm on 31/10/2016.
  90.  */
  91.  
  92. public class MyListAdapter extends BaseAdapter {
  93.     List<String> myList;
  94.     Context context;
  95.  
  96.     public MyListAdapter(List<String> myList, Context context) {
  97.         this.myList = myList;
  98.         this.context = context;
  99.     }
  100.  
  101.     @Override
  102.     public int getCount() {
  103.         return myList.size();
  104.     }
  105.  
  106.     @Override
  107.     public Object getItem(int i) {
  108.         return null;
  109.     }
  110.  
  111.     @Override
  112.     public long getItemId(int i) {
  113.         return 0;
  114.     }
  115.  
  116.     @Override
  117.     public View getView(int i, View view, ViewGroup viewGroup) {
  118.         TextView myText = new TextView(context);
  119.         myText.setText(myList.get(i));
  120.         myText.setTextSize(30);
  121.         return myText;
  122.     }
  123. }
  124.  
  125.  
  126. fg1.java
  127. ====================
  128. package com.example.zeevm.myfragment;
  129.  
  130. import android.app.Fragment;
  131. import android.os.Bundle;
  132. import android.support.annotation.Nullable;
  133. import android.view.LayoutInflater;
  134. import android.view.View;
  135. import android.view.ViewGroup;
  136. import android.widget.ListView;
  137.  
  138. import java.util.ArrayList;
  139. import java.util.List;
  140.  
  141. /**
  142.  * Created by zeevm on 31/10/2016.
  143.  */
  144.  
  145. public class fg1 extends Fragment {
  146.     ListView myList;
  147.     List<String> students;
  148.  
  149.     public fg1(){} //required empty public constractur
  150.  
  151.     @Nullable
  152.     @Override
  153.     public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
  154.         //infalte the layout for this fragment
  155.         View rootView = inflater.inflate(R.layout.fg1,container,false);
  156.         //set pointer to myList
  157.         myList = (ListView)rootView.findViewById(R.id.fmyList);
  158.         //create our adapter to the list view
  159.         MyListAdapter adapter= new MyListAdapter(createList(),getActivity());
  160.         myList.setAdapter(adapter);
  161.         return rootView;
  162.     }
  163.  
  164.     private List<String> createList()
  165.     {
  166.         students = new ArrayList<>();
  167.         students.add("Adam Tuki");
  168.         students.add("Lorien");
  169.         students.add("Michal");
  170.         students.add("Noa Apchi");
  171.         students.add("Itzik Safug");
  172.         students.add("Elinor sheela");
  173.         students.add("Elad i late");
  174.         students.add("Dudi computer");
  175.         students.add("Faruk ad 10");
  176.         students.add("Caram no hebrew");
  177.         students.add("Pavel i want mac");
  178.  
  179.         return students;
  180.     }
  181. }
  182.  
  183.  
  184. fg2.java
  185. =======================
  186. package com.example.zeevm.myfragment;
  187.  
  188. import android.app.Fragment;
  189. import android.os.Bundle;
  190. import android.support.annotation.Nullable;
  191. import android.view.LayoutInflater;
  192. import android.view.View;
  193. import android.view.ViewGroup;
  194. import android.widget.Button;
  195. import android.widget.Toast;
  196.  
  197. /**
  198.  * Created by zeevm on 31/10/2016.
  199.  */
  200.  
  201. public class fg2 extends Fragment {
  202.     Button btnTest;
  203.  
  204.     @Nullable
  205.     @Override
  206.     public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
  207.        //Inflate the layout for this fragment
  208.         View rootView = inflater.inflate(R.layout.fg2,container,false);
  209.         //set pointer
  210.         btnTest=(Button)rootView.findViewById(R.id.myBtnTest);
  211.         //set on click listner
  212.         btnTest.setOnClickListener(new View.OnClickListener() {
  213.             @Override
  214.             public void onClick(View view) {
  215.                 Toast.makeText(getActivity(), "Adam Showarma/humus/chips/salat", Toast.LENGTH_SHORT).show();
  216.             }
  217.         });
  218.  
  219.         return rootView;
  220.     }
  221.  
  222.     public fg2(){}
  223. }
  224.  
  225.  
  226. fg3.java
  227. =====================
  228. package com.example.zeevm.myfragment;
  229.  
  230. import android.app.Fragment;
  231. import android.os.Bundle;
  232. import android.support.annotation.Nullable;
  233. import android.view.LayoutInflater;
  234. import android.view.View;
  235. import android.view.ViewGroup;
  236. import android.widget.TextView;
  237.  
  238. /**
  239.  * Created by zeevm on 31/10/2016.
  240.  */
  241.  
  242. public class fg3 extends Fragment {
  243.     @Nullable
  244.     @Override
  245.     public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
  246.         //inflate the view
  247.         View rootView = inflater.inflate(R.layout.fg3,container,false);
  248.         TextView myTxt=(TextView)rootView.findViewById(R.id.fg3Text);
  249.         myTxt.setText("Elinor Sheela!!!");
  250.  
  251.         return rootView;
  252.     }
  253.  
  254.     public fg3(){}
  255. }
  256.  
  257.  
  258.  
  259. activity_main.xml
  260. =========================
  261. <?xml version="1.0" encoding="utf-8"?>
  262. <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
  263.     xmlns:tools="http://schemas.android.com/tools"
  264.     android:id="@+id/activity_main"
  265.     android:layout_width="match_parent"
  266.     android:layout_height="match_parent"
  267.     android:paddingBottom="@dimen/activity_vertical_margin"
  268.     android:paddingLeft="@dimen/activity_horizontal_margin"
  269.     android:paddingRight="@dimen/activity_horizontal_margin"
  270.     android:paddingTop="@dimen/activity_vertical_margin"
  271.     tools:context="com.example.zeevm.myfragment.MainActivity">
  272.  
  273.    <LinearLayout
  274.        android:layout_width="match_parent"
  275.        android:layout_height="wrap_content"
  276.        android:orientation="horizontal"
  277.        android:id="@+id/fButton">
  278.        <Button
  279.            android:layout_width="0dp"
  280.            android:layout_height="wrap_content"
  281.            android:layout_weight="1"
  282.            android:text="Fragment 1"
  283.            android:onClick="onClick1"/>
  284.        <Button
  285.            android:layout_width="0dp"
  286.            android:layout_height="wrap_content"
  287.            android:layout_weight="1"
  288.            android:text="Fragment 2"
  289.            android:onClick="onClick2"/>
  290.        <Button
  291.            android:layout_width="0dp"
  292.            android:layout_height="wrap_content"
  293.            android:layout_weight="1"
  294.            android:text="Fragment 3"
  295.            android:onClick="onClick3"/>
  296.    </LinearLayout>
  297.  
  298.     <LinearLayout
  299.         android:layout_width="match_parent"
  300.         android:layout_height="match_parent"
  301.         android:id="@+id/fContainer"
  302.         android:orientation="vertical"
  303.         android:layout_below="@id/fButton"
  304.         android:layout_margin="20dp">
  305.  
  306.     </LinearLayout>
  307. </RelativeLayout>
  308.  
  309.  
  310. fg1.xml
  311. =====================
  312. <?xml version="1.0" encoding="utf-8"?>
  313. <FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
  314.     android:layout_width="match_parent"
  315.     android:layout_height="match_parent">
  316.  
  317.     <ListView
  318.         android:layout_width="match_parent"
  319.         android:layout_height="match_parent"
  320.         android:id="@+id/fmyList"/>
  321. </FrameLayout>
  322.  
  323.  
  324.  
  325.  
  326. fg2.xml
  327. ====================
  328. <?xml version="1.0" encoding="utf-8"?>
  329. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  330.     android:orientation="vertical" android:layout_width="match_parent"
  331.     android:layout_height="match_parent">
  332.  
  333.     <Button
  334.         android:layout_width="match_parent"
  335.         android:layout_height="wrap_content"
  336.         android:text="PRESS ME"
  337.         android:background="#009fff"
  338.         android:textColor="#fff"
  339.         android:layout_marginTop="100dp"
  340.         android:id="@+id/myBtnTest"/>
  341. </LinearLayout>
  342.  
  343.  
  344.  
  345.  
  346. fg3.xml
  347. =================
  348. <?xml version="1.0" encoding="utf-8"?>
  349. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  350.     android:orientation="vertical" android:layout_width="match_parent"
  351.     android:layout_height="match_parent">
  352.  
  353.     <TextView
  354.         android:layout_width="match_parent"
  355.         android:layout_height="match_parent"
  356.         android:text="I am Adam !!!!!"
  357.         android:id="@+id/fg3Text"
  358.         android:gravity="center"
  359.         android:textSize="50sp"/>
  360.  
  361. </LinearLayout>
Add Comment
Please, Sign In to add comment