This commit is contained in:
Paul 2020-11-01 15:52:43 +01:00
parent c129b87b10
commit a4b29c7bfb
12 changed files with 210 additions and 41 deletions

View File

@ -1,2 +1,5 @@
Name: Paul Lödige
Matrikel: 15405036
Hinweis:
Der ViewPager ist im Slideshow-Fragment des NavigationDrawers implementiert

View File

@ -37,6 +37,7 @@ dependencies {
implementation 'androidx.navigation:navigation-ui:2.3.1'
implementation 'androidx.lifecycle:lifecycle-livedata-ktx:2.2.0'
implementation 'androidx.lifecycle:lifecycle-viewmodel-ktx:2.2.0'
implementation 'androidx.legacy:legacy-support-v4:1.0.0'
testImplementation 'junit:junit:4.+'
androidTestImplementation 'androidx.test.ext:junit:1.1.2'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.3.0'

View File

@ -1,3 +1,7 @@
/**
* @author Paul Lödige (Matrikel: 15405036)
*/
package com.ploedige.navdrawertest;
import android.os.Bundle;

View File

@ -1,3 +1,7 @@
/**
* @author Paul Lödige (Matrikel: 15405036)
*/
package com.ploedige.navdrawertest.ui.slideshow;
import android.os.Bundle;
@ -11,25 +15,23 @@ import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.lifecycle.Observer;
import androidx.lifecycle.ViewModelProvider;
import androidx.viewpager.widget.ViewPager;
import com.ploedige.navdrawertest.R;
public class SlideshowFragment extends Fragment {
private SlideshowViewModel slideshowViewModel;
TestPagerAdapter testPagerAdapter;
ViewPager viewPager;
public View onCreateView(@NonNull LayoutInflater inflater,
ViewGroup container, Bundle savedInstanceState) {
slideshowViewModel =
new ViewModelProvider(this).get(SlideshowViewModel.class);
View root = inflater.inflate(R.layout.fragment_slideshow, container, false);
final TextView textView = root.findViewById(R.id.text_slideshow);
slideshowViewModel.getText().observe(getViewLifecycleOwner(), new Observer<String>() {
@Override
public void onChanged(@Nullable String s) {
textView.setText(s);
}
});
testPagerAdapter = new TestPagerAdapter(getFragmentManager());
viewPager = (ViewPager) root.findViewById(R.id.pager);
viewPager.setAdapter(testPagerAdapter);
return root;
}
}

View File

@ -1,19 +0,0 @@
package com.ploedige.navdrawertest.ui.slideshow;
import androidx.lifecycle.LiveData;
import androidx.lifecycle.MutableLiveData;
import androidx.lifecycle.ViewModel;
public class SlideshowViewModel extends ViewModel {
private MutableLiveData<String> mText;
public SlideshowViewModel() {
mText = new MutableLiveData<>();
mText.setValue("This is slideshow fragment");
}
public LiveData<String> getText() {
return mText;
}
}

View File

@ -0,0 +1,58 @@
/**
* @author Paul Lödige (Matrikel: 15405036)
*/
package com.ploedige.navdrawertest.ui.slideshow;
import android.util.Log;
import androidx.annotation.NonNull;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentPagerAdapter;
import androidx.fragment.app.FragmentStatePagerAdapter;
import com.ploedige.navdrawertest.ui.slideshow.fragments.SwipeFragment1;
import com.ploedige.navdrawertest.ui.slideshow.fragments.SwipeFragment2;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
public class TestPagerAdapter extends FragmentStatePagerAdapter {
private Map<String, Class<? extends Fragment>> fragments = new HashMap();
public TestPagerAdapter(FragmentManager fm){
super(fm);
// add all the Classes for the SwipeView
fragments.put("Fragment 1", SwipeFragment1.class);
fragments.put("Fragment 2",SwipeFragment2.class);
}
@NonNull
@Override
public Fragment getItem(int position) {
if(position >= fragments.size()){
Log.v("TestPagerAdapter","Position is outside of Array.");
return new SwipeFragment1();
}
try {
return (Fragment) fragments.get(fragments.keySet().toArray()[position]).newInstance();
} catch (Exception e) {
e.printStackTrace();
return new SwipeFragment1();
}
}
@Override
public int getCount() {
return fragments.size();
}
@Override
public CharSequence getPageTitle(int position){
return fragments.keySet().toArray()[position].toString();
}
}

View File

@ -0,0 +1,35 @@
/**
* @author Paul Lödige (Matrikel: 15405036)
*/
package com.ploedige.navdrawertest.ui.slideshow.fragments;
import android.os.Bundle;
import androidx.fragment.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import com.ploedige.navdrawertest.R;
public class SwipeFragment1 extends Fragment {
public static SwipeFragment1 newInstance(String param1, String param2) {
SwipeFragment1 fragment = new SwipeFragment1();
return 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_swipe1, container, false);
}
}

View File

@ -0,0 +1,35 @@
/**
* @author Paul Lödige (Matrikel: 15405036)
*/
package com.ploedige.navdrawertest.ui.slideshow.fragments;
import android.os.Bundle;
import androidx.fragment.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import com.ploedige.navdrawertest.R;
public class SwipeFragment2 extends Fragment {
public static SwipeFragment2 newInstance(String param1, String param2) {
SwipeFragment2 fragment = new SwipeFragment2();
return 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_swipe2, container, false);
}
}

View File

@ -6,17 +6,15 @@
android:layout_height="match_parent"
tools:context=".ui.slideshow.SlideshowFragment">
<TextView
android:id="@+id/text_slideshow"
<androidx.viewpager.widget.ViewPager
android:id="@+id/pager"
android:layout_width="match_parent"
android:layout_height="match_parent">
<androidx.viewpager.widget.PagerTabStrip
android:id="@+id/pts"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
android:layout_marginEnd="8dp"
android:textAlignment="center"
android:textSize="20sp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
android:background="@color/design_default_color_secondary"/>
</androidx.viewpager.widget.ViewPager>
</androidx.constraintlayout.widget.ConstraintLayout>

View File

@ -0,0 +1,25 @@
<?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"
xmlns:app="http://schemas.android.com/apk/res-auto"
tools:context=".ui.slideshow.fragments.SwipeFragment1">
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:text="This is SwipeFragment1"
android:textSize="5mm"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"/>
</androidx.constraintlayout.widget.ConstraintLayout>
</FrameLayout>

View File

@ -0,0 +1,25 @@
<?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"
xmlns:app="http://schemas.android.com/apk/res-auto"
tools:context=".ui.slideshow.fragments.SwipeFragment2">
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:text="This is SwipeFragment2"
android:textSize="5mm"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"/>
</androidx.constraintlayout.widget.ConstraintLayout>
</FrameLayout>

View File

@ -10,4 +10,6 @@
<string name="menu_home">Home</string>
<string name="menu_gallery">Gallery</string>
<string name="menu_slideshow">Slideshow</string>
<!-- TODO: Remove or change this placeholder text -->
<string name="hello_blank_fragment">Hello blank fragment</string>
</resources>