2swan
슬라이드 예제 2 본문
main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".MainActivity2">
<FrameLayout
android:layout_marginTop="20dp"
android:layout_width="match_parent"
android:layout_height="320dp">
<androidx.viewpager2.widget.ViewPager2
android:id="@+id/viewpager"
android:clipToPadding="false"
android:clipChildren="false"
android:layout_width="match_parent"
android:layout_height="320dp"/>
<me.relex.circleindicator.CircleIndicator
android:id="@+id/indicator"
android:layout_width="match_parent"
android:layout_height="48dp"
android:layout_gravity="bottom"/>
</FrameLayout>
<TextView
android:id="@+id/text"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:text="texttext"/>
</LinearLayout>
fragment_one.xml
<?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"
tools:context=".OneFragment">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="320dp"
android:layout_marginLeft="40dp"
android:layout_marginRight="40dp">
<ImageView
android:id="@+id/imgBanner1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#ff0000"
android:contentDescription="myapp11"/>
<TextView
android:id="@+id/tvName1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:textColor="#ffffff"
android:textSize="20sp"
android:textStyle="bold"
android:text="Hello Page 1"/>
</RelativeLayout>
</FrameLayout>
fragment_two.xml
<?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"
tools:context=".TwoFragment">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="320dp"
android:layout_marginLeft="40dp"
android:layout_marginRight="40dp">
<ImageView
android:id="@+id/imgBanner2"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#00ff00"
android:contentDescription="myapp11"/>
<TextView
android:id="@+id/tvName2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:textColor="#ffffff"
android:textSize="20sp"
android:textStyle="bold"
android:text="Hello Page 2"/>
</RelativeLayout>
</FrameLayout>
fragment_three.xml
<?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"
tools:context=".ThreeFragment">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="320dp"
android:layout_marginLeft="40dp"
android:layout_marginRight="40dp">
<ImageView
android:id="@+id/imgBanner3"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#0000ff"
android:contentDescription="myapp11"/>
<TextView
android:id="@+id/tvName3"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:textColor="#ffffff"
android:textSize="20sp"
android:textStyle="bold"
android:text="Hello Page 3"/>
</RelativeLayout>
</FrameLayout>
fragment_four.xml
<?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"
tools:context=".FourFragment">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="320dp"
android:layout_marginLeft="40dp"
android:layout_marginRight="40dp">
<ImageView
android:id="@+id/imgBanner4"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#FF9800"
android:contentDescription="myapp11"/>
<TextView
android:id="@+id/tvName4"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:textColor="#ffffff"
android:textSize="20sp"
android:textStyle="bold"
android:text="Hello Page 4"/>
</RelativeLayout>
</FrameLayout>
main.activity
public class MainActivity2 extends AppCompatActivity {
private int num_page = 4;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main2);
ViewPager2 viewPager2 = findViewById(R.id.viewpager);
FragmentStateAdapter fragmentStateAdapter = new MyFragAdapter(this, num_page);
viewPager2.setAdapter(fragmentStateAdapter);
}
}
MyfragAdapter.java
// 뷰 페이저 화면 전환
public class MyFragAdapter extends FragmentStateAdapter {
private int mCount;
public MyFragAdapter(@NonNull FragmentActivity fragmentActivity, int mCount) {
super(fragmentActivity);
this.mCount = mCount;
}
@NonNull
@Override
public Fragment createFragment(int position) {
int index = getRealPosition(position);
if(index == 0) return new OneFragment();
else if (index == 1) return new TwoFragment();
else if (index == 2)return new ThreeFragment();
else return new FourFragment();
}
@Override
public int getItemCount() {
return 200;
}
////
private int getRealPosition(int position) {
return position % mCount;
}
}
결과 값
'Programming > Android Example' 카테고리의 다른 글
음악 듣기 예제 (0) | 2023.08.09 |
---|---|
Tab 예제 (0) | 2023.08.09 |
가로(세로) 슬라이드 (0) | 2023.08.09 |
RecyclerView 예제2 (0) | 2023.08.09 |
RecyclerView 추가, 전체보기 (0) | 2023.08.09 |