本文实例为大家分享了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 Listviews; public ViewPagerAdapter(List 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 (); } 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 Listviews = new ArrayList (); @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
<&#63;xml version="1.0" encoding="utf-8"&#63;>
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。