setOnTouchListener仅适用于一个视图

时间:2016-03-18 18:11:45

标签: java android

我有一个XML布局,我正在使用OnTouchListener,其任务是检测手指滑动方向;无论如何,它仅在布局上执行滑动时才起作用,但不在它的项目上(如ImageViews,Buttons等)。如何重写它以使滑动工作在整个布局上,但避免为其上的所有项目创建数十个监听器?

布局:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
                android:orientation="vertical"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:id="@+id/main_layout"  
                android:clickable="true"
                android:focusable="true"
                android:focusableInTouchMode="true"
                                                    >
</RelativeLayout>

代码:

View relative = findViewById(R.id.main_layout);
relative.setOnTouchListener(...)

3 个答案:

答案 0 :(得分:1)

因为您只为一个父视图设置OnclickListener。这就是为什么只有一个点击响应的原因。 你可以试试这个。

         Button button1 = (Button) findviewbyid(R.id.buttonid);
          button.setOnTouchListener(...)

同样的图像或其他视图。 编辑 - 对每个要列入滑动的视图执行此操作。

 ImageView im = (ImgaeView)  findviewbyid(R.id.imageid)   
   im.setOnTouchListener(...)

与每个用于列表滑动的视图类似。

编辑 - 所以你不想做更多的ONTouchListener。创建另一个实现OnTouchListener

的类
    public class Mytouchlistener implements OnTouchlistener{
     @Override
 public boolean onTouch(View v, MotionEvent event) {
   HERE PUT YOUR CODE.
    }

然后

         Youanyview.OnTouchlistener(MyTouchlistener.class);

有了主意???

答案 1 :(得分:1)

您可以实施View.OnTouchListener,如下所示

   public class SomeClass implements View.OnTouchListener {
    @Override
    public boolean onTouch(View v, MotionEvent event) {
        switch (v.getId()){
            case R.id.view1:
                //do something
                break;
            case R.id.view2:
                //do something here too
                break;
            default:
                break;
        }
        return false;
    }

   }

请注意,您必须使用setOnTouchListener

祝你好运。

答案 2 :(得分:0)

您可以使用以下代码制作完美的Android手势:

在Main_activity.java中键入以下代码:

package com.androidtutorialpoint.myapplication; 


public class MainActivity extends AppCompatActivity implements
                                  GestureDetector.OnGestureListener, 
                                  GestureDetector.OnDoubleTapListener { 

    private TextView output_text;               
    private GestureDetectorCompat DetectMe;       

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
        super.onCreate(savedInstanceState); 
        setContentView(R.layout.activity_main); 
        output_text = (TextView) findViewById(R.id.outputText);  // Taking reference of text to be displayed on screen
        DetectMe = new GestureDetectorCompat(this,this); 
        DetectMe.setOnDoubleTapListener(this); 

        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar); 
        setSupportActionBar(toolbar); 

        FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab); 
        fab.setOnClickListener(new View.OnClickListener() { 
            @Override 
            public void onClick(View view) { 
                Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG) 
                        .setAction("Action", null).show(); 
            } 
        }); 
    } 

// Following functions are overrided to show text when a particular method called.

    @Override 
    public boolean onSingleTapConfirmed(MotionEvent e) { 
        output_text.setText("onSingleTapConfirmed"); 
        return true; 
    } 

    @Override 
    public boolean onDoubleTap(MotionEvent e) { 
        output_text.setText("onDoubleTap"); 
        return true; 
    } 

    @Override 
    public boolean onDoubleTapEvent(MotionEvent e) { 
        output_text.setText("onDoubleTapEvent"); 
        return true; 
    } 

    @Override 
    public boolean onDown(MotionEvent e) { 
        output_text.setText("onDown"); 
        return true; 
    } 

    @Override 
    public void onShowPress(MotionEvent e) { 
        output_text.setText("onShowPress"); 

    } 

    @Override 
    public boolean onSingleTapUp(MotionEvent e) { 
        output_text.setText("onSingleTapUp"); 
        return true; 
    } 

    @Override 
    public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX, float distanceY) { 
        output_text.setText("onScroll"); 
        return true; 
    } 

    @Override 
    public void onLongPress(MotionEvent e) { 
        output_text.setText("onLongPress"); 
    } 

    @Override 
    public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) { 
        output_text.setText("onFling"); 
        return true; 
    } 

    @Override 
    public boolean onTouchEvent(MotionEvent event){ 
        this.DetectMe.onTouchEvent(event); 
        return super.onTouchEvent(event); 
    } 

    @Override 
    public boolean onCreateOptionsMenu(Menu menu) { 
        // Inflate the menu; this adds items to the action bar if it is present. 
        getMenuInflater().inflate(R.menu.menu_main, menu); 
        return true; 
    } 

    @Override 
    public boolean onOptionsItemSelected(MenuItem item) { 
        // Handle action bar item clicks here. The action bar will 
        // automatically handle clicks on the Home/Up button, so long 
        // as you specify a parent activity in AndroidManifest.xml. 
        int id = item.getItemId(); 
        //noinspection SimplifiableIfStatement 
        if (id == R.id.action_settings) { 
            return true; 
        } 

        return super.onOptionsItemSelected(item); 
    } 
}