1.布局文件activity_page_view.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="com.fsg.thinkpad.helloworld.PageViewActivity">
<android.support.v7.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="fill_parent"
android:layout_height="?attr/actionBarSize"
android:background="?attr/colorPrimary"
android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
/>
<android.support.design.widget.TabLayout
android:id="@+id/tabs"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#c4ffae"
></android.support.design.widget.TabLayout>
<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>
2.定义Fragment
package com.gst.thinkpad.helloworld;
import android.content.Context;
import android.content.Intent;
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.Button;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;
import java.util.ArrayList;
import java.util.List;
/**
* Created by ThinkPad on 2015/9/17.
*/
public class MyFragmentActivityFragment2 extends Fragment {
private static final String TAG="MyFragmentActivityFragment";
private ListView listView;
/*跳转到新页面的按钮*/
private Button mJumpToNewButton;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
//标题列表
String[] titles=new String[]{"花语大全",
"白玫瑰",
"薰衣草",
"红色风信子",
"时钟花",
"油桐花",
"樱花",
"黑色曼陀罗",
"蓝色妖姬/蓝玫瑰",
"红蔷薇",
"白蔷薇",
"三叶草",
"彼岸花",
"栀子花",
"昙花"
};
//图片列表1
// int[] imageID=new int[]{R.mipmap.a1,R.mipmap.a2,R.mipmap.a3,
// R.mipmap.a4,R.mipmap.a5,R.mipmap.a6,R.mipmap.a7
// ,R.mipmap.a8,R.mipmap.a9,R.mipmap.a10};
//图片列表2
int[] imageID=new int[]{R.mipmap.f0,R.mipmap.f1,R.mipmap.f2,
R.mipmap.f3,R.mipmap.f4,R.mipmap.f5,R.mipmap.f6
,R.mipmap.f7,R.mipmap.f8,R.mipmap.f9,R.mipmap.f10
,R.mipmap.f11,R.mipmap.f12,R.mipmap.f13,R.mipmap.f14};
//网址列表
String[] urls=new String[]{"http://www.xingzuo360.cn/huayu/117922.html",
"http://www.xingzuo360.cn/huayu/meigui/38606.html",
"http://www.xingzuo360.cn/huayu/gzhy/39113.html",
"http://www.xingzuo360.cn/huayu/fxz/39111.html",
"http://www.xingzuo360.cn/365shengrihuayu/12402.html",
"http://www.d1xz.net/astro/huayu/art67462.aspx",
"http://www.xingzuo360.cn/huayu/gzhy/55244.html",
"http://www.xingzuo360.cn/huayu/gzhy/55261.html",
"http://www.xingzuo360.cn/huayu/gzhy/55251.html",
"http://www.xingzuo360.cn/huayu/gzhy/55270.html",
"http://www.xingzuo360.cn/huayu/qw/39109.html",
"http://www.meilele.com/article/shipin/25511.html",
"http://baike.baidu.com/link?url=VJGXXr0HBMcVSkmmWkBa6GUQWh0SmNC-BCSIl-Jq2DCEUn6Av_qjolg1rw4R__vgAfy9qI6BeuZS3_stD5LmTq",
"http://www.xingzuo360.cn/huayu/gzhy/39107.html",
"http://www.xingzuo360.cn/huayu/hanjianhua/68768.html"
};
String[] descriptions=new String[]{"各种花的图片与花语",
"我足以与你相配,你是唯一与我相配的人",
"等待爱情(个人很喜欢的花,花语也很唯美)",
"让人感动的爱",
"爱在你身边",
"情窦初开",
"生命/等你回来(很浪漫的花)",
"无间的爱和复仇,绝望的爱,不可预知的死亡和爱",
"相守是一种承诺,人世轮回中,怎样才能拥有一份温柔的情意!\n" +
"双枝蓝色妖姬花语:相遇是一种宿命,心灵的交汇让我们诉不尽的浪漫情怀。\n" +
"三枝蓝色妖姬,送花人表达的是另一种情愫-你是我最深的爱恋,希望永远铭记我们这段美丽的爱情故事!",
"热恋",
"纯洁的爱情",
"一叶代表祈求,二叶代表希望,三叶代表爱情,四叶代表幸福(很幸运的草哦)",
"悲伤的回忆/相互思念",
"永恒的爱/一生的守侯/我们的爱",
"刹那的美丽,一瞬间永恒(昙花一现嘛)"
};
//加载布局文件
View rootView = inflater.inflate(R.layout.activity_list_view, container, false);
listView=(ListView)rootView.findViewById(R.id.listView);
List<News> list=new ArrayList<News>();
for(int i=0;i<15;i++){
News news=new News();
news.setTitle(titles[i]);
news.setDescription(descriptions[i]);
news.setImageID(imageID[i]);
news.setUrlPath(urls[i]);
list.add(news);
}
ListAdapter listAdapter=new ListAdapter(getActivity(),list);
listView.setAdapter(listAdapter);
return rootView;
}
3.定义继承FragmentStatePagerAdapter的Adapter.
package com.gst.thinkpad.helloworld;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentStatePagerAdapter;
import java.util.List;
/**
* Created by ThinkPad on 2015/9/21.
*/
public class FragmentAdapter extends FragmentStatePagerAdapter{
//Fragment列表
List<Fragment> mFragments;
//标题列表
List<String> mTitles;
public FragmentAdapter(FragmentManager fm, List<String> mTitles, List<Fragment> mFragments) {
super(fm);
this.mTitles = mTitles;
this.mFragments = mFragments;
}
public CharSequence getPageTitle(int position) {
return mTitles.get(position);
}
public int getCount() {
return mFragments.size();
}
@Override
public Fragment getItem(int position) {
return this.mFragments.get(position);
}
}
4.绑定数据,视图
package com.gst.thinkpad.helloworld;
import android.content.Intent;
import android.os.Bundle;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Toast;
import java.util.ArrayList;
import java.util.List;
public class PageViewActivity extends AppCompatActivity {
TabLayout mTabLayout;
ViewPager viewPager;
Toolbar mToolbar;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_page_view);
mTabLayout=(TabLayout)findViewById(R.id.tabs);
viewPager=(ViewPager)findViewById(R.id.viewpager);
mToolbar=(Toolbar)findViewById(R.id.toolbar);
//设置菜单栏
mToolbar.setNavigationIcon(R.mipmap.ic_menu);
mToolbar.setTitle("个人中心");
mToolbar.setNavigationOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(PageViewActivity.this,"单击了菜单栏",Toast.LENGTH_SHORT).show();
Intent intent=new Intent(PageViewActivity.this,TakePhotoActivity.class);
startActivity(intent);
}
});
startupViewPager();
}
public void startupViewPager(){
//初始化标题
List<String> titles=new ArrayList<String>();
titles.add("中秋佳节");
titles.add("花语大全");
titles.add("自然景观");
//增加Table
mTabLayout.addTab(mTabLayout.newTab().setText(titles.get(0)));
mTabLayout.addTab(mTabLayout.newTab().setText(titles.get(1)));
mTabLayout.addTab(mTabLayout.newTab().setText(titles.get(2)));
//增加Fragment
List<Fragment> mFragmentList=new ArrayList<Fragment>();
mFragmentList.add(new MyFragmentActivityFragment());
mFragmentList.add(new MyFragmentActivityFragment2());
mFragmentList.add(new MyFragmentActivityFragment3());
//通过Adapter关联
FragmentAdapter adapter=new FragmentAdapter(getSupportFragmentManager(), titles, mFragmentList);
viewPager.setAdapter(adapter);
mTabLayout.setupWithViewPager(viewPager);
mTabLayout.setTabsFromPagerAdapter(adapter);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_page_view, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id==android.R.id.home){
Toast.makeText(PageViewActivity.this,"单击了菜单栏",Toast.LENGTH_SHORT).show();
}
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}