垂直视图寻呼机中的滚动视图

时间:2014-11-16 16:09:15

标签: android android-fragments

我正在尝试在其中一个片段中创建一个带有scrollview的垂直viewpager。问题是当我向上滑动时,我不会进入第二个片段。我已将代码上传到Github(https://github.com/RajuMandala/verticalviewpager)。如果我做错了,请告诉我。谢谢你的时间。

我的FirstFragment.java代码如下,

public class FirstFragment extends Fragment {
    // Scrolling code
    private LinearLayout verticalOuterLayout;
    private ScrollView verticalScrollview;
    private TextView verticalTextView;
    private int verticalScrollMax;
    private Timer scrollTimer       =   null;
    private TimerTask clickSchedule;
    private TimerTask scrollerSchedule;
    private TimerTask faceAnimationSchedule;
    private int scrollPos = 0;
    private Boolean isFaceDown      =   true;
    private Timer clickTimer        =   null;
    private Timer faceTimer         =   null;
    private Button clickedButton    =   null;
    private String[] nameArray = {"Apple", "Banana", "Grapes", "Orange", "Strawberry","Apple", "Banana","Grapes"};
    private String[] imageNameArray = {"apple", "banana", "grapes", "orange", "strawberry","apple", "banana","grapes"};
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View v = inflater.inflate(R.layout.first_frag,container,false);

        // Scrolling code
        verticalScrollview  =   (ScrollView) v.findViewById(R.id.vertical_scrollview_id);
        verticalOuterLayout =   (LinearLayout) v.findViewById(R.id.vertical_outer_layout_id);

        //addImagesToView();
        addTextsToView();

        ViewTreeObserver vto        =   verticalOuterLayout.getViewTreeObserver();
        /*vto.addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
            @Override
            public void onGlobalLayout() {
                verticalOuterLayout.getViewTreeObserver().re
                getScrollMaxAmount();
                startAutoScrolling();
            }
        });*/
        vto.addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
            @Override
            public void onGlobalLayout() {
                //
                // mycode
                //
                if (Build.VERSION.SDK_INT<16) {
                    removeLayoutListenerPre16(verticalOuterLayout.getViewTreeObserver(),this);
                } else {
                    removeLayoutListenerPost16(verticalOuterLayout.getViewTreeObserver(), this);
                }
                getScrollMaxAmount();
                startAutoScrolling();
            }
        });
        verticalOuterLayout.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                v.getParent().requestDisallowInterceptTouchEvent(true);
                return true;
            }
        });

        return v;
    }
    @SuppressWarnings("deprecation")
    private void removeLayoutListenerPre16(ViewTreeObserver observer, ViewTreeObserver.OnGlobalLayoutListener listener){
        observer.removeGlobalOnLayoutListener(listener);
    }

    @TargetApi(16)
    private void removeLayoutListenerPost16(ViewTreeObserver observer, ViewTreeObserver.OnGlobalLayoutListener listener){
        observer.removeOnGlobalLayoutListener(listener);
    }


    // Scrolling code
    public void addTextsToView(){
        for (int i=0;i<nameArray.length;i++){
            final TextView textView = new TextView(getActivity());
            textView.setText(nameArray[i]);

            LinearLayout.LayoutParams params    =   new LinearLayout.LayoutParams(256,256);
            textView.setLayoutParams(params);
            verticalOuterLayout.addView(textView);
        }
    }

    public void getScrollMaxAmount(){
        int actualWidth = (verticalOuterLayout.getMeasuredHeight()-(256*3));
        verticalScrollMax   = actualWidth;
    }

    public void startAutoScrolling(){
        if (scrollTimer == null) {
            scrollTimer                 =   new Timer();
            final Runnable Timer_Tick   =   new Runnable() {
                public void run() {
                    moveScrollView();
                }
            };

            if(scrollerSchedule != null){
                scrollerSchedule.cancel();
                scrollerSchedule = null;
            }
            scrollerSchedule = new TimerTask(){
                @Override
                public void run(){
                    getActivity().runOnUiThread(Timer_Tick);
                }
            };

            scrollTimer.schedule(scrollerSchedule, 30, 30);
        }
    }

    public void moveScrollView(){
        scrollPos                           =   (int) (verticalScrollview.getScrollY() + 1.0);
        if(scrollPos >= verticalScrollMax){
            scrollPos                       =   0;
        }
        verticalScrollview.scrollTo(0,scrollPos);
    }

    public void onDestroy(){
        clearTimerTaks(clickSchedule);
        clearTimerTaks(scrollerSchedule);
        clearTimerTaks(faceAnimationSchedule);
        clearTimers(scrollTimer);
        clearTimers(clickTimer);
        clearTimers(faceTimer);

        clickSchedule         = null;
        scrollerSchedule      = null;
        faceAnimationSchedule = null;
        scrollTimer           = null;
        clickTimer            = null;
        faceTimer             = null;

        super.onDestroy();
    }

    private void clearTimers(Timer timer){
        if(timer != null) {
            timer.cancel();
            timer = null;
        }
    }

    private void clearTimerTaks(TimerTask timerTask){
        if(timerTask != null) {
            timerTask.cancel();
            timerTask = null;
        }
    }
}

请参阅下面第一个片段的屏幕截图, enter image description here

0 个答案:

没有答案