com.bangqu.eshow.view.sliding.ESSlidingPlayView.java Source code

Java tutorial

Introduction

Here is the source code for com.bangqu.eshow.view.sliding.ESSlidingPlayView.java

Source

/*
 * 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.bangqu.eshow.view.sliding;

import java.util.ArrayList;
import java.util.List;

import android.content.Context;
import android.graphics.drawable.Drawable;
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;

import com.bangqu.eshow.adapter.ESViewPagerAdapter;
import com.bangqu.eshow.util.ESViewUtil;
import com.bangqu.eshow.view.sample.ESInnerViewPager;

// TODO: Auto-generated Javadoc
/**
 * ?View.
 *
 */

public class ESSlidingPlayView extends LinearLayout {

    /** . */
    private Context context;

    /** ViewPager. */
    private ESInnerViewPager mViewPager;

    /** . */
    private LinearLayout navLinearLayout;

    /** ?. */
    public LinearLayout.LayoutParams navLayoutParams = null;

    /** . */
    private int count, position;

    /** . */
    private Drawable displayDrawable;
    private Drawable hideDrawable;

    /** . */
    private AbOnItemClickListener mOnItemClickListener;

    /** ?. */
    private AbOnChangeListener mAbChangeListener;

    /** . */
    private AbOnScrollListener mAbScrolledListener;

    /** . */
    private AbOnTouchListener mAbOnTouchListener;

    /** List views. */
    private ArrayList<View> mListViews = null;

    /** ?. */
    private ESViewPagerAdapter mAbViewPagerAdapter = null;

    /** View. */
    private LinearLayout mNavLayoutParent = null;

    /** ??. */
    private int navHorizontalGravity = Gravity.RIGHT;

    /** ?. */
    private int playingDirection = 0;

    /** . */
    private boolean play = false;

    /**
     * AbSlidingPlayView.
     *
     * @param context the context
     */
    public ESSlidingPlayView(Context context) {
        super(context);
        initView(context);
    }

    /**
     * xml?AbSlidingPlayView.
     *
     * @param context the context
     * @param attrs the attrs
     */
    public ESSlidingPlayView(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 ESInnerViewPager(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));

        mListViews = new ArrayList<View>();
        mAbViewPagerAdapter = new ESViewPagerAdapter(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);
        navLayoutParams.width = 15;
        navLayoutParams.height = 15;
        for (int j = 0; j < count; j++) {
            ImageView imageView = new ImageView(context);
            imageView.setLayoutParams(navLayoutParams);
            if (j == 0) {
                imageView.setImageDrawable(displayDrawable);
            } else {
                imageView.setImageDrawable(hideDrawable);
            }
            ESViewUtil.scaleView(imageView);
            navLinearLayout.addView(imageView, j);
        }

    }

    /**
     * ??.
     */
    public void makesurePosition() {
        position = mViewPager.getCurrentItem();
        for (int j = 0; j < count; j++) {
            if (position == j) {
                ((ImageView) navLinearLayout.getChildAt(position)).setImageDrawable(displayDrawable);
            } else {
                ((ImageView) navLinearLayout.getChildAt(j)).setImageDrawable(hideDrawable);
            }
        }
    }

    /**
     * ???.
     *
     * @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--;
                    } else {
                        i++;
                    }
                } else {
                    if (i == 0) {
                        playingDirection = 0;
                        i++;
                    } else {
                        i--;
                    }
                }

                mViewPager.setCurrentItem(i, true);
                if (play) {
                    handler.postDelayed(runnable, 5000);
                }
            }
        }

    };

    /** . */
    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;
    }

    /**
     * 
     *
     * @param displayDrawable ?
     * @param hideDrawable ?
     */
    public void setNavPageResources(int displayResId, int hideResId) {
        this.displayDrawable = this.getResources().getDrawable(displayResId);
        this.hideDrawable = this.getResources().getDrawable(hideResId);
        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);
    }

}