android:id="@+id/activity_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context="com.dct.dsna.MainActivity">
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"/>
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center_vertical"
android:orientation="horizontal"
android:background="@color/white">
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:button="@null"
android:checked="true"
android:drawableTop="@drawable/maintab_selector_home"
android:gravity="center"
android:paddingLeft="0dp"
android:text="首页"
android:textSize="12sp"/>
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:button="@null"
android:drawableTop="@drawable/maintab_selector_demander"
android:gravity="center"
android:paddingLeft="0dp"
android:text="出诊"
android:textSize="12sp"/>
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:button="@null"
android:drawableTop="@drawable/maintab_selector_message"
android:gravity="center"
android:paddingLeft="0dp"
android:text="消息"
android:textSize="12sp"/>
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:button="@null"
android:drawableTop="@drawable/maintab_selector_mine"
android:gravity="center"
android:paddingLeft="0dp"
android:text="我的"
android:textSize="12sp"/>
package com.dct.dsna;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import com.dct.dsna.fragment_demander.DemanderFragment;
import com.dct.dsna.fragment_home.HomeFragment;
import com.dct.dsna.fragment_home.MyGridViewAdapter;
import com.dct.dsna.fragment_message.MessageFragment;
import com.dct.dsna.fragment_mine.MineFragment;
import com.dct.dsna.utils.MyGridView;
import java.util.ArrayList;
import java.util.List;
public class MainActivity extends AppCompatActivity {
private static final String sessionfile = "sharedsession";
private ViewPager viewPager;
private RadioGroup radioGroup;
private RadioButton home;
private RadioButton demander;
private RadioButton message;
private RadioButton mine;
private List
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initView();
initViewPager();
getSharedPreferences(sessionfile, MODE_PRIVATE).getString("sessionId", "000000");
radioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
public void onCheckedChanged(RadioGroup group, int checkedId) {
switch (checkedId) {
case R.id.maintab_home:
viewPager.setCurrentItem(0);
break;
case R.id.maintab_demander:
viewPager.setCurrentItem(1);
break;
case R.id.maintab_message:
viewPager.setCurrentItem(2);
break;
case R.id.maintab_mine:
viewPager.setCurrentItem(3);
break;
}
}
});
}
public void initView() {
viewPager = (ViewPager) findViewById(R.id.main_viewpager);
radioGroup = (RadioGroup) findViewById(R.id.main_radiogroup);
home = (RadioButton) findViewById(R.id.maintab_home);
demander = (RadioButton) findViewById(R.id.maintab_demander);
message = (RadioButton) findViewById(R.id.maintab_message);
mine = (RadioButton) findViewById(R.id.maintab_mine);
}
private void initViewPager() {
fragmentsList = new ArrayList
fragmentsList.add(new HomeFragment());
fragmentsList.add(new DemanderFragment());
fragmentsList.add(new MessageFragment());
fragmentsList.add(new MineFragment());
viewPager.setAdapter(new MyViewPagerAdapter(getSupportFragmentManager(),fragmentsList));
viewPager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
public void onPageSelected(int arg0) {
switch (arg0) {
case 0:
home.setChecked(true);
demander.setChecked(false);
message.setChecked(false);
mine.setChecked(false);
break;
case 1:
home.setChecked(false);
demander.setChecked(true);
message.setChecked(false);
mine.setChecked(false);
break;
case 2:
home.setChecked(false);
demander.setChecked(false);
message.setChecked(true);
mine.setChecked(false);
break;
case 3:
home.setChecked(false);
demander.setChecked(false);
message.setChecked(false);
mine.setChecked(true);
break;
}
}
public void onPageScrolled(int arg0, float arg1, int arg2) {
}
public void onPageScrollStateChanged(int arg0) {
}
});
}
private class MyViewPagerAdapter extends FragmentPagerAdapter {
private List
public MyViewPagerAdapter(FragmentManager fm,List
super(fm);
this.fragmentsList = fragmentsList;
}
@Override
public Fragment getItem(int position) {
return fragmentsList.get(position);
}
@Override
public int getCount() {
return fragmentsList.size();
}
}
}