탭같은거 이용해서 뷰를 넘기면서 교체해가는거
implementation 'com.google.android.material:material:1.5.0-alpha01'
이거 임포트하면 생김
세가지 화면을 교체하는 방식을 액티비티가 아닌, 프래그먼트로 세 조각을 교체하는 과정을 만듦
프래그먼트 표시영역 지정 - 네모영역안에서 배치해줌에 따라서 표시가 될 것
( height 설정해줘서 margin영역 정해줌)
viewpager추가해주기
Monday fragment xml만들어줌
Main.js
package com.example.viewpagerexample;
import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.FragmentPagerAdapter;
import androidx.viewpager.widget.ViewPager;
import android.os.Bundle;
import android.widget.TableLayout;
import com.google.android.material.tabs.TabLayout;
public class MainActivity extends AppCompatActivity {
private FragmentPagerAdapter fragmentPagerAdapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//뷰페이저 세팅
ViewPager viewPager = findViewById(R.id.viewPager);
fragmentPagerAdapter = new ViewPagerAdapter(getSupportFragmentManager());
TabLayout tabLayout = findViewById(R.id.tab_layout); //상단영역
viewPager.setAdapter(fragmentPagerAdapter); //뷰페이저에 프래그먼트어댑터연동
tabLayout.setupWithViewPager(viewPager); //탭레이아웃에 뷰페이저연동
}
}
MondayFragment.js (Tuesday, Wednesday바꾸기만하면됨)
package com.example.viewpagerexample;
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 FragMonday extends Fragment {
private View view;
//뷰페이저는 상태저장해야함
//일단 객체를 자체적으로 생성
public static FragMonday newinstance(){
FragMonday fragMonday = new FragMonday();
return fragMonday; //어댑터에 맞춰서 주기적으로 인스턴스 생성해서 만들어지는 로직 실행구성구문
}
@Nullable
//프래그먼트에서는 oncreateView를 씀
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
view = inflater.inflate(R.layout.frag_monday,container,false);
return view;
}
}
ViewpagerAdapter.js
package com.example.viewpagerexample;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentPagerAdapter;
public class ViewPagerAdapter extends FragmentPagerAdapter {
public ViewPagerAdapter(@NonNull FragmentManager fm) {
super(fm);
}
@NonNull
@Override
//아까 선언한 인스턴스를 어댑터로 불러들이기
//프래그먼트 교체를 보여주는 처리를 구현하느곳
public Fragment getItem(int position) {
//switch문 , 프래그먼트의 위치에따라서 어떤 뷰를 띄워줄것이냐
switch (position){
case 0:
return FragMonday.newinstance();
case 1:
return FragTuesday.newinstance();
case 2:
return FragWednesday.newinstance();
default:
return null;
}
}
//갯수를 명시해주는, 우린 3가지 뷰페이저 구현
@Override
public int getCount() {
return 3;
}
//상단의 탭 레이아웃 인디케이터 쪽에 텍스트를 선언해주는곳
@Nullable
@Override
public CharSequence getPageTitle(int position) {
switch (position){
case 0:
return "Monday";
case 1:
return "Tuesday";
case 2:
return "Wednesday";
default:
return null;
}
}
}
main.xml (design탭으로했음)
<?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">
<com.google.android.material.tabs.TabLayout
android:id="@+id/tab_layout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:layout_constraintBottom_toTopOf="@+id/const_vp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent">
<com.google.android.material.tabs.TabItem
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Monday" />
<com.google.android.material.tabs.TabItem
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Tuesday" />
<com.google.android.material.tabs.TabItem
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Wednesday" />
</com.google.android.material.tabs.TabLayout>
<androidx.constraintlayout.widget.ConstraintLayout
android:id="@+id/const_vp"
android:layout_width="match_parent"
android:layout_height="0dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/tab_layout">
<androidx.viewpager2.widget.ViewPager2
android:id="@+id/viewPager"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
</androidx.constraintlayout.widget.ConstraintLayout>
Monday.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">
<TextView
android:id="@+id/textView3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="촬리의월요일"
android:textColor="#304FFE"
android:textSize="48sp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
결과
또!!어플중지
짜증난다진짜
2021-08-09 23:53:46.106 4675-4675/com.example.viewpagerexample E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.viewpagerexample, PID: 4675
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.viewpagerexample/com.example.viewpagerexample.MainActivity}: java.lang.ClassCastException: androidx.viewpager2.widget.ViewPager2 cannot be cast to androidx.viewpager.widget.ViewPager
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3449)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3601)
at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:85)
at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:135)
at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:95)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2066)
at android.os.Handler.dispatchMessage(Handler.java:106)
at android.os.Looper.loop(Looper.java:223)
at android.app.ActivityThread.main(ActivityThread.java:7656)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:592)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:947)
Caused by: java.lang.ClassCastException: androidx.viewpager2.widget.ViewPager2 cannot be cast to androidx.viewpager.widget.ViewPager
at com.example.viewpagerexample.MainActivity.onCreate(MainActivity.java:24)
at android.app.Activity.performCreate(Activity.java:8000)
at android.app.Activity.performCreate(Activity.java:7984)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1309)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3422)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3601)
at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:85)
at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:135)
at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:95)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2066)
at android.os.Handler.dispatchMessage(Handler.java:106)
at android.os.Looper.loop(Looper.java:223)
at android.app.ActivityThread.main(ActivityThread.java:7656)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:592)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:947)
일단 내일 오류찾기위해 로그캣올려놓는다.
아 진짜짜증난다 난 못해먹겠음
'✍2021,2022 > app(android studio)' 카테고리의 다른 글
앱만들기. 40 (android studio Github연동으로 협업하는 방법) (0) | 2021.08.12 |
---|---|
H &Y Calendar (0) | 2021.08.11 |
앱만들기 33.(Firebase RecyclerView) (0) | 2021.08.09 |
앱만들기.32(constraint Layout) (0) | 2021.08.04 |
앱만들기.32(VideoView) (0) | 2021.08.04 |