我们要做的是在Activity中加入一个ViewPager,利用ViewPager的适配器(继承于FragmentPagerAdapter)将Fragment加到其中,而我们在又在Fragment中又加入了一个ListVIew,这又要一个继承与BaseAdapter的适配器。
当我们滑动ViewPager时,顶栏也联动滑动,这需要一个第三方组件,PagerSlidingTabStrip。导包等一系列步骤暂不说明。
1、首先,建立一个Activity,里面有ViewPager和PagerSlidingTabStrip
代码如下:
import android.os.Bundle;import android.support.v4.app.Fragment;import android.support.v4.view.ViewPager;import android.support.v7.app.AppCompatActivity; import com.astuetz.PagerSlidingTabStrip;import com.example.administrator.homeworepar.Fragment.adapter.MyFragmentAdapter;import com.example.administrator.homeworepar.Fragment.adapter.PagerFragment;import com.example.administrator.homeworepar.R; import java.util.ArrayList;import java.util.List; public class FragmentPagerActivity extends AppCompatActivity { private ViewPager vp; private PagerSlidingTabStrip pst; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_fragment_pager); vp= (ViewPager) findViewById(R.id.vp); pst=(PagerSlidingTabStrip)findViewById(R.id.pst); List<Fragment> list=new ArrayList<>(); List<String> titles=new ArrayList<>(); //设置顶栏 titles.add("体育"); titles.add("娱乐"); titles.add("财经"); titles.add("国际"); titles.add("奥运"); for(int i=1;i<=5;i++){ //Bundle的作用为在Activity和Fragment中传值 Fragment fragment=new PagerFragment(); Bundle bundle=new Bundle(); bundle.putInt("arg",i); fragment.setArguments(bundle); list.add(fragment); } MyFragmentAdapter ma=new MyFragmentAdapter( //适配器名称 getSupportFragmentManager(),list,titles); vp.setAdapter(ma); pst.setIndicatorColor(getResources().getColor(R.color.colorPrimary)); //顶栏的各种属性 pst.setIndicatorHeight(3); pst.setUnderlineColor(getResources().getColor(R.color.colorAccent)); pst.setTextColor(getResources().getColor(R.color.colorPrimary)); pst.setDividerColor(getResources().getColor(R.color.color1)); //pst.setShouldExpand(true); 当标题过少时,可以设置标题均分标题栏 // pst.setTabBackground(5); pst.setViewPager(vp); }}
MyFragmentAdapter 适配器
把Fragment放到ViewPager
public class MyFragmentAdapter extends FragmentPagerAdapter {
private List<Fragment> list;
private List<String> titles;
public MyFragmentAdapter(FragmentManager fm,List<Fragment> list,List<String> titles) {
super(fm);
this.list=list;
this.titles=titles;
}
@Override
public Fragment getItem(int position) {
return list.get(position);
}
@Override
public int getCount() {
return list.size();
}
@Override
public CharSequence getPageTitle(int position) {
return titles.get(position);
}
}
再把ListView放到Fragment中,Fragment中建立一个ListView,在ListView中放一个NewsOne类,
还需要一个适配器,扩展BaseAdapter,作用是把新闻类数据传到ListView中
Fragment中的代码如下:
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.ListView;
import com.example.administrator.homeworepar.R;
import java.util.ArrayList;
import java.util.List;
/**
* A simple {@link Fragment} subclass.
*/
public class PagerFragment extends Fragment {
public PagerFragment() {
// Required empty public constructor
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
return inflater.inflate(R.layout.fragment_pager, container, false);
}
@Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
ListView lv= (ListView) getView().findViewById(R.id.lv);
List<NewsOne> list=new ArrayList<>(); //将新闻类放到Fragment中
MyListViewAdapter ma=new MyListViewAdapter(list,getActivity());
lv.setAdapter(ma);
Bundle bundle=getArguments();
if(bundle!=null){
int arg= bundle.getInt("arg");
switch (arg){
case 1: //将Bundle传来的值得到,设置五个左右滑动的页面
for(int i=1;i<20;i++){
list.add(new NewsOne(R.mipmap.image12,"刚刚","体育新闻"+i));
}
ma.notifyDataSetChanged();
break;
case 2:
for(int i=1;i<20;i++){
list.add(new NewsOne(R.mipmap.image12,"刚刚","奥运集锦"+i));
}
ma.notifyDataSetChanged();
break;
case 3:
for(int i=1;i<20;i++){
list.add(new NewsOne(R.mipmap.image12,"刚刚","国际新闻"+i));
}
ma.notifyDataSetChanged();
break;
case 4:
for(int i=1;i<20;i++){
list.add(new NewsOne(R.mipmap.image12,"刚刚","财经新闻"+i));
}
ma.notifyDataSetChanged();
break;
case 5:
for(int i=1;i<20;i++){
list.add(new NewsOne(R.mipmap.image12,"刚刚","国内新闻"+i));
}
ma.notifyDataSetChanged();
break;
}
}
}
}
名为MyListViewAdapter的适配器,作用是将News类加到Fragment中
package com.example.administrator.homeworepar.Fragment.adapter;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;
import com.example.administrator.homeworepar.R;
import java.util.List;
/**
* Created by Administrator on 2016/8/9.
*/
public class MyListViewAdapter extends BaseAdapter {
private List<NewsOne> list;
private Context context;
public MyListViewAdapter(List<NewsOne> list, Context context) {
this.list = list;
this.context = context;
}
@Override
public int getCount() {
return list.size();
}
@Override
public Object getItem(int position) {
return list.get(position);
}
@Override
public long getItemId(int position) {
return 0;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder vh;
if(convertView==null){
vh=new ViewHolder();
convertView= LayoutInflater.from(context).inflate(R.layout.list_layout,null);
vh.img=(ImageView)convertView.findViewById(R.id.img);
vh.title=(TextView)convertView.findViewById(R.id.title);
vh.pubdate=(TextView)convertView.findViewById(R.id.pubDate);
convertView.setTag(vh);
}else{
vh=(ViewHolder)convertView.getTag();
}
NewsOne newsOne=list.get(position);
vh.img.setImageResource(newsOne.getImg());
vh.title.setText(newsOne.getTitle());
vh.pubdate.setText(newsOne.getPubDate());
return convertView;
}
private class ViewHolder{
ImageView img;
TextView title;
TextView pubdate;
}
}