2013-04-25 4 views
0

Я создал пользовательский вид, в котором я хочу реализовать некоторые действия onClick. я создал следующее:onClicks в пользовательском представлении не отвечают:

настраиваемое представление XML Layout:

<?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) и только после того, как я установил Listener. причина, по которой я это делаю, заключается в том, что мой пользовательский просмотр равен viability = gone при представлении активности.

Любые советы о том, как это сделать правильно, будут очень благодарны.

Спасибо.

ответ

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

Я думаю, вам нужно поменять две эти две строки. Сначала нужно задать контент, а затем вызвать findViewById.

setContentView(R.layout.reports_list_activity_layout); 
leftSideMenu = (LeftSideMenu) findViewById(R.id.leftSideMenu); 
Смежные вопросы