2015-09-10 112 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