当前位置: 动力学知识库 > 问答 > 编程问答 >

Android RelativeLayout and OnClickListener

问题描述:

I found many many threads and answers on how to make a Relative layout respond to click event. I've implemented my layout according to them. But still OnClickListener is not called. I have also given selector for it and it works well.

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>

and this is where I inflate the view:

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");

}

});

}

Edit 1:

This Relative layout is added to a linear layout which is inside a scrollview, so that it works like a list view. Does this has something to do???

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" />

网友答案:

Once i had a RelativeLayout not firing onClick event and it was because i had a Button in the middle of it, and the Button's onClick event was being fired, instead of the RelativeLayout's one. Solved it implementing a listener for both of them:

OnClickListener listener4 = new OnClickListener() {
    public void onClick(View v) {
    //Handle onClick 
    }
};
mLayoutBtn4.setOnClickListener(listener4); // The RelativeLayout
mButton4.setOnClickListener(listener4); // The Button inside the layout

Hope it helps!

网友答案:

Add OnClickListener in your RelativeLayout java Class

分享给朋友:
您可能感兴趣的文章:
随机阅读: