以前写过一篇ViewPager:内容content+指示点的Demo;
这篇文章继续介绍ViewPager:内容content+标题title的Demo。
实现效果图:
源代码:
布局文件:activity_main:
<RelativeLayout 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" tools:context=".MainActivity" > <android.support.v4.view.ViewPager android:id="@+id/viewPager" android:layout_width="match_parent" android:layout_height="match_parent" android:layout_gravity="center" > <android.support.v4.view.PagerTitleStrip android:id="@+id/pagerTitle" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="top" > </android.support.v4.view.PagerTitleStrip> </android.support.v4.view.ViewPager> </RelativeLayout>
f1.xml(体育新闻布局文件):
<RelativeLayout 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" tools:context=".MainActivity" > <RatingBar android:id="@+id/ratingBar1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentLeft="true" android:layout_alignParentTop="true" /> </RelativeLayout>
f2.xml(娱乐新闻布局文件):
<RelativeLayout 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" tools:context=".MainActivity" > <ProgressBar android:id="@+id/progressBar1" style="?android:attr/progressBarStyleLarge" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginLeft="105dp" /> </RelativeLayout>
f3.xml(军事新闻布局文件):
<RelativeLayout 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" tools:context=".MainActivity" > <RadioGroup android:id="@+id/radioGroup1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginLeft="84dp" > <RadioButton android:id="@+id/radio0" android:layout_width="wrap_content" android:layout_height="wrap_content" android:checked="true" android:text="RadioButton1" /> <RadioButton android:id="@+id/radio1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="RadioButton2" /> <RadioButton android:id="@+id/radio2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="RadioButton3" /> </RadioGroup> </RelativeLayout>
代码文件:
MainActivity:
package com.fragmentdemo11_viewpager; import java.util.ArrayList; import java.util.List; import android.app.Activity; import android.os.Bundle; import android.support.v4.view.ViewPager; import android.view.LayoutInflater; import android.view.View; public class MainActivity extends Activity { private ViewPager viewPager; private List<String> title; private List<View> content; private LayoutInflater inflater; private MyViewPagerAdapter adapter; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); viewPager = (ViewPager) findViewById(R.id.viewPager); content = new ArrayList<View>(); inflater = LayoutInflater.from(this); /** * 添加内容content */ content.add(inflater.inflate(R.layout.f1, null)); content.add(inflater.inflate(R.layout.f2, null)); content.add(inflater.inflate(R.layout.f3, null)); /** * 添加每个内容对应的标题title */ title = new ArrayList<String>(); title.add("体育新闻"); title.add("娱乐新闻"); title.add("军事新闻"); adapter = new MyViewPagerAdapter(this,content,title); viewPager.setAdapter(adapter); } }
ViewPager适配器:
<pre name="code" class="java">package com.fragmentdemo11_viewpager; import java.util.List; import android.content.Context; import android.support.v4.view.PagerAdapter; import android.support.v4.view.ViewPager; import android.view.View; import android.view.ViewGroup; /** * ViewPager适配器 */ public class MyViewPagerAdapter extends PagerAdapter { private Context context; private List<View> content; private List<String> title; public MyViewPagerAdapter(Context context, List<View> content, List<String> title) { this.context = context; this.content = content; this.title = title; } /** * 初始化 */ @Override public Object instantiateItem(ViewGroup container, int position) { ((ViewPager)container).addView(content.get(position)); return content.get(position); } /** * title */ @Override public CharSequence getPageTitle(int position) { return title.get(position); } @Override public int getCount() { return content.size(); } @Override public boolean isViewFromObject(View arg0, Object arg1) { return arg0 == arg1; } /** * 销毁 */ @Override public void destroyItem(ViewGroup container, int position, Object object) { ((ViewPager)container).removeView(content.get(position)); } }
源代码下载: