Swipe 모션으로 Fragment 페이지 넘기기 기능을 하는 ViewPager2를 구현해보자.
1. build.gradle ( Module: ~~) 에서 ViewPager 라이브러리 설치
implementation 'androidx.viewpager2:viewpager2:1.0.0'
※ Tips. gradle에 androidx에서 제공하는 라이브러리 설치할 때 최신버전으로 받고 싶다면 타이핑말고 검색해서 설치할 수 있다.
1. File > Project Structure... ( 또는 Ctrl + Alt + Shift + S )
2. Dependencies > + > Library Dependency
3. 설치하고자 하는 라이브러리 (androidx.viewpager2) 검색해서 오른쪽에 버전 선택 (최상단이 최신버전)
4. OK > Apply > OK 하면 자동으로 설치되고 dependencies 확인해보면 알아서 입력되어있음.
2. 레이아웃 구현
- 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_container"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
- fragment1.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"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Fragment1"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
- fragment2.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"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Fragment2"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
3. 컨트롤러 구현
- Fragment1.java (생성)
package com.example.example_viewpager2;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
public class Fragment1 extends Fragment {
public static Fragment1 newInstance(int number) {
Fragment1 fragment1 = new Fragment1();
Bundle bundle = new Bundle();
bundle.putInt("number", number);
fragment1.setArguments(bundle);
return fragment1;
}
@Override
public void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if (getArguments() != null) {
int num = getArguments().getInt("number");
}
}
@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
return LayoutInflater.from(inflater.getContext()).inflate(R.layout.fragment1, container, false);
}
}
- Fragment2.java (생성)
package com.example.example_viewpager2;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
public class Fragment2 extends Fragment {
public static Fragment2 newInstance(int number) {
Fragment2 fragment2 = new Fragment2();
Bundle bundle = new Bundle();
bundle.putInt("number", number);
fragment2.setArguments(bundle);
return fragment2;
}
@Override
public void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if (getArguments() != null) {
int num = getArguments().getInt("number");
}
}
@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
return LayoutInflater.from(inflater.getContext()).inflate(R.layout.fragment2, container, false);
}
}
- ViewPager2Adapter.java (생성)
package com.example.example_viewpager2;
import androidx.annotation.NonNull;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentActivity;
import androidx.viewpager2.adapter.FragmentStateAdapter;
import java.util.ArrayList;
public class ViewPager2Adapter extends FragmentStateAdapter {
private ArrayList<Fragment> mFragments;
public ViewPager2Adapter(@NonNull FragmentActivity fragmentActivity, ArrayList list) {
super(fragmentActivity);
this.mFragments = list;
}
@NonNull
@Override
public Fragment createFragment(int position) {
return mFragments.get(position);
}
@Override
public int getItemCount() {
return mFragments.size();
}
}
- MainActivity.java
package com.example.example_viewpager2;
import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.Fragment;
import androidx.viewpager2.widget.ViewPager2;
import android.os.Bundle;
import java.util.ArrayList;
public class MainActivity extends AppCompatActivity {
private ViewPager2 viewPager2;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ArrayList<Fragment> fragments = new ArrayList<>();
fragments.add(Fragment1.newInstance(0));
fragments.add(Fragment2.newInstance(1));
viewPager2 = (ViewPager2) findViewById(R.id.viewPager2_container);
ViewPager2Adapter viewPager2Adapter = new ViewPager2Adapter(this, fragments);
viewPager2.setAdapter(viewPager2Adapter);
}
}
>>> 참고
끝
'앱 개발 > 안드로이드(Java)' 카테고리의 다른 글
glide를 사용하여 GIF 로딩 화면 구현하기 (4) | 2021.08.17 |
---|---|
이미지 회전 로딩 화면 구현 (0) | 2021.08.17 |
RecyclerView ScrollBar 설정 (0) | 2021.07.10 |
RecyclerView 아이템 클릭 리스너 구현하기 (0) | 2021.06.17 |
RecyclerView를 커스텀해서 가로,세로 구현하기 (0) | 2021.06.12 |