Android仿微信5实现滑动导航条

本文实例为大家分享了Android 仿微信5滑动导航效果,供大家参考,具体内容如下

ViewPageAdapter.java

package com.rong; import java.util.ArrayList; import java.util.List; import android.support.v4.view.PagerAdapter; import android.support.v4.view.ViewPager; import android.view.View; public class ViewPagerAdapter extends PagerAdapter { private List<View> views; public ViewPagerAdapter(List<View> views) { this.views = views; } @Override public int getCount() { return views.size(); } @Override public boolean isViewFromObject(View arg0, Object arg1) { return arg0 == arg1; } @Override public void destroyItem(View container, int position, Object object) { ((ViewPager) container).removeView(views.get(position)); } @Override public Object instantiateItem(View container, int position) { ((ViewPager) container).addView(views.get(position)); return views.get(position); } /** * 插入一个View到viewpager中 * * @param view * @return */ public boolean insert(View view) { if (this.views == null) { this.views = new ArrayList<View>(); } return this.views.add(view); } }

HomeAcitivity.java

package com.rong; import java.util.ArrayList; import java.util.List; import android.app.Activity; import android.os.Bundle; import android.support.v4.view.ViewPager; import android.support.v4.view.ViewPager.OnPageChangeListener; import android.util.DisplayMetrics; import android.util.Log; import android.view.Display; import android.view.View; import android.view.ViewGroup.LayoutParams; import android.widget.LinearLayout; import com.rong.wechat5.R; public class HomeActivity extends Activity { private View home_line; private int screenWidth1_3; private ViewPager home_vp; private ViewPagerAdapter viewPagerAdapter; private List<View> views = new ArrayList<View>(); @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.layout_home); initView(); } private void initView() { Display display = getWindow().getWindowManager().getDefaultDisplay(); DisplayMetrics dm = new DisplayMetrics(); display.getMetrics(dm); home_line = (View) findViewById(R.id.home_line); home_vp = (ViewPager) findViewById(R.id.home_vp); LayoutParams lp = home_line.getLayoutParams(); screenWidth1_3 = dm.widthPixels / 3; lp.width = screenWidth1_3; lp.height = 5; home_line.setLayoutParams(lp); View layout1 = View.inflate(this, R.layout.layout_view1, null); View layout2 = View.inflate(this, R.layout.layout_view2, null); View layout3 = View.inflate(this, R.layout.layout_view3, null); views.add(layout1); views.add(layout2); views.add(layout3); viewPagerAdapter = new ViewPagerAdapter(views); home_vp.setAdapter(viewPagerAdapter); home_vp.setOnPageChangeListener(new OnPageChangeListener() { @Override public void onPageSelected(int arg0) { } @Override public void onPageScrolled(int position, float positionOffset,int positionOffsetPixels) { Log.i("TAG", position+"=="+positionOffset+"=="+positionOffsetPixels); LinearLayout.LayoutParams lp = (android.widget.LinearLayout.LayoutParams) home_line.getLayoutParams(); lp.leftMargin = (int) (screenWidth1_3 * position + screenWidth1_3* positionOffset); home_line.setLayoutParams(lp); } @Override public void onPageScrollStateChanged(int arg0) { } }); } }

layout_home.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:background="#ffffff" android:orientation="vertical" > <LinearLayout android:layout_width="match_parent" android:layout_height="60dp" android:orientation="horizontal" > <RelativeLayout android:layout_width="0dp" android:layout_height="match_parent" android:layout_weight="1" > <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerInParent="true" android:text="消息" android:textColor="#000000" android:textSize="24sp" /> </RelativeLayout> <RelativeLayout android:layout_width="0dp" android:layout_height="match_parent" android:layout_weight="1" > <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerInParent="true" android:text="朋友圈" android:textColor="#000000" android:textSize="24sp" /> </RelativeLayout> <RelativeLayout android:layout_width="0dp" android:layout_height="match_parent" android:layout_weight="1" > <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerInParent="true" android:text="我" android:textColor="#000000" android:textSize="24sp" /> </RelativeLayout> </LinearLayout> <View android:id="@+id/home_line" android:layout_width="0dp" android:layout_height="0dp" android:background="#ff0000" /> <android.support.v4.view.ViewPager android:id="@+id/home_vp" android:layout_width="match_parent" android:layout_height="0dp" android:layout_weight="1" /> </LinearLayout>

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持脚本之家。

时间: 2024-10-28 21:09:02

Android仿微信5实现滑动导航条的相关文章

Android仿微信5实现滑动导航条_Android

本文实例为大家分享了Android 仿微信5滑动导航效果,供大家参考,具体内容如下 ViewPageAdapter.java package com.rong; import java.util.ArrayList; import java.util.List; import android.support.v4.view.PagerAdapter; import android.support.v4.view.ViewPager; import android.view.View; publi

Android仿微信实现首字母导航条_Android

本文介绍Android实现首字母导航条,先看张效果图,具体怎么实现看代码吧 具体的步骤 1.整体布局的显示 2. 实现A-Z的分组 3. 自定义A-Z的导航条 4. 中间显示/隐藏触摸到导航条具体的字母 activity_main.xml <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/re

Android仿微信实现首字母导航条

本文介绍Android实现首字母导航条,先看张效果图,具体怎么实现看代码吧 具体的步骤 1.整体布局的显示 2. 实现A-Z的分组 3. 自定义A-Z的导航条 4. 中间显示/隐藏触摸到导航条具体的字母 activity_main.xml <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/re

Android仿微信底部按钮滑动变色

Android仿微信底部按钮滑动变色,这里只针对使用Fragment为Tab页的滑动操作,进行简单的变色讲解. 首先说下OnPageChangeListener这个监听 //这个监听有三个方法 public abstract void onPageScrollStateChanged (int state) public abstract void onPageScrolled (int position, float positionOffset, int positionOffsetPixe

Android仿微信对话列表滑动删除效果_Android

微信对话列表滑动删除效果很不错的,借鉴了github上SwipeListView(项目地址:https://github.com/likebamboo/SwipeListView),在其上进行了一些重构,最终实现了微信对话列表滑动删除效果. 实现原理 1.通过ListView的pointToPosition(int x, int y)来获取按下的position,然后通过android.view.ViewGroup.getChildAt(position)来得到滑动对象swipeView  2.

Android仿微信对话列表滑动删除效果

微信对话列表滑动删除效果很不错的,借鉴了github上SwipeListView(项目地址:https://github.com/likebamboo/SwipeListView),在其上进行了一些重构,最终实现了微信对话列表滑动删除效果. 实现原理  1.通过ListView的pointToPosition(int x, int y)来获取按下的position,然后通过android.view.ViewGroup.getChildAt(position)来得到滑动对象swipeView  2

Android仿微信列表滑动删除之可滑动控件(一)_Android

这次是列表滑动删除的第三波,仿微信的列表滑动删除.先上个效果图:   前面的文章里面说过开源框架SwipeListView的实现原理是每个列表item中包含上下两层view,普通状态下上层的view覆盖着下层的view,当用户滑开上层的view,下层的view就显示出来了.但是仔细观察微信列表的item,很明显并非这个实现方案,微信的item应该一个单层view,只不过这个item超出了所在的ListView的宽度,在用户滑动item的时候,item超出屏幕的view则会显示在屏幕之上,这种滑动

Android仿微信页面底部导航效果代码实现_Android

大家在参考本地代码的时候要根据需要适当的修改,里面有冗余代码小编没有删除.好了,废话不多说了,一切让代码说话吧! 关键代码如下所示: .java里面的主要代码 public class MainActivity extends BaseActivity implements TabChangeListener { private Fragment[] fragments; private FragZaiXianYuYue fragZaiXianYuYue; private FragDaoLuJi

Android仿微信通讯录滑动快速定位功能

先给大家展示下效果图: 实现代码如下: 下面简单说下实现原理. public class IndexBar extends LinearLayout implements View.OnTouchListener { private static final String[] INDEXES = new String[]{"#", "A", "B", "C", "D", "E", &qu