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,布 ...
随机推荐
- Kali信息收集-DNS
1.whois查询 直接在终端输入whois 域名 2.查找dns服务器 (1)host (2)dig (3)nslookup 3.域传输 4.域名枚举 (1)dnsdict6 kali没有集成这款工 ...
- SpringCloud IDEA 教学 番外篇 后台运行Eureka服务注册中心
写在开头 研发过程中经常要做的事就是启动Eureka服务注册中心,每每都要启动一个IDEA,很是困扰.现在分享一个后台启动服务注册中心的方法. 准备工作 1打包一个eureka服务注册中心jar包(注 ...
- Java学习个人备忘录之抽象类
抽象类 特点:1. 方法只有声明没有实现时,该方法就是抽象方法,需要被abstract修饰,抽象方法必须定义在抽象类中.该类必须也被abstract修饰2. 抽象类不可以被实例化. 为什么? 因为调 ...
- window.open居中显示
window.open中会发现怎么打开的浏览器窗口怎么不在正中间,而且还比较随性,那不是逼死强迫症患者了,所以查了一下资料始终打开的窗口能显示在正中间,下面是js中的代码分享 // url 转向网页的 ...
- lol人物模型提取(一)
前段时间去青岛搞团建去了,闲来无事逛了会儿淘宝,无想买个lol手办,意之间发现了这张店铺宣传图: 哎呀我去,这模型做得挺逼真啊,然而这家店铺是卖zoe的cosplay道具的,不是手办-_-|| ...
- Jrebel 工具学习
Jrebel 可快速实现热部署,节省了大量重启时间,提高了个人开发效率.网上可搜索到破解版. http://baike.baidu.com/link?url=wuzv7Wa7SMUKltJr-dyta ...
- 【Redis】- 安装为windows服务
1.安装redis服务 echo install redis-server redis-server.exe --service-install redis.windows.conf --loglev ...
- Redis 学习之主从复制
该文使用centos6.5 64位 redis3.2.8 主从复制 Redis的复制功能是支持多个数据库之间的数据同步.一类是主数据库(master)一类是从数据库(slave),主数据库可以进 ...
- svn checkout不包括根目录
在后面加 “.” 即可,如下: svn co svn://127.0.0.1/ylshop/ . 转载请注明博客出处:http://www.cnblogs.com/cjh-notes/
- [计算机网络-传输层] 面向连接的传输:TCP
参考:http://blog.csdn.net/macdroid/article/details/49070185 在学习TCP之前我们先来看一下可靠数据传输需要提供什么样的机制: ·差错检测机制:检 ...