2015-09-10 113 views
1

这是我的问题与Android。Android布局焦点问题:布局可点击,但其元素窃取焦点

我正在使用xml文件作为布局模板我多次膨胀以获取可显示和可点击布局的列表。这个模板布局充满了仅仅是可视化的文本浏览和按钮(根本没有与他们计划的交互)。

<ScrollView 
     android:id="@+id/scroller" 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:orientation="vertical" 
     android:fillViewport="true" 
     android:layout_above="@+id/bottomcontent"> 

     <LinearLayout 
      android:id="@+id/scrollcontentcontainer" 
      android:layout_width="fill_parent" 
      android:layout_height="wrap_content" 
      android:orientation="vertical"> 

      //Dynamically added layouts go there 

     </LinearLayout> 
    </ScrollView> 

在视觉上没有问题。但我想可以触摸所有这些布局,以便显示其中包含更多信息的片段。

事情是我必须多次触摸屏幕,或布局的某些特定部分没有元素才能使onclick方法工作。

鉴于布局是填充按钮和textviews,所有设置为可点击和可聚焦的假,有没有办法强制布局成为点击的接收器,而不是它的一个元素?因为它似乎是链接到XML文件的焦点问题...

这里的布局使用的模板进行充气几次:

<RelativeLayout 
xmlns:android="http://schemas.android.com/apk/res/android" 
android:layout_width="match_parent" 
android:layout_height="wrap_content" 
android:layout_margin="10dp" 
android:background="@drawable/cornerbackground" 
android:clickable="true" 
android:focusable="true" 
android:paddingBottom="10dp" 
android:paddingLeft="10dp" 
android:paddingRight="10dp"> 

    //buttons views textviews and any random stuff go there 

</RelativeLayout> 

谢谢!

回答

1

我面临同样的问题。我所做的是,我创建了一个自定义布局,它将采用所有触摸事件并添加到布局中。

public class CustomRelativeLayout extends RelativeLayout { 
    public CustomRelativeLayout(Context context) { 
     super(context); 
    } 

    public CustomRelativeLayout(Context context, AttributeSet attrs) { 
     super(context, attrs); 
    } 

    public CustomRelativeLayout(Context context, AttributeSet attrs, int defStyleAttr) { 
     super(context, attrs, defStyleAttr); 
    } 

    @Override 
    public boolean onInterceptTouchEvent(MotionEvent ev) { 
     return true; 
    } 
} 

您需要在xml中使用此布局。

+0

工作正常!非常感谢 :) – Virthuss