仿淘宝继续上拉进入商品详情页的效果,双Fragment实现;
动画效果:
slide_above_in.xml
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
<translate
android:fromYDelta="-100%p"
android:toYDelta="0%p"
android:duration="300"/>
<alpha
android:fromAlpha="0.5"
android:toAlpha="1.0"
android:duration="300"/>
</set>
slide_above_out.xml
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
<translate
android:toYDelta="-100%p"
android:fromXDelta="0%p"
android:duration="300"/>
<alpha
android:fromAlpha="1.0"
android:toAlpha="0.5"
android:duration="300"/>
</set>
slide_below_in.xml
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
<translate
android:fromYDelta="100%p"
android:toYDelta="0%p"
android:duration="300"/>
<alpha
android:fromAlpha="0.5"
android:toAlpha="1.0"
android:duration="300"/>
</set>
slide_below_out.xml
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
<translate
android:toYDelta="100%p"
android:fromYDelta="0%p"
android:duration="300"/>
<alpha
android:toAlpha="0.5"
android:fromAlpha="1.0"
android:duration="300"/>
</set>
代码:
public class MainActivity extends AppCompatActivity implements View.OnClickListener {
private FrameLayout fl;
private AboveFragment AF;
private BelowFragment BF;
private Button btn;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_double_fragment);
fl = (FrameLayout) findViewById(R.id.fl);
btn = (Button) findViewById(R.id.qiehuan);
btn.setOnClickListener(this);
AF = new AboveFragment();
BF = new BelowFragment();
initList(); //先给Fragment设置监听
getSupportFragmentManager().beginTransaction().add(R.id.fl,AF).commit();
}
private void initList() {
AF.setContinueSlideScrollView(new ContinueSlideScrollView.onContinueSlide() {
@Override
public void onContinueSlideTop() {
}
@Override
public void onContinueSlideBottom() {
if (BF.isAdded()){
getSupportFragmentManager().beginTransaction().setCustomAnimations(R.anim.slide_below_in,R.anim.slide_below_out).show(BF).commit();
}else{
getSupportFragmentManager().beginTransaction()
.setCustomAnimations(R.anim.slide_below_in,R.anim.slide_below_out)
.add(R.id.fl,BF).commit();
}
getSupportFragmentManager().beginTransaction()
.setCustomAnimations(R.anim.slide_above_in,R.anim.slide_above_out)
.hide(AF).commit();
}
});
BF.setContinueSlideScrollView(new ContinueSlideScrollView.onContinueSlide() {
@Override
public void onContinueSlideTop() {
if (AF.isAdded()){
getSupportFragmentManager().beginTransaction()
.setCustomAnimations(R.anim.slide_above_in,R.anim.slide_above_out)
.show(AF).commit();
}else{
getSupportFragmentManager().beginTransaction()
.setCustomAnimations(R.anim.slide_above_in,R.anim.slide_above_out)
.add(R.id.fl,AF).commit();
}
getSupportFragmentManager().beginTransaction()
.setCustomAnimations(R.anim.slide_below_in,R.anim.slide_below_out)
.hide(BF).commit();
}
@Override
public void onContinueSlideBottom() {
}
});
}
@Override
public void onClick(View view) {
if ("0".equals(btn.getTag().toString())){
btn.setTag("1");
if (BF.isAdded()){
getSupportFragmentManager().beginTransaction().setCustomAnimations(R.anim.slide_below_in,R.anim.slide_below_out).show(BF).commit();
}else{
getSupportFragmentManager().beginTransaction()
.setCustomAnimations(R.anim.slide_below_in,R.anim.slide_below_out)
.add(R.id.fl,BF).commit();
}
getSupportFragmentManager().beginTransaction()
.setCustomAnimations(R.anim.slide_above_in,R.anim.slide_above_out)
.hide(AF).commit();
}else{
btn.setTag("0");
if (AF.isAdded()){
getSupportFragmentManager().beginTransaction()
.setCustomAnimations(R.anim.slide_above_in,R.anim.slide_above_out)
.show(AF).commit();
}else{
getSupportFragmentManager().beginTransaction()
.setCustomAnimations(R.anim.slide_above_in,R.anim.slide_above_out)
.add(R.id.fl,AF).commit();
}
getSupportFragmentManager().beginTransaction()
.setCustomAnimations(R.anim.slide_below_in,R.anim.slide_below_out)
.hide(BF).commit();
}
}
}
其中 initList() 函数是我定义的ScrollView滑动到底部再继续滑动的监听,https://mp.csdn.net/postedit/85257597
项目这个Demo已上传Github:https://github.com/CuiChenbo/DoubleFragmentSwitch