From d2551ce734a88d69a832ffe962d168f5bba03d2f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Yi=C4=9Fit=20=C3=87olako=C4=9Flu?= Date: Sun, 31 Mar 2019 01:01:27 +0300 Subject: [PATCH] Done tabbing --- .../java/gq/yigit/mycity/MainActivity.java | 6 +- ...yFragment.java => UtilityElectricity.java} | 98 ++++-------- .../mycity/utilityFragment/UtilityMain.java | 143 ++++++++++++++++++ .../src/main/res/layout/fragment_utility.xml | 91 ++--------- .../layout/fragment_utility_electricity.xml | 80 ++++++++++ 5 files changed, 267 insertions(+), 151 deletions(-) rename MyCity/app/src/main/java/gq/yigit/mycity/utilityFragment/{UtilityFragment.java => UtilityElectricity.java} (57%) create mode 100644 MyCity/app/src/main/java/gq/yigit/mycity/utilityFragment/UtilityMain.java create mode 100644 MyCity/app/src/main/res/layout/fragment_utility_electricity.xml diff --git a/MyCity/app/src/main/java/gq/yigit/mycity/MainActivity.java b/MyCity/app/src/main/java/gq/yigit/mycity/MainActivity.java index b5c52c2..0635824 100644 --- a/MyCity/app/src/main/java/gq/yigit/mycity/MainActivity.java +++ b/MyCity/app/src/main/java/gq/yigit/mycity/MainActivity.java @@ -27,7 +27,7 @@ import android.widget.Toast; import gq.yigit.mycity.navigation.MapsFragment; import gq.yigit.mycity.tools.*; import gq.yigit.mycity.tools.WebRequest.responseListener; -import gq.yigit.mycity.utilityFragment.UtilityFragment; +import gq.yigit.mycity.utilityFragment.UtilityMain; import gq.yigit.mycity.votesFragment.VoteFragment; import gq.yigit.mycity.votesFragment.VotesContent; import gq.yigit.mycity.votesFragment.VotesFragment; @@ -47,7 +47,7 @@ public class MainActivity extends AppCompatActivity MainFragment.OnFragmentInteractionListener, RateFragment.OnFragmentInteractionListener, MapsFragment.OnFragmentInteractionListener, - UtilityFragment.OnFragmentInteractionListener, + UtilityMain.OnFragmentInteractionListener, OnFragmentInteractionListener, responseListener, imageListener { @@ -185,7 +185,7 @@ public class MainActivity extends AppCompatActivity fragmentTransaction.commit(); fragmentTransaction.addToBackStack(null); } else if (id == R.id.utilities) { - UtilityFragment fragment = new UtilityFragment(); + UtilityMain fragment = new UtilityMain(); fragmentTransaction.replace(R.id.app_bar_main, fragment); fragmentTransaction.commit(); fragmentTransaction.addToBackStack(null); diff --git a/MyCity/app/src/main/java/gq/yigit/mycity/utilityFragment/UtilityFragment.java b/MyCity/app/src/main/java/gq/yigit/mycity/utilityFragment/UtilityElectricity.java similarity index 57% rename from MyCity/app/src/main/java/gq/yigit/mycity/utilityFragment/UtilityFragment.java rename to MyCity/app/src/main/java/gq/yigit/mycity/utilityFragment/UtilityElectricity.java index faee840..5395d9c 100644 --- a/MyCity/app/src/main/java/gq/yigit/mycity/utilityFragment/UtilityFragment.java +++ b/MyCity/app/src/main/java/gq/yigit/mycity/utilityFragment/UtilityElectricity.java @@ -4,12 +4,7 @@ import android.content.Context; import android.graphics.Color; import android.net.Uri; import android.os.Bundle; -import android.support.design.widget.TabLayout; import android.support.v4.app.Fragment; -import android.support.v4.app.FragmentManager; -import android.support.v4.app.FragmentPagerAdapter; -import android.support.v4.view.ViewPager; -import android.support.v7.widget.RecyclerView; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; @@ -20,23 +15,31 @@ import com.jjoe64.graphview.series.DataPoint; import com.jjoe64.graphview.series.LineGraphSeries; import gq.yigit.mycity.R; -import java.util.ArrayList; -import java.util.List; - - -public class UtilityFragment extends Fragment { +/** + * A simple {@link Fragment} subclass. + * Activities that contain this fragment must implement the + * {@link UtilityElectricity.OnFragmentInteractionListener} interface + * to handle interaction events. + * Use the {@link UtilityElectricity#newInstance} factory method to + * create an instance of this fragment. + */ +public class UtilityElectricity extends Fragment { + // TODO: Rename parameter arguments, choose names that match + // the fragment initialization parameters, e.g. ARG_ITEM_NUMBER + // TODO: Rename and change types of parameters private OnFragmentInteractionListener mListener; - public UtilityFragment() { + public UtilityElectricity() { + // Required empty public constructor } - public static UtilityFragment newInstance(String param1, String param2) { - UtilityFragment fragment = new UtilityFragment(); + public static UtilityElectricity newInstance(String param1, String param2) { + UtilityElectricity fragment = new UtilityElectricity(); return fragment; } @@ -49,7 +52,8 @@ public class UtilityFragment extends Fragment { @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { - View rootView = inflater.inflate(R.layout.fragment_utility, container, false); + // Inflate the layout for this fragment + View rootView = inflater.inflate(R.layout.fragment_utility_electricity, container, false);; GraphView graph = (GraphView) rootView.findViewById(R.id.utility_graph); LineGraphSeries series = new LineGraphSeries<>(new DataPoint[] { new DataPoint(0, 1), @@ -74,41 +78,23 @@ public class UtilityFragment extends Fragment { graph.addSeries(series); graph.addSeries(series2); - ViewPager viewPager = (ViewPager) rootView.findViewById(R.id.viewpager); - setupViewPager(viewPager); - - TabLayout tabs = (TabLayout) rootView.findViewById(R.id.result_tabs); - tabs.setupWithViewPager(viewPager); - return rootView; } + // TODO: Rename method, update argument and hook method into UI event public void onButtonPressed(Uri uri) { if (mListener != null) { mListener.onFragmentInteraction(uri); } } - private void setupViewPager(ViewPager viewPager) { - - - Adapter adapter = new Adapter(getChildFragmentManager()); - adapter.addFragment(new UtilityFragment(), "Electricity"); - viewPager.setAdapter(adapter); - - - - } - - @Override public void onAttach(Context context) { super.onAttach(context); if (context instanceof OnFragmentInteractionListener) { mListener = (OnFragmentInteractionListener) context; } else { - throw new RuntimeException(context.toString() - + " must implement OnFragmentInteractionListener"); + } } @@ -118,42 +104,18 @@ public class UtilityFragment extends Fragment { mListener = null; } + /** + * This interface must be implemented by activities that contain this + * fragment to allow an interaction in this fragment to be communicated + * to the activity and potentially other fragments contained in that + * activity. + *

+ * See the Android Training lesson Communicating with Other Fragments for more information. + */ public interface OnFragmentInteractionListener { // TODO: Update argument type and name void onFragmentInteraction(Uri uri); } } - - -class Adapter extends FragmentPagerAdapter { - private final List mFragmentList = new ArrayList<>(); - private final List mFragmentTitleList = new ArrayList<>(); - - public Adapter(FragmentManager manager) { - super(manager); - } - - @Override - public Fragment getItem(int position) { - return mFragmentList.get(position); - } - - @Override - public int getCount() { - return mFragmentList.size(); - } - - public void addFragment(Fragment fragment, String title) { - mFragmentList.add(fragment); - mFragmentTitleList.add(title); - } - - @Override - public CharSequence getPageTitle(int position) { - return mFragmentTitleList.get(position); - } -} - - - - diff --git a/MyCity/app/src/main/java/gq/yigit/mycity/utilityFragment/UtilityMain.java b/MyCity/app/src/main/java/gq/yigit/mycity/utilityFragment/UtilityMain.java new file mode 100644 index 0000000..108f598 --- /dev/null +++ b/MyCity/app/src/main/java/gq/yigit/mycity/utilityFragment/UtilityMain.java @@ -0,0 +1,143 @@ +package gq.yigit.mycity.utilityFragment; + +import android.content.Context; +import android.graphics.Color; +import android.net.Uri; +import android.os.Bundle; +import android.support.design.widget.TabLayout; +import android.support.v4.app.Fragment; +import android.support.v4.app.FragmentManager; +import android.support.v4.app.FragmentPagerAdapter; +import android.support.v4.view.ViewPager; +import android.view.LayoutInflater; +import android.view.View; +import android.view.ViewGroup; + +import com.jjoe64.graphview.GraphView; +import com.jjoe64.graphview.helper.StaticLabelsFormatter; +import com.jjoe64.graphview.series.DataPoint; +import com.jjoe64.graphview.series.LineGraphSeries; +import gq.yigit.mycity.R; + +import java.util.ArrayList; +import java.util.List; + + +public class UtilityMain extends Fragment { + + + + + private OnFragmentInteractionListener mListener; + + public UtilityMain() { + } + + + public static UtilityMain newInstance(String param1, String param2) { + UtilityMain fragment = new UtilityMain(); + return fragment; + } + + @Override + public void onCreate(Bundle savedInstanceState) { + super.onCreate(savedInstanceState); + + } + + @Override + public View onCreateView(LayoutInflater inflater, ViewGroup container, + Bundle savedInstanceState) { + View rootView = inflater.inflate(R.layout.fragment_utility, container, false); + + + + // Setting ViewPager for each Tabs + ViewPager viewPager = (ViewPager) rootView.findViewById(R.id.viewpager); + setupViewPager(viewPager); + // Set Tabs inside Toolbar + TabLayout tabs = (TabLayout) rootView.findViewById(R.id.result_tabs); + tabs.setupWithViewPager(viewPager); + + + return rootView; + } + + public void onButtonPressed(Uri uri) { + if (mListener != null) { + mListener.onFragmentInteraction(uri); + } + } + + private void setupViewPager(ViewPager viewPager) { + + + Adapter adapter = new Adapter(getChildFragmentManager()); + adapter.addFragment(new UtilityElectricity(), "Electricity"); + viewPager.setAdapter(adapter); + + + + } + + + @Override + public void onAttach(Context context) { + super.onAttach(context); + if (context instanceof OnFragmentInteractionListener) { + mListener = (OnFragmentInteractionListener) context; + } else { + throw new RuntimeException(context.toString() + + " must implement OnFragmentInteractionListener"); + } + } + + @Override + public void onDetach() { + super.onDetach(); + mListener = null; + } + + public interface OnFragmentInteractionListener { + // TODO: Update argument type and name + void onFragmentInteraction(Uri uri); + } + + static class Adapter extends FragmentPagerAdapter { + private final List mFragmentList = new ArrayList<>(); + private final List mFragmentTitleList = new ArrayList<>(); + + public Adapter(FragmentManager manager) { + super(manager); + } + + @Override + public Fragment getItem(int position) { + return mFragmentList.get(position); + } + + @Override + public int getCount() { + return mFragmentList.size(); + } + + public void addFragment(Fragment fragment, String title) { + mFragmentList.add(fragment); + mFragmentTitleList.add(title); + } + + @Override + public CharSequence getPageTitle(int position) { + return mFragmentTitleList.get(position); + } + } +} + + + + + + + + + diff --git a/MyCity/app/src/main/res/layout/fragment_utility.xml b/MyCity/app/src/main/res/layout/fragment_utility.xml index b47f5c2..8e95fe6 100644 --- a/MyCity/app/src/main/res/layout/fragment_utility.xml +++ b/MyCity/app/src/main/res/layout/fragment_utility.xml @@ -1,11 +1,13 @@ - + + + android:layout_height="wrap_content" + app:layout_behavior="@string/appbar_scrolling_view_behavior"> + - - - - - - - - - - - - - - - -