Android开发ScrollView嵌套ViewPager,ViewPager嵌套RecyclerView根据RecyclerView动态设置ViewPager高度好看ViewPager指示器gif

视频效果

 xml代码

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:orientation="vertical">

    <ScrollView
        android:layout_width="match_parent"
        android:layout_height="match_parent">
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:orientation="vertical">

            <TextView
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:padding="@dimen/dp_72"
                android:text="nihao"/>

    <com.example.my.zdx.HeightViewPager
        android:id="@+id/myViewPager"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="#fff"></com.example.my.zdx.HeightViewPager>
            <com.example.my.zdx.BannerIndicator
                android:id="@+id/indicator"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:gravity="center"
                android:layout_marginTop="10dp"
                app:slider_height="10dp"
                app:slider_width="24dp"
                app:sleider_align="Center"
                app:layout_constraintLeft_toLeftOf="parent"/>
            <TextView
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:padding="@dimen/dp_72"
                android:text="nihao"/>
            <TextView
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:padding="@dimen/dp_72"
                android:text="nihao"/>
            <TextView
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:padding="@dimen/dp_72"
                android:text="nihao"/>
            <TextView
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:padding="@dimen/dp_72"
                android:text="nihao"/>
            <TextView
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:padding="@dimen/dp_72"
                android:text="nihao"/>
        </LinearLayout>
    </ScrollView>

</LinearLayout>

代码

package com.example.my.zdx;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.Toast;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.example.xywy.mysoketdemo.R;

import java.util.ArrayList;

public class MainActivity extends AppCompatActivity {

    private HeightViewPager myViewPager;
    private BannerIndicator indicator;

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

    private void initData() {
        ArrayList<TypeModelBean> typearray = new ArrayList<>();
        for (int i = 0; i < 20; i++) {
            TypeModelBean typeModelBean;
            if (i%2 == 0){
                typeModelBean = new TypeModelBean();
                typeModelBean.setTypeImage(R.mipmap.laoshu);
                typeModelBean.setTypetitle("类型"+i);
            }else{
                typeModelBean = new TypeModelBean();
                typeModelBean.setTypeImage(R.mipmap.dongtu);
                typeModelBean.setTypetitle("类型"+i);
            }
            typearray.add(typeModelBean);
        }
        final ArrayList<ArrayList<TypeModelBean>> arrayLists = new ArrayList<>();
        ArrayList<TypeModelBean> modelBeans = new ArrayList<>();
        for (int i = 0; i < typearray.size(); i++) {
            modelBeans.add(typearray.get(i));
            if (((i+1)%10 == 0) || (i == (typearray.size()-1))){
                ArrayList<TypeModelBean> typeModelBeanArrayList = new ArrayList<>();
                typeModelBeanArrayList.addAll(modelBeans);
                arrayLists.add(typeModelBeanArrayList);
                modelBeans.clear();
            }

        }

        ArrayList<View> views = new ArrayList<>();
        for (int i = 0; i < arrayLists.size(); i++) {
            final ArrayList<TypeModelBean> modelBeanArrayList = arrayLists.get(i);
            View view = View.inflate(MainActivity.this,R.layout.item_home_type_parent,null);
            RecyclerView recycleviewtypeparent = view.findViewById(R.id.recycleviewtypeparent);
            RecyclerViewGridAdapter recyclerViewGridAdapter = new RecyclerViewGridAdapter( modelBeanArrayList);
            GridLayoutManager gridLayoutManager = new GridLayoutManager(MainActivity.this, 5);
            //设置RecycleView显示的方向是水平还是垂直 GridLayout.HORIZONTAL水平  GridLayout.VERTICAL默认垂直
            gridLayoutManager.setOrientation(RecyclerView.VERTICAL );
            gridLayoutManager.setSmoothScrollbarEnabled(true);
            gridLayoutManager.setAutoMeasureEnabled(true);
            recycleviewtypeparent.setHasFixedSize(true);
            recycleviewtypeparent.setNestedScrollingEnabled(false);
            //设置布局管理器, 参数gridLayoutManager对象
            recycleviewtypeparent.setLayoutManager(gridLayoutManager);

            recycleviewtypeparent.setAdapter(recyclerViewGridAdapter);
            recyclerViewGridAdapter.setOnItemChildClickListener(new BaseQuickAdapter.OnItemChildClickListener() {
                @Override
                public void onItemChildClick(BaseQuickAdapter adapter, View view, int position) {
                    switch (view.getId()){
                        case R.id.ll_type:
                            Toast.makeText(MainActivity.this,"点我干嘛~"+modelBeanArrayList.get(position).getTypetitle(),Toast.LENGTH_SHORT).show();
                            break;
                    }
                }
            });
            views.add(view);
        }
        HomeTypeParentAdapter homeTypeParentAdapter = new HomeTypeParentAdapter(MainActivity.this,views);
        myViewPager.setAdapter(homeTypeParentAdapter);
        indicator.setUpWidthViewPager(myViewPager);
    }

    private void initView() {
        myViewPager = findViewById(R.id.myViewPager);
        indicator = findViewById(R.id.indicator);
    }
}

 bean类

package com.example.my.zdx;

public class TypeModelBean {
    String typetitle;
    int typeImage;
    String typeId;
    String typeClickContent;
    String typeImageW;
    String typeImageH;
    String type;
    String productSources;

    public String getProductSources() {
        return productSources;
    }

    public void setProductSources(String productSources) {
        this.productSources = productSources;
    }

    public String getTypetitle() {
        return typetitle;
    }

    public void setTypetitle(String typetitle) {
        this.typetitle = typetitle;
    }

    public int getTypeImage() {
        return typeImage;
    }

    public void setTypeImage(int typeImage) {
        this.typeImage = typeImage;
    }

    public String getTypeId() {
        return typeId;
    }

    public void setTypeId(String typeId) {
        this.typeId = typeId;
    }

    public String getTypeClickContent() {
        return typeClickContent;
    }

    public void setTypeClickContent(String typeClickContent) {
        this.typeClickContent = typeClickContent;
    }

    public String getTypeImageW() {
        return typeImageW;
    }

    public void setTypeImageW(String typeImageW) {
        this.typeImageW = typeImageW;
    }

    public String getTypeImageH() {
        return typeImageH;
    }

    public void setTypeImageH(String typeImageH) {
        this.typeImageH = typeImageH;
    }

    public String getType() {
        return type;
    }

    public void setType(String type) {
        this.type = type;
    }
}

 附上demo连接:https://download.csdn.net/download/congcongguniang/12067535

猜你喜欢

转载自blog.csdn.net/congcongguniang/article/details/103777146