Fragmentの使用概要【Android】
Fragmentは実際のプロジェクト開発で使われることが多くなりましたが、簡単に紹介します.
レイアウトファイル:
Javaファイル:
Fragment 1,Fragment 2-5コード:
レイアウトファイル:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".MainActivity" >
<FrameLayout
android:id="@+id/layout_content"
android:layout_width="match_parent"
android:layout_height="0dip"
android:layout_weight="1.0" />
<RadioGroup
android:id="@+id/main_radio"
android:layout_width="fill_parent"
android:layout_height="50dp"
android:layout_gravity="bottom"
android:paddingTop="8dp"
android:background="@drawable/bottom_tab_bg"
android:gravity="center_vertical"
android:orientation="horizontal" >
<RadioButton
android:id="@+id/rb_1"
style="@style/main_tab_bottom"
android:layout_width="wrap_content"
android:layout_height="50dp"
android:drawableTop="@drawable/icon_function" />
<RadioButton
android:id="@+id/rb_2"
style="@style/main_tab_bottom"
android:layout_width="wrap_content"
android:layout_height="50dp"
android:drawableTop="@drawable/icon_newscenter" />
<RadioButton
android:id="@+id/rb_3"
style="@style/main_tab_bottom"
android:layout_width="wrap_content"
android:layout_height="50dp"
android:drawableTop="@drawable/icon_govaffairs" />
<RadioButton
android:id="@+id/rb_4"
style="@style/main_tab_bottom"
android:layout_width="wrap_content"
android:layout_height="50dp"
android:drawableTop="@drawable/icon_setting" />
<RadioButton
android:id="@+id/rb_5"
style="@style/main_tab_bottom"
android:layout_width="wrap_content"
android:layout_height="50dp"
android:drawableTop="@drawable/icon_smartservice" />
</RadioGroup>
</LinearLayout>
Javaファイル:
package com.succ7.fragmenttest;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentStatePagerAdapter;
import android.view.Window;
import android.widget.FrameLayout;
import android.widget.RadioGroup;
import android.widget.RadioGroup.OnCheckedChangeListener;
public class MainActivity extends FragmentActivity {
private FrameLayout layout_content;
/**
* fragment
*/
private int index = 0;
private int current_index = R.id.rb_1;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.activity_main);
layout_content = (FrameLayout) findViewById(R.id.layout_content);
RadioGroup main_radio = (RadioGroup) findViewById(R.id.main_radio);
main_radio.setOnCheckedChangeListener(new OnCheckedChangeListener() {
@Override
public void onCheckedChanged(RadioGroup group, int checkedId) {
switch (checkedId) {
case R.id.rb_1:
index = 0;
break;
case R.id.rb_2:
index = 1;
break;
case R.id.rb_3:
index = 2;
break;
case R.id.rb_4:
index = 3;
break;
case R.id.rb_5:
index = 4;
break;
}
// fragment
Fragment fragment = (Fragment) fragments.instantiateItem(layout_content, index);
// 0 fragment
fragments.setPrimaryItem(layout_content, 0, fragment);
//fragment
fragments.finishUpdate(layout_content);
}
});
main_radio.check(current_index);
}
// getSupportFragmentManager() FragmentActivity
FragmentStatePagerAdapter fragments = new FragmentStatePagerAdapter(
getSupportFragmentManager()) {
// fragment
@Override
public int getCount() {
return 5;
}
@Override
public Fragment getItem(int arg0) {
Fragment fragment = null;
switch (arg0) {
case 0:
fragment = new Fragment1();
break;
case 1:
fragment = new Fragment2();
break;
case 2:
fragment = new Fragment3();
break;
case 3:
fragment = new Fragment4();
break;
case 4:
fragment = new Fragment5();
break;
}
return fragment;
}
};
}
Fragment 1,Fragment 2-5コード:
package com.succ7.fragmenttest;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
public class Fragment1 extends Fragment {
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
ImageView imageview = new ImageView(getActivity());
imageview.setBackgroundResource(R.drawable.g1);
return imageview;
}
@Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
@Override
public void setMenuVisibility(boolean menuVisible) {
super.setMenuVisibility(menuVisible);
if(getView() != null){
getView().setVisibility(menuVisible?View.VISIBLE:View.INVISIBLE);
}
/*if(menuVisible){
getView().setVisibility(View.VISIBLE);
}else{
getView().setVisibility(View.INVISIBLE);
}*/
}
}