Java tutorial
/* * Flan.Zeng 2011-2016 http://git.oschina.net/signup?inviter=flan * * 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.flan.stock.view; import java.util.List; import com.flan.stock.R; import android.content.Context; import android.content.res.TypedArray; import android.support.v4.view.PagerAdapter; import android.support.v4.view.ViewPager; import android.support.v4.view.ViewPager.OnPageChangeListener; import android.util.AttributeSet; import android.util.DisplayMetrics; import android.view.View; import android.view.ViewGroup; import android.view.animation.Animation; import android.view.animation.TranslateAnimation; import android.widget.ImageView; import android.widget.RelativeLayout; /** * ?ViewPager * @author flan * @date 2015117 */ public class TouTiaoPagerView extends RelativeLayout implements OnPageChangeListener { private int currentTab; private int tabImgWidth; private ImageView img_tab; private ViewPager viewPager; public TouTiaoPagerView(Context context, int LayoutId) { super(context); inflate(context, LayoutId, this); viewPager = (ViewPager) this.findViewById(R.id.vp_fragment); img_tab = (ImageView) this.findViewById(R.id.img_tab); } public TouTiaoPagerView(Context context, AttributeSet attrs) { super(context, attrs); TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.SimplePagerView); int layout = typedArray.getResourceId(R.styleable.SimplePagerView_pager_layout, R.layout.layout_news_toutio); typedArray.recycle(); inflate(context, layout, this); viewPager = (ViewPager) this.findViewById(R.id.vp_fragment); img_tab = (ImageView) this.findViewById(R.id.img_tab); } public void setOnPageChangeListener(OnPageChangeListener listener) { viewPager.addOnPageChangeListener(listener); } /** * ? */ public void setPagerList(List<View> viewlist) { viewPager.setAdapter(new SimplePagerAdapter(viewlist)); //?? DisplayMetrics dm = new DisplayMetrics(); dm = getResources().getDisplayMetrics(); int screenW = dm.widthPixels; if (viewlist != null && viewlist.size() > 0) { tabImgWidth = screenW / viewlist.size(); } else { tabImgWidth = screenW; } LayoutParams para = (LayoutParams) img_tab.getLayoutParams(); para.width = tabImgWidth; img_tab.setLayoutParams(para); } /** * ? ? * @param position */ private void setCurrentTab(int position) { Animation animation = null; animation = new TranslateAnimation(tabImgWidth * currentTab, tabImgWidth * position, 0, 0); animation.setFillAfter(true);//True:??? animation.setDuration(300); //? img_tab.startAnimation(animation); currentTab = position; } @Override public void onPageScrollStateChanged(int arg0) { } @Override public void onPageScrolled(int arg0, float arg1, int arg2) { } @Override public void onPageSelected(int position) { setCurrentTab(position); } /** * ?ViewPager? * @author flan * @date 2015117 */ class SimplePagerAdapter extends PagerAdapter { private List<View> viewList; public SimplePagerAdapter(List<View> viewList) { super(); this.viewList = viewList; } @Override public int getCount() { if (viewList != null) { return viewList.size(); } return 0; } @Override public boolean isViewFromObject(View arg0, Object arg1) { return arg0 == arg1; } @Override public void destroyItem(ViewGroup container, int position, Object object) { container.removeView(viewList.get(position)); } @Override public Object instantiateItem(ViewGroup container, int position) { container.addView(viewList.get(position)); return viewList.get(position); } } }