recycleview實(shí)現(xiàn)拼多多首頁(yè)水平滑動(dòng)效果
本文實(shí)例為大家分享了recycleview實(shí)現(xiàn)拼多多首頁(yè)水平滑動(dòng)效果的具體代碼,供大家參考,具體內(nèi)容如下
1.說(shuō)明 本例子模仿拼多多首頁(yè)的水平菜單,原本計(jì)劃用viewpager實(shí)現(xiàn),但是太麻煩,不合適,嘗試用recycleview實(shí)現(xiàn),親測(cè)可運(yùn)行,自定義支持各種樣式效果,高度擴(kuò)展
2.效果圖:

3.下載地址
4.首頁(yè) 貼一下核心代碼 需要源碼的請(qǐng)自行下載
/*
* 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; //設(shè)置行數(shù)
private int mColumns = 4; //設(shè)置列數(shù)
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 = {"多多果園","九塊九特賣(mài)","多多愛(ài)消除","天天領(lǐng)現(xiàn)金"
,"行家?guī)湍氵x","限時(shí)秒殺","斷碼清倉(cāng)","跟著好評(píng)買(mǎi)"
,"充值中心","醫(yī)藥館","簽到","多多賺大錢(qián)"
,"砍價(jià)免費(fèi)拿","多多精靈","省錢(qián)月卡","現(xiàn)金大轉(zhuǎn)盤(pán)"
};
@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);
// 系統(tǒng)帶的 RecyclerView,無(wú)需自定義
mRecyclerView = (RecyclerView) findViewById(R.id.recycler_view);
// 水平分頁(yè)布局管理器
mLayoutManager.setPageListener(this); // 設(shè)置頁(yè)面變化監(jiān)聽(tīng)器
mRecyclerView.setLayoutManager(mLayoutManager);
// 如果需要查看調(diào)試日志可以設(shè)置為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", "總頁(yè)數(shù) = " + pageSize);
}
@Override
public void onPageSelect(int pageIndex, int pageSize) {
mCurrent = pageIndex;
Log.e("TAG", "選中頁(yè)碼 = " + pageIndex + "\t" + pageSize);
//計(jì)算滾動(dòng)條寬度
float proportion = (float) ((pageIndex + 1) / pageSize);
float transMaxRange = lineParent.getWidth() - lineChild.getWidth();
//設(shè)置滾動(dòng)條移動(dòng)
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);
}*/
}
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
基于TransactionTooLargeException異常分析
下面小編就為大家分享一篇基于TransactionTooLargeException異常分析,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2017-11-11
Android實(shí)現(xiàn)底部切換標(biāo)簽
這篇文章主要為大家詳細(xì)介紹了Android實(shí)現(xiàn)底部切換標(biāo)簽,嵌套Fragment,方便自定義布局,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-07-07
老生常談ProgressBar、ProgessDialog的用法
下面小編就為大家?guī)?lái)一篇老生常談ProgressBar、ProgessDialog的用法。小編覺(jué)得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2017-07-07
Android用SharedPreferences實(shí)現(xiàn)登錄注冊(cè)注銷(xiāo)功能
這篇文章主要為大家詳細(xì)介紹了Android用SharedPreferences實(shí)現(xiàn)登錄注冊(cè)注銷(xiāo)功能,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-04-04
android-使用環(huán)信SDK開(kāi)發(fā)即時(shí)通信功能(附源碼下載)
本篇文章主要介紹了android-使用環(huán)信SDK開(kāi)發(fā)即時(shí)通信功能,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。2016-12-12
Android和PHP MYSQL交互開(kāi)發(fā)實(shí)例
這篇文章主要介紹了Android和PHP MYSQL交互開(kāi)發(fā)實(shí)例,對(duì)此感興趣的同學(xué),可以試一下2021-04-04
Android開(kāi)發(fā)手機(jī)無(wú)線調(diào)試的方法
今天小編就為大家分享一篇關(guān)于Android開(kāi)發(fā)手機(jī)無(wú)線調(diào)試的方法,小編覺(jué)得內(nèi)容挺不錯(cuò)的,現(xiàn)在分享給大家,具有很好的參考價(jià)值,需要的朋友一起跟隨小編來(lái)看看吧2018-12-12
Android通過(guò)PHP服務(wù)器實(shí)現(xiàn)登錄功能
這篇文章主要為大家詳細(xì)介紹了Android通過(guò)PHP服務(wù)器實(shí)現(xiàn)登錄功能的相關(guān)資料,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-01-01

