Android TV HorizontalScrollView picture carousel remote control left and right button processing

/**
     * Load next image
     */
    protected void loadNextImg()
    {
        View view;

// int oldViewPos = mContainer.getChildCount() - 1;
// mViewPos.remove(mContainer.getChildAt(oldViewPos));
// mContainer .removeViewAt(oldViewPos);
        //Remove the first image and set the horizontal scroll position to 0
// scrollTo(0, 0);
        mViewPos.remove(mContainer.getChildAt(0));
        mContainer.removeViewAt(0);
        //Get the next picture, set the onclick event, and add it to the container
        Log.d("mContainer"," mCurrentIndex=" + mCurrentIndex + " mAdapter.getCount() = " + mAdapter.getCount() );
        if( mCurrentIndex == mAdapter.getCount() - 1){
            Log.d("mCurrentIndex","mCurrentIndex = " + mCurrentIndex);
            Log.d("mAdapter.getCount() - 1","mAdapter.getCount() - 1 = " + mAdapter.getCount());

            view = mAdapter.getView(0, null, mContainer);
            Log.d("view","0 = " + 0);

            mCurrentIndex = 0;
        }else{
            view = mAdapter.getView(++mCurrentIndex, null, mContainer);

            Log.d("view","mCurrentIndex = " + mCurrentIndex);

        }
        view.setOnClickListener(this);
        mContainer.addView(view);
        Log.d("mCurrentIndex",mCurrentIndex + "");
//        if(keyCodevalue == 1){
//            mViewPos.put(view, mCurrentIndex - 1);
// } else{
            mViewPos.put(view, mCurrentIndex);
// }

        Log.d("mContainer","++++++++ mCurrentIndex=" + mCurrentIndex + " mAdapter.getCount() = " + mAdapter .getCount() );
//The experiment here is so many times that it can only be done like this, but there is still a little problem with sliding
//Delete two lines, you can
        //scrollTo((findViewById(R.id.id_gallery) .getWidth()), 0);

        //smoothScrollBy(-(findViewById(R.id.id_gallery).getWidth()), 0);

        Log.d("id",(findViewById(R.id.id_gallery). getWidth()) + "");
        Log.d("mChildWidth",mChildWidth + "");
        //The current first picture small scale
        if(mFristIndex == mAdapter.getCount() - 1){
            mFristIndex = 0;
        } else {
            mFristIndex++;
        }
        Log.d(TAG,mFristIndex + " ");
        //If scroll monitor is set, trigger

        Log.d("mContainer","++++++++ mCurrentIndex=" + mCurrentIndex + " mFristIndex = " + mFristIndex );
    }


/**
     * Load previous image
     */
    protected void loadPreImg()
    {
        int oldViewPos = mContainer.getChildCount() - 1;
        Log.d("oldViewPos","oldViewPos = "+ oldViewPos);
        Log. d("mFristIndex","mFristIndex = "+ mFristIndex);
        if (mFristIndex == 0) {
            mViewPos.remove(mContainer.getChildAt(oldViewPos));
            mContainer.removeViewAt(oldViewPos);
            View view = mAdapter.getView(mAdapter.getCount() - 1, null,
                    mContainer);
            Log.d("view","mAdapter.getCount() - 1" + mAdapter.getCount());
            mViewPos.put(view, mAdapter.getCount() - 1);
            mContainer.addView(view, 0);
            view.setOnClickListener(this);
            // 水平滚动位置向左移动view的宽度个像素
            scrollTo(0, 0);
            Log.d("mChildWidth","mChildWidth ="+mChildWidth);
            mFristIndex = mAdapter.getCount() - 1;
            Log.d("mFristIndex","mFristIndex = "+mFristIndex);
        } else {
            int index = mCurrentIndex - mCountOneScreen;
            Log.d("mCurrentIndex","mCurrentIndex "+ mCurrentIndex);
            Log.d("mCountOneScreen","mCountOneScreen "+ mCountOneScreen);
            Log.d("index","mCurrentIndex - mCountOneScreen = "+ index);
            if (index >= 0) {
                mViewPos.remove(mContainer.getChildAt(oldViewPos));
                mContainer.removeViewAt(oldViewPos);
                View view = mAdapter.getView(index, null, mContainer);
                Log.d("view","index = " + index);
                mViewPos.put(view, index);
                mContainer.addView(view, 0);
                view.setOnClickListener(this);
                // Move the horizontal scroll position to the left by the width of the view
                scrollTo(0, 0);
                Log.d("mChildWidth","mChildWidth = "+mChildWidth);
                mFristIndex--;
                Log.d("mFristIndex","mFristIndex = "+mFristIndex);
            } else {
                mViewPos.remove(mContainer.getChildAt(oldViewPos));
                mContainer.removeViewAt(oldViewPos);
                View view = mAdapter.getView(index + mAdapter.getCount(), null,
                        mContainer);
                Log.d("view","index + mAdapter.getCount() = " + index + mAdapter.getCount());
                mViewPos.put(view, index + mAdapter.getCount());
                mContainer.addView(view, 0);
                view.setOnClickListener(this);
                // Move the horizontal scroll position to the left by the width of the view
                scrollTo(0, 0);
                Log.d("mChildWidth","mChildWidth = "+mChildWidth);
                // Current position --, The current first displayed index --
                mFristIndex--;
                Log.d("mFristIndex","mFristIndex = "+mFristIndex);
            }
        }
        mCurrentIndex = mViewPos
                .get(mContainer.getChildAt(mCountOneScreen - 1));
        Log. d("mCurrentIndex","mCurrentIndex = "+mCurrentIndex);
    }

@Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        // TODO Auto-generated method stub

        if(keyCode == event.KEYCODE_DPAD_LEFT){

            Log.d("keyCodevalue","keyCodevalue = " + keyCodevalue);
            if(keyCodevalue == 2){
                Log.d("keyCodevalue","zhixing = " + keyCodevalue);
            } else {
                loadPreImg();
            }

            keyCodevalue = 1;
        }else if(keyCode == event.KEYCODE_DPAD_RIGHT){

            Log.d("keyCodevalue","keyCodevalue = " + keyCodevalue);
            if(keyCodevalue == 1){
                Log.d("keyCodevalue","zhixing = " + keyCodevalue);

            } else {
                loadNextImg();
            }
            keyCodevalue = 2;
            //Prevent the processing of the right button
            if(keyCodevalue == 1){ //When the left mouse button was pressed before, execute
                for (int i = 0; i < mContainer.getChildCount(); i++)
                {
                    mContainer.getChildAt(i).setBackgroundColor(Color.WHITE);
                }

                mListener.onCurrentImgChanged(mFristIndex, mContainer.getChildAt(0));
            } else {
                for (int i = 0; i < mContainer.getChildCount(); i++)
                {
                    mContainer.getChildAt(i).setBackgroundColor(Color.WHITE);
                }

                mListener.onCurrentImgChanged(mFristIndex, mContainer.getChildAt(1));
            }
            return true;
        }
        if(keyCodevalue == 1){  //当之前按下的是鼠标左键的情况下,执行
            for (int i = 0; i < mContainer.getChildCount(); i++)
            {
                mContainer.getChildAt(i).setBackgroundColor(Color.WHITE);
            }

            mListener.onCurrentImgChanged(mFristIndex, mContainer.getChildAt(0));
        } else {
            for (int i = 0; i < mContainer.getChildCount(); i++)
            {
                mContainer.getChildAt(i).setBackgroundColor(Color.WHITE);
            }

            mListener.onCurrentImgChanged(mFristIndex, mContainer.getChildAt(1));
        }

        Log.d("keyCodeValue","mFristIndex = " + mFristIndex);
        return super.onKeyDown(keyCode, event);
    }

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326123291&siteId=291194637