Added TabLayout/Viewpager to About-Fragment

Removed Padding (should be in every single Fragment)
This commit is contained in:
Xcreen
2018-10-02 12:22:50 +02:00
parent 09224c6032
commit 7d7bdb2dc7
8 changed files with 131 additions and 7 deletions

View File

@@ -1,13 +1,25 @@
package net.xcreen.restsms;
import androidx.appcompat.widget.LinearLayoutCompat;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentStatePagerAdapter;
import androidx.viewpager.widget.ViewPager;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import com.google.android.material.appbar.AppBarLayout;
import com.google.android.material.tabs.TabLayout;
public class AboutFragment extends Fragment {
private TabLayout tabLayout;
private ViewPager viewPager;
private AppBarLayout mainAppBarLayout;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
@@ -15,7 +27,74 @@ public class AboutFragment extends Fragment {
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
// Inflate the layout for this fragment
return inflater.inflate(R.layout.fragment_about, container, false);
View rootView = inflater.inflate(R.layout.fragment_about, container, false);
//Set Tabs
tabLayout = new TabLayout(getActivity());
tabLayout.addTab(tabLayout.newTab().setText(getResources().getText(R.string.about_about_me)));
tabLayout.addTab(tabLayout.newTab().setText(getResources().getText(R.string.about_app_information)));
tabLayout.addTab(tabLayout.newTab().setText(getResources().getText(R.string.about_third_party_librarys)));
tabLayout.setTabGravity(TabLayout.GRAVITY_FILL);
tabLayout.setBackgroundColor(getResources().getColor(R.color.colorDarkBlack, null));
tabLayout.setTabTextColors(getResources().getColor(R.color.colorWhite, null), getResources().getColor(R.color.colorLightBlue, null));
tabLayout.addOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
@Override
public void onTabSelected(TabLayout.Tab tab) {
viewPager.setCurrentItem(tab.getPosition());
}
@Override
public void onTabUnselected(TabLayout.Tab tab) { }
@Override
public void onTabReselected(TabLayout.Tab tab) { }
});
//Add TabLayout to the AppBarLayout
mainAppBarLayout = getActivity().findViewById(R.id.appbar_layout);
mainAppBarLayout.addView(tabLayout, new LinearLayoutCompat.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT));
//Set Page-Adapter to ViewPager
FragmentPageAdapter pagerAdapter = new FragmentPageAdapter(getFragmentManager(), tabLayout.getTabCount());
viewPager = rootView.findViewById(R.id.about_view_pager);
viewPager.setAdapter(pagerAdapter);
viewPager.addOnPageChangeListener(new TabLayout.TabLayoutOnPageChangeListener(tabLayout));
return rootView;
}
@Override
public void onStop() {
super.onStop();
//Remove Tab-Layout
mainAppBarLayout.removeView(tabLayout);
}
}
class FragmentPageAdapter extends FragmentStatePagerAdapter {
private int numberOfTabs;
public FragmentPageAdapter(FragmentManager fm, int numberOfTabs) {
super(fm);
this.numberOfTabs = numberOfTabs;
}
@Override
public Fragment getItem(int position) {
switch (position) {
case 0:
return new AboutMeFragment();
case 1:
return new AboutMeFragment();
case 2:
return new AboutMeFragment();
default:
return null;
}
}
@Override
public int getCount() {
return numberOfTabs;
}
}

View File

@@ -0,0 +1,25 @@
package net.xcreen.restsms;
import android.os.Bundle;
import androidx.fragment.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
public class AboutMeFragment extends Fragment {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
return inflater.inflate(R.layout.fragment_about_me, container, false);
}
}

View File

@@ -22,6 +22,7 @@
android:orientation="vertical">
<com.google.android.material.appbar.AppBarLayout
android:id="@+id/appbar_layout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:theme="@style/AppTheme.AppBarOverlay">
@@ -39,8 +40,7 @@
android:id="@+id/main_framelayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/colorLightBlack"
android:padding="@dimen/fragment_padding"/>
android:background="@color/colorLightBlack" />
</LinearLayout>

View File

@@ -5,9 +5,9 @@
android:layout_height="match_parent"
tools:context=".AboutFragment">
<TextView
<androidx.viewpager.widget.ViewPager
android:id="@+id/about_view_pager"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="ABOUT" />
android:layout_height="match_parent" />
</FrameLayout>

View File

@@ -0,0 +1,14 @@
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="@dimen/fragment_padding"
tools:context=".AboutMeFragment">
<TextView
android:id="@+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="TextView" />
</FrameLayout>

View File

@@ -4,6 +4,7 @@
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="@dimen/fragment_padding"
tools:context=".HomeFragment">
<ScrollView

View File

@@ -3,6 +3,7 @@
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="@dimen/fragment_padding"
tools:context=".SettingsFragment">
<TextView

View File

@@ -13,4 +13,8 @@
<string name="server_failed_to_start">Server failed to start!</string>
<string name="invalid_sim">Sim-Card is missing or not ready to send sms!</string>
<string name="no_sms_permission">SMS-Permission is required to run the server!</string>
<string name="about_about_me">About Me</string>
<string name="about_app_information">App-Information</string>
<string name="about_third_party_librarys">Third-Party Librarys</string>
</resources>