Android viewpager片段按钮setOnClickListener无法正常工作

时间:2014-11-05 21:01:21

标签: android android-fragments android-viewpager

我有一个viewpager,其中包含一个包含按钮的片段。这是片段的onCreateView。

@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(清晰可见)时没有调用refreshButton_Click。我猜测片段没有获得焦点或者它不允许子元素具有焦点。

以下是片段的布局:

<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不能正常工作,而是按钮无法获得焦点

1 个答案:

答案 0 :(得分:1)

嗯,我相信我解决了它。我将ViewPager嵌套在DrawerLayout中。我猜他们的滑动手势的相似性使它们不能一起工作。

相关问题