这是csdn上的以个demo,很适合初学者。来源:http://download.csdn.net/detail/endlife99/7274419,侵删。

MainActiviy:

package com.example.viewpagerdemo;

import java.util.ArrayList;

import android.content.res.Resources;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.view.ViewPager;
import android.support.v4.view.ViewPager.OnPageChangeListener;
import android.util.DisplayMetrics;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.animation.TranslateAnimation;
import android.widget.ImageView;
import android.widget.TextView; public class MainActivity extends FragmentActivity {
private ArrayList<Fragment> list = null;
private ViewPager mViewPager;
private ImageView iv_bottom_line;
private Resources resources;
private TextView zh;
private TextView xw;
private TextView yl;
private TextView ty;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initView();
initWidth();
initViewPager();
}
private void initViewPager() {
Fragment zh = MyFragment.newInstance(Contents.ZH);
Fragment xw = MyFragment.newInstance(Contents.XW);
Fragment yl = MyFragment.newInstance(Contents.YL);
Fragment ty = MyFragment.newInstance(Contents.TY);
list = new ArrayList<Fragment>();
list.add(zh);
list.add(xw);
list.add(yl);
list.add(ty);
mViewPager.setAdapter(new FragmentAdapter(getSupportFragmentManager(),
list));
mViewPager.setCurrentItem(0);
mViewPager.setOnPageChangeListener(new MyViewPagerChangedListener());
}
private void initView() {
mViewPager = (ViewPager) findViewById(R.id.myviewpager);
iv_bottom_line = (ImageView) findViewById(R.id.iv_bottom_line);
zh = (TextView) findViewById(R.id.zh);
xw = (TextView) findViewById(R.id.xw);
yl = (TextView) findViewById(R.id.yl);
ty = (TextView) findViewById(R.id.ty);
zh.setOnClickListener(new MyClickListener(0));
xw.setOnClickListener(new MyClickListener(1));
yl.setOnClickListener(new MyClickListener(2));
ty.setOnClickListener(new MyClickListener(3));
}
private int first = 0;
private int second = 0;
private int third = 0;
private void initWidth() {
int lineWidth = iv_bottom_line.getLayoutParams().width;
Log.d("lineWidth ", lineWidth + "");
DisplayMetrics dm = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(dm);
int width = dm.widthPixels;
resources = getResources();
first = width / 4;
second = first * 2;
third = first * 3;
}
private int currPosition = 0;
class MyViewPagerChangedListener implements OnPageChangeListener {
@Override
public void onPageScrollStateChanged(int arg0) {
}
@Override
public void onPageScrolled(int arg0, float arg1, int arg2) {
}
@Override
public void onPageSelected(int arg0) {
Log.d("onchanged", "onchanged " + arg0);
TranslateAnimation ta = null;
switch (arg0) {
case 0:
if (currPosition == 1) {
ta = new TranslateAnimation(first, 0, 0, 0);
}
if (currPosition == 2) {
ta = new TranslateAnimation(second, 0, 0, 0);
}
if (currPosition == 3) {
ta = new TranslateAnimation(third, 0, 0, 0);
}
break;
case 1:
if (currPosition == 0) {
ta = new TranslateAnimation(0, first, 0, 0);
}
if (currPosition == 2) {
ta = new TranslateAnimation(second, first, 0, 0);
}
if (currPosition == 3) {
ta = new TranslateAnimation(third, first, 0, 0);
}
break;
case 2:
if (currPosition == 0) {
ta = new TranslateAnimation(0, second, 0, 0);
}
if (currPosition == 1) {
ta = new TranslateAnimation(first, second, 0, 0);
}
if (currPosition == 3) {
ta = new TranslateAnimation(third, second, 0, 0);
}
break;
case 3:
if (currPosition == 0) {
ta = new TranslateAnimation(0, third, 0, 0);
}
if (currPosition == 1) {
ta = new TranslateAnimation(first, third, 0, 0);
}
if (currPosition == 2) {
ta = new TranslateAnimation(second, third, 0, 0);
}
break;
}
currPosition = arg0;
ta.setDuration(300);
ta.setFillAfter(true);
iv_bottom_line.startAnimation(ta);
}
}
class MyClickListener implements OnClickListener {
private int index = 0;
public MyClickListener(int i) {
index = i;
}
@Override
public void onClick(View v) {
mViewPager.setCurrentItem(index);
}
}
}

  MyFragment:

package com.example.viewpagerdemo;

import java.util.ArrayList;
import java.util.List; import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.AbsListView;
import android.widget.AbsListView.OnScrollListener;
import android.widget.Button;
import android.widget.ListView; public class MyFragment extends Fragment{ private String key = null; static MyFragment newInstance(String s){
MyFragment myFragment = new MyFragment();
Bundle bundle = new Bundle();
bundle.putString("key", s);
myFragment.setArguments(bundle);
return myFragment; } @Override
public void onCreate(Bundle savedInstanceState) { Bundle bundle = getArguments();
key = bundle != null? bundle.getString("key") : null;
super.onCreate(savedInstanceState);
} private ListView listView; private MyListViewAdapter adapter; private List<String> list ; @Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = null;
Button button = null;
if (key == null || key.equals(Contents.ZH)) {
view = inflater.inflate(R.layout.lay1, container, false);
button = (Button) view.findViewById(R.id.confirm);
button.setOnClickListener(new MyClickListener(1));
} if (key != null && key.equals(Contents.XW)) {
view = inflater.inflate(R.layout.lay2,container,false);
button = (Button) view.findViewById(R.id.confirm02);
button.setOnClickListener(new MyClickListener(2));
} if (key != null && key.equals(Contents.YL)) {
view = inflater.inflate(R.layout.lay3,container,false);
} if (key != null && key.equals(Contents.TY)) {
view = inflater.inflate(R.layout.lay4,container,false);
listView = (ListView) view.findViewById(R.id.mylistview);
list = getdata();
adapter = new MyListViewAdapter(getActivity(),list);
listView.setAdapter(adapter); //listView滑动状态判断
listView.setOnScrollListener(new OnScrollListener() { @Override
public void onScrollStateChanged(AbsListView arg0, int arg1) {
// TODO Auto-generated method stub } @Override
public void onScroll(AbsListView arg0, int firstItem, int visibleItemCount, int totalItemCount) {
// TODO Auto-generated method stub //到达底部
if (firstItem + visibleItemCount == totalItemCount) {
list = getdata();
adapter.notifyDataSetChanged();
}
}
}); } return view;
} private List<String> getdata(){
int size = 0;
if (list != null) {
size = list.size(); }
if (list == null) {
list = new ArrayList<String>();
} for (int i = 0; i < 20; i++) {
list.add("item" + i + size);
}
return list;
} class MyClickListener implements OnClickListener{ int index; public MyClickListener(int i) {
index = i;
} @Override
public void onClick(View v) {
Log.d("onclick", "onclick " + index);
} } }

  MyListViewAdapter:

package com.example.viewpagerdemo;

import java.util.List;

import android.content.Context;
import android.support.v4.app.FragmentActivity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView; public class MyListViewAdapter extends BaseAdapter { List<String>list; LayoutInflater inflater; FragmentActivity activity; public MyListViewAdapter(FragmentActivity fragmentActivity, List<String> list) {
// TODO Auto-generated constructor stub
this.list = list;
this.activity = fragmentActivity;
inflater = (LayoutInflater) fragmentActivity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
} @Override
public int getCount() {
// TODO Auto-generated method stub
return list.size();
} @Override
public Object getItem(int arg0) {
// TODO Auto-generated method stub
return list.get(arg0);
} @Override
public long getItemId(int arg0) {
// TODO Auto-generated method stub
return arg0;
} @Override
public View getView(int arg0, View view, ViewGroup arg2) {
// TODO Auto-generated method stub
if (view == null) {
view = inflater.inflate(R.layout.item, null);
} return view;
} class ViewHolder {
TextView tv;
ImageView im;
} }

  FragmentAdapter:

package com.example.viewpagerdemo;

import java.util.ArrayList;

import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter; public class FragmentAdapter extends FragmentPagerAdapter{ private ArrayList<Fragment> list; public FragmentAdapter(FragmentManager fm) {
super(fm);
} public FragmentAdapter(FragmentManager fm,
ArrayList<Fragment> list) {
super(fm);
this.list = list;
} @Override
public Fragment getItem(int arg0) {
// TODO Auto-generated method stub
return list.get(arg0);
} @Override
public int getCount() {
// TODO Auto-generated method stub
return list.size();
} }

  Contents:

package com.example.viewpagerdemo;

public class Contents {

	public static final String ZH = "zonghe";

	public static final String XW = "xinwen";

	public static final String YL = "yule";

	public static final String TY = "tiyu";

}

  layout文件夹:

<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=".MainActivity" > <LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#20d5f0"
android:paddingBottom="2dp"
android:paddingTop="2dp" > <TextView android:id="@+id/zh"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="center"
android:text="综合"
android:textColor="#fff"
android:textSize="16sp" /> <TextView
android:id="@+id/xw"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="center"
android:text="新闻"
android:textColor="#fff"
android:textSize="16sp" /> <TextView
android:id="@+id/yl"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="center"
android:text="娱乐"
android:textColor="#fff"
android:textSize="16sp" /> <TextView
android:id="@+id/ty"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="center"
android:text="体育"
android:textColor="#fff"
android:textSize="16sp" />
</LinearLayout> <LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="bottom"
android:orientation="vertical"
android:paddingBottom="3dip"
android:background="#20d5f0"
> <ImageView
android:id="@+id/iv_bottom_line"
android:layout_width="40dip"
android:layout_height="2dip"
android:layout_marginLeft="20dip"
android:scaleType="matrix"
android:src="#fff" />
</LinearLayout> <android.support.v4.view.ViewPager
android:id="@+id/myviewpager"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#ffffff" /> </LinearLayout>

  效果图如下:

item.xml:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
xmlns:android="http://schemas.android.com/apk/res/android">
<ImageView
android:id="@+id/itemImage"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:src="@drawable/ic_launcher"
android:layout_alignParentLeft="true" />
<TextView
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:text="前段时间做了一个与LBS有关的项目"
android:layout_toRightOf="@+id/itemImage"
android:layout_centerVertical="true"
/> </RelativeLayout>

  lay1.xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="第一页"
android:gravity="center"
/>
<Button
android:id="@+id/confirm"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="2dp"
android:layout_marginRight="2dp"
android:text="确定"
/>
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="2dp"
android:layout_marginRight="2dp"
android:text="分享"
/>
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="2dp"
android:layout_marginRight="2dp"
android:text="浏览"
/>
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="2dp"
android:layout_marginRight="2dp"
android:text="取消"
/> </LinearLayout>

  lay02.xml,lay03.xml除了最顶端设置的页码不一样,其他都一直,所以之类就不放上其对应的xml了。

lay04:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
>
<ListView
android:id="@+id/mylistview"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
/> </LinearLayout>

  最终做出来的效果图:

listview,fragment结合起来的更多相关文章

  1. Android之ActionBar、Tabs、Fragment、ViewPager实现标签页切换并缓存页面

    感觉 Android 到处都是坑,每个地方都要把人折腾半天. 今天来简单说说 Android之ActionBar.Tabs.Fragment.ViewPager 实现标签页切换并缓存页面 关于他们的介 ...

  2. Android 类似时间轴的实现

    想要实现图片中的的时间轴的效果,设定了三种颜色,但是出来的只有一个黑色,还不是设定好的,而且长度很长的话不能滚动,下面上代码: 布局文件: <LinearLayout xmlns:android ...

  3. 【原版的:参赛作品】窥秘懒---android打开下拉手势刷新时代

    小飒的成长史原创作品:窥视懒人的秘密---android下拉刷新开启手势的新纪元转载请注明出处 **************************************************** ...

  4. 【原创】窥视懒人的秘密---android下拉刷新开启手势的新纪元

    小飒的成长史原创作品:窥视懒人的秘密---android下拉刷新开启手势的新纪元转载请注明出处 **************************************************** ...

  5. 国外源码精品-Android-PullToRefresh 简介与DEMO导入

    转载地址:http://my.oschina.net/cuitongliang/blog/170708 (一)&&http://my.oschina.net/cuitongliang/ ...

  6. Android tab页制作

    全屏启动页 activity_launcher <?xml version="1.0" encoding="utf-8"?> <Relativ ...

  7. ViewPager和Fragment的结合使用fragment里包含着listview的常见问题

    在我们开发的过程中可能会遇到类似需求,我们需要做一个左右滑动的tab导航,这个时候有些朋友可能会想到viewpager和fragment的结合来实现这个功能:当然实现的方法不单单着一种.我们这个随笔讨 ...

  8. Fragment中添加ListView而不使用ListFragment

    最初的构想是,将Fragment和ViewPager结合起来, 然后突发奇想,在第一个Fragment里添加了ListView, 依照网上的建议,extends了ListFragment,接着各种报错 ...

  9. 向 ViewPager 中添加 包含 ListView 的 Fragment

    对与fragment就不说什么了,直接看API手册吧,亲. 向 ViewPager 中添加 包含 ListView 的 Fragment 的过程比较麻烦.他所表现的效果就是新闻客户端的滑动翻页效果. ...

随机推荐

  1. yii教程

    http://www.yiichina.com/doc 官网是很好的参考文档

  2. 文件上传 PHP

    参考http://www.w3school.com.cn/php/php_file_upload.asp 文件上传实际上是一个文件复制的过程  当我们选中一个文件之后  php默认的tmp文件夹中就有 ...

  3. linux 的 ping 原理

    ping命令的工作原理是: ping命令是用来查看网络上另一个主机系统的网络连接是否正常的一个工具. 他向网络上的另一个主机系统发送ICMP报文,如果指定系统得到了报文,它将把报文原样传回给发送者,这 ...

  4. VS2010/MFC设置对话框控件的Tab顺序

    设置对话框控件的Tab顺序 前面几节为大家演示了加法计算器程序完整的编写过程,本节主要讲对话框上控件的Tab顺序如何调整. 上一讲为“计算”按钮添加了消息处理函数后,加法计算器已经能够进行浮点数的加法 ...

  5. JAVA中List、Map、Set的区别与选用

    类层次关系如下: Collection ├List│├LinkedList│├ArrayList│└Vector│ └Stack└SetMap├Hashtable├HashMap └WeakHashM ...

  6. grunt用来压缩前端脚本

    grunt作为一个任务管理工具,提供丰富的插件和强大的自动化管理功能.需要安装node及npm. 主要使用到两个文件,一个是npm的依赖配置文件package.json { "name&qu ...

  7. 1043 - Triangle Partitioning(数学)

    1043 - Triangle Partitioning   PDF (English) Statistics Forum Time Limit: 0.5 second(s) Memory Limit ...

  8. [置顶] JDK-Future 模式和实现

    最近的项目用到了多线程,发现java.util.concurrent.Future蛮好用的. 像平时,写多线程一般使用Thread/Runnable,直接扔给线程池执行就好了.但是遇到了一些需要获取线 ...

  9. React Native-目前最火的前端技术?

    做为一名产品经理,你是否遇到过这样的窘境,“帮我把字体调成 16号呗,颜色变成 #FFFF00FF,老大说这里最好改一下”,作为一名 app 的开发只能无奈但心里窃喜的告诉你,“只能等下个版本了,必须 ...

  10. UVA 674 Coin Change(dp)

    UVA 674  Coin Change  解题报告 题目链接:http://acm.hust.edu.cn/vjudge/contest/view.action?cid=87730#problem/ ...