我使用ViewPager
和Fragments
创建了一个Android应用。在这里,我使用FragmentTransaction
使用按钮动作在彼此之间滑动片段。我也使用一种方法在片段之间转换。它在MainActivity类中。
这是MainActivity类,
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.app.FragmentTransaction;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.MotionEvent;
import android.view.View;
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);
}
}
}
这是我的目的的示例代码,
import android.app.Activity;
import android.content.pm.ActivityInfo;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.TextView;
/**
* Created by user on 5/11/2016.
*/
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) {
FragmentManager fragmentManager=getActivity().getSupportFragmentManager();
FragmentTransaction transaction = fragmentManager.beginTransaction();
transaction.setCustomAnimations(R.anim.from_right, R.anim.to_left);
FirstFragment1 firstFragment1=FirstFragment1.newInstance("Fragment Main");
transaction.replace(R.id.firstFragment, firstFragment1, "first_fragment");
transaction.commit();
( (MainActivity)getActivity()).setCurrentItem(0);
}
}
});
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);
}
}
}
这些是我的动画文件,
from_left.xml
<?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>
from_right.xml
<?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>
to_left.xml
<?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>
to_right.xml
<?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>
问题: - 使用此动画文件在片段之间切换时无效。
当我点击 SecondFragment1 中的按钮时,第0个片段应从右向左加载到ViewPager
。但是,这个过程是不同的。首先,第0个片段从左到右加载到ViewPager
,然后再从右到左加载。
我的目的是在开始时从右到左方向将{0}片段加载到ViewPager
。
我怎样才能以正确的方式看到这个?有什么想法吗?
深深感谢。
答案 0 :(得分:1)
根据我所理解的问题,您只想在按下按钮时替换片段?首先,我将删除视图寻呼机并将其替换为FrameLayout。
在类顶部声明framelayout:
FrameLayout fl_frag_container = (FrameLayout) findViewById(R.id.fl_frag_container);
然后在MainActivity中创建一个方法,您可以从片段调用以更改正在显示的片段:
在我的情况下,我使用:
public void setSecondaryFrag(int page, String name, String url, String id) {
transaction = fragMan.beginTransaction();
switch (page) {
case Constants.VIEW_EVENTS:
transaction.replace(fl_frag_container.getId(), new ViewEventsFragment());
break;
case Constants.VIEW_ARTIST_PROFILE:
transaction.replace(fl_frag_container.getId(), BrandArtistProfileFragment.newInstance(Constants.ARTISTS, name, url, id));
break;
case Constants.VIEW_BRAND_PROFILE:
transaction.replace(fl_frag_container.getId(), BrandArtistProfileFragment.newInstance(Constants.BRANDS,name, url, id) );
break;
case Constants.VIEW_TICKETS:
transaction.replace(fl_frag_container.getId(), new ViewTicketFragment());
break;
}
transaction.addToBackStack(null);
transaction.setCustomAnimations(R.anim.from_right, R.anim.to_left);
transaction.commitAllowingStateLoss();
}
然后在片段中我会打电话
((MainActivity) getActivity).setSecondaryFrag(page, "", "", "");
可以根据您的需要进行修改
使用框架布局替换片段不需要为viewpager使用适配器
希望这有帮助
答案 1 :(得分:1)
如果您需要使用viewpager,请查看此SO帖子以覆盖viewpager动画
Android ViewPager With different Animation like zoom in,fade etc