생각자유의 안드로이드 이야기

구글 플레이 스타일에 탭 레이아웃 만들기 본문

Android/Design support

구글 플레이 스타일에 탭 레이아웃 만들기

생각자유 2015. 8. 17. 15:40
336x280(권장), 300x250(권장), 250x250, 200x200 크기의 광고 코드만 넣을 수 있습니다.

안녕하세요. 


생각자유 입니다. 이번 글 주제는 아래에 소개된 디자인 서포트 라이브러리에 추가로 가장 빈번하고 많이 사용


되는 레이아웃중 하나인 탭 레이아웃에 대해서 알아보고자 합니다.


지끔까지 탭 레이아웃을 만들기 위해서는 다양한 방법과 복잡한 방법을 사용해야 했었는데 이번에 디자인 서포


트 라이브러리가 배포되면서 아주 쉽게 만들 수 있게되었습니다.




탭인 만큼 슬라이딩 탭으로 만들기로 하겠습니다.


필요한 클래스로는 android.support.design.widget.TabLayoutandroid.support.v4.view.ViewPager를 사용하면 됩니다.


우선 메인화면 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"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">

<android.support.design.widget.TabLayout
android:id="@+id/sliding_tabs"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:tabMode="fixed" />

<android.support.v4.view.ViewPager
android:id="@+id/viewpager"
android:layout_width="match_parent"
android:layout_height="0px"
android:layout_weight="1"
android:background="@android:color/white" />

</LinearLayout>


그다음 viewPager를 사용해서 sliding을 해야하니 fragment 레이아웃도 생성을 해봅니다.

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

저 레이아웃을 올릴 fragment를 만들도록 해봅니다.

public class fragmentOne extends Fragment {
public static final String ARG_PAGE = "ARG_PAGE";

private int mPage;

public static fragmentOne newInstance(int page) {
Bundle args = new Bundle();
args.putInt(ARG_PAGE, page);
fragmentOne fragment = new fragmentOne();
fragment.setArguments(args);
return fragment;
}

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mPage = getArguments().getInt(ARG_PAGE);
}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_page, container, false);
TextView textView = (TextView) view;
textView.setText("Fragment #" + mPage);
return view;
}
}

FragmentPagerAdapter 구현을 해봅니다.

public class SampleFragmentPagerAdapter extends FragmentPagerAdapter {
final int PAGE_COUNT = 3;
private String tabTitles[] = new String[] { "1", "2", "3" };
private Context context;

public SampleFragmentPagerAdapter(FragmentManager fm, Context context) {
super(fm);
this.context = context;
}

@Override
public int getCount() {
return PAGE_COUNT;
}

@Override
public Fragment getItem(int position) {
return fragmentOne.newInstance(position + 1);
}

@Override
public CharSequence getPageTitle(int position) {
// Generate title based on item position
return tabTitles[position];
}
}

마지막으로 메인에 탭 레이아웃과 fragmet를 연결해 봅니다.

public class MainActivity extends AppCompatActivity {

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

// Get the ViewPager and set it's PagerAdapter so that it can display items
ViewPager viewPager = (ViewPager) findViewById(R.id.viewpager);
viewPager.setAdapter(new SampleFragmentPagerAdapter(getSupportFragmentManager(),
MainActivity.this));

// Give the TabLayout the ViewPager
TabLayout tabLayout = (TabLayout) findViewById(R.id.sliding_tabs);
tabLayout.setupWithViewPager(viewPager);

}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();

//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}

return super.onOptionsItemSelected(item);
}
}

일단 이정도로 하고 중간중간 쫌더 자세하게 설명을 달도록 하겠습니다.

'Android > Design support' 카테고리의 다른 글

[Android Support Library, revision 23.0.1]  (0) 2015.09.09
Android Design Support Library 소개  (0) 2015.08.11
Comments