Fragment+ViewPager实现仿微信点击和滑动切换界面
这是在我写的新闻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实现仿微信点击和滑动切换界面的更多相关文章
- Android控件-Fragment+ViewPager(高仿微信界面)
什么是Fragment? Fragment是Android3.0后新增的概念,Fragment名为碎片,不过却和Activity十分相似,具有自己的生命周期,它是用来描述一些行为或一部分用户界面在一个 ...
- 【转】android 欢迎界面翻页成效,仿微信第一次登陆介绍翻页界面
android 欢迎界面翻页效果,仿微信第一次登陆介绍翻页界面 本实例做的相对比较简单主要是对翻页控件的使用,有时候想要做一些功能是主要是先了解下是否有现成的控件可以使用,做起来比较简单不用费太大的劲 ...
- 使用jQuery实现点击左右滑动切换特效
使用jQuery实现点击左右滑动切换特效: HTML代码如下: <!--整体背景div--> <div class="warp"> <!--中间内容d ...
- 微信小程序左右滑动切换页面示例代码--转载
微信小程序——左右滑动切换页面事件 微信小程序的左右滑动触屏事件,主要有三个事件:touchstart,touchmove,touchend. 这三个事件最重要的属性是pageX和pageY,表示X, ...
- Android控件-ViewPager(仿微信引导界面)
什么是ViewPager? ViewPager是安卓3.0之后提供的新特性,继承自ViewGroup,专门用以实现左右滑动切换View的效果. 如果想向下兼容就必须要android-support-v ...
- Fragment生命周期及实现点击导航图片切换fragment,Demo
PS:Fragment简介 Fragment是Android3.0后引入的一个新的API,他出现的初衷是为了适应大屏幕的平板电脑, 当然现在他仍然是平板APP UI设计的宠儿,而且我们普通手机开发也会 ...
- 仿it快播顶部button点击背景滑动切换的效果
最近在it快播中看见它顶部的几个button可以点击后 背景会滑动到相应的button后面 就得很好看 就想办法实现了那效果 思路 大概就是通过view的叠加 把3个button通过RelativeL ...
- Android仿微信SlideView聊天列表滑动删除效果
package com.ryg.slideview; import com.ryg.slideview.MainActivity.MessageItem; //Download by http://w ...
- Android随笔--使用ViewPager实现简单地图片的左右滑动切换
Android中图片的左右切换随处可见,今天我也试着查阅资料试着做了一下,挺简单的一个小Demo,却也发现了一些问题,话不多说,上代码~: 使用了3个xml文件作为ViewPager的滑动page,布 ...
随机推荐
- Hadoop学习(一):完全分布式集群环境搭建
1. 设置免密登录 (1) 新建普通用户hadoop:useradd hadoop(2) 在主节点master上生成密钥对,执行命令ssh-keygen -t rsa便会在home文件夹下生成 .ss ...
- POJ 3348 Cows(凸包+多边形面积)
Description Your friend to the south is interested in building fences and turning plowshares into sw ...
- codeforces 295C Greg and Friends(BFS+DP)
One day Greg and his friends were walking in the forest. Overall there were n people walking, includ ...
- 转:Linux 编译安装 Mysql5.7
http://broqiang.com/2017/04/18/Mysql-Install-5.7.18-Linux-Compile/ 原文 Linux 编译安装 Mysql5.7 Ubuntu 下快速 ...
- lintcode-187-加油站
187-加油站 在一条环路上有 N 个加油站,其中第 i 个加油站有汽油gas[i],并且从第_i_个加油站前往第_i_+1个加油站需要消耗汽油cost[i]. 你有一辆油箱容量无限大的汽车,现在要从 ...
- OSG学习:多重纹理映射
#include<osgViewer\Viewer> #include<osg\Node> #include<osg\Geode> #include<osg\ ...
- 3dContactPointAnnotationTool开发日志(二三)
smpl模型得是一个整体,于是我让子物体的选项卡的删除按钮消失,这样就不会删除不必要的东西然后产生奇怪现象:
- sql sever 数据表
对视图进行操作,要在第三块区域进行添加记录操作,回车,然后会同步到所有相关数据表中. 记录不是列,而是行,不要混淆. 第二块区域是各个属性,就是说明: 第一块区域是要进行显示的字段,选中什么 显示什么 ...
- linux svn apache
借助apache运行的svn服务器 一:安装Apache HTTP Server 1.安装Apache HTTP Server yum install httpd httpd-devel -y 2.修 ...
- 当重写了 httpservlet重写了GenericServlet的init方法时候 必须显示调用GenericServlet的init方法时候 才能在别的方法(父类创建config实例) 例如 doget里面使用servletContext对象 不重写init 则可以直接使用