gpt4 book ai didi

java - 如何在android中的viewpager上从右到左获取上一个 fragment

转载 作者:太空狗 更新时间:2023-10-29 13:58:13 26 4
gpt4 key购买 nike

我使用 viewpagerfragment 创建了一个 android 应用程序。我在 fragment 上使用两个按钮前进和后退。当我使用向前 Action 时,下一个 fragment 从右到左。这意味着 fragment 在这个 Action 中从右到左变化。

这是我的工作代码,

MainActivity.java

public class MainActivity extends FragmentActivity {

private ViewPager viewPager;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

viewPager=(ViewPager)findViewById(R.id.viewPager);
viewPager.setAdapter(new MyPagerAdapter2(getSupportFragmentManager()));
viewPager.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
if (viewPager.getCurrentItem() == 0) {
return true;
}

if (viewPager.getCurrentItem() == 1) {
return true;
}

if (viewPager.getCurrentItem() == 2) {
return true;
}

if (viewPager.getCurrentItem() == 3) {
return true;
}

if (viewPager.getCurrentItem() == 4) {
return true;
}

return false;
}
});



}

private class MyPagerAdapter2 extends FragmentPagerAdapter {

public MyPagerAdapter2(FragmentManager fm) {
super(fm);
}

@Override
public Fragment getItem(int pos) {
switch(pos) {

case 0: return FirstFragment1.newInstance("FirstFragment_1");
case 1: return SecondFragment1.newInstance("SecondFragment_1");
case 2: return ThirdFragment1.newInstance("ThirdFragment_1");
case 3: return FourthFragment1.newInstance("FourthFragment_1");
case 4: return FifthFragment1.newInstance("FifthFragment_1");
default: return FirstFragment1.newInstance("DefaultFragment_1");
}
}

@Override
public int getCount() {
return 5;
}
}

public void setCurrentItem(int which) {
if(viewPager != null && which >= 0 && which <= 4) {
viewPager.setCurrentItem(which);
}
}
}

FirstFragment1.java

public class FirstFragment1 extends Fragment {

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.first_frag_1, container, false);

TextView tv = (TextView) v.findViewById(R.id.tvFragFirst);
tv.setText(getArguments().getString("msg"));


Button button1=(Button)v.findViewById(R.id.nextButton);
button1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (getActivity() != null) {
( (MainActivity)getActivity()).setCurrentItem(1); // fourth fragment index is 3
}
}
});

return v;
}

public static FirstFragment1 newInstance(String text) {

FirstFragment1 f = new FirstFragment1();
Bundle b = new Bundle();
b.putString("msg", text);

f.setArguments(b);

return f;
}

@Override
public void setUserVisibleHint(boolean isVisibleToUser) {
super.setUserVisibleHint(isVisibleToUser);
if(isVisibleToUser) {
Activity a = getActivity();
if(a != null) a.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LOCKED);
}
}

}

SecondFragment1.java

public class SecondFragment1 extends Fragment {

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.second_frag_1, container, false);

TextView tv = (TextView) v.findViewById(R.id.tvFragSecond);
tv.setText(getArguments().getString("msg"));


Button button=(Button)v.findViewById(R.id.printButton);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {

if(getActivity() != null) {
( (MainActivity)getActivity()).setCurrentItem(0); // second fragment index is 1


}

}
});

Button button1=(Button)v.findViewById(R.id.nextButton);
button1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (getActivity() != null) {
( (MainActivity)getActivity()).setCurrentItem(2); // fourth fragment index is 3


}
}
});

return v;
}

public static SecondFragment1 newInstance(String text) {

SecondFragment1 f = new SecondFragment1();
Bundle b = new Bundle();
b.putString("msg", text);

f.setArguments(b);

return f;
}

@Override
public void setUserVisibleHint(boolean isVisibleToUser) {
super.setUserVisibleHint(isVisibleToUser);
if(isVisibleToUser) {
Activity a = getActivity();
if(a != null) a.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LOCKED);
}
}

}

当我使用向后 Action 时,前一个 fragment 从左到右。但它应该是从右到左,我不知道该怎么做。

例如:-

当我点击 SecondFragment1 中的 button 时,FirstFragment1 应该在 viewpager 上从右到左出现.但目前它是从左到右。

我怎样才能做到这一点?我应该使用动画吗?

有什么想法吗?谢谢。

最佳答案

我建议您设置自定义过渡:

从右边进入:

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
android:shareInterpolator="false">
<translate
android:fromXDelta="100%" android:toXDelta="0%"
android:fromYDelta="0%" android:toYDelta="0%"
android:duration="500" />
</set>

向左退出:

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
android:shareInterpolator="false">
<translate
android:fromXDelta="0%" android:toXDelta="-100%"
android:fromYDelta="0%" android:toYDelta="0%"
android:duration="500"/>
</set>

从左边进入:

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
android:shareInterpolator="false">
<translate
android:fromXDelta="-100%" android:toXDelta="0%"
android:fromYDelta="0%" android:toYDelta="0%"
android:duration="500"/>
</set>

向右导出:

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
android:shareInterpolator="false">
<translate
android:fromXDelta="0%" android:toXDelta="100%"
android:fromYDelta="0%" android:toYDelta="0%"
android:duration="500" />
</set>

然后你可以这样设置转换:

fragmentTransaction.setCustomAnimations(R.anim.from_right, R.anim.to_left);

希望对你有帮助!

关于java - 如何在android中的viewpager上从右到左获取上一个 fragment ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37983978/

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