gpt4 book ai didi

java - 点击 Android 滑动 fragment

转载 作者:行者123 更新时间:2023-11-29 20:24:54 26 4
gpt4 key购买 nike

我有一个 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/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com