티스토리 뷰
build.gradle (Module: app)
dependencies {
implementation 'androidx.viewpager2:viewpager2:1.0.0'
}
viewpager_fragment.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:background="#FFBBAA"
android:orientation="vertical">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="혀가 길지 않은 개발자"
android:textStyle="bold"
android:textSize="30dp"
android:gravity="center"/>
<TextView
android:id="@+id/txtCount"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="1 page"
android:textSize="30dp"
android:textStyle="bold"
android:gravity="center" />
</LinearLayout>
ViewPagerFragment.java
package com.jwsoft.javaproject;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
public class ViewPagerFragment extends Fragment {
TextView txtCount;
int position;
public ViewPagerFragment(int position) {
this.position = position;
}
@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.viewpager_fragment, container, false);
txtCount = view.findViewById(R.id.txtCount);
txtCount.setText(Integer.toString(position + 1));
return view;
}
}
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
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"
tools:context=".MainActivity">
<androidx.viewpager2.widget.ViewPager2
android:id="@+id/viewPager2"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintBottom_toBottomOf="parent"/>
</androidx.constraintlayout.widget.ConstraintLayout>
ViewPagerAdapter.java
package com.jwsoft.javaproject;
import androidx.annotation.NonNull;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentActivity;
import androidx.viewpager2.adapter.FragmentStateAdapter;
public class ViewPagerAdapter extends FragmentStateAdapter {
public ViewPagerAdapter(@NonNull FragmentActivity fragmentActivity) {
super(fragmentActivity);
}
@NonNull
@Override
public Fragment createFragment(int position) {
ViewPagerFragment fragment = new ViewPagerFragment(position);
return fragment;
}
@Override
public int getItemCount() {
return 10;
}
}
MainActivity.java
package com.jwsoft.javaproject;
import androidx.appcompat.app.AppCompatActivity;
import androidx.viewpager2.widget.ViewPager2;
import android.os.Bundle;
public class MainActivity extends AppCompatActivity {
private ViewPager2 viewPager2;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ViewPagerAdapter adapter = new ViewPagerAdapter(this);
viewPager2 = findViewById(R.id.viewPager2);
viewPager2.setOrientation(ViewPager2.ORIENTATION_VERTICAL);
viewPager2.setAdapter(adapter);
}
}
'Android > Java' 카테고리의 다른 글
[Java] DataBinding (0) | 2020.07.08 |
---|---|
[Java] ViewPager2 + RecyclerView.Adapter (0) | 2020.07.04 |
[Java] CoordinatorLayout + AppBarLayout + Toolbar + RecyclerView (0) | 2020.07.03 |
[Java] TabLayout (0) | 2020.07.03 |
[Java] FrameLayout (0) | 2020.07.02 |
댓글
공지사항
최근에 올라온 글
최근에 달린 댓글
- Total
- Today
- Yesterday
링크
TAG
- JSONObject
- 자바
- Intent
- 코틀린
- JSONArray
- fragment
- Architecture Pattern
- DataBinding
- XML
- MVVM
- coroutine
- James Kim
- ViewModel
- ArrayList
- 안드로이드
- java
- handler
- View
- Android
- activity
- CoordinatorLayout
- Livedata
- ViewPager2
- 안드로이드 #코틀린 #Android #Kotlin
- TabLayout
- 혀가 길지 않은 개발자
- Kotlin
- Vue.js #Vue.js + javascript
- Design Pattern
- recyclerview
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | 2 | 3 | 4 | |||
5 | 6 | 7 | 8 | 9 | 10 | 11 |
12 | 13 | 14 | 15 | 16 | 17 | 18 |
19 | 20 | 21 | 22 | 23 | 24 | 25 |
26 | 27 | 28 | 29 | 30 | 31 |
글 보관함