您好,登錄后才能下訂單哦!
這篇文章主要介紹了Android如何實(shí)現(xiàn)京東首頁(yè)效果,具有一定借鑒價(jià)值,感興趣的朋友可以參考下,希望大家閱讀完這篇文章之后大有收獲,下面讓小編帶著大家一起了解一下。
本文實(shí)例為大家分享了Android實(shí)現(xiàn)京東首頁(yè)效果的具體代碼,供大家參考,具體內(nèi)容如下
源碼鏈接
<?xml version="1.0" encoding="utf-8"?> <com.scwang.smart.refresh.layout.SmartRefreshLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" android:id="@+id/refresh_view" android:layout_width="match_parent" android:layout_height="match_parent" android:background="@color/background_color" app:srlDisableContentWhenRefresh="true" app:srlEnableOverScrollBounce="false" app:srlHeaderMaxDragRate="2" app:srlPrimaryColor="@color/theme_color"> <RelativeLayout android:layout_width="match_parent" android:layout_height="match_parent"> <androidx.coordinatorlayout.widget.CoordinatorLayout android:layout_width="match_parent" android:layout_height="match_parent"> <com.google.android.material.appbar.AppBarLayout android:id="@+id/app_bar" android:layout_width="match_parent" android:layout_height="wrap_content" app:elevation="0dp" app:layout_behavior="com.yuruiyin.appbarlayoutbehavior.AppBarLayoutBehavior"> <com.google.android.material.appbar.CollapsingToolbarLayout android:layout_width="match_parent" android:layout_height="wrap_content" app:layout_scrollFlags="scroll|exitUntilCollapsed"> <RelativeLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:background="@color/white_color"> <ImageView android:layout_width="match_parent" android:layout_height="180dp" android:scaleType="fitXY" android:visibility="gone" android:src="@drawable/ic_launcher_background" /> <RelativeLayout android:id="@+id/ll_header" android:layout_width="match_parent" android:layout_height="wrap_content" /> </RelativeLayout> <!-- 給SlidingTabLayout占位的--> <androidx.appcompat.widget.Toolbar android:id="@+id/toolbar" android:layout_width="match_parent" android:layout_height="40dp" /> </com.google.android.material.appbar.CollapsingToolbarLayout> <com.flyco.tablayout.SlidingTabLayout android:id="@+id/tab_layout" android:layout_width="match_parent" android:layout_height="50dp" android:background="@color/background_color" app:tl_indicator_height="0dp" app:tl_textBold="SELECT" app:tl_textSelectColor="#f000" app:tl_textUnselectColor="@color/txt_color" app:tl_textsize="16sp" /> </com.google.android.material.appbar.AppBarLayout> <androidx.viewpager.widget.ViewPager android:id="@+id/view_pager" android:layout_width="match_parent" android:layout_height="match_parent" app:layout_behavior="@string/appbar_scrolling_view_behavior" /> </androidx.coordinatorlayout.widget.CoordinatorLayout> <RelativeLayout android:id="@+id/ll_fotter" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginBottom="50dp" android:layout_alignParentBottom="true"> </RelativeLayout> <RelativeLayout android:id="@+id/rl_float_search" android:layout_width="match_parent" android:layout_height="40dp" android:background="@color/theme_color"> <include layout="@layout/top_view_layout" /> </RelativeLayout> </RelativeLayout> </com.scwang.smart.refresh.layout.SmartRefreshLayout>
package com.example.doublesucktopdemo; import android.os.Bundle; import android.util.Log; import android.view.View; import android.widget.ImageView; import android.widget.LinearLayout; import android.widget.RelativeLayout; import android.widget.Toast; import androidx.annotation.NonNull; import androidx.annotation.Nullable; import androidx.cardview.widget.CardView; import androidx.coordinatorlayout.widget.CoordinatorLayout; import androidx.fragment.app.Fragment; import androidx.fragment.app.FragmentActivity; import androidx.recyclerview.widget.GridLayoutManager; import androidx.recyclerview.widget.LinearLayoutManager; import androidx.recyclerview.widget.RecyclerView; import androidx.viewpager.widget.ViewPager; import com.chad.library.adapter.base.BaseQuickAdapter; import com.example.doublesucktopdemo.adapter.HomeViewPagerAdapter; import com.example.doublesucktopdemo.adapter.MyBannerAdapter; import com.example.doublesucktopdemo.adapter.RecommendAdapter; import com.example.doublesucktopdemo.grid.MyAdapter2; import com.example.doublesucktopdemo.grid.PagerConfig; import com.example.doublesucktopdemo.grid.PagerGridLayoutManager; import com.example.doublesucktopdemo.widget.RecyclerViewDivider; import com.example.doublesucktopdemo.widget.RefreshHeaderView.TodayNewsHeader; import com.flyco.tablayout.SlidingTabLayout; import com.flyco.tablayout.listener.OnTabSelectListener; import com.google.android.material.appbar.AppBarLayout; import com.leaf.library.StatusBarUtil; import com.scwang.smart.refresh.layout.SmartRefreshLayout; import com.scwang.smart.refresh.layout.api.RefreshLayout; import com.scwang.smart.refresh.layout.listener.OnRefreshListener; import com.youth.banner.Banner; import com.youth.banner.config.IndicatorConfig; import com.youth.banner.indicator.CircleIndicator; import java.util.ArrayList; import java.util.List; /** * Created by weioule * on 2019/6/26. */ public class MainActivity extends FragmentActivity implements PagerGridLayoutManager .PageListener { private SmartRefreshLayout refreshView; private AppBarLayout mAppBarLayout; private RelativeLayout mFloatSearchRl; ///非常重要 private RelativeLayout mHeaderLl; private RelativeLayout mFotterLl; CardView mCardView; private SlidingTabLayout mTabView; private ViewPager mViewPager; private int totalScrollRange, oldVerticalOffset = -1; private HomeViewPagerAdapter fragmentAdapter; private String[] titles = {"首頁(yè)", "手機(jī)", "食品", "生鮮", "休閑零食", "家居廚具", "美妝"}; private ArrayList<ProductBean> list = new ArrayList<>(); private List<Fragment> fragmentList; private int mRows = 1; //設(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 ImageView iv_icon_bottom; private RelativeLayout rl_cancel_bottom; 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(@Nullable Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); initView(); addListener(); } private void initView() { // StatusBarUtil.setColor(this, getColor(R.color.theme_color)); refreshView = findViewById(R.id.refresh_view); mFloatSearchRl = findViewById(R.id.rl_float_search); mAppBarLayout = findViewById(R.id.app_bar); mTabView = findViewById(R.id.tab_layout); mViewPager = findViewById(R.id.view_pager); mHeaderLl = findViewById(R.id.ll_header); mFotterLl = findViewById(R.id.ll_fotter); refreshView.setRefreshHeader(new TodayNewsHeader(this)); addProductBeans(); getData(); } private void addListener() { mAppBarLayout.addOnOffsetChangedListener(offsetChangedListener); refreshView.setOnRefreshListener(new OnRefreshListener() { @Override public void onRefresh(@NonNull RefreshLayout refreshLayout) { loadData(); } }); } private void loadData() { refreshView.postDelayed(new Runnable() { @Override public void run() { refreshView.finishRefresh(); refreshView.setEnabled(true); } }, 800); } // 這里的處理是當(dāng)AppBarLayout處于最頂部也就是完全打開(kāi)狀態(tài)verticalOffse=0時(shí)才允許刷新控件可用. private AppBarLayout.OnOffsetChangedListener offsetChangedListener = new AppBarLayout.OnOffsetChangedListener() { @Override public void onOffsetChanged(AppBarLayout appBarLayout, int verticalOffse) { totalScrollRange = appBarLayout.getTotalScrollRange(); if (oldVerticalOffset == verticalOffse) return; if (verticalOffse == 0) { refreshView.setEnabled(true); } else { refreshView.setEnabled(false); } if (verticalOffse <= -Utils.dp2px(40)) { mFloatSearchRl.setVisibility(View.VISIBLE); } else { mFloatSearchRl.setVisibility(View.GONE); } if (Math.abs(verticalOffse) >= totalScrollRange && totalScrollRange != 0) { mTabView.setBackgroundColor(getResources().getColor(R.color.white_color)); } else { mTabView.setBackgroundColor(getResources().getColor(R.color.background_color)); } oldVerticalOffset = verticalOffse; } }; public void getData() { mHeaderLl.removeAllViews(); mFotterLl.removeAllViews(); addTopView(); addBannerView(); addBannerTab(); addBottom(); // addRecommendView(); // addFeaturedView(); 圖片 initViewpager(); } private void addBannerTab() { View mTopView = getLayoutInflater().inflate(R.layout.activity_two, mHeaderLl, false); lineParent = mTopView.findViewById(R.id.rl_line_parent); lineChild = mTopView.findViewById(R.id.view_line_child); mLayoutManager = new PagerGridLayoutManager(mRows, mColumns, PagerGridLayoutManager .HORIZONTAL); // 系統(tǒng)帶的 RecyclerView,無(wú)需自定義 mRecyclerView =mTopView.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(); mHeaderLl.addView(mTopView); } 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(MainActivity.this, list); mRecyclerView.setAdapter(mAdapter); } private void addTopView() { View mTopView = getLayoutInflater().inflate(R.layout.top_view_layout, mHeaderLl, false); mHeaderLl.addView(mTopView); } private void addBottom() { View mTopView = getLayoutInflater().inflate(R.layout.bottom_layout, mFotterLl, false); iv_icon_bottom = mTopView.findViewById(R.id.iv_icon_bottom); rl_cancel_bottom= mTopView.findViewById(R.id.rl_cancel_bottom); iv_icon_bottom.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { Log.e("TAG","click iv_icon_bottom "); } }); rl_cancel_bottom.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { Log.e("TAG","click cancel"); } }); mFotterLl.addView(mTopView); } private void addBannerView() { View mBannerView = getLayoutInflater().inflate(R.layout.banner_layout, mHeaderLl, false); Banner mBanner = mBannerView.findViewById(R.id.banner); ArrayList<ProductBean> list = new ArrayList<>(); for (int i = 0; i < 3; i++) { list.add(new ProductBean("三會(huì)一課","2","會(huì)議",R.mipmap.ic_launcher)); } mBanner.addBannerLifecycleObserver(this) .setAdapter(new MyBannerAdapter(list)) .setIndicator(new CircleIndicator(this)) .setIndicatorGravity(IndicatorConfig.Direction.RIGHT) .start(); mHeaderLl.addView(mBannerView); } private void addRecommendView() { RecyclerView mRecyclerView = new RecyclerView(this); GridLayoutManager gridLayoutManager = new GridLayoutManager(this, 6); mRecyclerView.setLayoutManager(gridLayoutManager); RecommendAdapter adapter = new RecommendAdapter(list); adapter.setOnItemChildClickListener(new BaseQuickAdapter.OnItemChildClickListener() { @Override public void onItemChildClick(BaseQuickAdapter adapter, View view, int position) { ProductBean productBean = (ProductBean) adapter.getItem(position); Toast.makeText(MainActivity.this, productBean.getName(), Toast.LENGTH_SHORT).show(); } }); mRecyclerView.setAdapter(adapter); RecyclerViewDivider divider = new RecyclerViewDivider.Builder(this) .setStyle(RecyclerViewDivider.Style.BOTH) .setColor(0x00000000) .setOrientation(RecyclerViewDivider.GRIDE_VIW) .setSize(5) .build(); mRecyclerView.addItemDecoration(divider); mHeaderLl.addView(mRecyclerView); } private void addFeaturedView() { View view = getLayoutInflater().inflate(R.layout.featured_layout, mHeaderLl, false); mHeaderLl.addView(view); } private void initViewpager() { if (fragmentList == null) { fragmentList = new ArrayList<>(); } else { fragmentList.clear(); } for (int i = 0; i < titles.length; i++) { fragmentList.add(new HomeFragment(list)); } fragmentAdapter = new HomeViewPagerAdapter(getSupportFragmentManager(), fragmentList); mViewPager.setAdapter(fragmentAdapter); mTabView.setOnTabSelectListener(onTabSelectListener); mViewPager.addOnPageChangeListener(onPageChangeListener); mTabView.setViewPager(mViewPager, titles); } private OnTabSelectListener onTabSelectListener = new OnTabSelectListener() { @Override public void onTabSelect(int position) { mTabView.setCurrentTab(position); } @Override public void onTabReselect(int position) { } }; private ViewPager.OnPageChangeListener onPageChangeListener = new ViewPager.OnPageChangeListener() { @Override public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) { } @Override public void onPageSelected(int position) { mTabView.setCurrentTab(position); } @Override public void onPageScrollStateChanged(int state) { } }; /** * 控制appbar的滑動(dòng) * * @param isScroll true 允許滑動(dòng) false 禁止滑動(dòng) */ private void banAppBarScroll(boolean isScroll) { View mAppBarChildAt = mAppBarLayout.getChildAt(0); AppBarLayout.LayoutParams mAppBarParams = (AppBarLayout.LayoutParams) mAppBarChildAt.getLayoutParams(); if (isScroll && mAppBarParams.getScrollFlags() == 0) { mAppBarParams.setScrollFlags(AppBarLayout.LayoutParams.SCROLL_FLAG_SCROLL | AppBarLayout.LayoutParams.SCROLL_FLAG_EXIT_UNTIL_COLLAPSED); mAppBarChildAt.setLayoutParams(mAppBarParams); } else if (mAppBarParams.getScrollFlags() != 0) mAppBarParams.setScrollFlags(0); } /** * 控制appbar的開(kāi)關(guān) * 需要手動(dòng)開(kāi)關(guān)時(shí)調(diào)用 * * @param open true 打開(kāi) false 關(guān)閉 */ public void switchAppbarLayout(boolean open) { CoordinatorLayout.Behavior behavior = ((CoordinatorLayout.LayoutParams) mAppBarLayout.getLayoutParams()).getBehavior(); if (behavior instanceof AppBarLayout.Behavior) { AppBarLayout.Behavior appBarLayoutBehavior = (AppBarLayout.Behavior) behavior; int topAndBottomOffset = appBarLayoutBehavior.getTopAndBottomOffset(); if (open && topAndBottomOffset != 0) { //打開(kāi) appBarLayoutBehavior.setTopAndBottomOffset(0); } else if (!open) { //關(guān)閉 if (totalScrollRange == 0) totalScrollRange = mAppBarLayout.getHeight(); appBarLayoutBehavior.setTopAndBottomOffset(-totalScrollRange); } } } private void addProductBeans() { for (int i = 0; i < 10; i++) { list.add(new ProductBean("智慧美食","2","美食",R.mipmap.ic_launcher)); } } @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); } }
感謝你能夠認(rèn)真閱讀完這篇文章,希望小編分享的“Android如何實(shí)現(xiàn)京東首頁(yè)效果”這篇文章對(duì)大家有幫助,同時(shí)也希望大家多多支持億速云,關(guān)注億速云行業(yè)資訊頻道,更多相關(guān)知識(shí)等著你來(lái)學(xué)習(xí)!
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀(guān)點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。