点击或者滑动切换画面,用ViewPager实现,
首先是布局文件:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" > <LinearLayout android:layout_width="match_parent" android:layout_height="62dip" android:orientation="vertical" android:background="@drawable/top_theme_blue"> <LinearLayout android:layout_width="match_parent" android:layout_height="36dip" android:orientation="horizontal" android:gravity="center_vertical"> <ImageView android:id="@+id/main_avatar" android:layout_width="32dip" android:layout_height="32dip" android:src="@drawable/avatar" /> <TextView android:id="@+id/main_nick" android:layout_width="wrap_content" android:layout_height="wrap_content" android:textColor="#FFFFFF" android:text="Vestigge"/> <ImageView android:layout_width="14dip" android:layout_height="14dip" android:src="@drawable/status_online"/> </LinearLayout> <LinearLayout android:layout_width="match_parent" android:layout_height="26dip" android:orientation="horizontal" android:gravity="bottom"> <RadioGroup android:id="@+id/main_radiogroup" android:orientation="horizontal" android:paddingTop="1dp" android:layout_width="fill_parent" android:layout_height="wrap_content"> <RadioButton android:id="@+id/main_radio_recent" android:checked="true" android:text="动态" android:textColor="@color/tab_text" android:drawableBottom="@drawable/top_tab_selector" style="@style/radio_style"/> <RadioButton android:id="@+id/main_radio_buddy" android:text="群组" android:textColor="@color/tab_text" android:drawableBottom="@drawable/top_tab_selector" style="@style/radio_style"/> <RadioButton android:id="@+id/main_radio_group" android:text="好友" android:textColor="@color/tab_text" android:drawableBottom="@drawable/top_tab_selector" style="@style/radio_style" android:checked="true"/> <RadioButton android:id="@+id/main_radio_trends" android:text="会话" android:textColor="@color/tab_text" android:drawableBottom="@drawable/top_tab_selector" style="@style/radio_style"/> </RadioGroup> </LinearLayout> </LinearLayout> <android.support.v4.view.ViewPager android:id="@+id/main_viewpager" android:layout_width="match_parent" android:layout_height="match_parent" > </android.support.v4.view.ViewPager> </LinearLayout>
代码:
public class MainActivity extends ActivityGroup { private static final String TRENDS="动态"; private static final String GROUP="群组"; private static final String BUDDY="好友"; private static final String RECENT="会话"; private ArrayList<View> pageViews; private RadioGroup radioGroup; private ViewPager viewPager; public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); requestWindowFeature(Window.FEATURE_NO_TITLE); setContentView(R.layout.activity_main); initView(); viewPager=(ViewPager) findViewById(R.id.main_viewpager); viewPager.setAdapter(new PagerAdapter(){ public int getCount() { return pageViews.size(); } public boolean isViewFromObject(View view, Object objcet) { return view==objcet; } //这里会对需要进行水平切换的页面进行了加载和初始化 android:tileMode="repeat" public Object instantiateItem(View view, int id) { ((ViewPager)view).addView(pageViews.get(id)); return pageViews.get(id); } public void destroyItem(View view, int id, Object arg2) { ((ViewPager) view).removeView(pageViews.get(id)); } }); viewPager.setCurrentItem(2);//默认显示的是好友页面 radioGroup = (RadioGroup) findViewById(R.id.main_radiogroup); radioGroup.setOnCheckedChangeListener(new OnCheckedChangeListener() { public void onCheckedChanged(RadioGroup group, int checkedId) { setClick(checkedId); } }); } void initView() { pageViews=new ArrayList<View>(); View view1 = getLocalActivityManager().startActivity(TRENDS, new Intent(this, TrendsActivity.class)).getDecorView(); View view2 = getLocalActivityManager().startActivity(GROUP, new Intent(this, GroupActivity.class)).getDecorView(); View view3 = getLocalActivityManager().startActivity(BUDDY, new Intent(this, BuddyActivity.class)).getDecorView(); View view4 = getLocalActivityManager().startActivity(RECENT, new Intent(this, RecentActivity.class)).getDecorView(); pageViews.add(0,view1); pageViews.add(1,view2); pageViews.add(2,view3); pageViews.add(3,view4); } public void setClick(int id) { switch(id){ case R.id.main_radio_trends: viewPager.setCurrentItem(0); break; case R.id.main_radio_group: viewPager.setCurrentItem(1); break; case R.id.main_radio_buddy: viewPager.setCurrentItem(2); break; case R.id.main_radio_recent: viewPager.setCurrentItem(3); break; } } @Override public boolean onCreateOptionsMenu(Menu menu) { getMenuInflater().inflate(R.menu.activity_main, menu); return true; } }