近期在做一个须要使用Frament+ViewPage制作一个滑动的效果,看了非常多资料,最终实现了,这与大家分享一下战果

总结一下。这里我做了一个Demo分享给大家

我的文件文件夹结构图

1。首先要有一个ViewPage组件,他是3.0以后出现的,所以要导入android.support.v4这个包

先来建立一个mian布局文件

activity_main.xml

这个布局使用RadioGroup和RadioButton组合,在上面显示第一页。和第二页。以下就是ViewPage

<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" 

    android:background="#ffffff"

    >



    <RadioGroup

        android:id="@+id/radio_group"

        android:layout_width="match_parent"

        android:layout_height="wrap_content"    

        android:orientation="horizontal" >



        <RadioButton

            android:id="@+id/first"

            android:layout_width="wrap_content"

            android:layout_height="wrap_content"

            android:layout_weight="1.0"

            android:background="@drawable/chat_tab_selector_2"

            android:button="@null"

            android:checked="true"

            android:gravity="center"

            android:text="第一页"

            android:textColor="#000000"

            android:textSize="18sp" />

        <RadioButton

            android:id="@+id/second"

            android:layout_width="wrap_content"

            android:layout_height="wrap_content"

            android:layout_weight="1.0"

            android:background="@drawable/chat_tab_selector_2"

            android:button="@null"

            android:gravity="center"

            android:text="第二页"

            android:textColor="#000000"

            android:textSize="18sp" />

    </RadioGroup>

    <android.support.v4.view.ViewPager

        android:id="@+id/view_pager"

        android:layout_width="match_parent"

        android:layout_height="match_parent"

        android:layout_below="@id/radio_group" >

    </android.support.v4.view.ViewPager>



</RelativeLayout>

然后在MainActivity中代码:

注意这里一定要继承FragmentActivity ,不然是Frament管理器是建立不了滴。

package com.example.activity;

import java.util.ArrayList;

import java.util.List;

import android.os.Bundle;

import android.support.v4.app.Fragment;

import android.support.v4.app.FragmentActivity;

import android.support.v4.app.FragmentManager;

import android.support.v4.app.FragmentPagerAdapter;

import android.support.v4.view.ViewPager;

import android.support.v4.view.ViewPager.OnPageChangeListener;

import android.widget.RadioGroup;

import android.widget.RadioGroup.OnCheckedChangeListener;





import com.example.content.NumberConstant;

import com.example.frament.MyExampleFragment;

import com.example.viewpagedemo.R;



public class MainActivity extends FragmentActivity implements OnCheckedChangeListener {

private RadioGroup radioGroup;

private ViewPager viewPager;

private List<MyExampleFragment> pagerList = new ArrayList<MyExampleFragment>();



@Override

protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.activity_main);

radioGroup = (RadioGroup) findViewById(R.id.radio_group);

viewPager = (ViewPager) findViewById(R.id.view_pager);

radioGroup.check(R.id.first);

radioGroup.setOnCheckedChangeListener(this);// 设置监听器

pagerList.clear();

initDataFrament();//初始化Frament



}



/**

这里我用的是同一个Frament展示不同的数据,我的这个Demo里展示的数据类似,所哟不须要用两个Frament,假设你有须要能够建立多个Frament,然后我还传递了FramentType来区分,展示不同的数据

*/

public void initDataFrament() {

MyExampleFragment fragment1 = MyExampleFragment.newInstance();

Bundle bundle1 = new Bundle();

bundle1.putString("fragmentType", "fragment1");

fragment1.setArguments(bundle1);

pagerList.add(fragment1);





MyExampleFragment fragment2 = MyExampleFragment.newInstance();

Bundle bundle2 = new Bundle();

bundle2.putString("fragmentType", "fragment2");

fragment2.setArguments(bundle2);

pagerList.add(fragment2);



MyPageListAdpter pageAdapter = new                   MyPageListAdpter(this.getSupportFragmentManager(),pagerList,radioGroup);

viewPager.setAdapter(pageAdapter);//设置适配器

viewPager.setOnPageChangeListener(pageAdapter);//设置监听

}





@Override

public void onCheckedChanged(RadioGroup group, int checkedId) {

if (checkedId == R.id.first) {

viewPager.setCurrentItem(NumberConstant.ZERO, true);//这里是两个常量来区分是欢动哪一个frament

} else if (checkedId == R.id.second) {

viewPager.setCurrentItem(NumberConstant.ONE, true);

}

}



/**

* 定义一个适配器实现滑动

* @author shaozucheng

*

*/

private class MyPageListAdpter extends FragmentPagerAdapter implements OnPageChangeListener {





/**

* 保存滑动子页的list

*/

private List<MyExampleFragment> pagerList;



/**

* 标签页

*/

private RadioGroup selectTag;



public MyPageListAdpter(FragmentManager fm, List<MyExampleFragment> pagerList, RadioGroup selectTag) {

super(fm);

this.pagerList = pagerList;

this.selectTag = selectTag;

}



@Override

public Fragment getItem(int arg0) {

return pagerList.get(arg0);

}



@Override

public int getCount() {

return pagerList.size();

}



@Override

public void onPageScrollStateChanged(int arg0) {



}



@Override

public void onPageScrolled(int arg0, float arg1, int arg2) {



}



@Override

public void onPageSelected(int arg0) {

if (arg0 == 0) {

selectTag.check(R.id.first);

} else if (arg0 == 1) {

selectTag.check(R.id.second);

}

}



}



}

然后就是要先建立一个Frament布局。这个布局里就是存放展示数据的listView的了由于你是要在Frament里展示的。

文件名称为:example_widget_listview.xml

<?xml version="1.0" encoding="utf-8"?>

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"

    android:layout_width="match_parent"

    android:layout_height="match_parent"

    android:orientation="vertical" >





    <ListView

        android:id="@+id/example_listview"

        android:layout_width="match_parent"

        android:layout_height="match_parent"

        android:background="@null"//去掉背景色

        android:divider="@null"//去掉线条

        android:scrollbars="none"//去掉滚动栏

        android:visibility="visible" >

    </ListView>

</LinearLayout>

然后就用写在Frament中初始化数据了,或者你也能够在Activity中初始化数据,然后传到Frament中。那样Frament仅仅负责展示数据,两种方式都能够

注意这里的自己定义的Frament要继承Frament

package com.example.frament;

import java.util.ArrayList;

import java.util.List;

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.adapter.PersonListAdapter;

import com.example.ato.Person;

import com.example.viewpagedemo.R;





/**

 * 

 * @author shaozucheng

 * 

 */



public class MyExampleFragment extends Fragment {





/**

* 列表

*/

private ListView listView;

/**

* 适配器

*/

private PersonListAdapter adapter;





public PersonListAdapter getAdapter() {

return adapter;

}





public void setAdapter(PersonListAdapter adapter) {

this.adapter = adapter;

}





public static MyExampleFragment newInstance() {

MyExampleFragment fragment = new MyExampleFragment();

return fragment;

}





public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

View view = inflater.inflate(R.layout.example_widget_listview, container, false);

listView = (ListView) view.findViewById(R.id.example_listview);

initMettingFrament();

return view;

}





/**

* 初始化数据源

*/

private void initMettingFrament() {

Person p0 = new Person();

Person p1 = new Person();

Person p2 = new Person();

p0.setName("荔枝");

p1.setName("香蕉");

p2.setName("苹果");





Person p3 = new Person();

Person p4 = new Person();

Person p5 = new Person();

p3.setName("黎明");

p4.setName("邓超");

p5.setName("董洁");





List<Person> list1 = new ArrayList<Person>();

list1.add(p1);

list1.add(p2);

list1.add(p0);





List<Person> list2 = new ArrayList<Person>();

list2.add(p3);

list2.add(p4);

list2.add(p5);





String framentType = this.getArguments().getString("fragmentType");

if (framentType.equals("fragment1")) {





adapter = new PersonListAdapter(getActivity(), list1);

} else if (framentType.equals("fragment2")) {

adapter = new PersonListAdapter(getActivity(), list2);

}

listView.setAdapter(adapter);//设置展示数据的适配器

listView.setOnItemClickListener(adapter);//设置监听。点击item每一栏的跳转事件,假设不须要的话,你能够不用设置

}





}

items_list.xml布局文件

<?xml version="1.0" encoding="utf-8"?>

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"

    android:id="@+id/forum_toipc_list"

    android:layout_width="match_parent"

    android:layout_height="50dp"

    android:background="#ffffff" >





    <RelativeLayout

        android:id="@+id/rel_conrent"

        android:layout_width="match_parent"

        android:layout_height="wrap_content"

        android:layout_marginLeft="5dp" >



<!--为了效果好看我在每个item里面放了一张固定大小的图片  -->

        <ImageView

            android:id="@+id/image"

            android:layout_width="50dp"

            android:layout_height="50dp"

            android:layout_centerVertical="true"

            android:layout_marginTop="5dp"

            android:contentDescription="@null"

            android:paddingTop="5dp"

            android:src="@drawable/image1"

            android:visibility="visible" />





        <TextView

            android:id="@+id/content"

            android:layout_width="wrap_content"

            android:layout_height="match_parent"

            android:layout_marginLeft="5dp"

            android:layout_toRightOf="@id/image"

            android:gravity="center_vertical"

            android:layout_centerVertical="true"

            android:textColor="#ff0000"

            android:textSize="18sp" />

    </RelativeLayout>





    <ImageView

        android:id="@+id/item_forum_line"

        android:layout_width="match_parent"

        android:layout_height="wrap_content"

        android:layout_below="@id/rel_conrent"

        android:background="@drawable/list_divide_line"

        android:contentDescription="@null"

        android:visibility="visible" />





</RelativeLayout>

然后须要一个适配器展示item

package com.example.adapter;





import java.util.List;





import android.app.Activity;

import android.content.Context;

import android.content.Intent;

import android.view.LayoutInflater;

import android.view.View;

import android.view.ViewGroup;

import android.widget.AdapterView;

import android.widget.AdapterView.OnItemClickListener;

import android.widget.BaseAdapter;

import android.widget.TextView;





import com.example.activity.SecondActivity;

import com.example.ato.Person;

import com.example.viewpagedemo.R;





/**

 * 

 * @author shaozucheng

 * 

 */

public class PersonListAdapter extends BaseAdapter implements OnItemClickListener {





/**

* 自己定义图层

*/

private LayoutInflater inflater;





private Activity mActivity;





private List<Person> list;





public PersonListAdapter(Activity mActivity, List<Person> list) {

this.mActivity = mActivity;

this.list = list;

}





@Override

public int getCount() {

return list.size();

}





@Override

public Object getItem(int position) {

return null;

}





@Override

public long getItemId(int position) {

return position;

}





@Override

public View getView(int position, View convertView, ViewGroup parent) {

ViewHolder holder = null;

if (convertView == null) {

inflater = (LayoutInflater) mActivity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);

convertView = inflater.inflate(R.layout.items_list, null);

holder = new ViewHolder();

holder.content = (TextView) convertView.findViewById(R.id.content);

convertView.setTag(holder);

} else {

holder = (ViewHolder) convertView.getTag();

}





Person dto = list.get(position);

if (dto != null) {

holder.content.setText(dto.getName());

}





return convertView;

}





/**

* 自己定义内部类



* @author shaozucheng



*/

class ViewHolder {

TextView content;// 内容





}

/**

实现OnItemClickListener 接口。然后在这个监听器里就能够跳转到下一个Activity了

*/

@Override

public void onItemClick(AdapterView<?

> arg0, View arg1, int arg2, long arg3) {

Intent intent = new Intent(mActivity, SecondActivity.class);

mActivity.startActivity(intent);

}





}

点击每个Activity跳转到SecondActivity,在SecondActivty 中你能够做你须要做的业务了,这里我就写下去了,依据你项目的须要

package com.example.activity;

import com.example.viewpagedemo.R;

import android.app.Activity;

import android.os.Bundle;



public class SecondActivity extends Activity {

@Override

protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

   setContentView(R.layout.activity_second);

}

}

SecondActivity中的布局文件 activity_second.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"

    android:background="#ffffff" >





    <Button

        android:layout_width="match_parent"

        android:layout_height="wrap_content"

        android:text="secondActivity" />





</RelativeLayout>

不要忘记了在AndroidManifest.xml注冊一下SecondActivity。

最后的效果图

这里提供一个源码下载地址:不懂的能够去下载看一下:须要3个积分哟点击打开链接

ViewPage+Frament+listView滑动效果的更多相关文章

  1. ListView滑动删除效果实现

    通过继承ListView然后结合PopupWindow实现 首先是布局文件: delete_btn.xml:这里只需要一个Button <?xml version="1.0" ...

  2. ViewPage+frament不预载入下一个Frament数据解决的方法

    在做一个ViewPage+Frament 滑动数效果,当滑动到每一页时载入哪一页的数据,可是ViewPage会预载入下一也数据.这个问题之前做项目是一直未解决,今天找到一个方法一下子就解决的这个问题, ...

  3. Android——用PagerAdapter实现View滑动效果

    效果: ViewPage来源于android -support.v4 什么是viewPage?ViewPage 类似于ListView 用于显示多个View集合. 支持页面左右滑动. 如何使用view ...

  4. Android实现多页左右滑动效果,支持子view动态创建和cache

    要实现多页滑动效果,主要是需要处理onTouchEvent和onInterceptTouchEvent,要处理好touch事件的子控件和父控件的传递问题. 滚动控制可以利用android的Scroll ...

  5. Jquery Mobile左右滑动效果

    首先在一个页面里面定义两个< div data-role="page">,这里为了突出重点,就没有写出footer和header.定义的页面如下: <body&g ...

  6. 仿饿了吗点餐界面两个ListView联动效果

    这篇文章主要介绍了仿饿了点餐界面2个ListView联动效果的相关资料,非常不错,具有参考借鉴价值,需要的朋友可以参考下 如图是效果图: 是仿饿了的点餐界面 1.点击左侧的ListView,通过在在适 ...

  7. Scroller应用:ListView滑动删除

    1.设计思路 在Scroller的应用--滑屏实现中使用Scroller实现滑屏效果,这里使用Scroller与ListView实现相似QQ滑动.然后点击删除功能.设计思路是Item使用Scrolle ...

  8. 【Android UI】顶部or底部菜单的循环滑动效果一

    实现了分页的滑动效果,做的demo流畅运行 注:貌似支持的样式(控件)有一定的限制,我试过短信的listview页面,暂无法实现滑动效果 java文件:MainActivity.java.Activi ...

  9. Android Animation动画实战(一): 从布局动画引入ListView滑动时,每一Item项的显示动画

    前言: 之前,我已经写了两篇博文,给大家介绍了Android的基础动画是如何实现的,如果还不清楚的,可以点击查看:Android Animation动画详解(一): 补间动画 及 Android An ...

随机推荐

  1. Gym - 101981I The 2018 ICPC Asia Nanjing Regional Contest I.Magic Potion 最大流

    题面 题意:n个英雄,m个怪兽,第i个英雄可以打第i个集合里的一个怪兽,一个怪兽可以在多个集合里,有k瓶药水,每个英雄最多喝一次,可以多打一只怪兽,求最多打多少只 n,m,k<=500 题解:显 ...

  2. php 图片生成器

    一.需求 最近公司由于有大量的海报要做,而且海报的布局规模都是一样的,只是内容不同,所以老板想我开发一个图片的生成器.可以根据你输入的内容生成海报图片. 具体有需求有以下的需求 1.可以根据将每条数据 ...

  3. python 4:str.lstrip()、str.rstrip()、str.strip()(分别去除首空格,尾空格,首尾空格;不改变原有变量,除非赋给)

    name = " Hello,World! Hello,Python! " print(name + "检测行末空格的") print(name.lstrip( ...

  4. Jquery中绑定事件的异同

    谈论jquery中bind(),live(),delegate(),on()绑定事件方式 1. Bind() $(selector).bind(event,data,function) Event:必 ...

  5. JQuery 遍历没有id的控件

    html代码: <tr> <td field="ck" > <div><input type="checkbox" / ...

  6. SQL Server中char与varchar数据类型区别

    在SQL Server中char类型的长度是不可变的,而varchar的长度是可变的 . 存入数据时: 如果数据类型为char时,当定义一个字段固定长度时,如果存进去数据长度小于char的长度,那么存 ...

  7. android 将手机号中间隐藏为星号(*)

    ){ StringBuilder sb =new StringBuilder(); ; i < pNumber.length(); i++) { char c = pNumber.charAt( ...

  8. Leetcode0092 & 0206--Reverse Linked List 链表逆转

    [转载请注明]http://www.cnblogs.com/igoslly/p/8670038.html 链表逆序在链表题目中还是较为常见的,这里将Leetcode中的两道题放在一起,分别是 0092 ...

  9. classNum 表示学生的班号,例如“class05”。 有如下List  List list = new ArrayList();

    package a927; import java.util.ArrayList; import java.util.List; class Student { private String name ...

  10. C# 获取所有网卡信息

    private void Form1_Load(object sender, EventArgs e) { //获取说有网卡信息 NetworkInterface[] nics = NetworkIn ...