Android onTouch不在listview上工作

Android onTouch不在listview上工作,android,listview,android-fragments,ontouchlistener,Android,Listview,Android Fragments,Ontouchlistener,我正在创建一个有片段的应用程序,下面给出了片段的xml <?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" andr

我正在创建一个有片段的应用程序,下面给出了片段的xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    xmlns:app="http://schemas.android.com/apk/res-auto">

    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/cvCalendar"
        android:layout_below="@+id/toolbar"
        android:orientation="vertical"/>

    <ListView
        android:id="@+id/lvList"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_below="@+id/Calendar"
        android:divider="#cecece"
        android:dividerHeight="2dp"
        android:background="#e4c966" />

    </RelativeLayout>
public View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState) 
        {
        View rootView = inflater.inflate(R.layout.layout, container, false);
        cala=(LinearLayout)rootView.findViewById(R.id.Calendar);
        rootView.setOnTouchListener(new View.OnTouchListener() {
        public boolean onTouch(View v, MotionEvent event) {
        if(event.getAction() == MotionEvent.ACTION_MOVE){

            onTouchEvent(event);
          }
        return true;
           }
        });
          return rootView;
         }
public boolean onTouchEvent(MotionEvent touchevent)
    {
       switch (touchevent.getAction())
        {
         case MotionEvent.ACTION_DOWN:
            {
                x1 = touchevent.getX();
                y1 = touchevent.getY();
                break;
              }
            case MotionEvent.ACTION_UP:
            {
                x2 = touchevent.getX();
                y2 = touchevent.getY();

                if (y1 < y2)
                {
                    Toast.makeText(getActivity(), "UP to Down Swap Performed", Toast.LENGTH_SHORT).show();
                    if ((cala.getVisibility())== View.GONE){
                       cala.setVisibility(View.VISIBLE); }
                  }

                if (y1 > y2)
                {
                    Toast.makeText(getActivity(), "Down to UP Swap Performed",Toast.LENGTH_SHORT).show();
                   if ((cala.getVisibility())== View.VISIBLE){
                       cala.setVisibility(View.GONE); }
                   }
                break;
               }
          }
       return false;
}

我在顶部有一个Linearlayout,用于显示日历,日历的事件显示在下面的列表中。 我想有一个功能,向上滑动时,日历被隐藏,向下滑动时,它会显示日历。 下面给出了java代码

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    xmlns:app="http://schemas.android.com/apk/res-auto">

    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/cvCalendar"
        android:layout_below="@+id/toolbar"
        android:orientation="vertical"/>

    <ListView
        android:id="@+id/lvList"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_below="@+id/Calendar"
        android:divider="#cecece"
        android:dividerHeight="2dp"
        android:background="#e4c966" />

    </RelativeLayout>
public View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState) 
        {
        View rootView = inflater.inflate(R.layout.layout, container, false);
        cala=(LinearLayout)rootView.findViewById(R.id.Calendar);
        rootView.setOnTouchListener(new View.OnTouchListener() {
        public boolean onTouch(View v, MotionEvent event) {
        if(event.getAction() == MotionEvent.ACTION_MOVE){

            onTouchEvent(event);
          }
        return true;
           }
        });
          return rootView;
         }
public boolean onTouchEvent(MotionEvent touchevent)
    {
       switch (touchevent.getAction())
        {
         case MotionEvent.ACTION_DOWN:
            {
                x1 = touchevent.getX();
                y1 = touchevent.getY();
                break;
              }
            case MotionEvent.ACTION_UP:
            {
                x2 = touchevent.getX();
                y2 = touchevent.getY();

                if (y1 < y2)
                {
                    Toast.makeText(getActivity(), "UP to Down Swap Performed", Toast.LENGTH_SHORT).show();
                    if ((cala.getVisibility())== View.GONE){
                       cala.setVisibility(View.VISIBLE); }
                  }

                if (y1 > y2)
                {
                    Toast.makeText(getActivity(), "Down to UP Swap Performed",Toast.LENGTH_SHORT).show();
                   if ((cala.getVisibility())== View.VISIBLE){
                       cala.setVisibility(View.GONE); }
                   }
                break;
               }
          }
       return false;
}
创建视图时的公共视图(布局、充气机、视图组容器、捆绑包保存状态) { 视图根视图=充气机。充气(R.layout.layout,container,false); cala=(LinearLayout)rootView.findviewbyd(R.id.Calendar); setOnTouchListener(新视图.OnTouchListener(){ 公共布尔onTouch(视图v,运动事件){ if(event.getAction()==MotionEvent.ACTION\u MOVE){ 事件; } 返回true; } }); 返回rootView; } 公共布尔onTouchEvent(MotionEvent touchevent) { 开关(touchevent.getAction()) { case MotionEvent.ACTION\u DOWN: { x1=touchevent.getX(); y1=touchevent.getY(); 打破 } case MotionEvent.ACTION\u UP: { x2=touchevent.getX(); y2=touchevent.getY(); if(y1y2) { Toast.makeText(getActivity(),“执行了从下到上的交换”,Toast.LENGTH_SHORT.show(); if((cala.getVisibility())==View.VISIBLE){ cala.setVisibility(View.GONE);} } 打破 } } 返回false; } 这对我很有用: --onCreate()

这对我很有用: --onCreate()


你找到解决办法了吗?你找到解决办法了吗?