MainActivity.javajava
package com.qf.day21_viewpagerfragmentrg_demo4;
import java.util.ArrayList;
import java.util.List;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v4.view.ViewPager.OnPageChangeListener;
import android.text.Layout;
import android.view.Gravity;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.RadioGroup.OnCheckedChangeListener;
import android.widget.TableLayout.LayoutParams;
public class MainActivity extends FragmentActivity {
private ViewPager viewPager;
private RadioGroup rgMain;
// 數據集合
private List<Fragment> list = new ArrayList<Fragment>();
private String[] titles = { "新聞", "娛樂", "軍事", "體育" };
private RadioButton[] rbs;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// 初始化View
initView();
// 初始化ViewPager
initViewPager();
// 初始化導航書籤
initTab();
}
// 初始化導航書籤
public void initTab() {
rbs = new RadioButton[titles.length];
for (int i = 0; i < titles.length; i++) {
// 動態建立RadioButton
rbs[i] = new RadioButton(getApplicationContext());
// rbs[i].setText(titles[i]);
rbs[i].setGravity(Gravity.CENTER);
BitmapDrawable a = null;
rbs[i].setButtonDrawable(a);
rbs[i].setBackgroundResource(R.drawable.selector_main);
int screenWith = getResources().getDisplayMetrics().widthPixels;
int eachWith = screenWith / titles.length;
rbs[i].setWidth(eachWith);
rgMain.addView(rbs[i]);
}
// 默認第0個元素是被選中的
rbs[0].setChecked(true);
rgMain.setOnCheckedChangeListener(new OnCheckedChangeListener() {
@Override
public void onCheckedChanged(RadioGroup group, int checkedId) {
// TODO Auto-generated method stub
for (int i = 0; i < titles.length; i++) {
if (rbs[i].getId() == checkedId) {
// Viewpager滑動到 點擊的RadioButton上
viewPager.setCurrentItem(i);
}
}
}
});
}
// 初始化ViewPager
public void initViewPager() {
// 獲取數據源
for (int i = 0; i < titles.length; i++) {
MyFragment myFragment = MyFragment.getInstance(i + 1);
list.add(myFragment);
}
viewPager.setAdapter(new MyFragmentPagerAdapter(getSupportFragmentManager()));
viewPager.setOnPageChangeListener(new OnPageChangeListener() {
@Override
public void onPageSelected(int arg0) {
// TODO Auto-generated method stub
rbs[arg0].setChecked(true);
}
@Override
public void onPageScrolled(int arg0, float arg1, int arg2) {
// TODO Auto-generated method stub
}
@Override
public void onPageScrollStateChanged(int arg0) {
// TODO Auto-generated method stub
}
});
}
// 初始化View
public void initView() {
viewPager = (ViewPager) findViewById(R.id.viewPager);
rgMain = (RadioGroup) findViewById(R.id.rg_main);
}
class MyFragmentPagerAdapter extends FragmentPagerAdapter {
public MyFragmentPagerAdapter(FragmentManager fm) {
super(fm);
// TODO Auto-generated constructor stub
}
@Override
public Fragment getItem(int arg0) {
// TODO Auto-generated method stub
return list.get(arg0);
}
@Override
public int getCount() {
// TODO Auto-generated method stub
return list.size();
}
}
}
MyFragment.javaandroid
package com.qf.day21_viewpagerfragmentrg_demo4;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import android.app.Activity;
import android.os.Bundle;
import android.support.v4.app.ListFragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.SimpleAdapter;
import android.widget.TextView;
public class MyFragment extends ListFragment {
private TextView tvShow;
private int index =0;
public static MyFragment getInstance(int index){
MyFragment myFragment = new MyFragment();
Bundle args = new Bundle();
args.putInt("index", index);
myFragment.setArguments(args);
return myFragment;
}
@Override
public void onAttach(Activity activity) {
// TODO Auto-generated method stub
super.onAttach(activity);
Bundle bundle = getArguments();
if(bundle!=null){
index = bundle.getInt("index");
}
}
@Override
public void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// TODO Auto-generated method stub
View v = inflater.inflate(R.layout.fragment_layout, container, false);
tvShow = (TextView) v.findViewById(R.id.tv_show);
return v;
}
@Override
public void onActivityCreated(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onActivityCreated(savedInstanceState);
switch (index) {
case 1:
tvShow.setText("您點擊了書籤1");
break;
case 2:
tvShow.setText("您點擊了書籤2");
break;
case 3:
tvShow.setText("您點擊了書籤3");
break;
case 4:
tvShow.setText("您點擊了書籤4");
break;
default:
break;
}
SimpleAdapter adapter = new SimpleAdapter(
getActivity(),
loadNetWorkData(),
R.layout.item,
new String[]{"icon","title","content"},
new int[]{R.id.iv_item,R.id.title_item,R.id.content_item});
setListAdapter(adapter);
}
/** * 假設從網絡獲取數據 * @return */
private List<Map<String,Object>> loadNetWorkData(){
List<Map<String,Object>> list = new ArrayList<Map<String,Object>>();
for(int i=0;i<20;i++){
Map<String, Object> map = new HashMap<String, Object>();
map.put("icon", R.drawable.ic_launcher);
map.put("title", "郭XX大戰曹XXX"+i+"tab"+index);
map.put("content", "降龍十八掌贏"+i+"tab"+index);
list.add(map);
}
return list;
}
@Override
public void onStart() {
// TODO Auto-generated method stub
super.onStart();
}
@Override
public void onResume() {
// TODO Auto-generated method stub
super.onResume();
}
@Override
public void onPause() {
// TODO Auto-generated method stub
super.onPause();
}
@Override
public void onStop() {
// TODO Auto-generated method stub
super.onStop();
}
@Override
public void onDestroyView() {
// TODO Auto-generated method stub
super.onDestroyView();
}
@Override
public void onDestroy() {
// TODO Auto-generated method stub
super.onDestroy();
}
@Override
public void onDetach() {
// TODO Auto-generated method stub
super.onDetach();
}
}
selector_main.xmlmarkdown
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android" >
<item android:state_checked="true" android:drawable="@android:drawable/ic_menu_add"></item>
<item android:state_checked="false" android:drawable="@android:drawable/ic_menu_day"></item>
</selector>
activity_main.xml網絡
<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" >
<RadioGroup android:id="@+id/rg_main" android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal" >
</RadioGroup>
<View android:layout_width="match_parent" android:layout_height="1dp" android:background="#f00" />
<android.support.v4.view.ViewPager android:id="@+id/viewPager" android:layout_width="match_parent" android:layout_height="match_parent" ></android.support.v4.view.ViewPager>
</LinearLayout>
fragment_layout.xmlapp
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" >
<TextView android:id="@+id/tv_show" android:layout_width="match_parent" android:layout_height="wrap_content" android:textColor="#f00" android:text="AAA" />
<ListView android:id="@android:id/list" android:layout_width="match_parent" android:layout_height="match_parent" ></ListView>
</LinearLayout>
item.xmlide
<?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" >
<ImageView android:id="@+id/iv_item" android:layout_width="wrap_content" android:layout_height="wrap_content" android:src="@drawable/ic_launcher" />
<TextView android:id="@+id/title_item" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_toRightOf="@id/iv_item" android:text="name" />
<TextView android:id="@+id/content_item" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_toRightOf="@id/iv_item" android:text="aaa" android:layout_alignBottom="@id/iv_item" />
</RelativeLayout>