【问题标题】:GestureDetector working only on part of the activityGestureDetector 仅在部分活动上工作
【发布时间】:2015-02-20 16:56:52
【问题描述】:

我有一个实现 onGestureListener 的活动,我在它的 onFling 方法中有一些代码。我用于此活动的 xml 布局包含一个包含相对布局和自定义布局类的线性布局(垂直)。

这个自定义布局类扩展了相对布局。在这个自定义布局类中,我有一个 readerView(从 Mupdf 库打开 pdf)。当我在相对布局部分上滑动时,它可以正常工作,但是当我在我的自定义布局 (pdfLayout) 上滑动时,它不会检测到手势。我什至为 onCreate 上的 pdfLayout 设置了一个触摸监听器。我意识到,当我在 PdfLayout 上滑动时,正在调用 PdfLayout 内部的 ReaderView 中的 onFling 方法。我不明白如何从我的活动与 PdfLayout 内的读者视图进行通信。我不知道我在哪里犯了错误。感谢您的帮助!

这是我的 PdfActivity:

    import java.io.File;
    import android.annotation.SuppressLint;
    import android.app.Activity;
    import android.content.Context;
    import android.content.Intent;
    import android.graphics.Color;
    import android.graphics.Typeface;
    import android.net.Uri;
    import android.os.Bundle;
    import android.os.Environment;
    import android.util.Log;
    import android.view.MotionEvent;
    import android.view.View;
    import android.view.View.OnClickListener;
    import android.view.View.OnTouchListener;
    import android.view.Window;
    import android.view.WindowManager;
    import android.widget.ImageButton;
    import android.widget.LinearLayout;
    import android.widget.RelativeLayout;
    import android.widget.TextView;
    import android.view.GestureDetector;
    import android.view.GestureDetector.OnGestureListener;


    public class PdfActivity extends Activity implements OnGestureListener{

        private static final int SWIPE_MIN_DISTANCE = 120;
        private static final int SWIPE_MAX_OFF_PATH = 250;
        private static final int SWIPE_THRESHOLD_VELOCITY = 200;

        static Context c;
        static LinearLayout pdfLl;
        PdfLayout pdfLayout;
        static View buttons;
        static RelativeLayout rl;
        static String title;
        TextView tv;
        TextView page;

        int n = 1;
        int totalpages;

        GestureDetector detector;

        @SuppressWarnings("deprecation")
        @SuppressLint("NewApi")
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            // TODO Auto-generated method stub
            super.onCreate(savedInstanceState);
            this.requestWindowFeature(Window.FEATURE_NO_TITLE); 
            this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
            setContentView(R.layout.pdf_act);

             detector=new GestureDetector(this);

             View rootView = findViewById(R.layout.pdf_act);

            pdfLl = (LinearLayout) findViewById(R.id.readerCont);
            rl = (RelativeLayout) findViewById(R.id.buttonsCont);
            pdfLayout = (PdfLayout) findViewById(R.id.pdfLayout);
            ImageButton prev = (ImageButton) findViewById(R.id.prev);
            ImageButton next = (ImageButton) findViewById(R.id.next);
            tv = (TextView) findViewById(R.id.title);
            page = (TextView) findViewById(R.id.pageNo);
            page.setTextColor(Color.BLACK);
            page.setTypeface(null, Typeface.ITALIC);
            c = this;
            Intent intent = getIntent();
            Uri uri = intent.getData();
            title = intent.getStringExtra("title");
            totalpages = intent.getIntExtra("totalpages", 1);
            page.setText(n+" of "+totalpages+" Pages");

            tv.setTextColor(Color.BLACK);
            tv.setTypeface(null, Typeface.BOLD_ITALIC);
            tv.setText(title);

            prev.setOnClickListener(new OnClickListener() {

                @Override
                public void onClick(View paramView) {
                    // TODO Auto-generated method stub
                    Log.e("Pdf Act", "prev button clicked! & n="+n);
                    if(n>1 && n<=totalpages){
                        pdfLayout.pre();
                        n--;
                        File file = new File(Environment.getExternalStorageDirectory()+"/PaperBoy/AndhraBhoomi/AB"+n+".pdf");
                        if(file.exists() && file!=null){
                            pdfLayout.setCore(file.getAbsolutePath());
                        }
                        else{
                            pdfLayout.setCore(Environment.getExternalStorageDirectory()+"/pdpage.pdf");
                        }
                        page.setText(n+" of "+totalpages+" Pages");
                    }
                }
            });

            next.setOnClickListener(new OnClickListener() {

                @Override
                public void onClick(View v) {
                    // TODO Auto-generated method stub
                    Log.e("Pdf Act", "next button clicked! & n="+n);
                    if(n>=1 && n<totalpages){
                        pdfLayout.next();
                        n++;
                        File file = new File(Environment.getExternalStorageDirectory()+"/PaperBoy/AndhraBhoomi/AB"+n+".pdf");
                        if(file.exists() && file!=null){
                            pdfLayout.setCore(file.getAbsolutePath());
                        }
                        else{                       pdfLayout.setCore(Environment.getExternalStorageDirectory()+"/pdpage.pdf");
                        }
                        page.setText(n+" of "+totalpages+" Pages");
                    }
                }
            });

            pdfLayout.setOnTouchListener(new OnTouchListener() {

                @Override
                public boolean onTouch(View paramView, MotionEvent paramMotionEvent) {
                    // TODO Auto-generated method stub
                    return detector.onTouchEvent(paramMotionEvent);
                }
            });
        }   

         @Override
            protected void onPause() {
                super.onPause();
                onStop();
            }

         public void screenTapped(View view) {
                Log.e("pdf act", "tapped");

                if(rl.getVisibility()==View.VISIBLE){
                    rl.setVisibility(View.GONE);
                }

                if(rl.getVisibility()==View.GONE){
                    rl.setVisibility(View.VISIBLE);
                }
            }

         @Override
            public boolean onTouchEvent(MotionEvent event) {
                // TODO Auto-generated method stub
                return detector.onTouchEvent(event);
            }

        @Override
        public boolean onDown(MotionEvent paramMotionEvent) {
            // TODO Auto-generated method stub
            Log.e("Pdf Act", "on down gesture");
            return false;
        }


        @Override
        public void onShowPress(MotionEvent paramMotionEvent) {
            // TODO Auto-generated method stub
            Log.e("Pdf Act", "on show press gesture");
        }


        @Override
        public boolean onSingleTapUp(MotionEvent paramMotionEvent) {
            // TODO Auto-generated method stub
            Log.e("Pdf Act", "on single tap up gesture");
            return true;
        }


        @Override
        public boolean onScroll(MotionEvent paramMotionEvent1,
                MotionEvent paramMotionEvent2, float paramFloat1, float paramFloat2) {
            // TODO Auto-generated method stub
            Log.e("Pdf Act", "on scroll gesture");
            return false;
        }


        @Override
        public void onLongPress(MotionEvent paramMotionEvent) {
            // TODO Auto-generated method stub
            Log.e("Pdf Act", "on long press gesture");
        }


        @Override
        public boolean onFling(MotionEvent e1,
                MotionEvent e2, float velocityX, float velocityY) {
            // TODO Auto-generated method stub
            Log.e("Pdf Act", "on fling gesture");

            try {
                if (Math.abs(e1.getY() - e2.getY()) > SWIPE_MAX_OFF_PATH){
                    return false;
                }
                // right to left swipe
                if (e1.getX() - e2.getX() > SWIPE_MIN_DISTANCE
                        && Math.abs(velocityX) > SWIPE_THRESHOLD_VELOCITY) {
                //    onLeftSwipe();    
                    if(n>=1 && n<totalpages){
                                pdfLayout.next();
                                n++;
                                File file = new File(Environment.getExternalStorageDirectory()+"/PaperBoy/AndhraBhoomi/AB"+n+".pdf");
                                if(file.exists() && file!=null){
                                    pdfLayout.setCore(file.getAbsolutePath());
                                }
                                else{
                                    pdfLayout.setCore(Environment.getExternalStorageDirectory()+"/pdpage.pdf");
                                }
                                page.setText(n+" of "+totalpages+" Pages");
                            }
                } 
                // left to right swipe
                else if (e2.getX() - e1.getX() > SWIPE_MIN_DISTANCE
                        && Math.abs(velocityX) > SWIPE_THRESHOLD_VELOCITY) {
                  //  onRightSwipe();
                    if(n>1 && n<=totalpages){
                                pdfLayout.pre();
                                n--;
                                File file = new File(Environment.getExternalStorageDirectory()+"/PaperBoy/AndhraBhoomi/AB"+n+".pdf");
                                if(file.exists() && file!=null){
                                    pdfLayout.setCore(file.getAbsolutePath());
                                }
                                else{
                                    pdfLayout.setCore(Environment.getExternalStorageDirectory()+"/pdpage.pdf");
                                }
                                page.setText(n+" of "+totalpages+" Pages");
                            }
                }
            } catch (Exception e) {

            }
            return false;
          }


    }

这是我用于上述活动的 xml 布局文件:

    <LinearLayout  xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/readerCont"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical" 
        android:background="@drawable/tiled_background"> 

        <RelativeLayout
            android:id="@+id/buttonsCont"
            android:layout_width="match_parent"
            android:layout_height="40dp"
            android:background="@color/grey"
            android:layout_gravity="top">  
        <ImageButton
            android:id="@+id/prev"
            android:src="@drawable/left_arrow"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:background="#0000"
            android:layout_alignParentLeft="true"
            android:layout_centerVertical="true"
            android:paddingLeft="3dp"
            android:clickable="true"/>
        <TextView
            android:id="@+id/title"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_centerHorizontal="true"
            android:paddingTop="3dp"/>
        <TextView 
            android:id="@+id/pageNo"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_centerHorizontal="true"
            android:layout_below="@+id/title"
            android:paddingBottom="3dp"/>
        <ImageButton 
            android:id="@+id/next"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:background="#0000"
            android:src="@drawable/right_arrow"
            android:layout_alignParentRight="true"
            android:layout_centerVertical="true"
            android:clickable="true"
            android:paddingRight="3dp"/>
        </RelativeLayout>

        <com.paper.PdfLayout
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/pdfLayout"
            android:clickable="true"
            android:focusable="true"
            android:focusableInTouchMode="true"/>

      </LinearLayout>

【问题讨论】:

    标签: android android-relativelayout mupdf gesturedetector onfling


    【解决方案1】:

    我想通了。我刚刚创建了一个使用 get 和 set 方法扩展 Application 的 GlobalState 类。我在阅读器视图的 onFling 中使用 GlobalState 类的 getter 和 setter 方法输入我的代码,它运行良好。希望它可以帮助某人。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-02-04
      • 1970-01-01
      • 1970-01-01
      • 2013-09-16
      • 1970-01-01
      • 2018-05-08
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多