Java tutorial
/* * Copyright (C) 2012 www.amsoft.cn * * 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.lxh.util.pullview; import java.util.ArrayList; import java.util.List; import com.lxh.util.utils.FileUtil; import android.content.Context; import android.graphics.Bitmap; import android.graphics.Color; import android.os.Handler; import android.os.Message; import android.support.v4.view.ViewPager; import android.support.v4.view.ViewPager.OnPageChangeListener; import android.util.AttributeSet; import android.view.Gravity; import android.view.MotionEvent; import android.view.View; import android.view.ViewGroup; import android.widget.AbsListView; import android.widget.ImageView; import android.widget.LinearLayout; import android.widget.ListView; import android.widget.RelativeLayout; import android.widget.ScrollView; // TODO: Auto-generated Javadoc /** * 2012 amsoft.cn ??AbPlayView ???View. * * @author * @version * @date 2011-11-28 */ public class AbSlidingPlayView extends LinearLayout { /** . */ private Context context; /** ViewPager. */ private AbInnerViewPager mViewPager; /** . */ private LinearLayout navLinearLayout; /** ?. */ public LinearLayout.LayoutParams navLayoutParams = null; /** . */ private int count, position; /** . */ private Bitmap displayImage, hideImage; /** . */ private AbOnItemClickListener mOnItemClickListener; /** ?. */ private AbOnChangeListener mAbChangeListener; /** . */ private AbOnScrollListener mAbScrolledListener; /** . */ private AbOnTouchListener mAbOnTouchListener; /** List views. */ private ArrayList<View> mListViews = null; /** ?. */ private AbViewPagerAdapter mAbViewPagerAdapter = null; /** View. */ private LinearLayout mNavLayoutParent = null; /** ??. */ private int navHorizontalGravity = Gravity.RIGHT; /** ?. */ private int playingDirection = 0; private int time = 5000; /** . */ private boolean play = false; /** * AbSlidingPlayView. * * @param context * the context */ public AbSlidingPlayView(Context context) { super(context); initView(context); } /** * * @param time */ public void setTime(int time) { this.time = time; } /** * xml?AbSlidingPlayView. * * @param context * the context * @param attrs * the attrs */ public AbSlidingPlayView(Context context, AttributeSet attrs) { super(context, attrs); initView(context); } /** * ???View. * * @param context * the context */ public void initView(Context context) { this.context = context; navLayoutParams = new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT); this.setOrientation(LinearLayout.VERTICAL); RelativeLayout mRelativeLayout = new RelativeLayout(context); mViewPager = new AbInnerViewPager(context); // ViewPager,fragmentsetId()id mViewPager.setId(1985); // mNavLayoutParent = new LinearLayout(context); mNavLayoutParent.setPadding(0, 5, 0, 5); navLinearLayout = new LinearLayout(context); navLinearLayout.setPadding(15, 1, 15, 1); navLinearLayout.setVisibility(View.INVISIBLE); mNavLayoutParent.addView(navLinearLayout, new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT)); RelativeLayout.LayoutParams lp1 = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT); lp1.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM, RelativeLayout.TRUE); lp1.addRule(RelativeLayout.CENTER_HORIZONTAL, RelativeLayout.TRUE); lp1.addRule(RelativeLayout.CENTER_VERTICAL, RelativeLayout.TRUE); mRelativeLayout.addView(mViewPager, lp1); RelativeLayout.LayoutParams lp2 = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.FILL_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT); lp2.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM, RelativeLayout.TRUE); mRelativeLayout.addView(mNavLayoutParent, lp2); addView(mRelativeLayout, new LinearLayout.LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT)); displayImage = FileUtil.getBitmapFromSrc("image/play_display.png"); hideImage = FileUtil.getBitmapFromSrc("image/play_hide.png"); mListViews = new ArrayList<View>(); mAbViewPagerAdapter = new AbViewPagerAdapter(context, mListViews); mViewPager.setAdapter(mAbViewPagerAdapter); mViewPager.setFadingEdgeLength(0); mViewPager.setOnPageChangeListener(new OnPageChangeListener() { @Override public void onPageSelected(int position) { makesurePosition(); onPageSelectedCallBack(position); } @Override public void onPageScrollStateChanged(int state) { } @Override public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) { onPageScrolledCallBack(position); } }); } /** * . */ public void creatIndex() { // ? navLinearLayout.removeAllViews(); mNavLayoutParent.setHorizontalGravity(navHorizontalGravity); navLinearLayout.setGravity(Gravity.CENTER); navLinearLayout.setVisibility(View.VISIBLE); count = mListViews.size(); navLayoutParams.setMargins(5, 5, 5, 5); for (int j = 0; j < count; j++) { ImageView imageView = new ImageView(context); imageView.setLayoutParams(navLayoutParams); if (j == 0) { imageView.setImageBitmap(displayImage); } else { imageView.setImageBitmap(hideImage); } navLinearLayout.addView(imageView, j); } } /** * ??. */ public void makesurePosition() { position = mViewPager.getCurrentItem(); for (int j = 0; j < count; j++) { if (position == j) { ((ImageView) navLinearLayout.getChildAt(position)).setImageBitmap(displayImage); } else { ((ImageView) navLinearLayout.getChildAt(j)).setImageBitmap(hideImage); } } } /** * ???. * * @param view * the view */ public void addView(View view) { mListViews.add(view); if (view instanceof AbsListView) { } else { view.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { if (mOnItemClickListener != null) { mOnItemClickListener.onClick(position); } } }); view.setOnTouchListener(new OnTouchListener() { @Override public boolean onTouch(View view, MotionEvent event) { if (mAbOnTouchListener != null) { mAbOnTouchListener.onTouch(event); } return false; } }); } mAbViewPagerAdapter.notifyDataSetChanged(); creatIndex(); } /** * ???. * * @param views * the views */ public void addViews(List<View> views) { mListViews.addAll(views); for (View view : views) { view.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { if (mOnItemClickListener != null) { mOnItemClickListener.onClick(position); } } }); view.setOnTouchListener(new OnTouchListener() { @Override public boolean onTouch(View view, MotionEvent event) { if (mAbOnTouchListener != null) { mAbOnTouchListener.onTouch(event); } return false; } }); } mAbViewPagerAdapter.notifyDataSetChanged(); creatIndex(); } /** * ???. * */ @Override public void removeAllViews() { mListViews.clear(); mAbViewPagerAdapter.notifyDataSetChanged(); creatIndex(); } /** * ????. * * @param position * the position */ private void onPageScrolledCallBack(int position) { if (mAbScrolledListener != null) { mAbScrolledListener.onScroll(position); } } /** * ????. * * @param position * the position */ private void onPageSelectedCallBack(int position) { if (mAbChangeListener != null) { mAbChangeListener.onChange(position); } } /** ? handler. */ private Handler handler = new Handler() { @Override public void handleMessage(Message msg) { if (msg.what == 0) { int count = mListViews.size(); int i = mViewPager.getCurrentItem(); if (playingDirection == 0) { if (i == count - 1) { playingDirection = -1; i = 0; } else { i++; } } else { if (i == 0) { count = playingDirection = 0; i++; } else { i = 0; } } mViewPager.setCurrentItem(i, true); if (play) { handler.postDelayed(runnable, time); } } } }; /** . */ private Runnable runnable = new Runnable() { public void run() { if (mViewPager != null) { handler.sendEmptyMessage(0); } } }; /** * ??. */ public void startPlay() { if (handler != null) { play = true; handler.postDelayed(runnable, 5000); } } /** * ??. */ public void stopPlay() { if (handler != null) { play = false; handler.removeCallbacks(runnable); } } /** * ?. * * @param onItemClickListener * the new on item click listener */ public void setOnItemClickListener(AbOnItemClickListener onItemClickListener) { mOnItemClickListener = onItemClickListener; } /** * ?????. * * @param abChangeListener * the new on page change listener */ public void setOnPageChangeListener(AbOnChangeListener abChangeListener) { mAbChangeListener = abChangeListener; } /** * ????. * * @param abScrolledListener * the new on page scrolled listener */ public void setOnPageScrolledListener(AbOnScrollListener abScrolledListener) { mAbScrolledListener = abScrolledListener; } /** * ???Touch?. * * @param abOnTouchListener * the new on touch listener */ public void setOnTouchListener(AbOnTouchListener abOnTouchListener) { mAbOnTouchListener = abOnTouchListener; } /** * Sets the page line image. * * @param displayImage * the display image * @param hideImage * the hide image */ public void setPageLineImage(Bitmap displayImage, Bitmap hideImage) { this.displayImage = displayImage; this.hideImage = hideImage; creatIndex(); } /** * ???ViewPager. * * @return the view pager */ public ViewPager getViewPager() { return mViewPager; } /** * ????View?. * * @return the count */ public int getCount() { return mListViews.size(); } /** * ????,AddView?. * * @param horizontalGravity * the nav horizontal gravity */ public void setNavHorizontalGravity(int horizontalGravity) { navHorizontalGravity = horizontalGravity; } /** * ScrollView?. * * @param parentScrollView * the new parent scroll view */ public void setParentScrollView(ScrollView parentScrollView) { this.mViewPager.setParentScrollView(parentScrollView); } /** * ListView?. * * @param parentListView * the new parent list view */ public void setParentListView(ListView parentListView) { this.mViewPager.setParentListView(parentListView); } /** * ??. * * @param resid * the new nav layout background */ public void setNavLayoutBackground(int resid) { navLinearLayout.setBackgroundResource(resid); } /** * ?. * * @see AbOnChangeEvent */ public interface AbOnChangeListener { /** * ?. * * @param position * the position */ public void onChange(int position); } /** * ??. * * @see AbOnItemClickEvent */ public interface AbOnItemClickListener { /** * ??. * * @param position * */ public void onClick(int position); } /** * . * * @see AbOnScrollEvent */ public interface AbOnScrollListener { /** * . * * @param arg1 * ? */ public void onScroll(int arg1); /** * ?. */ public void onScrollStoped(); /** * . */ public void onScrollToLeft(); /** * ?. */ public void onScrollToRight(); } /** * ??. * * @see AbOnTouchEvent */ public interface AbOnTouchListener { /** * ??Touch. * * @param event * */ public void onTouch(MotionEvent event); } }