这是在我写的新闻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. 持续集成之TeamCity 配置

    xcopy /S /Y CodeFirstDemo\CodefirstDemo.Web D:\publish\welcome\Web

  2. Python高级编程-多线程

    (一)进程线程概述: 很多同学都听说过,现代操作系统比如Mac OS X,UNIX,Linux,Windows等,都是支持“多任务”的操作系统. 什么叫“多任务”呢?简单地说,就是操作系统可以同时运行 ...

  3. HDU 3726 Graph and Queries(平衡二叉树)(2010 Asia Tianjin Regional Contest)

    Description You are given an undirected graph with N vertexes and M edges. Every vertex in this grap ...

  4. http://www.cnblogs.com/120626fj/p/7545958.html

    1.本周PSP 2.本周进度条: 代码行,博文字数,用到的知识点 3.累计进度图 3.1累计代码折线图 3.2累计博文字数折线图 4.本周PSP饼状图

  5. 欢迎来怼--第七次Scrum会议

    一.小组信息 队名:欢迎来怼 小组成员: 队长:田继平 成员:李圆圆,葛美义,王伟东,姜珊,邵朔,冉华 小组照片 二.开会信息 时间:2017/10/19 17:05~17:17,总计12min. 地 ...

  6. Java学习个人备忘录之关键字static

    被static标记的东西会放在内存中被共享的,对象用到时,就会来取的. class Person { String name; //成员变量,实例变量 static String country = ...

  7. how to install pygraphviz on windows 10 with python 3.6

    Here's what worked for me: Win 7 AMD64 Install MSFT C++ compiler. Install Anaconda for Win AMD64, Py ...

  8. winform 删除,清空指定文件夹上的所有文件或文件夹

    //递归删除文件夹及子文件C#代码: public void DeleteFolder(string dir) { if (Directory.Exists(dir)) //如果存在这个文件夹删除之 ...

  9. YaoLingJump开发者日志(四)

      这么有意思的游戏没有剧情怎么行?开始剧情的搭建.   用到了LGame中的AVGScreen,确实是个好东西呢,只需要准备图片和对话脚本就行了.   经过不断的ps,yy,ps,yy,游戏开头的剧 ...

  10. Ubuntu下找不到php5,phpize等可执行程序的解决办法

    Ubuntu下找不到php5,phpize等可执行程序的解决办法 [日期:2010-10-25] 来源:eetag.com  作者:eetag [字体:大 中 小]     环境:Linux Ubun ...