0

我创建了一个自定义视图,我想在其中实现一些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>

应用视图线性布局:

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

左侧菜单:

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在呈现活动时使用的。

任何有关如何正确操作的建议将不胜感激。

谢谢。

4

2 回答 2

1

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

setContentView(R.layout.reports_list_activity_layout);
leftSideMenu = (LeftSideMenu) findViewById(R.id.leftSideMenu);
于 2013-04-25T14:52:26.880 回答
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);
于 2013-04-25T15:31:44.463 回答