如何在Child Fragment中使用OnClickListener?

时间:2014-12-06 11:37:37

标签: android android-fragments

我无法在OnClickListener中使用Fragment

public class Note_Fragment extends Fragment{
    ImageView btn_back;

    private OnClickListener mclick = new OnClickListener() {

        @Override
        public void onClick(View v) {
            Log.i("test", "YEH");
        }
    };

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

    }

    public Note_Fragment()
    {
        super();
    }
    @Override
    public void onAttach(Activity activity)
    {
        super.onAttach(activity);
    }
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
    {
        View rootView = inflater.inflate(R.layout.note_fragment, container, false);
        btn_back = (ImageView) rootView.findViewById(R.id.btn_back);
        btn_back.setOnClickListener(mclick);

        return rootView;
    }
}

注意:

Fragment Fragment是另一个Fragment的{​​{1}}。(Child Fragment

以上代码不起作用。

我的xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:materialdesign="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

    <LinearLayout
        android:id="@+id/Linear_BTNS"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_marginTop="10dp"
        android:layout_marginBottom="10dp"
        android:layout_centerHorizontal="true" >

        <com.gc.materialdesign.views.LayoutRipple
            android:id="@+id/btn_back_root"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginLeft="10dp"
            android:layout_marginRight="10dp"
            android:background="#1E88E5"
            android:clickable="true" >

            <ImageView
                android:id="@+id/btn_back"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:background="#00ffffff"
                android:src="@drawable/ic_action_remove" />
        </com.gc.materialdesign.views.LayoutRipple>

        <com.gc.materialdesign.views.LayoutRipple
            android:id="@+id/btn_save_root"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:background="#1E88E5"
            android:clickable="true" >

            <ImageView
                android:id="@+id/btn_save"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:background="#00ffffff"
                android:src="@drawable/ic_action_save" />
        </com.gc.materialdesign.views.LayoutRipple>
    </LinearLayout>

    <EditText
        android:id="@+id/txtNote"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_below="@+id/Linear_BTNS"
        android:layout_centerHorizontal="true"
        android:layout_marginLeft="20dp"
        android:layout_marginRight="20dp"
        android:gravity="top|right"
        android:inputType="textMultiLine"
        android:background="@drawable/shape_border_note" >
    </EditText>

</RelativeLayout>

1 个答案:

答案 0 :(得分:1)

嗯。好 。您使用的是:

com.gc.materialdesign.views.LayoutRipple

在你的xml中,所以你应该这样做:

public class Note_Fragment extends Fragment{
    com.gc.materialdesign.views.LayoutRipple btn_back_root;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

    }

    public Note_Fragment()
    {
        super();
    }
    @Override
    public void onAttach(Activity activity)
    {
        super.onAttach(activity);
    }
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
    {
        View rootView = inflater.inflate(R.layout.note_fragment, container, false);
        btn_back_root = (com.gc.materialdesign.views.LayoutRipple) rootView.findViewById(R.id.btn_back_root);
        btn_back_root.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                Log.i("test", "YEH");

            }
        });

        return rootView;
    }
}