2017-10-20 28 views
0

我的活动中有几个按钮没有被点击第一次尝试,但他们从第二次开始点击。似乎该按钮的重点是第一次点击,并从第二次起实际上被按下。这些按钮第一次没有被点击的原因是什么? 我注意到,这种行为只显示在我的活动最底部的按钮。按钮没有被点击第一次尝试

activity_main.xml中

<?xml version="1.0" encoding="utf-8"?> 

<android.support.design.widget.CoordinatorLayout 
xmlns:android="http://schemas.android.com/apk/res/android" 
xmlns:app="http://schemas.android.com/apk/res-auto" 
xmlns:tools="http://schemas.android.com/tools" 
android:layout_width="match_parent" 
android:layout_height="match_parent" 
tools:context="com.me.proj.view.activities.MallActivity"> 

<android.support.design.widget.AppBarLayout 
    android:id="@+id/appbar_layout" 
    android:layout_width="match_parent" 
    android:layout_height="200dp" 
    android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"> 

    <android.support.design.widget.CollapsingToolbarLayout 
     android:id="@+id/collapsing_toolbar" 
     android:layout_width="match_parent" 
     app:title=" " 
     android:layout_height="match_parent" 
     app:contentScrim="?attr/colorPrimary" 
     app:layout_scrollFlags="scroll|exitUntilCollapsed"> 

     <android.support.v4.view.ViewPager 
      android:id="@+id/viewpager_mall_showcase_images" 
      android:layout_width="match_parent" 
      android:layout_height="match_parent" 
      /> 


     <android.support.v7.widget.Toolbar 
      android:id="@+id/toolbar" 
      android:layout_width="match_parent" 
      android:layout_height="?attr/actionBarSize" 
      app:layout_collapseMode="pin" /> 

    </android.support.design.widget.CollapsingToolbarLayout> 

</android.support.design.widget.AppBarLayout> 

<android.support.v4.widget.NestedScrollView 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:background="#ffe5e5e5" 
    app:layout_behavior="@string/appbar_scrolling_view_behavior"> 

    <LinearLayout 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:orientation="vertical" 
     > 

     <include layout="@layout/info_card" /> 
     <include layout="@layout/photo_card" /> 
     <include layout="@layout/address_card" /> 
     <include layout="@layout/opening_hours_card" /> 
     <include layout="@layout/review_card" /> <!-- Button in this file has the problem--> 


    </LinearLayout> 


</android.support.v4.widget.NestedScrollView> 

</android.support.design.widget.CoordinatorLayout> 

review_card.xml

<?xml version="1.0" encoding="utf-8"?> 
<android.support.v7.widget.CardView 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:card_view="http://schemas.android.com/apk/res-auto" 
    card_view:cardCornerRadius="4dp" 
    card_view:cardElevation="0.5dp" 
    card_view:cardMaxElevation="1dp" 
    card_view:cardPreventCornerOverlap="false" 
    card_view:cardUseCompatPadding="true" 
    android:layout_width="match_parent" 
    android:layout_marginTop="7dp" 
    android:background="@color/colorWhite" 
    android:layout_height="match_parent"> 

    <RelativeLayout 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:background="@color/colorWhite"> 

     <TextView 
      android:id="@+id/txt_rating" 
      android:background="@drawable/blue_button_background" 
      android:text="3.8" 
      android:textColor="@color/colorWhite" 
      android:layout_marginTop="10dp" 
      android:textSize="24sp" 
      android:gravity="center" 
      android:layout_marginStart="10dp" 
      android:layout_width="60dp" 
      android:layout_height="40dp" /> 
     <TextView 
      android:id="@+id/txt_reviews_title" 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:textColor="@color/primary_text" 
      android:textSize="16sp" 
      android:layout_toEndOf="@+id/txt_rating" 
      android:layout_marginStart="20dp" 
      android:textStyle="bold" 
      android:paddingTop="1dp" 
      android:layout_alignTop="@+id/txt_rating" 
      android:text="Based on 98 reviews"/> 

     <TextView 
      android:id="@+id/txt_read_all_reviews" 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:text="Read All Reviews" 
      android:layout_alignStart="@id/txt_reviews_title" 
      android:layout_toEndOf="@id/txt_rating" 
      android:layout_alignBottom="@id/txt_rating" 
      android:gravity="bottom" 
      android:paddingBottom="1dp" 
      android:textColor="@color/red" 
      android:layout_below="@id/txt_reviews_title"/> 

     <View 
      android:id="@+id/view_dummy1" 
      android:layout_width="match_parent" 
      android:layout_height="0.2dp" 
      android:layout_below="@id/txt_read_all_reviews" 
      android:layout_marginTop="10dp" 
      android:background="@color/divider"/> 

     <LinearLayout 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:orientation="horizontal" 
      android:layout_marginStart="16dp" 
      android:layout_below="@+id/view_dummy1"> 

      <LinearLayout 
       android:layout_width="0dp" 
       android:layout_height="wrap_content" 
       android:layout_marginTop="10dp" 
       android:layout_weight="0.6" 
       android:layout_marginBottom="10dp" 

       android:orientation="vertical"> 
       <TextView 
        android:id="@+id/txt_how_did_your_visit_go" 
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content" 
        android:text="How did your visit go?" 
        android:textStyle="bold" 
        android:textSize="14sp" 
        android:textColor="@color/primary_text" /> 
       <TextView 
        android:id="@+id/txt_tell_everyone_about_it" 
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content" 
        android:text="Tell everyone about it!" 
        android:layout_marginTop="5dp" 
        android:textColor="@color/secondary_text" 
        android:textSize="12sp" 
        /> 
      </LinearLayout> 


<!--This got some problem--> 
      <Button         
       android:id="@+id/btn_add_review" 
       android:layout_width="0dp" 
       android:layout_height="40dp" 
       android:text="Add Your Review" 
       android:layout_marginEnd="16dp" 
       android:layout_marginTop="8dp" 
       android:textAllCaps="false" 
       android:textColor="@color/colorWhite" 
       android:background="@drawable/blue_button_background" 
       android:layout_weight="0.5"/> 
     </LinearLayout> 


    </RelativeLayout> 

</android.support.v7.widget.CardView> 

MainActivity.java

protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 


     findViewById(R.id.btn_add_review).setOnClickListener(new View.OnClickListener() { 
      @Override 
      public void onClick(View view) { 
       Toast.makeText(MainActivity.this, "clicked", Toast.LENGTH_SHORT).show(); 
      } 
     }); 
    } 
+0

你说过“我已经注意到,这种行为只是在我的活动最底层的按钮中显示。”如果您将** btn_add_review **按钮放在布局** review_card **的顶部,是否也有相同的问题? –

+0

不,当我将review_card放在最上方时,按钮会被点击,但是放置在最下方的卡片的按钮现在已经开始出现问题,而以前不在那里。 –

+0

无论卡在底部是否失去焦点,因此需要点击两次。只是无法摆脱为什么它首先失去了重点。 –

回答

0

这个问题实际上是一个bugNestedScrollView类有一段时间了。 我解决了这个问题,我做了一些调整至NestedScrollView类的源代码,如下所示: -

public class NestedScrollView extends FrameLayout implements NestedScrollingParent, 
    NestedScrollingChild, ScrollingView { 
      ... 
     @Override 
     public boolean onTouchEvent(MotionEvent ev) { 
      switch (actionMasked) { 
      case MotionEvent.ACTION_DOWN: { 
       if (getChildCount() == 0) { 
        return false; 
       } 
       //add this condition 
       if (!inChild((int) ev.getX(), (int) ev.getY())) { 
        return false; 
       } 
       if ((mIsBeingDragged = !mScroller.isFinished())) { 
         final ViewParent parent = getParent(); 
        if (parent != null) { 
         parent.requestDisallowInterceptTouchEvent(true); 
        } 
       } 
       ... 
      } 
     } 

    @Override 
    public boolean onInterceptTouchEvent(MotionEvent ev) { 
     ... 
     switch (action & MotionEventCompat.ACTION_MASK) { 
      ... 
      case MotionEvent.ACTION_DOWN: { 
       ... 
       // replace this line: 
       // mIsBeingDragged = !mScroller.isFinished(); 
       mIsBeingDragged = false; 
       ... 
      } 
     } 
    } 
} 

这样它应该工作正常后。

0

代码在我MainActivity.class:

public class MainActivity extends AppCompatActivity { 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 
     View view = findViewById(R.id.sub_layout); 
     Button button = (Button) view.findViewById(R.id.button); 
     button.setOnClickListener(new View.OnClickListener() { 
      @Override 
      public void onClick(View v) { 
       //Do your stuff 
       Toast.makeText(MainActivity.this, "hi", Toast.LENGTH_SHORT).show(); 
      } 
     }); 
    } 
} 

代码在我activity_main.xml中

<?xml version="1.0" encoding="utf-8"?> 
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:app="http://schemas.android.com/apk/res-auto" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    tools:context="eflair.myapplication.MainActivity"> 

    <include 
     android:id="@+id/sub_layout" 
     layout="@layout/sub_layout" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" /> 

</android.support.constraint.ConstraintLayout> 

代码在我sub_layout.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:orientation="vertical"> 

    <Button 
     android:id="@+id/button" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:text="@string/app_name" /> 
</LinearLayout> 
+0

我没有名为review_card的任何id,我应该为review_card id分配什么? –

+0

为** include布局创建一个ID ** – Karthik

+0

我这样做了,但它不起作用。 –

相关问题