在自定义视图onClicks都没有响应: [英] onClicks in a custom view are not responding:

查看:142
本文介绍了在自定义视图onClicks都没有响应:的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经创建的,我想实现一些的onClick 操作的自定义视图。
我创建了以下内容:

I have created a custom view in which I want to implement some onClick actions. I created the following:

自定义XML查看布局:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/leftMenu"
android:layout_width="100dp"
android:layout_height="fill_parent"
android:background="@drawable/left_menu_background"
android:orientation="vertical" >

<ImageButton
    android:id="@+id/left_menu_close_button"
    android:layout_width="fill_parent"
    android:layout_height="49dp"
    android:background="@drawable/left_menu_close_button" />

<ImageButton
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:background="@drawable/left_menu_separator" />

<ScrollView
    android:layout_width="fill_parent"
    android:layout_height="fill_parent" >

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

        <LinearLayout
            android:id="@+id/left_menu_data_layout"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:background="@drawable/left_menu_button_transparent"
            android:gravity="center_horizontal"
            android:orientation="vertical"
            android:paddingBottom="10dp"
            android:paddingTop="10dp" >

            <ImageButton
                android:id="@+id/left_menu_data_image"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:background="@drawable/transparent_rectangle"
                android:scaleType="fitXY"
                android:src="@drawable/folder" />

            <TextView
                 android:id="@+id/left_menu_data_text"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginTop="5dp"
                android:text="Data"
                android:textColor="@color/my_white" />
        </LinearLayout>

        <ImageButton
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:background="@drawable/left_menu_separator" />

        <LinearLayout
            android:id="@+id/left_menu_settings_layout"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:background="@color/transparent"
            android:gravity="center_horizontal"
            android:orientation="vertical"
            android:paddingBottom="10dp"
            android:paddingTop="10dp" >


            <ImageButton
                android:id="@+id/left_menu_settings_image"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:background="@drawable/transparent_rectangle"
                android:scaleType="fitXY"
                android:src="@drawable/settings" />

            <TextView
                android:id="@+id/left_menu_settings_text"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginTop="5dp"
                android:text="Settings"
                android:textColor="@color/my_white" >
            </TextView>
        </LinearLayout>

        <ImageButton
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:background="@drawable/left_menu_separator" />
    </LinearLayout>
</ScrollView>

</LinearLayout>

AppViewLinearLayout:

package com.emildesign.sgreportmanager.ui;

import android.content.Context;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.LinearLayout;

public class AppViewLinearLayout extends LinearLayout {

public AppViewLinearLayout(Context context, AttributeSet attrs, int layoutResource)
{
    super(context, attrs);
    if (isInEditMode())
        return;
    View view = LayoutInflater.from(context).inflate(layoutResource, null);
    addView(view);
}
}

LeftSideMenu:

public class LeftSideMenu extends AppViewLinearLayout
{
private LeftSideMenuClickListener mListener;

public LeftSideMenu(Context context, AttributeSet attrs) 
{
    super(context, attrs, R.layout.left_menu);
    findViewById(R.id.left_menu_data_layout).setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {

            if (mListener != null)
                mListener.settingsOnClick(v);
        }
    });

    findViewById(R.id.left_menu_data_image).setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {

            if (mListener != null)
                mListener.settingsOnClick(v);
        }
    });

    findViewById(R.id.left_menu_data_text).setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {

            if (mListener != null)
                mListener.settingsOnClick(v);
        }
    });

    findViewById(R.id.left_menu_settings_layout).setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {

            if (mListener != null)
                mListener.settingsOnClick(v);
        }
    });

    findViewById(R.id.left_menu_settings_image).setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {

            if (mListener != null)
                mListener.settingsOnClick(v);
        }
    });

    findViewById(R.id.left_menu_settings_text).setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {

            if (mListener != null)
                mListener.settingsOnClick(v);
        }
    });

    findViewById(R.id.left_menu_close_button).setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) 
        {
            if (mListener != null)
                mListener.closeMenuOnClick(v);
        }
    });

}

public void setListener(LeftSideMenuClickListener listener) 
{
    mListener = listener;
}

public interface LeftSideMenuClickListener 
{
    void dataOnClick(View v);

    void settingsOnClick(View v);

    void closeMenuOnClick(View v);
}
}

在我的活动我这样做:

public class ReportsTableActivity extends Activity 
{
LeftSideMenu leftSideMenu;
static final String TAG = ReportsTableActivity.class.getSimpleName();
private SGRaportManagerAppObj application;
private List<Report> reportsRepository;

protected void onCreate(Bundle savedInstanceState) 
{
    super.onCreate(savedInstanceState);
    leftSideMenu = (LeftSideMenu) findViewById(R.id.leftSideMenu);
    setContentView(R.layout.reports_list_activity_layout);

    application = (SGRaportManagerAppObj)getApplication();
    reportsRepository = application.reportsRepository.getReportsRepository();
    createReportsList();

    if (leftSideMenu != null)
    {
        leftSideMenu.setListener(new LeftSideMenu.LeftSideMenuClickListener() {

            @Override
            public void settingsOnClick(View v) {
            }

            @Override
            public void dataOnClick(View v) {
            }

            @Override
            public void closeMenuOnClick(View v) 
            {
                Log.d(TAG, "Close left menu button was clicked");
                ToggleButton button = (ToggleButton) findViewById(R.id.showLeftMenuButton);
                button.setChecked(false);
                leftSideMenu.setVisibility(View.GONE);
            }
        });
    }
  //rest of the code...

问题:由于某种原因,closeMenuOnClick不会被调用和日志:关闭左侧菜单按钮被点击了从未presented。

The Problem: for some reason the closeMenuOnClick is never called and the log: "Close left menu button was clicked" is never presented.

我的猜测是发生在问题,因为我做这个检查:如果(!leftSideMenu = NULL),仅在我设置监听器。我这样做的原因是因为我的自定义视图是活力=不见了当活动是presented。

My guess is that the problem happens because I make this check: if (leftSideMenu != null) and only after I set the Listener. the reason I do that is because my custom view is with viability = gone when the activity is presented.

这是如何做到这一点的权利将是非常美联社preciated任何意见。

Any advice on how to do it right will be very appreciated.

感谢。

推荐答案

变化:

leftSideMenu = (LeftSideMenu) findViewById(R.id.leftSideMenu);
setContentView(R.layout.reports_list_activity_layout);

setContentView(R.layout.reports_list_activity_layout);
leftSideMenu = (LeftSideMenu) findViewById(R.id.leftSideMenu);

也不会产生7 OnClickListener,使之一:

also don't create 7 OnClickListener, make one:

OnClickListener mClickListener = new OnClickListener() {
    @Override
    public void onClick(View v) {
        if (mListener == null) {
            return;
        }
        int id = v.getId();
        if (id == R.id.left_menu_data_layout ||
            id == R.id.left_menu_data_image ||
            id == R.id.left_menu_data_text) {
            // skipped three more ids
            mListener.settingsOnClick(v);
        } else {
            mListener.closeMenuOnClick(v);
        }
    }
}

和注册:

// repeat 7 times
findViewById(R.id.xxx).setOnClickListener(mClickListener);

甚至更好的让LeftSideMenu实现OnClickListener和注册:

or even better let LeftSideMenu implements OnClickListener and register:

// repeat 7 times
findViewById(R.id.xxx).setOnClickListener(this);

这篇关于在自定义视图onClicks都没有响应:的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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