Skip to content

Commit

Permalink
slidingtab支持超出屏幕滚动
Browse files Browse the repository at this point in the history
  • Loading branch information
zhaoqp2010 committed Nov 19, 2013
1 parent bb128e7 commit 5093328
Show file tree
Hide file tree
Showing 22 changed files with 191 additions and 30 deletions.
Binary file modified AndBase/bin/andbase.jar
Binary file not shown.
Binary file modified AndBase/bin/classes/com/ab/global/AbAppData.class
Binary file not shown.
Binary file modified AndBase/bin/classes/com/ab/view/app/AbPopoverView.class
Binary file not shown.
Binary file not shown.
Binary file modified AndBase/bin/classes/com/ab/view/sliding/AbSlidingTabView$1.class
Binary file not shown.
Binary file modified AndBase/bin/classes/com/ab/view/sliding/AbSlidingTabView$2.class
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file modified AndBase/bin/classes/com/ab/view/sliding/AbSlidingTabView.class
Binary file not shown.
2 changes: 1 addition & 1 deletion AndBase/src/com/ab/global/AbAppData.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
public class AbAppData {

/** 日志开关. */
public static boolean DEBUG = false;
public static boolean DEBUG = true;

/** 性能测试. */
public static boolean mMonitorOpened = false;
Expand Down
6 changes: 6 additions & 0 deletions AndBase/src/com/ab/view/app/AbPopoverView.java
Original file line number Diff line number Diff line change
Expand Up @@ -705,6 +705,12 @@ public View getPopoverContentView() {
return popoverContentView;
}

/**
*
* 描述:设置显示的View
* @param popoverContentView
* @throws
*/
public void setPopoverContentView(View popoverContentView) {
this.popoverContentView = popoverContentView;
popoverView.removeAllViews();
Expand Down
55 changes: 55 additions & 0 deletions AndBase/src/com/ab/view/sample/AbHorizontalScrollView1.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
/*
* Copyright (C) 2013 www.418log.org
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.ab.view.sample;

import android.content.Context;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.widget.HorizontalScrollView;

// TODO: Auto-generated Javadoc
/**
* The Class AbHorizontalScrollView.
*/
public class AbHorizontalScrollView1 extends HorizontalScrollView {

/** The m last x. */
private float mLastX = -1;

public AbHorizontalScrollView1(Context context, AttributeSet attrs) {
super(context, attrs);
}

@Override
public boolean onTouchEvent(MotionEvent ev) {
switch (ev.getAction()) {
case MotionEvent.ACTION_DOWN:
mLastX = ev.getRawX();
break;
case MotionEvent.ACTION_MOVE:
final float deltaX = ev.getRawX() - mLastX;
mLastX = ev.getRawX();
break;
case MotionEvent.ACTION_UP:
break;
default:
break;
}
return super.onTouchEvent(ev);
}


}
138 changes: 119 additions & 19 deletions AndBase/src/com/ab/view/sliding/AbSlidingTabView.java
Original file line number Diff line number Diff line change
Expand Up @@ -30,14 +30,19 @@
import android.util.Log;
import android.view.Display;
import android.view.Gravity;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.MeasureSpec;
import android.view.animation.TranslateAnimation;
import android.widget.HorizontalScrollView;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.ab.adapter.AbFragmentPagerAdapter;
import com.ab.global.AbAppData;
import com.ab.util.AbViewUtil;

// TODO: Auto-generated Javadoc
/**
Expand All @@ -61,6 +66,9 @@ public class AbSlidingTabView extends LinearLayout {
/** tab的线性布局. */
private LinearLayout mTabLayout = null;

/** tab的线性布局父. */
private HorizontalScrollView mTabScrollView = null;

/** The m view pager. */
private ViewPager mViewPager;

Expand All @@ -86,7 +94,7 @@ public class AbSlidingTabView extends LinearLayout {
private ImageView mTabImg;

/**当前页卡编号*/
private int currIndex = 0;
private int mSelectedTabIndex = 0;

/**屏幕宽度*/
private int displayWidth = 0;
Expand All @@ -109,6 +117,12 @@ public class AbSlidingTabView extends LinearLayout {
/**当前tab的位置*/
private int startX = 0;

/**当前tab的位置*/
private int startX2 = 0;

/**当前移动的距离*/
private int scrollX = 0;

public AbSlidingTabView(Context context, AttributeSet attrs) {
super(context, attrs);
this.context = context;
Expand All @@ -124,13 +138,19 @@ public AbSlidingTabView(Context context, AttributeSet attrs) {
this.setOrientation(LinearLayout.VERTICAL);
this.setBackgroundColor(Color.rgb(255, 255, 255));

mTabScrollView = new HorizontalScrollView(context);
mTabScrollView.setHorizontalScrollBarEnabled(false);
mTabLayout = new LinearLayout(context);
mTabLayout.setOrientation(LinearLayout.HORIZONTAL);
mTabLayout.setGravity(Gravity.CENTER);
mTabScrollView.addView(mTabLayout, new ViewGroup.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.FILL_PARENT));


//定义Tab栏
tabItemList = new ArrayList<TextView>();
tabItemTextList = new ArrayList<String>();
this.addView(mTabLayout,layoutParamsFW);

this.addView(mTabScrollView,layoutParamsFW);

//页卡滑动图片
mTabImg = new ImageView(context);
Expand All @@ -155,8 +175,39 @@ public AbSlidingTabView(Context context, AttributeSet attrs) {
mViewPager.setAdapter(mFragmentPagerAdapter);
mViewPager.setOnPageChangeListener(new MyOnPageChangeListener());
mViewPager.setOffscreenPageLimit(3);
mViewPager.setCurrentItem(0);

mTabScrollView.setOnTouchListener(new OnTouchListener() {

@Override
public boolean onTouch(View v, MotionEvent ev) {
switch (ev.getAction()) {
case MotionEvent.ACTION_DOWN:
Log.d(TAG, "ACTION_DOWN");
break;
case MotionEvent.ACTION_MOVE:
View view = ((HorizontalScrollView) v).getChildAt(0);
scrollX = v.getScrollX();
Log.d(TAG, "滑动X"+startX2+"to"+(int)(startX-scrollX));
imageSlide(mTabImg,startX2,(int)(startX-scrollX),0,0);
startX2 = (int)(startX-scrollX);

if(view.getMeasuredWidth() == v.getWidth()+v.getScrollX()){
Log.d(TAG, "滑动到底了");
}else if(v.getScrollX()==0){
Log.d(TAG, "滑动到顶了");
}else {

}
break;
case MotionEvent.ACTION_UP:
Log.d(TAG, "ACTION_UP");
break;
default:
break;
}
return false;
}
});
}


Expand All @@ -177,20 +228,36 @@ public void onPageScrolled(int arg0, float arg1, int arg2) {
public void onPageSelected(int arg0) {
//计算滑块偏移
computeTabImg(arg0);

}

}

/**
*
* 描述:滑动动画
* @param v
* @param startX
* @param toX
* @param startY
* @param toY
* @throws
*/
public void imageSlide(View v, int startX, int toX, int startY, int toY) {
TranslateAnimation anim = new TranslateAnimation(startX, toX, startY, toY);
anim.setDuration(100);
anim.setFillAfter(true);
v.startAnimation(anim);
}

/**
*
* 描述:滑动条
* @param index
* @throws
*/
public void computeTabImg(int index){


for(int i = 0;i<tabItemList.size();i++){
TextView tv = tabItemList.get(i);
tv.setTextColor(tabColor);
Expand All @@ -199,16 +266,26 @@ public void computeTabImg(int index){
}
}

final View tabView = mTabLayout.getChildAt(index);
AbViewUtil.measureView(tabView);
//计算滑块偏移
int count = mFragmentPagerAdapter.getCount();
int width = displayWidth/count;
int toX = width * index;
currIndex = index;
LayoutParams mParams = new LayoutParams(width,tabSlidingHeight);
int toX = tabView.getLeft();
mSelectedTabIndex = index;
Log.d(TAG, "computeTabImg:width"+tabView.getMeasuredWidth());
LayoutParams mParams = new LayoutParams(tabView.getMeasuredWidth(),tabSlidingHeight);
mParams.topMargin = -tabSlidingHeight;
mTabImg.setLayoutParams(mParams);
imageSlide(mTabImg,startX,toX,0,0);
imageSlide(mTabImg,startX2,toX-scrollX,0,0);
startX = toX;
startX2 = startX;

/*//被屏幕遮挡的
if(tabView.getRight() >= mTabScrollView.getWidth()+scrollX){
mTabScrollView.scrollBy(tabView.getMeasuredWidth(), 0);
}*/



}


Expand All @@ -233,7 +310,9 @@ public void addItemViews(List<String> tabTexts,List<Fragment> fragments){
tv.setTextSize(tabTextSize);
tv.setText(text);
tv.setGravity(Gravity.CENTER);
tv.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.FILL_PARENT,1));
tv.setLayoutParams(new LayoutParams(0,LayoutParams.FILL_PARENT,1));
tv.setPadding(12, 5, 12, 5);
tv.setFocusable(false);
tabItemList.add(tv);
mTabLayout.addView(tv);
tv.setOnClickListener(new OnClickListener() {
Expand All @@ -246,9 +325,9 @@ public void onClick(View arg0) {
}

//重新
computeTabImg(0);

mFragmentPagerAdapter.notifyDataSetChanged();
mViewPager.setCurrentItem(0);
computeTabImg(0);

}

Expand All @@ -273,7 +352,9 @@ public void addItemView(String tabText,Fragment fragment){
tv.setTextSize(tabTextSize);
tv.setText(text);
tv.setGravity(Gravity.CENTER);
tv.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT,1));
tv.setLayoutParams(new LayoutParams(0,LayoutParams.FILL_PARENT,1));
tv.setPadding(12, 5, 12, 5);
tv.setFocusable(false);
tabItemList.add(tv);
mTabLayout.addView(tv);
tv.setOnClickListener(new OnClickListener() {
Expand All @@ -286,10 +367,10 @@ public void onClick(View arg0) {
}

//重新
computeTabImg(0);

Log.d(TAG, "addItemView finish");
mFragmentPagerAdapter.notifyDataSetChanged();

mViewPager.setCurrentItem(0);
computeTabImg(0);
}


Expand All @@ -306,7 +387,6 @@ public void removeItemView(int index){
pagerItemList.remove(index);

mFragmentPagerAdapter.notifyDataSetChanged();

}

/**
Expand All @@ -319,7 +399,6 @@ public void removeAllItemView(int index){
mTabLayout.removeAllViews();
pagerItemList.clear();
mFragmentPagerAdapter.notifyDataSetChanged();

}


Expand Down Expand Up @@ -380,6 +459,22 @@ public int getTabTextSize() {
public void setTabTextSize(int tabTextSize) {
this.tabTextSize = tabTextSize;
}

/**
*
* 描述:设置每个tab的边距
* @param left
* @param top
* @param right
* @param bottom
* @throws
*/
public void setTabPadding(int left, int top, int right, int bottom) {
for(int i = 0;i<tabItemList.size();i++){
TextView tv = tabItemList.get(i);
tv.setPadding(left, top, right, bottom);
}
}

public int getTabSlidingHeight() {
return tabSlidingHeight;
Expand All @@ -396,4 +491,9 @@ public void setTabSlidingHeight(int tabSlidingHeight) {
}


@Override
public void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
}

}
Binary file modified AndBaseDemo/bin/AndBaseDemo.apk
Binary file not shown.
Binary file modified AndBaseDemo/bin/classes.dex
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
4 changes: 2 additions & 2 deletions AndBaseDemo/src/com/andbase/demo/activity/Fragment1.java
Original file line number Diff line number Diff line change
Expand Up @@ -108,8 +108,8 @@ public void get() {
for (int i = 0; i < 10; i++) {
map = new HashMap<String, Object>();
map.put("itemsIcon",mPhotoList.get(new Random().nextInt(mPhotoList.size())));
map.put("itemsTitle", "item"+i);
map.put("itemsText", "item..."+i);
map.put("itemsTitle", "于禁"+i);
map.put("itemsText", "于禁..."+i);
newList.add(map);
}
} catch (Exception e) {
Expand Down
4 changes: 2 additions & 2 deletions AndBaseDemo/src/com/andbase/demo/activity/Fragment2.java
Original file line number Diff line number Diff line change
Expand Up @@ -101,8 +101,8 @@ public void get() {
for (int i = 0; i < 10; i++) {
map = new HashMap<String, Object>();
map.put("itemsIcon",mPhotoList.get(new Random().nextInt(mPhotoList.size())));
map.put("itemsTitle", "item"+i);
map.put("itemsText", "item..."+i);
map.put("itemsTitle", "关羽"+i);
map.put("itemsText", "关羽..."+i);
newList.add(map);
}
} catch (Exception e) {
Expand Down
Loading

0 comments on commit 5093328

Please sign in to comment.