2014-11-05 59 views
0

我有一個viewpager與包含一個按鈕的片段。這是片段的onCreateView。Android的viewpager片段按鈕setOnClickListener不工作

@Override 
public View onCreateView(LayoutInflater inflater, ViewGroup container, 
         Bundle savedInstanceState) { 
    // Inflate the layout for this fragment 
    View v = inflater.inflate(R.layout.fragment, container, false); 
    refreshButton = (Button) v.findViewById(R.id.refreshButton); 

    refreshButton.setOnClickListener(new View.OnClickListener() { 
     @Override 
     public void onClick(View view) { 
      refreshButton_Click(); 
     } 
    }); 

    return v; 
} 

我可以驗證refreshButton_Click沒有被調用,當我點擊refreshButton(清晰可見)。我猜測這個片段沒有得到關注,或者它不允許子元素具有焦點。

下面是片段的佈局:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
xmlns:tools="http://schemas.android.com/tools" 
android:layout_width="match_parent" 
android:layout_height="match_parent" 
tools:context="com.me.philip.firehydrant.FeedFragment" 
android:orientation="vertical"> 

<LinearLayout 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:background="#ffededed"> 

    <TextView 
     android:id="@+id/headerTextView" 
     android:layout_width="0dp" 
     android:layout_weight="1" 
     android:layout_height="wrap_content" 
     android:gravity="center" 
     android:text="header" 
     android:textAppearance="?android:attr/textAppearanceLarge" /> 

    <Button 
     android:layout_margin="2dp" 
     android:text="refresh" 
     android:id="@+id/refreshButton" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:focusable="true"/> 
</LinearLayout> 

<ExpandableListView 
    android:id="@+id/postList" 
    android:layout_width="match_parent" 
    android:layout_height="0dp" 
    android:layout_weight="1"></ExpandableListView> 

</LinearLayout> 

編輯:我忘了提:按鈕不會做平常藍色燈光了被單擊時按鈕做的,所以我很確定它不是setOnClickListener不工作,而是該按鈕無法獲得焦點

+0

你怎麼知道它沒有被調用? – Blackbelt 2014-11-05 21:04:06

+0

我設置了一個斷點,並沒有命中。此外,該方法中的代碼沒有做任何事情。 – phil 2014-11-05 21:06:37

+0

你可以發佈佈局嗎? – Blackbelt 2014-11-05 21:09:05

回答

1

嗯,我相信我解決了它。我將ViewPager嵌套在DrawerLayout中。我猜他們的滑動手勢的相似性使他們不能一起工作。