android短影片開發,兩個ViewPager聯動效果

zhibo系統開發發表於2023-09-22

android短影片開發,兩個ViewPager聯動效果

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.NestedScrollView xmlns:android="
    xmlns:app="
    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聯動效果, 更多內容歡迎關注之後的文章 


來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/69978258/viewspace-2985321/,如需轉載,請註明出處,否則將追究法律責任。

相關文章