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);
    }*/
}

以上就是本文的全部內容,希望對大傢的學習有所幫助,也希望大傢多多支持WalkonNet。

推薦閱讀: