java - 点击 Android 滑动 fragment

标签 java android fragment swipe

我有一个 fragment 寻呼机,我在第一个 fragment 上构建了一个按钮。 现在我想让它在我点击那个按钮时滑动到第二个 fragment 。 到目前为止,我构建了这个:

public class HowTo extends Fragment {

        @TargetApi(Build.VERSION_CODES.JELLY_BEAN)
        @Override
        public View onCreateView(final LayoutInflater inflater, final ViewGroup container,
                                 Bundle savedInstanceState) {

            View android = inflater.inflate(R.layout.how_to, container, false);
            ((TextView) android.findViewById(R.id.howTo)).setText("howto");

                Button next_frag = (Button) android.findViewById(R.id.next_frag);
                next_frag.setOnClickListener(new View.OnClickListener() {
                    @TargetApi(Build.VERSION_CODES.JELLY_BEAN)
                    public void onClick(View v) {
                        // Swipe to page`?
                        View android = inflater.inflate(R.layout.how_to, container, false);

                    }
                });
            return android;

        }


}

它没有用。有谁知道如何解决这个问题。

编辑: activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.view.ViewPager
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/pager"
    android:textStyle="bold"
    android:layout_width="match_parent"
    android:layout_height="match_parent" />

我的按钮的 xml 将改变 fragment :

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical"
    android:background="@drawable/how_to_gradient">
    <Button
        android:id="@+id/next_frag"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:textColor="#fff"
        android:gravity="center"
        android:layout_weight="1"
        android:shadowDx="0"
        android:shadowDy="0"
        android:shadowRadius="5"
        android:textSize="21sp"
        android:textStyle="bold"
        android:text="test"
        android:background="@drawable/border_inner_orange"/>

</LinearLayout>

最佳答案

尝试在按钮点击时使用它:

viewPager.setCurrentItem(nextposition);

或者如果你想要一个平滑的滚动你可以使用

viewPager.setCurrentItem(position,true);

编辑:

您可以通过以下方式获取 View pager 的当前页面:

viewPager.getCurrentItem();

获取数字并加+1以将值放在下一个位置

已编辑:

主要 Activity :

在 MainActivity 内部初始化 MainActivity 的静态实例:

public static MainActivity mInstance = null;

然后在 MainActivity 的 onCreate 方法中这样做:

mInstance = this;

以上是您的 MainActivity 中的两个附加项..

Now Your Fragment

public class HowTo extends Fragment {

        @TargetApi(Build.VERSION_CODES.JELLY_BEAN)
        @Override
        public View onCreateView(final LayoutInflater inflater, final ViewGroup container,
                                 Bundle savedInstanceState) {

            View android = inflater.inflate(R.layout.how_to, container, false);
            ((TextView) android.findViewById(R.id.howTo)).setText("howto");

ViewPager viewPager = (ViewPager) MainActivity.mInstance.findViewById(R.id.pager);

int nextFragment = viewPager.getCurrentItem() + 1;

                Button next_frag = (Button) android.findViewById(R.id.next_frag);
                next_frag.setOnClickListener(new View.OnClickListener() {
                    @TargetApi(Build.VERSION_CODES.JELLY_BEAN)
                    public void onClick(View v) {
                        // Swipe to page`?

viewPager.setCurrentItem(nextFragment);

                    }
                });
            return android;

        }
}

关于java - 点击 Android 滑动 fragment ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32681273/

相关文章:

internet-explorer - Internet Explorer多个jdk版本

java - 读取守护进程的输入流导致程序卡住

Android Phone EditText 未更新设备上的显示

android - 是否有包含Android设备中所有崩溃报告的文件

android - Tab + 滑动 View - 无法从 fragment 中的文本文件加载数据

java - 为什么在传递给 XPathExpression.evaluate 方法时不需要转换 Document 对象?

java - 有什么可以根据语言环境决定默认纸张尺寸的吗?

java - Android:计算哈希值的更快方法

Android:Fragment 布局中的 ListView

java - findviewbyid 错误无法解决