Created
September 12, 2017 22:14
-
-
Save zizibaloob/074af39fdb87c69ddd23142d86ab9918 to your computer and use it in GitHub Desktop.
Tiny app to demonstrate PagerAdapter
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?xml version="1.0" encoding="utf-8"?> | |
<android.support.v4.view.ViewPager | |
xmlns:android="http://schemas.android.com/apk/res/android" | |
android:id="@+id/pager" | |
android:layout_width="match_parent" | |
android:layout_height="match_parent" | |
android:background="#eee"/> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?xml version="1.0" encoding="utf-8"?> | |
<android.support.v7.widget.CardView | |
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"> | |
<TextView | |
android:layout_width="match_parent" | |
android:layout_height="match_parent" | |
android:padding="8dp" | |
android:gravity="center" | |
tools:text="hello world"/> | |
</android.support.v7.widget.CardView> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.example.stackoverflow; | |
import android.os.Bundle; | |
import android.support.constraint.ConstraintLayout; | |
import android.support.v4.view.PagerAdapter; | |
import android.support.v4.view.ViewPager; | |
import android.support.v7.app.AppCompatActivity; | |
import android.view.LayoutInflater; | |
import android.view.View; | |
import android.view.ViewGroup; | |
import android.widget.TextView; | |
import java.util.Arrays; | |
import java.util.List; | |
public class MainActivity extends AppCompatActivity { | |
@Override | |
protected void onCreate(Bundle savedInstanceState) { | |
super.onCreate(savedInstanceState); | |
setContentView(R.layout.activity_main); | |
List<String> items = Arrays.asList("one", "two", "three", "four", "five", "six", "seven", "eight", "nine"); | |
ViewPager pager = findViewById(R.id.pager); | |
pager.setAdapter(new MyAdapter(items)); | |
} | |
private static class MyAdapter extends PagerAdapter { | |
private final List<String> items; | |
private MyAdapter(List<String> items) { | |
this.items = items; | |
} | |
@Override | |
public int getCount() { | |
return (items != null) | |
? ((items.size() - 1) / 6) + 1 | |
: 0; | |
} | |
@Override | |
public Object instantiateItem(ViewGroup container, int pageNumber) { | |
LayoutInflater inflater = LayoutInflater.from(container.getContext()); | |
ConstraintLayout page = (ConstraintLayout) inflater.inflate(R.layout.page, container, false); | |
for (int i = 0; i < 6; ++i) { | |
View child = page.getChildAt(i); | |
int position = (pageNumber * 6) + i; | |
if (position < items.size()) { | |
TextView text = (TextView) ((ViewGroup) child).getChildAt(0); | |
text.setText(items.get(position)); | |
} | |
else { | |
child.setVisibility(View.GONE); | |
} | |
} | |
container.addView(page); | |
return page; | |
} | |
@Override | |
public void destroyItem(ViewGroup container, int position, Object key) { | |
container.removeView((View) key); | |
} | |
@Override | |
public boolean isViewFromObject(View view, Object key) { | |
return view == key; | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?xml version="1.0" encoding="utf-8"?> | |
<android.support.constraint.ConstraintLayout | |
xmlns:android="http://schemas.android.com/apk/res/android" | |
xmlns:app="http://schemas.android.com/apk/res-auto" | |
android:layout_width="match_parent" | |
android:layout_height="match_parent" | |
android:padding="6dp"> | |
<include | |
layout="@layout/card" | |
android:layout_width="0dp" | |
android:layout_height="0dp" | |
android:layout_margin="6dp" | |
app:layout_constraintTop_toTopOf="parent" | |
app:layout_constraintLeft_toLeftOf="parent" | |
app:layout_constraintRight_toRightOf="@+id/verticalGuideline" | |
app:layout_constraintBottom_toBottomOf="@+id/horizontalGuidelineTop"/> | |
<include | |
layout="@layout/card" | |
android:layout_width="0dp" | |
android:layout_height="0dp" | |
android:layout_margin="6dp" | |
app:layout_constraintTop_toTopOf="parent" | |
app:layout_constraintLeft_toLeftOf="@+id/verticalGuideline" | |
app:layout_constraintRight_toRightOf="parent" | |
app:layout_constraintBottom_toBottomOf="@+id/horizontalGuidelineTop"/> | |
<include | |
layout="@layout/card" | |
android:layout_width="0dp" | |
android:layout_height="0dp" | |
android:layout_margin="6dp" | |
app:layout_constraintTop_toTopOf="@+id/horizontalGuidelineTop" | |
app:layout_constraintLeft_toLeftOf="parent" | |
app:layout_constraintRight_toRightOf="@+id/verticalGuideline" | |
app:layout_constraintBottom_toBottomOf="@+id/horizontalGuidelineBottom"/> | |
<include | |
layout="@layout/card" | |
android:layout_width="0dp" | |
android:layout_height="0dp" | |
android:layout_margin="6dp" | |
app:layout_constraintTop_toTopOf="@+id/horizontalGuidelineTop" | |
app:layout_constraintLeft_toLeftOf="@+id/verticalGuideline" | |
app:layout_constraintRight_toRightOf="parent" | |
app:layout_constraintBottom_toBottomOf="@+id/horizontalGuidelineBottom"/> | |
<include | |
layout="@layout/card" | |
android:layout_width="0dp" | |
android:layout_height="0dp" | |
android:layout_margin="6dp" | |
app:layout_constraintTop_toTopOf="@+id/horizontalGuidelineBottom" | |
app:layout_constraintLeft_toLeftOf="parent" | |
app:layout_constraintRight_toRightOf="@+id/verticalGuideline" | |
app:layout_constraintBottom_toBottomOf="parent"/> | |
<include | |
layout="@layout/card" | |
android:layout_width="0dp" | |
android:layout_height="0dp" | |
android:layout_margin="6dp" | |
app:layout_constraintTop_toTopOf="@+id/horizontalGuidelineBottom" | |
app:layout_constraintLeft_toLeftOf="@+id/verticalGuideline" | |
app:layout_constraintRight_toRightOf="parent" | |
app:layout_constraintBottom_toBottomOf="parent"/> | |
<android.support.constraint.Guideline | |
android:id="@+id/verticalGuideline" | |
android:layout_width="0dp" | |
android:layout_height="0dp" | |
android:orientation="vertical" | |
app:layout_constraintGuide_percent="0.5"/> | |
<android.support.constraint.Guideline | |
android:id="@+id/horizontalGuidelineTop" | |
android:layout_width="0dp" | |
android:layout_height="0dp" | |
android:orientation="horizontal" | |
app:layout_constraintGuide_percent="0.33"/> | |
<android.support.constraint.Guideline | |
android:id="@+id/horizontalGuidelineBottom" | |
android:layout_width="0dp" | |
android:layout_height="0dp" | |
android:orientation="horizontal" | |
app:layout_constraintGuide_percent="0.67"/> | |
</android.support.constraint.ConstraintLayout> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment