2013-11-26 76 views
1

我发现许多线程和答案如何使相对布局响应点击事件。我已经根据他们实现了我的布局。但仍然没有调用OnClickListener。我也给了选择器,它运作良好。Android RelativeLayout和OnClickListener

my_list_item XML:

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
xmlns:app="http://schemas.android.com/apk/res/air.com.jingit.mobile" 
android:layout_width="match_parent" android:layout_height="@dimen/actionBarHeight" 
android:clickable="true" 
android:background="@drawable/my_list_selector"> 

<com.loopj.android.image.SmartImageView 
    android:layout_width="@dimen/actionBarHeight" 
    android:layout_height="@dimen/actionBarHeight" 
    android:id="@+id/image" 
    android:scaleType="fitCenter" 
    android:layout_marginLeft="20dp" 
    android:clickable="false" 
    android:focusable="false"/> 

<com.uma.mobile.view.CustomTextView 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:text="Loading..." 
    android:id="@+id/userName" 
    app:typeface="fonts/HelveticaNeue" 
    app:customStyle="Regular" 
    android:textSize="20sp" 
    android:layout_centerVertical="true" 
    android:layout_toRightOf="@+id/image" 
    android:paddingTop="5dp" 
    android:paddingBottom="5dp" 
    android:textColor="#000" 
    android:clickable="false" 
    android:focusable="false"/> 

<FrameLayout 
    android:layout_width="match_parent" 
    android:layout_height="1dp" 
    android:background="#aaa" 
    android:layout_alignBottom="@+id/image"></FrameLayout> 

而这正是我充气视图:

public MyListItem(final Context context, final Integer retailerId) { 
     super(context); 
     LayoutInflater layoutInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
     View view = layoutInflater.inflate(R.layout.my_list_item, this); 
     image = (SmartImageView) view.findViewById(R.id.image); 
     userName = (CustomTextView) view.findViewById(R.id.userName); 


     setOnClickListener(new OnClickListener() { 
      @Override 
      public void onClick(View v) { 

       Log.i("INSIDE LIST","CLICK"); 

      } 
     }); 
    } 

编辑1:

此相对布局被添加到滚动视图内的线性布局,以便它像列表视图一样工作。这有什么事吗?

list_view.xml

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

<ScrollView 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:id="@+id/scrollView" > 

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

<ImageView 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:id="@+id/imageView" 
    android:layout_centerHorizontal="true" 
    android:src="@drawable/up_indicator" 
    android:layout_alignBottom="@+id/scrollView" /> 

+0

use'android添加OnClickListener:点击= “真”',而不是'机器人:可点击= “假”'进行布局可点击 –

+0

使用view.setOnClickListener – Subbu

+0

@Homosapiens即使没有工作。 – Uma

回答

2

我曾有过一个RelativeLayout不触发onClick事件,那是因为我在它的中间有一个ButtonButton的onClick事件正在被发射,而不是RelativeLayout之一。解决了它为他们实施一个听众:

OnClickListener listener = new OnClickListener() { 
    public void onClick(View v) { 
     //Handle onClick 
    } 
}; 
relativeLayout.setOnClickListener(listener); 
button.setOnClickListener(listener); 

希望它有帮助!

0

RelativeLayout java Class