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

视频效果

 xml代码




    
        

            

    
            
            
            
            
            
            
        
    

代码

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 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> arrayLists = new ArrayList<>();
        ArrayList 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 typeModelBeanArrayList = new ArrayList<>();
                typeModelBeanArrayList.addAll(modelBeans);
                arrayLists.add(typeModelBeanArrayList);
                modelBeans.clear();
            }

        }

        ArrayList views = new ArrayList<>();
        for (int i = 0; i < arrayLists.size(); i++) {
            final ArrayList 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

你可能感兴趣的:(Android,RecyclerView,ViewPager)