recycleview实现拼多多首页水平滑动效果

 更新时间:2021年05月26日 11:55:41   作者:勘察加熊人  
这篇文章主要为大家详细介绍了recycleview实现拼多多首页水平滑动效,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下

本文实例为大家分享了recycleview实现拼多多首页水平滑动效果的具体代码,供大家参考,具体内容如下

1.说明  本例子模仿拼多多首页的水平菜单,原本计划用viewpager实现,但是太麻烦,不合适,尝试用recycleview实现,亲测可运行,自定义支持各种样式效果,高度扩展

2.效果图:

3.下载地址

4.首页 贴一下核心代码  需要源码的请自行下载

/*
 * Copyright 2017 GcsSloop
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *    http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 * Last modified 2017-09-18 23:47:01
 *
 * GitHub: https://github.com/GcsSloop
 * WeiBo: http://weibo.com/GcsSloop
 * WebSite: http://www.gcssloop.com
 */
 
package com.example.mepositry;
 
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.View;
import android.widget.RelativeLayout;
 
 
import java.util.ArrayList;
import java.util.List;
 
public class TwoActivity extends AppCompatActivity implements PagerGridLayoutManager
        .PageListener {
 
    private int mRows = 2;  //设置行数
    private int mColumns = 4;  //设置列数
    private RecyclerView mRecyclerView;
    private MyAdapter2 mAdapter;
    private PagerGridLayoutManager mLayoutManager;
    private RelativeLayout lineParent;
    private int mTotal = 0;
    private int mCurrent = 0;
    private View lineChild;
    private String[] names = {"多多果园","九块九特卖","多多爱消除","天天领现金"
            ,"行家帮你选","限时秒杀","断码清仓","跟着好评买"
            ,"充值中心","医药馆","签到","多多赚大钱"
            ,"砍价免费拿","多多精灵","省钱月卡","现金大转盘"
    };
 
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_two);
        lineParent = findViewById(R.id.rl_line_parent);
        lineChild = findViewById(R.id.view_line_child);
        mLayoutManager = new PagerGridLayoutManager(mRows, mColumns, PagerGridLayoutManager
                .HORIZONTAL);
        // 系统带的 RecyclerView,无需自定义
        mRecyclerView = (RecyclerView) findViewById(R.id.recycler_view);
        // 水平分页布局管理器
        mLayoutManager.setPageListener(this);    // 设置页面变化监听器
        mRecyclerView.setLayoutManager(mLayoutManager);
        // 如果需要查看调试日志可以设置为true,一般情况忽略即可
        PagerConfig.setShowLog(true);
        initData();
 
    }
 
    private void initData() {
        List<String> list = new ArrayList<>();
        for (int i = 0; i < 16; i++) {
            list.add(names[i]);
        }
        //   mAdapter.refreshDataList(list);
        // 使用原生的 Adapter 即可
        mAdapter = new MyAdapter2(TwoActivity.this, list);
        mRecyclerView.setAdapter(mAdapter);
    }
 
    @Override
    public void onPageSizeChanged(int pageSize) {
        mTotal = pageSize;
        Log.e("TAG", "总页数 = " + pageSize);
    }
 
    @Override
    public void onPageSelect(int pageIndex, int pageSize) {
        mCurrent = pageIndex;
        Log.e("TAG", "选中页码 = " + pageIndex + "\t" + pageSize);
        //计算滚动条宽度
        float proportion = (float) ((pageIndex + 1) / pageSize);
        float transMaxRange = lineParent.getWidth() - lineChild.getWidth();
        //设置滚动条移动
        lineChild.setTranslationX(transMaxRange * proportion);
    }
}

5.适配器

/**
 * Copyright 2017 GcsSloop
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *    http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 * Last modified 2017-09-18 23:47:01
 *
 * GitHub: https://github.com/GcsSloop
 * WeiBo: http://weibo.com/GcsSloop
 * WebSite: http://www.gcssloop.com
 **/
 
package com.example.mepositry;
 
 
import android.annotation.SuppressLint;
import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
 
import java.util.ArrayList;
import java.util.List;
 
public class MyAdapter2 extends RecyclerView.Adapter<BaseRecyclerViewHolder> {
    private List<String> mDataList = new ArrayList<>();
    private Context context;
 
    public MyAdapter2(Context mContext, List<String> list) {
        this.context=mContext;
        this.mDataList = list;
    }
 
 
    @Override
    public BaseRecyclerViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(context).inflate(R.layout.layout_item2, parent, false);
        return new BaseRecyclerViewHolder(view);
    }
 
    @SuppressLint("SetTextI18n")
    @Override
    public void onBindViewHolder(BaseRecyclerViewHolder holder, final int position) {
        TextView name = holder.findBindItemView(R.id.tv_title);
        name.setText("id:"+position+mDataList.get(position));
        //抽象方法
        name.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Log.e("TAG", "holder.itemView:" + position);
            }
        });
    }
 
    @Override
    public int getItemCount() {
        return mDataList.size();
    }
 
/*    @Override
    protected int setupItemLayoutId() {
        return ;
    }
    @Override
    protected void findBindView(int position, BaseRecyclerViewHolder holder) {
        String data = mDataList.get(position);
        TextView name = holder.findBindItemView(R.id.tv_title);
        name.setText(data);
    }*/
}

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持脚本之家。

相关文章

  • 基于TransactionTooLargeException异常分析

    基于TransactionTooLargeException异常分析

    下面小编就为大家分享一篇基于TransactionTooLargeException异常分析,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
    2017-11-11
  • Android实现底部切换标签

    Android实现底部切换标签

    这篇文章主要为大家详细介绍了Android实现底部切换标签,嵌套Fragment,方便自定义布局,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2018-07-07
  • 老生常谈ProgressBar、ProgessDialog的用法

    老生常谈ProgressBar、ProgessDialog的用法

    下面小编就为大家带来一篇老生常谈ProgressBar、ProgessDialog的用法。小编觉得挺不错的,现在就分享给大家,也给大家做个参考。一起跟随小编过来看看吧
    2017-07-07
  • Android用SharedPreferences实现登录注册注销功能

    Android用SharedPreferences实现登录注册注销功能

    这篇文章主要为大家详细介绍了Android用SharedPreferences实现登录注册注销功能,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2022-04-04
  • android耳机左右声道接反具体修正方法

    android耳机左右声道接反具体修正方法

    android 耳机左右声道接反如何修正,具体的修改方法如下,感兴趣的朋友可以参考下哈,希望对大家有所帮助
    2013-06-06
  • android-使用环信SDK开发即时通信功能(附源码下载)

    android-使用环信SDK开发即时通信功能(附源码下载)

    本篇文章主要介绍了android-使用环信SDK开发即时通信功能,小编觉得挺不错的,现在分享给大家,也给大家做个参考。
    2016-12-12
  • Android实现摇一摇功能

    Android实现摇一摇功能

    这篇文章主要为大家详细介绍了Android实现摇一摇功能的相关代码,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2016-11-11
  • Android和PHP MYSQL交互开发实例

    Android和PHP MYSQL交互开发实例

    这篇文章主要介绍了Android和PHP MYSQL交互开发实例,对此感兴趣的同学,可以试一下
    2021-04-04
  • Android开发手机无线调试的方法

    Android开发手机无线调试的方法

    今天小编就为大家分享一篇关于Android开发手机无线调试的方法,小编觉得内容挺不错的,现在分享给大家,具有很好的参考价值,需要的朋友一起跟随小编来看看吧
    2018-12-12
  • Android通过PHP服务器实现登录功能

    Android通过PHP服务器实现登录功能

    这篇文章主要为大家详细介绍了Android通过PHP服务器实现登录功能的相关资料,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2017-01-01

最新评论