自定义视图中的onClicks没有响应:

时间:2013-04-25 14:28:17

标签: android view onclick android-custom-view

我创建了一个自定义视图,我希望在其中实现一些onClick操作。 我创建了以下内容:

自定义视图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,并且永远不会显示日志:"Close left menu button was clicked"

我的猜测是问题发生了,因为我做了这个检查:if (leftSideMenu != null)并且只有在我设置了监听器之后。我之所以这样做是因为我的自定义视图在显示活动时会显示viability = gone

非常感谢任何有关如何正确行事的建议。

感谢。

2 个答案:

答案 0 :(得分:1)

我认为你需要两个交换这两行。您需要先设置内容视图,然后再调用findViewById。

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

答案 1 :(得分:1)

变化:

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,创建一个:

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并注册:

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