Skip to content

Commit 764ec9f

Browse files
author
Daniel Novak
committed
Add viewpager sample
1 parent 760ab00 commit 764ec9f

File tree

10 files changed

+147
-0
lines changed

10 files changed

+147
-0
lines changed

sample/src/main/AndroidManifest.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
<category android:name="android.intent.category.LAUNCHER" />
1818
</intent-filter>
1919
</activity>
20+
<activity android:name=".activity.ViewPagerActivity"/>
2021
</application>
2122

2223
</manifest>
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
package eu.inloop.viewmodel.sample.activity;
2+
3+
import android.os.Bundle;
4+
import android.support.v4.app.Fragment;
5+
import android.support.v4.app.FragmentManager;
6+
import android.support.v4.app.FragmentStatePagerAdapter;
7+
import android.support.v4.view.ViewPager;
8+
import android.support.v7.app.AppCompatActivity;
9+
10+
import eu.inloop.viewmodel.sample.R;
11+
import eu.inloop.viewmodel.sample.fragment.PagerFragment;
12+
13+
public class ViewPagerActivity extends AppCompatActivity {
14+
15+
@Override
16+
protected void onCreate(Bundle savedInstanceState) {
17+
super.onCreate(savedInstanceState);
18+
setContentView(R.layout.activity_pager);
19+
20+
final ViewPager viewPager = (ViewPager) findViewById(R.id.pager);
21+
viewPager.setAdapter(new TestPagerAdapter(getSupportFragmentManager()));
22+
}
23+
24+
private final static class TestPagerAdapter extends FragmentStatePagerAdapter {
25+
public TestPagerAdapter(FragmentManager fm) {
26+
super(fm);
27+
}
28+
29+
@Override
30+
public Fragment getItem(int position) {
31+
return PagerFragment.newInstance(position);
32+
}
33+
34+
@Override
35+
public int getCount() {
36+
return 10;
37+
}
38+
}
39+
}
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
package eu.inloop.viewmodel.sample.fragment;
2+
3+
import android.os.Bundle;
4+
import android.support.annotation.Nullable;
5+
import android.support.v4.app.Fragment;
6+
import android.view.LayoutInflater;
7+
import android.view.View;
8+
import android.view.ViewGroup;
9+
import android.widget.TextView;
10+
11+
import com.squareup.leakcanary.RefWatcher;
12+
13+
import eu.inloop.viewmodel.base.ViewModelBaseFragment;
14+
import eu.inloop.viewmodel.sample.R;
15+
import eu.inloop.viewmodel.sample.SampleApplication;
16+
import eu.inloop.viewmodel.sample.viewmodel.PageModel;
17+
import eu.inloop.viewmodel.sample.viewmodel.view.IPageView;
18+
19+
public class PagerFragment extends ViewModelBaseFragment<IPageView, PageModel> {
20+
21+
public static PagerFragment newInstance(int position) {
22+
final Bundle bundle = new Bundle();
23+
bundle.putInt("position", position);
24+
final PagerFragment fragment = new PagerFragment();
25+
fragment.setArguments(bundle);
26+
return fragment;
27+
}
28+
29+
@Nullable
30+
@Override
31+
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
32+
return inflater.inflate(R.layout.fragment_pager, container, false);
33+
}
34+
35+
@Override
36+
public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
37+
super.onViewCreated(view, savedInstanceState);
38+
((TextView)view.findViewById(R.id.text)).setText(Integer.toString(getArguments().getInt("position")));
39+
}
40+
41+
@Override
42+
public Class<PageModel> getViewModelClass() {
43+
return PageModel.class;
44+
}
45+
46+
@Override
47+
public void onDestroy() {
48+
super.onDestroy();
49+
50+
// watch for memory leaks
51+
RefWatcher refWatcher = SampleApplication.getRefWatcher(getActivity());
52+
refWatcher.watch(this);
53+
}
54+
}

sample/src/main/java/eu/inloop/viewmodel/sample/fragment/UserListFragment.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package eu.inloop.viewmodel.sample.fragment;
22

3+
import android.content.Intent;
34
import android.os.Bundle;
45
import android.support.annotation.Nullable;
56
import android.view.LayoutInflater;
@@ -20,6 +21,7 @@
2021
import eu.inloop.viewmodel.base.ViewModelBaseFragment;
2122
import eu.inloop.viewmodel.sample.R;
2223
import eu.inloop.viewmodel.sample.SampleApplication;
24+
import eu.inloop.viewmodel.sample.activity.ViewPagerActivity;
2325
import eu.inloop.viewmodel.sample.viewmodel.UserListViewModel;
2426
import eu.inloop.viewmodel.sample.viewmodel.view.IUserListView;
2527

@@ -65,6 +67,12 @@ public void onClick(View view) {
6567
getActivity().startActivity(getActivity().getIntent());
6668
}
6769
});
70+
headerView.findViewById(R.id.button3).setOnClickListener(new View.OnClickListener() {
71+
@Override
72+
public void onClick(View view) {
73+
startActivity(new Intent(getContext(), ViewPagerActivity.class));
74+
}
75+
});
6876
mListview.addHeaderView(headerView, null, false);
6977
return view;
7078
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
package eu.inloop.viewmodel.sample.viewmodel;
2+
3+
import eu.inloop.viewmodel.AbstractViewModel;
4+
import eu.inloop.viewmodel.sample.viewmodel.view.IPageView;
5+
6+
public class PageModel extends AbstractViewModel<IPageView> {
7+
}
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
package eu.inloop.viewmodel.sample.viewmodel.view;
2+
3+
import eu.inloop.viewmodel.IView;
4+
5+
public class IPageView implements IView {
6+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
3+
android:orientation="vertical" android:layout_width="match_parent"
4+
android:layout_height="match_parent">
5+
6+
<android.support.v4.view.ViewPager
7+
android:id="@+id/pager"
8+
android:layout_width="match_parent"
9+
android:layout_height="match_parent"/>
10+
11+
</LinearLayout>
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
3+
android:orientation="vertical" android:layout_width="match_parent"
4+
android:layout_height="match_parent"
5+
android:gravity="center">
6+
7+
<TextView
8+
android:id="@+id/text"
9+
android:layout_width="wrap_content"
10+
android:layout_height="wrap_content"
11+
android:textSize="30sp"
12+
/>
13+
14+
</LinearLayout>

sample/src/main/res/layout/view_header_info.xml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,4 +22,10 @@
2222
android:text="@string/userlist_close_activity"
2323
android:layout_height="wrap_content" />
2424

25+
<Button
26+
android:id="@+id/button3"
27+
android:layout_width="match_parent"
28+
android:text="@string/userlist_open_fragment_pager"
29+
android:layout_height="wrap_content" />
30+
2531
</LinearLayout>

sample/src/main/res/values/strings.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
The result will be kept in the ViewModel memory. You can also schedule item deletion by clicking on a row.</string>
99
<string name="userlist_open_next_fragment">Open next fragment in backstack</string>
1010
<string name="userlist_close_activity">Restart activity</string>
11+
<string name="userlist_open_fragment_pager">Open fragment pager</string>
1112
<string name="empty_fragment_text">Test</string>
1213

1314
</resources>

0 commit comments

Comments
 (0)