当自定义视图在android中有touchlisteners时如何停止滚动?

时间:2013-12-26 10:59:56

标签: android ontouchlistener

我正在为相对布局动态添加自定义视图。

在我的自定义视图中,我有一个可以左右拖动的子视图。它只能在相对布局中只有一个视图正常工作。但是当有多个视图时,不会为拖动子视图调用ACTION_UP事件。所以,我想在用户触摸拖动子视图时停止滚动。

Main xml:

    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"

        tools:context=".MainActivity" >


            <com.example.dynamicscroll.ObservableScrollView
            android:id="@+id/mainScrollView"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent" >


        <HorizontalScrollView
    android:layout_width="wrap_content" android:layout_height="fill_parent">   

         <RelativeLayout
                android:id="@+id/relativeLayout1"
                android:layout_width="match_parent"
                android:layout_height="match_parent"

                />

      </HorizontalScrollView>
      </com.example.dynamicscroll.ObservableScrollView>
    </RelativeLayout>

inflatedfeedmodebloops.xml:

 <?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
   android:background="#ababab"

    android:orientation="vertical" >

 <RelativeLayout
       android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="@drawable/transparent"
     >  


  <com.example.dynamicscroll.ConstrainedDragAndDropView
            android:id="@+id/dndView"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"

    >     

        <ImageView 
            android:layout_height="wrap_content"
            android:layout_width="wrap_content"
            android:layout_marginTop="20dp"
            android:background="@drawable/like_small"
            android:id="@+id/dummy"
            android:visibility="gone"

           />  

     <RelativeLayout
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="10dp"

        android:id="@+id/images">

         <!-- left circle  -->
      <RelativeLayout
           android:layout_width="wrap_content"
          android:layout_height="wrap_content"
          android:orientation="vertical"

           android:layout_alignParentLeft="true"
           android:layout_centerVertical="true"


          android:id="@+id/half_left">


      <ImageView 
            android:layout_height="wrap_content"
            android:layout_width="wrap_content"
            android:layout_marginTop="20dp"
            android:background="@drawable/like_small"
            android:id="@+id/like_image"

           />  

      <TextView
            android:layout_height="wrap_content"
            android:layout_width="wrap_content"
            android:layout_marginTop="10dp"
            android:layout_below="@+id/like_image"
            android:text="36"
            android:textSize="25dp"
            android:textColor="#DF013A"
            android:id="@+id/likes_count"

            />

     </RelativeLayout>
      <!-- end of left circle  -->
   <!--   <ImageView 
            android:layout_height="wrap_content"
            android:layout_width="wrap_content"
            android:layout_toLeftOf="@+id/img_1"
            android:background="@drawable/pinkcircle"
            android:id="@+id/full_left"
            android:layout_centerVertical="true"
            android:visibility="gone"

            />
         -->
     <!-- right circle  -->
     <ImageView 
            android:layout_height="wrap_content"
            android:layout_width="wrap_content"
            android:layout_alignParentRight="true"

            android:id="@+id/backbtn"

            android:layout_centerInParent="true"/>

       <!-- end of right circle  -->

       <!-- cartoon image  -->

       <RelativeLayout
        android:layout_width="200dp"
        android:layout_height="200dp"           ----->I want to drag this image,so I want to stop
                                                      scrolling when user touches this view.           

         android:layout_centerHorizontal="true"
        android:background="@drawable/img_1"
        android:layout_marginTop="20dp"
        android:id="@+id/cartoon_image"> 

       <ImageView 
            android:layout_height="wrap_content"
            android:layout_width="wrap_content"
            android:background="@drawable/like_small"
            android:layout_centerInParent="true"
            android:id="@+id/ivheart"
            android:visibility="gone"
           />    

      <RelativeLayout
            android:layout_height="wrap_content"
            android:layout_width="wrap_content"
            android:background="@drawable/play_btn"
            android:orientation="horizontal"
            android:layout_centerInParent="true"
            android:id="@+id/play_btn"
           >


           </RelativeLayout>
      </RelativeLayout> 

       <!-- end of cartoon image  --> 

     </RelativeLayout>

     </com.example.dynamicscroll.ConstrainedDragAndDropView>  



   </RelativeLayout>  

</LinearLayout>

将视图添加到相对布局:

 for(int i=0;i<al_new.size();i++)
      {

                Log.e("inflating the bloops ","....................");
                final View view= getLayoutInflater().inflate(R.layout.inflatedfeedmodebloops, null);

       iv=  (ImageView)view.findViewById(R.id.main_img);
       ivLike=(ImageView)view.findViewById(R.id.like_image);
       txtlikesCount=(TextView)view.findViewById(R.id.likes_count);
     loader.DisplayImage(al_new.get(i).replace(" ", "%20"), iv);
      ConstrainedDragAndDropView dndView = (ConstrainedDragAndDropView)view.findViewById(R.id.dndView);
            dndView.setDragHandle(view.findViewById(R.id.cartoon_image),view.findViewById(R.id.half_left),heart);
            dndView.setAllowVerticalDrag(false,ScrollingFeedmode.this); 
      rl.addView(view);




 if (view.findViewById(R.id.cartoon_image)!= null && myscrollview!= null) {

            view.findViewById(R.id.cartoon_image).setOnTouchListener(new View.OnTouchListener() {
                                @Override
                                public boolean onTouch(View v, MotionEvent event) {
                                    mainScrollView.getParent()
                                            .requestDisallowInterceptTouchEvent(false);
                            return false;
                                }
                        });

}

          }

所以,任何人都可以建议我在用户触摸子视图时如何停止滚动。

1 个答案:

答案 0 :(得分:0)

使用以下代码在触摸子视图时停止父视图滚动

if (childView != null && parentView != null) {

            childView.setOnTouchListener(new View.OnTouchListener() {
                                @Override
                                public boolean onTouch(View v, MotionEvent event) {
                                    findViewById(R.id.parent_view).getParent()
                                            .requestDisallowInterceptTouchEvent(false);
                            return false;
                                }
                        });

}