프로그래밍

안드로이드 스튜디오 프래그먼트 2

안드로이드 스튜디오
반응형

ColorFragment / BlankFragment
ItemFragment
MyPagerAdapter

slide 실습이기 때문에 프래그먼트는 손대지 않음

 

(MyPagerAdapter.java)

package com.example.slide_tab;

import android.nfc.tech.NfcA;

import java.util.ArrayList;

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 MyPagerAdapter extends FragmentPagerAdapter {
    String[] Name = {"Left","Center","Right"};
    private ArrayList<Fragment> myData;
    public MyPagerAdapter(@NonNull FragmentManager fm) {
        super(fm);
        myData = new ArrayList<>();
        myData.add(new ColorFragment());
        myData.add(new ItemFragment());
        myData.add(new BlankFragment());
    }

    @NonNull
    @Override
    public Fragment getItem(int position) {
        return myData.get(position);
    }

    @Override
    public int getCount() {
        return myData.size();
    }

    @Nullable
    @Override
    public CharSequence getPageTitle(int position) {
        return Name[position];
    }
}

(activity_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=".MainActivity">
    <com.google.android.material.tabs.TabLayout
        android:id="@+id/tab"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        app:tabSelectedTextColor="#0000ff"
        app:tabTextColor="#7D7D7D"/>
    
    <androidx.viewpager.widget.ViewPager
        android:id="@+id/pager"
        android:layout_width="match_parent"
        android:layout_height="match_parent"/>
</LinearLayout>

build.gradle (:app) 안에 이것들 모두 있어야 함

implementation 'androidx.appcompat:appcompat:1.1.0'
implementation 'androidx.constraintlayout:constraintlayout:1.1.3'
implementation 'androidx.legacy:legacy-support-v4:1.0.0'
implementation 'androidx.recyclerview:recyclerview:1.1.0'
implementation 'androidx.lifecycle:lifecycle-extensions:2.2.0'
testImplementation 'junit:junit:4.12'
androidTestImplementation 'androidx.test.ext:junit:1.1.1'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.2.0'
implementation 'com.android.support:design:28.0.0'

 

 

(MainActivity)

package com.example.slide_tab;

import androidx.appcompat.app.AppCompatActivity;
import androidx.viewpager.widget.ViewPager;

import android.os.Bundle;
import android.widget.Toast;

import com.example.slide_tab.dummy.DummyContent;
import com.google.android.material.tabs.TabLayout;

public class MainActivity extends AppCompatActivity implements ItemFragment.OnListFragmentInteractionListener {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        ViewPager viewpager = findViewById(R.id.pager);
        MyPagerAdapter adapter = new MyPagerAdapter(getSupportFragmentManager());
        viewpager.setAdapter(adapter);

        TabLayout tabLayout = findViewById(R.id.tab);
        tabLayout.setupWithViewPager(viewpager);
    }

    @Override
    public void onListFragmentInteraction(DummyContent.DummyItem item) {
        Toast.makeText(getApplicationContext(),item.toString(),Toast.LENGTH_SHORT).show();
    }
}

반응형