这是在我写的新闻App中实现的界面切换

贴出切换界面的主要代码:

xml代码:

<span style="font-size:14px;">    <android.support.v4.view.ViewPager
android:id="@+id/viewpager"
android:layout_width="wrap_content"
android:layout_height="0dp"
android:layout_weight="8"
android:flipInterval="30"
android:persistentDrawingCache="animation" /> </span>

Activity类代码:

<span style="font-size:14px;">public class MainActivity extends FragmentActivity implements OnClickListener{
private Button bottom_button_one = null;
private Button bottom_button_two = null;
private Button bottom_button_three = null;
private Button top_break = null;
private Button top_menu = null;
private Button menu_home = null;
private Button menu_refresh = null;
private Button menu_search = null;
private LinearLayout bottom_back = null;
private XinwenFragment xinwen = null;
private TupianFragment tupian = null;
private ShipinFragment shipin = null;
private LayoutInflater inflater = null;
private View view =null;
private PopupWindow pop = null;
//构造适配器
private List<Fragment> fragments = null;
//设定适配器
private ViewPager vp = null;
private FragmentManager fm = null;
private FragmentAdapter adapter = null;
@SuppressLint("InflateParams")
@SuppressWarnings("deprecation")
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//让进度条显示在标题栏上
requestWindowFeature(Window.FEATURE_PROGRESS);
setContentView(R.layout.activity_main); xinwen = new XinwenFragment();
tupian = new TupianFragment();
shipin = new ShipinFragment();
fragments = new ArrayList<Fragment>();
fragments.add(xinwen);
fragments.add(tupian);
fragments.add(shipin); fm = this.getSupportFragmentManager();
adapter = new FragmentAdapter(fm, fragments);
vp = (ViewPager) findViewById(R.id.viewpager);
vp.setAdapter(adapter); bottom_button_one = (Button) findViewById(R.id.bottom_button_one);
bottom_button_two = (Button) findViewById(R.id.bottom_button_two);
bottom_button_three = (Button) findViewById(R.id.bottom_button_three);
top_break = (Button) findViewById(R.id.top_break);
top_menu = (Button) findViewById(R.id.top_menu); bottom_back = (LinearLayout) findViewById(R.id.bottom_back);
bottom_button_one.setOnClickListener(this);
bottom_button_two.setOnClickListener(this);
bottom_button_three.setOnClickListener(this);
top_break.setOnClickListener(this);
top_menu.setOnClickListener(this); inflater = LayoutInflater.from(this);
//引入窗口配置文件
view = inflater.inflate(R.layout.menu_layout, null);
menu_home = (Button) view.findViewById(R.id.menu_home);
menu_refresh = (Button) view.findViewById(R.id.menu_refresh);
menu_search = (Button) view.findViewById(R.id.menu_search);
menu_home.setOnClickListener(this);
menu_refresh.setOnClickListener(this);
menu_search.setOnClickListener(this);
//创建PopupWindow对象
pop= new PopupWindow(view, LayoutParams.WRAP_CONTENT,
LayoutParams.WRAP_CONTENT, false);
//需要设置一下此参数,点击外边可消息
pop.setBackgroundDrawable(new BitmapDrawable());
//设置点击窗口外边窗口消失
pop.setFocusable(true); //设置默认的Fragment
setDefaultFragment();
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
return super.onOptionsItemSelected(item);
} private void setDefaultFragment(){
//注意:remove会销毁Fragment,hide不销毁只是隐藏,所有add方法要重新new
vp.setCurrentItem(0,false); //false表示取消滑动效果,true则有滑动效果
}
//弹出带输入框的对话框
@SuppressLint("InflateParams")
private void inputTitleDialog(){
/*
Android中得到布局文件对象有两种方式
第一种,在Activity所在类中
this.getLayoutInflater().inflater(R.layout.布局文件名,null);
第二种,在非Activity所在类中
Context.getSystemService(Context.LAYOUT_INFLATER_SERVICE).inflater(R.layout.布局文件名,null);
*/
View view = this.getLayoutInflater().inflate(R.layout.dialog_layout, null);
final EditText text = (EditText) view.findViewById(R.id.edit_search);
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle("对话框标题")
.setIcon(R.drawable.ic_launcher)
.setView(view)
.setNegativeButton("取消", null)
.setPositiveButton("确定", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
String str = text.getText().toString();
if(!str.equals("")){
if(str.indexOf("http://")==0){
xinwen.wv.loadUrl(str);
}else{
xinwen.wv.loadUrl("http://www.baidu.com/s?wd="+str);
}
}
}
});
builder.show();
}
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.bottom_button_one:
bottom_back.setBackgroundResource(R.drawable.bottom_one);
vp.setCurrentItem(0, false);
break;
case R.id.bottom_button_two:
bottom_back.setBackgroundResource(R.drawable.bottom_two);
vp.setCurrentItem(1, false);
break;
case R.id.bottom_button_three:
bottom_back.setBackgroundResource(R.drawable.bottom_three);
vp.setCurrentItem(2, false);
break;
case R.id.top_break:
if(xinwen!=null)
xinwen.wv.goBack(); //后退
break;
case R.id.top_menu:
if(pop.isShowing()){
//隐藏窗口,如果设置了点击窗口外小时即不需要此方法隐藏
pop.dismiss();
}else{
//显示窗口
pop.showAsDropDown(v);
}
// openOptionsMenu(); //显示menu菜单项
break;
case R.id.menu_home:
pop.dismiss();
xinwen.wv.loadUrl("http://news.sina.com.cn");
break;
case R.id.menu_refresh:
pop.dismiss();
xinwen.wv.reload(); //刷新
break;
case R.id.menu_search:
pop.dismiss();
inputTitleDialog();
break;
default:
break;
}
/*
mWebView.goBack(); //后退
mWebView.goForward();//前进
mWebView.reload(); //刷新
*/
}
}
</span>

adapter代码:

<span style="font-size:14px;">public class FragmentAdapter extends FragmentPagerAdapter{
private List<Fragment> mFragments;
public Fragment currentFragment;
public FragmentAdapter(FragmentManager fm, List<Fragment> fragments) {
super(fm);
mFragments = fragments;
}
@Override
public Fragment getItem(int position) {
return mFragments.get(position);
}
@Override
public int getCount() {
return mFragments.size();
}
//重载FragmentPagerAdapter.setPrimaryItem方法
@Override
public void setPrimaryItem(ViewGroup container, int position, Object object){
currentFragment = (Fragment)object;
super.setPrimaryItem(container, position, object);
}
}</span>

setCurrentItem()方法在这里要说一下,设置为false取消滑动效果,true显示滑动效果,准确来说是通过控制滑动时间来实现,当设置滑动时间为0时,滑动效果就没有了。

我要实现点击底部按钮实现没有滑动效果的切换,刚开始以为是其它问题,没有从setCurrentItem()入手,在哪里啥弄了半天。

关注公众号,分享干货,讨论技术

Fragment+ViewPager实现仿微信点击和滑动切换界面的更多相关文章

  1. Android控件-Fragment+ViewPager(高仿微信界面)

    什么是Fragment? Fragment是Android3.0后新增的概念,Fragment名为碎片,不过却和Activity十分相似,具有自己的生命周期,它是用来描述一些行为或一部分用户界面在一个 ...

  2. 【转】android 欢迎界面翻页成效,仿微信第一次登陆介绍翻页界面

    android 欢迎界面翻页效果,仿微信第一次登陆介绍翻页界面 本实例做的相对比较简单主要是对翻页控件的使用,有时候想要做一些功能是主要是先了解下是否有现成的控件可以使用,做起来比较简单不用费太大的劲 ...

  3. 使用jQuery实现点击左右滑动切换特效

    使用jQuery实现点击左右滑动切换特效: HTML代码如下: <!--整体背景div--> <div class="warp"> <!--中间内容d ...

  4. 微信小程序左右滑动切换页面示例代码--转载

    微信小程序——左右滑动切换页面事件 微信小程序的左右滑动触屏事件,主要有三个事件:touchstart,touchmove,touchend. 这三个事件最重要的属性是pageX和pageY,表示X, ...

  5. Android控件-ViewPager(仿微信引导界面)

    什么是ViewPager? ViewPager是安卓3.0之后提供的新特性,继承自ViewGroup,专门用以实现左右滑动切换View的效果. 如果想向下兼容就必须要android-support-v ...

  6. Fragment生命周期及实现点击导航图片切换fragment,Demo

    PS:Fragment简介 Fragment是Android3.0后引入的一个新的API,他出现的初衷是为了适应大屏幕的平板电脑, 当然现在他仍然是平板APP UI设计的宠儿,而且我们普通手机开发也会 ...

  7. 仿it快播顶部button点击背景滑动切换的效果

    最近在it快播中看见它顶部的几个button可以点击后 背景会滑动到相应的button后面 就得很好看 就想办法实现了那效果 思路 大概就是通过view的叠加 把3个button通过RelativeL ...

  8. Android仿微信SlideView聊天列表滑动删除效果

    package com.ryg.slideview; import com.ryg.slideview.MainActivity.MessageItem; //Download by http://w ...

  9. Android随笔--使用ViewPager实现简单地图片的左右滑动切换

    Android中图片的左右切换随处可见,今天我也试着查阅资料试着做了一下,挺简单的一个小Demo,却也发现了一些问题,话不多说,上代码~: 使用了3个xml文件作为ViewPager的滑动page,布 ...

随机推荐

  1. Python3 Tkinter-Menu

    1.创建 from tkinter import * root=Tk() menubar=Menu(root) def hello(): print('Hello Menu!') for item i ...

  2. 十一:Centralized Cache Management in HDFS 集中缓存管理

    集中的HDFS缓存管理,该机制可以让用户缓存特定的hdfs路径,这些块缓存在堆外内存中.namenode指导datanode完成这个工作. Centralized cache management i ...

  3. vs调试时报503错误

    开发中遇到了一个神问题,困扰了几个月没解决. 在本机调试,或者用iis服务器直接指向项目目录,访问网页任何页面都是报503. 一直找不到原因,配置文件也修改了,还是解决不了. 今天20170110一次 ...

  4. 《C陷阱与缺陷》之1词法"陷阱"

    编译器中负责将程序分解为一个一个符号的部分,一般称为"词法分析器".在C语言中,符号之间的空白(包括空格符.制表符或换行符)将被忽略. 1.=不同于== C语言使用符号" ...

  5. 《学习OpenCV》课后习题解答3

    题目:(P104) 创建一个大小为100*100的三通道RGB图像.将它的元素全部置0.使用指针算法以(20,5)与(40,20)为项点绘制一个绿色平面. 解答: #include "cv. ...

  6. 3dContactPointAnnotationTool开发日志(十一)

      把image也做成panel的形式了,并且放进了scrollView里,真实地显示出图像:   其它两个scrollView的content也做成自适应大小了,就是添加一项content的heig ...

  7. 软工网络15团队作业4——Alpha阶段敏捷冲刺-3

    一.当天站立式会议照片: 二.项目进展 昨天已完成的工作: 依靠HTML 逻辑框架等技术完成程序界面前端的实现. 明天计划完成的工作: 依靠css 逻辑框架等技术完成程序界面前端的实现. 工作中遇到的 ...

  8. Oracle AWR日志使用

    SQL>@?/rdbms/admin/awrrpt.sql Specify the Report Type ~~~~~~~~~~~~~~~~~~~~~~~ Would you like an H ...

  9. C# 知识回顾 - 你真的懂异常(Exception)吗?

    你真的懂异常(Exception)吗? 目录 异常介绍 异常的特点 怎样使用异常 处理异常的 try-catch-finally 捕获异常的 Catch 块 释放资源的 Finally 块 一.异常介 ...

  10. MVP开发模式的理解

    1.MVP是什么 如果从层次关系来讲,MVP属于Presentation层的设计模式.对于一个UI模块来说,它的所有功能被分割为三个部分,分别通过Model.View和Presenter来承载.Mod ...