欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  移动技术

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

程序员文章站 2024-03-06 18:09:56
本文实例为大家分享了android 仿微信5滑动导航效果,供大家参考,具体内容如下 viewpageadapter.java package com.rong;...

本文实例为大家分享了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>

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