android短视频开发,两个ViewPager联动效果
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.NestedScrollView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<com.yundi.piano.ceshidemo.WrapContentHeightViewPager
android:id="@+id/body_vp"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<TextView
android:layout_width="match_parent"
android:text="hhh"
android:padding="10dp"
android:layout_height="wrap_content" />
<com.yundi.piano.ceshidemo.WrapContentHeightViewPager
android:id="@+id/header_vp"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</LinearLayout>
</android.support.v4.widget.NestedScrollView>
MainActivity.java
package com.yundi.piano.ceshidemo;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import java.util.ArrayList;
import java.util.List;
public class MainActivity extends BaseActivity {
private WrapContentHeightViewPager mVp1,mVp2;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mVp1=findViewById(R.id.body_vp);
mVp2=findViewById(R.id.header_vp);
List<String> list = new ArrayList<>();
for (int i = 0; i < 3; i++) {
list.add("第" + i + "个View");
}
mVp1.setAdapter(new MyPagerAdapter1(MainActivity.this, list));
List<String> list2 = new ArrayList<>();
for (int i = 0; i < 3; i++) {
list2.add("第" + i + "个Views");
}
mVp2.setAdapter(new MyPagerAdapter1(MainActivity.this, list2));
mVp1.addOnPageChangeListener(new BaseLinkPageChangeListener(mVp1, mVp2) {
@Override
public void onPageSelected(int position) {
super.onPageSelected(position);
// pageScrollToTop();
mVp1.resetHeight(position);//设置viewpager高度
mVp2.resetHeight(position);
}
});
mVp2.addOnPageChangeListener(new BaseLinkPageChangeListener(mVp2, mVp1) {
@Override
public void onPageSelected(int position) {
super.onPageSelected(position);
// tabLayout.onPageSelected(position);
}
@Override
public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
super.onPageScrolled(position, positionOffset, positionOffsetPixels);
// tabLayout.onPageScrolled(position, positionOffset, positionOffsetPixels);
mVp1.resetHeight(position);
mVp2.resetHeight(position);
}
});
}
}
以上就是android短视频开发,两个ViewPager联动效果, 更多内容欢迎关注之后的文章