简体   繁体   English

GestureDetector仅适用于部分活动

[英]GestureDetector working only on part of the activity

I have an activity that implements onGestureListener and I have some code in its onFling method. 我有一个实现onGestureListener的活动,我的onFling方法中有一些代码。 The xml layout I am using for this activity contains a Linear layout (vertical) wrapping a relative layout and a custom layout class. 我用于此活动的xml布局包含一个包含相对布局和自定义布局类的线性布局(垂直)。

This custom layout class extends Relative layout. 此自定义布局类扩展了相对布局。 Inside this custom layout class, I have a readerView (from Mupdf library to open pdfs). 在这个自定义布局类中,我有一个readerView(从Mupdf库打开pdfs)。 When I swipe on the relative layout part, it works fine but when I swipe on my custom layout (pdfLayout), it does not detect the gesture. 当我在相对布局部分上滑动时,它工作正常,但是当我在我的自定义布局(pdfLayout)上滑动时,它不会检测到手势。 I even set up an on touch listener for the pdfLayout on onCreate. 我甚至在onCreate上为pdfLayout设置了一个触摸式监听器。 I realized that the onFling method in ReaderView that is inside the PdfLayout is being called when I swipe on the PdfLayout. 我意识到当我在PdfLayout上滑动时,在PdfLayout内部的ReaderView中的onFling方法被调用。 I do not understand how to communicate from my activity to my reader view that is inside the PdfLayout. 我不明白如何从我的活动传达到PdfLayout内部的读者视图。 I do not know where I made a mistake. 我不知道我犯了什么错误。 I appreciate any help! 我感谢任何帮助!

Here is my PdfActivity: 这是我的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;
          }


    }

Here is my xml layout file for the above activity: 这是我上面活动的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>

I figured it out. 我想到了。 I just created a GlobalState class that extends Application with get and set methods. 我刚刚创建了一个GlobalState类,它使用get和set方法扩展Application。 I put in my code using getter and setter methods of the GlobalState class in the onFling of reader view and it works perfectly. 我在阅读器视图的onFling中使用GlobalState类的getter和setter方法输入我的代码,它完美地运行。 Hope it helps someone. 希望它可以帮助某人。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM