首页 > 代码库 > Android ViewPager的简单使用
Android ViewPager的简单使用
package test.view.weitop; public class AnimActivity extends BaseActivity { private View vline; private int width = 0; private ViewPager mViewPager; private PagerTabStrip pagerTabStrip; private Button leftBtn,rightBtn; private int[] drawableResIds = {R.drawable.tip1,R.drawable.tip2}; int w; @Override protected void onCreate(Bundle saveInstance) { super.onCreate(saveInstance); setContentView(R.layout.anim_layout); w = getWindow().getWindowManager().getDefaultDisplay().getWidth(); vline = findViewById(R.id.line); LayoutParams lp = vline.getLayoutParams(); width = lp.width = w / 2; vline.setLayoutParams(lp); vline.setTag("0"); leftBtn = (Button) findViewById(R.id.fade_anim_left); rightBtn = (Button) findViewById(R.id.fade_anim_right); mViewPager = (ViewPager) findViewById(R.id.anim_view_pager); /*pagerTabStrip = (PagerTabStrip) mViewPager.findViewById(R.id.anim_view_pager_tabsctrip); pagerTabStrip.setTabIndicatorColor(getResources().getColor(R.color.red)); pagerTabStrip.setDrawFullUnderline(false); pagerTabStrip.setBackgroundColor(getResources().getColor(R.color.transparent)); pagerTabStrip.setTextSpacing(50); */ mViewPager.setAdapter(new ViewPagerAdapter()); mViewPager.setOnPageChangeListener(pageChangedListener); } private OnPageChangeListener pageChangedListener = new OnPageChangeListener() { @Override public void onPageSelected(int position) { Log.e("ViewPager", "position===>"+position); setTabItem(position); } @SuppressLint("NewApi") @Override public void onPageScrolled(int arg0, float arg1, int arg2) { Log.d("ViewPager", "arg0="+arg0+" arg1="+arg1+" arg2="+arg2); } @Override public void onPageScrollStateChanged(int arg0) { Log.i("ViewPager", "=====>arg0="+arg0); } }; private void setTabItem(int position) { if(position==0) { vline.setTag("0"); TranslateAnimation ta = new TranslateAnimation(width, 0, 1, 1); ta.setDuration(200); ta.setFillBefore(true); vline.clearAnimation(); vline.startAnimation(ta); } else { vline.setTag("1"); TranslateAnimation ta = new TranslateAnimation(0, width, 1, 1); ta.setDuration(200); ta.setFillBefore(true); ta.setFillAfter(true); vline.clearAnimation(); vline.startAnimation(ta); } } private class ViewPagerAdapter extends PagerAdapter { @Override public int getCount() { return drawableResIds.length; } @Override public Object instantiateItem(ViewGroup container, int position) { ImageView imageView = (ImageView) layoutInflater.inflate(R.layout.image_display, null); imageView.setImageResource(drawableResIds[position]); imageView.setTag(position); container.addView(imageView); LayoutParams lp = imageView.getLayoutParams(); lp.width = LayoutParams.MATCH_PARENT; lp.height = LayoutParams.MATCH_PARENT; imageView.setLayoutParams(lp); return imageView; } @Override public void destroyItem(ViewGroup container, int position, Object object) { ImageView image = (ImageView)((ViewPager) container).findViewWithTag(position); ((ViewPager) container).removeView(image); } @Override public boolean isViewFromObject(View arg0, Object arg1) { return arg0==arg1; } } public void doSwicth(View v) { switch (v.getId()) { case R.id.fade_anim_left: { mViewPager.setCurrentItem(0,true); } break; case R.id.fade_anim_right: mViewPager.setCurrentItem(1,true); break; default: break; } } }
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" > <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal" > <Button android:id="@+id/fade_anim_left" android:layout_width="match_parent" android:layout_height="40dip" android:layout_weight="1" android:text="Left" android:onClick="doSwicth" android:background="@drawable/fade_anim" /> <View android:layout_width="1dip" android:layout_height="30dip" android:background="@color/red" /> <Button android:id="@+id/fade_anim_right" android:layout_width="match_parent" android:layout_height="40dip" android:text="Right" android:layout_weight="1" android:onClick="doSwicth" android:background="@drawable/fade_anim" /> </LinearLayout> <View android:id="@+id/line" android:layout_width="160dip" android:layout_height="5dip" android:background="@color/red" /> <android.support.v4.view.ViewPager android:id="@+id/anim_view_pager" android:layout_width="match_parent" android:layout_height="match_parent" android:scrollbars="vertical" android:keepScreenOn="true" > <!-- <android.support.v4.view.PagerTabStrip android:id="@+id/anim_view_pager_tabsctrip" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="top"/> --> </android.support.v4.view.ViewPager> </LinearLayout>
Android ViewPager的简单使用
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。