膨胀视图的onClickListener未被触发

时间:2016-02-29 06:47:19

标签: android onclicklistener

我有一个子视图和一个根视图。我试图将 onClick 监听器添加到子视图,但 onClickListener永远不会被解雇

@Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
    rootView = inflater.inflate(R.layout.notification, container, false);

    linear = (LinearLayout) rootView.findViewById(R.id.linear);

    for (int i = 0; i <= 10; i++) {

        childView = inflater
                .inflate(R.layout.notification_fragment, linear);

        TextView txt = (TextView) childView.findViewById(R.id.textView1);
        txt.setText("welcome");
        txt.setId(i);
    }

childView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
            Toast.makeText(getActivity().getApplicationContext(),""+mChildView.getId(),Toast.LENGTH_SHORT).show();
        }
     });
   return rootView;
}

notification_fragment.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:clickable="true"
    android:focusable="true"
    android:onClick="onClick"
    android:orientation="vertical">

    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:textAppearance="?android:attr/textAppearanceMedium"
        android:text="PI 12343"
        android:id="@+id/textView1"
        android:layout_marginTop="20dp"
        android:layout_marginLeft="20dp"
        android:layout_marginBottom="8dp"/>
</LinearLayout>

notification.xml

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

    <com.stack.widgets.SegmentedButton
        android:id="@+id/segment_text"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_centerInParent="true"
        android:layout_gravity="center"
        android:layout_margin="5dip"
        android:checkedButton="@+id/button_one"
        android:background="#3dd2c6"
        android:orientation="horizontal" >

        <RadioButton
            android:id="@id/button_one"
            android:button="@null"
            android:gravity="center"
            android:minHeight="33dip"
            android:minWidth="40dip"
            android:text="Pending"
            android:textAppearance="?android:attr/textAppearanceSmall"
            android:textColor="#01A9DB" />

        <RadioButton
            android:id="@+id/button_two"
            android:button="@null"
            android:gravity="center"
            android:minHeight="33dip"
            android:minWidth="40dip"
            android:text="Completed"
            android:textAppearance="?android:attr/textAppearanceSmall"
            android:textColor="#01A9DB" />
    </com.stack.widgets.SegmentedButton>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical"
        android:clickable="true"
        android:id="@+id/linear"></LinearLayout>

</LinearLayout>

按下待处理的单选按钮时,子视图将在rootview中膨胀。儿童视图应该是可点击的。

1 个答案:

答案 0 :(得分:1)

将你的监听器设置为for循环,目前它只为你的上一个视图设置监听器。

for (int i = 0; i <= 10; i++) {

    childView = inflater
            .inflate(R.layout.notification_fragment, linear);

    TextView txt = (TextView) childView.findViewById(R.id.textView1);
    txt.setText("welcome");
    txt.setId(i);

    childView.setOnClickListener(new View.OnClickListener() {
      @Override
      public void onClick(View v) {
         Toast.makeText(getActivity().getApplicationContext(),""+mChildView.getId(),Toast.LENGTH_SHORT).show();
      }
    });
}