Android——简单实现顶部滑动菜单效果

   
   
   
   
最近项目中用到了下图的滑动菜单:

Android——简单实现顶部滑动菜单效果_第1张图片

这个在现实运用中用到的还是蛮多了,之前用的都是用的第三方,适用于菜单项比较多的地方。
但是像这种只有两三个菜单的,用第三方个人感觉还是比较麻烦的,自己用ViewPager做了下感觉还行。代码虽然比较简单,但是还是记录一下。


XML布局:

     
     
     
     
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >
 
<LinearLayout
android:id="@+id/ll_top"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#ffffff"
android:orientation="horizontal" >
 
<TextView
android:id="@+id/tv_menu1"
android:layout_width="0dp"
android:layout_height="50dp"
android:layout_weight="1"
android:gravity="center"
android:text="全部评测"
android:textColor="@color/blue_light"
android:textStyle="bold" />
 
<TextView
android:id="@+id/tv_menu2"
android:layout_width="0dp"
android:layout_height="50dp"
android:layout_weight="1"
android:gravity="center"
android:text="我的评测"
android:textColor="@color/gray"
android:textStyle="bold" />
</LinearLayout>
 
<View
android:layout_width="match_parent"
android:layout_height="2dp"
android:layout_below="@id/ll_top"
android:background="@color/gray" />
 
<View
android:id="@+id/line"
android:layout_width="wrap_content"
android:layout_height="2dp"
android:layout_below="@id/ll_top"
android:background="@color/blue_light" />
 
<android.support.v4.view.ViewPager
android:id="@+id/viewPager"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="@id/ll_top"
android:layout_gravity="center" />
 
</RelativeLayout>



代码:

     
     
     
     
public class MainActivity extends Activity {
 
private TextView mMenu1;
private TextView mMenu2;
private View mLine;
private ViewPager mViewPager;
private List<View> lists = new ArrayList<>();
private DisplayMetrics dm;
 
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
dm = getResources().getDisplayMetrics();
mMenu1 = (TextView) findViewById(R.id.tv_menu1);
mMenu2 = (TextView) findViewById(R.id.tv_menu2);
mViewPager = (ViewPager) findViewById(R.id.viewPager);
mLine = findViewById(R.id.line);
android.view.ViewGroup.LayoutParams params = mLine.getLayoutParams();
params.height = DensityUtil.dip2px(this, 2);
params.width = dm.widthPixels / 2;
mLine.setLayoutParams(params);
View view1 = getLayoutInflater().inflate(R.layout.layout1, null);
View view2 = getLayoutInflater().inflate(R.layout.layout2, null);
lists.add(view1);
lists.add(view2);
mViewPager.setAdapter(new ViewPagerAdapter(lists));
mViewPager.addOnPageChangeListener(new OnPageChangeListener() {
private Animation animation;
@Override
public void onPageSelected(int arg0) {
switch (arg0) {
case 0:
mMenu1.setTextColor(getResources().getColor(R.color.blue_light));
mMenu2.setTextColor(getResources().getColor(R.color.gray));
animation = new TranslateAnimation(dm.widthPixels / 2, 0, 0, 0);
break;
case 1:
mMenu2.setTextColor(getResources().getColor(R.color.blue_light));
mMenu1.setTextColor(getResources().getColor(R.color.gray));
animation = new TranslateAnimation(0, dm.widthPixels / 2, 0, 0);
break;
}
animation.setDuration(150);
animation.setFillAfter(true);
mLine.startAnimation(animation);
}
@Override
public void onPageScrolled(int arg0, float arg1, int arg2) {
}
@Override
public void onPageScrollStateChanged(int arg0) {
}
});
}
class ViewPagerAdapter extends PagerAdapter{
List<View> list;
public ViewPagerAdapter(List<View> list) {
this.list = list;
}
 
@Override
public int getCount() {
return list.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(list.get(position));
}
@Override
public Object instantiateItem(View container, int position) {
((ViewPager)container).addView(list.get(position));
return list.get(position);
}
}
}

主要就是监听了ViewPager的滑动,然后根据滑动做出对应的颜色改变以及动画效果。简单的不能再简单了。。
当菜单项比较少的时候,用这种写,减少代码量吧。。

源码下载地址:http://download.csdn.net/detail/airsaid/9411586

你可能感兴趣的:(Android——简单实现顶部滑动菜单效果)