2016-02-29 85 views
-1

我有一個子視圖和一個根視圖。我試圖添加onClick監聽器子視圖,但onClickListener永遠不會被解僱充氣視圖的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> 

當按下未決單選按鈕,則childviews將在rootview膨脹。 Childviews應該是可點擊的。

+0

whats mChildView? – Dhina

+0

這是一個錯字錯誤。我改變它爲childView,但它仍然不起作用 –

+0

在notification_fragment.xml中添加一個圍繞textview(如果它不存在)的線性/相對佈局,併爲該佈局設置onClickListener ID – Dhina

回答

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(); 
     } 
    }); 
} 
+0

好的,謝謝,讓我試試看 –

+0

仍然存在同樣的問題 –

+0

我已更新我的問題 –