2014-11-05 57 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中。我猜他们的滑动手势的相似性使他们不能一起工作。