天天看點

android開發 視圖關聯_Android自定義控件之關聯視圖

效果麼就是ViewPage + 底部導航,效果還不錯...Adapter類請在《Android自定義控件之廣告視圖》中查找....

public abstract class GangedPageAdapter extends Adapter{

public int getMenuWidth(Context context){

return getFixPx(context,100);

}

public int getMenuHeight(Context context){

return getFixPx(context,50);

}

public View[] getMenus() {

int count=getCount();

View[] views=new View[count];

for (int i = 0; i < count; i++) {

views[i]=getMenu(i);

}

return views;

}

public abstract View getMenu(int position);

}

public class GangedPage extends LinearLayout{

public interface OnPageSelectedListener{

public void onPageSelected(int position);

};

private OnPageSelectedListener listener;

private GangedPageAdapter adapter;

//标題欄

private HorizontalScrollView topMenuScrollView;

private RadioButton[] radioBtns;

private View[] menuViews;

//頁面欄

private View[] pages;

//選中的背景

private View checkedBg;

//頁面

private ViewPager viewPager;

//附加屬性

private int menuWidth;

private int menuHeight;

private float currentCheckedBtnToLeftSpace;

private int lastCheckedId;

public void setOnPageSelectedListener(OnPageSelectedListener listener) {

this.listener = listener;

}

public View getPage(int position){

return pages[position];

}

public boolean setSelected(int position){

if(position<0||position>radioBtns.length)

return false;

if(lastCheckedId!=position)

radioBtns[position].performClick();

else if(listener!=null)

listener.onPageSelected(position);

return true;

}

public void setTitleBackground(int color){

topMenuScrollView.setBackgroundColor(color);

}

public void setTitleBackground(Drawable drawable){

topMenuScrollView.setBackgroundDrawable(drawable);

}

public void setMenuItemBlankSpace(int size){

for (int i = 0; i < radioBtns.length; i++) {

LayoutParams btnParams=(LayoutParams) radioBtns[i].getLayoutParams();

if(i!=radioBtns.length-1)

btnParams.setMargins(0, 0,size, 0);

menuViews[i].setLayoutParams(btnParams);

radioBtns[i].setLayoutParams(btnParams);

}

}

public GangedPage(Context context) {

super(context);

setOrientation(VERTICAL);

}

public void setAdapter(GangedPageAdapter adapter){

this.adapter=adapter;

initResource();

initPages();

initMenus();

lastCheckedId=0;

radioBtns[lastCheckedId].setChecked(true);

}

public GangedPage(Context context,GangedPageAdapter adapter) {

this(context);

setAdapter(adapter);

}

protected void initResource(){

menuWidth=adapter.getMenuWidth(getContext());

menuHeight=adapter.getMenuHeight(getContext());

menuViews=adapter.getMenus();

checkedBg=adapter.getCheckedBg();

pages=adapter.getPages();

}

protected void initMenus(){

//最外層卷軸框

topMenuScrollView=new HorizontalScrollView(getContext());

topMenuScrollView.setHorizontalScrollBarEnabled(false);

topMenuScrollView.setBackgroundColor(Color.WHITE);

topMenuScrollView.setFadingEdgeLength(0);

LayoutParams svParams=new LayoutParams(

LayoutParams.FILL_PARENT,menuHeight);

this.addView(topMenuScrollView,svParams);

RelativeLayout outLayout=new RelativeLayout(getContext());

topMenuScrollView.addView(outLayout,

new LayoutParams(LayoutParams.FILL_PARENT,LayoutParams.FILL_PARENT));

//Frame可疊加層

FrameLayout menuFrame=new FrameLayout(getContext());

RelativeLayout.LayoutParams frameParams=new RelativeLayout.LayoutParams(

LayoutParams.FILL_PARENT,menuHeight);

outLayout.addView(menuFrame,frameParams);

//疊加層:背景特效容器

LinearLayout menuBgLayout=new LinearLayout(getContext());

menuBgLayout.setOrientation(RadioGroup.HORIZONTAL);

FrameLayout.LayoutParams bParams=new FrameLayout.LayoutParams(

LayoutParams.FILL_PARENT,LayoutParams.FILL_PARENT);

menuFrame.addView(menuBgLayout,bParams);

//疊加層:顯示内容容器

LinearLayout menuItemLayout=new LinearLayout(getContext());

menuItemLayout.setOrientation(RadioGroup.HORIZONTAL);

FrameLayout.LayoutParams cParams=new FrameLayout.LayoutParams(

LayoutParams.FILL_PARENT,LayoutParams.FILL_PARENT);

menuFrame.addView(menuItemLayout,cParams);

//疊加層:單選按鈕容器

RadioGroup menuRadioGroup=new RadioGroup(getContext());

menuRadioGroup.setOrientation(RadioGroup.HORIZONTAL);

menuFrame.addView(menuRadioGroup,cParams);

radioBtns=new RadioButton[menuViews.length];

for (int i = 0; i < menuViews.length; i++) {

//背景内容視圖

LinearLayout.LayoutParams btnParams=new LinearLayout.LayoutParams(

menuWidth, menuHeight,1.0f);

menuItemLayout.addView(menuViews[i],btnParams);

//單選按鈕視圖

radioBtns[i]=new RadioButton(getContext());

radioBtns[i].setId(i);

radioBtns[i].setButtonDrawable(android.R.color.transparent);

menuRadioGroup.addView(radioBtns[i],btnParams);

}

menuRadioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {

@Override

public void onCheckedChanged(RadioGroup group, int checkedId) {

//下方線條動畫

AnimationSet aniSet = new AnimationSet(true);

TranslateAnimation tAni=new TranslateAnimation(

currentCheckedBtnToLeftSpace,checkedId*menuWidth, 0f, 0f);

aniSet.addAnimation(tAni);

//aniSet.setFillBefore(false);

aniSet.setFillAfter(true);

aniSet.setDuration(200);

checkedBg.startAnimation(aniSet);

//設定目前顯示的Page

viewPager.setCurrentItem(checkedId);

//重置currentCheckedBtnToLeftSpace 并滾動标題位置

currentCheckedBtnToLeftSpace = getCurrentCheckedBtnToLeftSpace();

float x=currentCheckedBtnToLeftSpace-menuWidth;

topMenuScrollView.smoothScrollTo((int)x,0);

//修改顔色

lastCheckedId=checkedId;

}

});

//選中背景

LayoutParams ivParams=new LayoutParams(

menuWidth,menuHeight-adapter.getFixPx(getContext(), 4));

menuBgLayout.setGravity(Gravity.CENTER_VERTICAL);

menuBgLayout.addView(checkedBg,ivParams);

currentCheckedBtnToLeftSpace=getCurrentCheckedBtnToLeftSpace();

}

protected void initPages(){

viewPager=new ViewPager(getContext()){

@Override

public boolean onInterceptTouchEvent(MotionEvent arg0) {

return super.onInterceptTouchEvent(arg0);

//return false;

}

};

LayoutParams vpParams=new LayoutParams(

LayoutParams.FILL_PARENT,0,1.0f);

this.addView(viewPager,vpParams);

viewPager.setAdapter(new PagerAdapter() {

@Override

public boolean isViewFromObject(View arg0, Object arg1) {

return arg0==arg1;

}

@Override

public int getCount() {

return pages.length;

}

@Override

public void destroyItem(View container, int position, Object object) {

((ViewPager)container).removeView(pages[position]);

}

@Override

public Object instantiateItem(View v, int position) {

((ViewPager)v).addView(pages[position]);

return pages[position];

}

});

viewPager.setOnPageChangeListener(new OnPageChangeListener() {

@Override

public void onPageSelected(int position) {

radioBtns[position].performClick();

if(listener!=null)listener.onPageSelected(position);

}

@Override

public void onPageScrolled(int arg0, float arg1, int arg2) {

}

@Override

public void onPageScrollStateChanged(int arg0) {

}

});

}

protected float getCurrentCheckedBtnToLeftSpace(){

for (int i = 0; i < radioBtns.length; i++) {

if(radioBtns[i].isChecked()){

return menuWidth*i;

}

}

return 0f;

}

}