Android RelativeLayout和OnClickListener [英] Android RelativeLayout and OnClickListener

查看:96
本文介绍了Android RelativeLayout和OnClickListener的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我发现了许多线程和关于如何使相对布局响应单击事件的答案.我已经根据他们实现了自己的布局.但是仍然没有调用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:

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

            }
        });
    }

此相对布局被添加到滚动视图内部的线性布局,因此其工作方式类似于列表视图.这有什么事要做吗?

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

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

推荐答案

一旦我有一个RelativeLayout没有触发onClick事件,那是因为我在它的中间有一个Button,而的onClick事件被触发,而不是RelativeLayout事件.解决了为它们两个实现监听器的问题:

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 one. Solved it implementing a listener for both of them:

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

希望有帮助!

这篇关于Android RelativeLayout和OnClickListener的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆