본문 바로가기

잡다한 기술

[안드로이드] 프래그먼트 작업




# activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">

<LinearLayout
android:id="@+id/ll"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">

<Button
android:id="@+id/btn_first"
android:layout_width="0dip"
android:layout_height="match_parent"
android:layout_weight="1"
android:text="First Tab"/>

<Button
android:id="@+id/btn_second"
android:layout_width="0dip"
android:layout_height="match_parent"
android:layout_weight="1"
android:text="Second Tab"
/>

<Button
android:id="@+id/btn_third"
android:layout_width="0dip"
android:layout_height="match_parent"
android:layout_weight="1"
android:text="Third Tab"
/>

</LinearLayout>

<android.support.v4.view.ViewPager
android:id="@+id/pager"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="@+id/ll"
>
</android.support.v4.view.ViewPager>

</RelativeLayout>



# MainActivity.java

package com.example.bhj28.fragment;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentStatePagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;

public class MainActivity extends AppCompatActivity {

ViewPager pager;

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

pager = (ViewPager)findViewById(R.id.pager);
Button btn_first = (Button)findViewById(R.id.btn_first);
Button btn_second = (Button)findViewById(R.id.btn_second);
Button btn_third = (Button)findViewById(R.id.btn_third);

pager.setAdapter(new pagerAdapter(getSupportFragmentManager()));
pager.setCurrentItem(0);

View.OnClickListener movePageListener = new View.OnClickListener()
{
@Override
public void onClick(View view) {
int tag = (int)view.getTag();
pager.setCurrentItem(tag);
}
};

btn_first.setOnClickListener(movePageListener);
btn_first.setTag(0);
btn_second.setOnClickListener(movePageListener);
btn_second.setTag(1);
btn_third.setOnClickListener(movePageListener);
btn_third.setTag(2);
}

private class pagerAdapter extends FragmentStatePagerAdapter
{
public pagerAdapter(FragmentManager fm )
{
super(fm);
}

@Override
public Fragment getItem(int position) {
switch(position)
{
case 0:
return new frag1();
case 1:
return new frag2();
case 2:
return new frag3();
default:
return null;
}
}

@Override
public int getCount() {
// total page count
return 3;
}
}
}


# activity_frag1.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#FF0000">

<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:textSize="30dp"
android:textStyle="bold"
android:text="Second_Page" />

</RelativeLayout>


# frag1.java

package com.example.bhj28.fragment;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.RelativeLayout;

public class frag1 extends Fragment {
public frag1() {
// required
}

@Override
public void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}

@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container,                                 @Nullable Bundle savedInstanceState) {
RelativeLayout layout =         (RelativeLayout) inflater.inflate(R.layout.activity_frag1, container, false);
return layout;
}
}


# 마무리


위 포스트는 제가 직접 제작한 것 입니다.

그렇기 때문에 틀린점이나 설명이 엉성한 점이 존재할 수 있습니다.

만약 틀린점이나 설명이 엉성한 부분이 존재하면 댓글로 알려주세요.

빠른 처리 하도록 하겠습니다.


티스토리 앱으로는 댓글 이용이 불가능 하므로 웹 브라우저로 봐 주세요

(URL : http://junprogramer.tistory.com/)


읽어주셔서 감사합니다.