<kbd id="afajh"><form id="afajh"></form></kbd>
<strong id="afajh"><dl id="afajh"></dl></strong>
    <del id="afajh"><form id="afajh"></form></del>
        1. <th id="afajh"><progress id="afajh"></progress></th>
          <b id="afajh"><abbr id="afajh"></abbr></b>
          <th id="afajh"><progress id="afajh"></progress></th>

          Android仿內(nèi)涵段子詳情頁評論功能

          共 5746字,需瀏覽 12分鐘

           ·

          2022-02-20 09:18

          首先看實(shí)現(xiàn)效果圖:



          類似的這種需求在實(shí)際的項目中還是挺多的。說說的詳情頁,頂部顯示內(nèi)容,然后顯示一個熱門評論,最后顯示全部評論。兩個評論列表數(shù)量都是動態(tài)的,并且全部評論還可以下拉刷新。?


          我們來實(shí)現(xiàn)這個效果。

          ?

          1、布局


          最關(guān)鍵的是 NestedScrollView 控件,看這張圖

          NestedScrollView  // 滾動頁 - LinearLayout  // NestedScrollView 只能包含一個 LinearLayout   - LinearLayout // 說說詳情   - LinearLayout // 熱門評論     - RecyclerView   - LinearLayout // 全部評論     - RecyclerView


          2、重寫 LinearLayoutManager


          初始化 RecyclerView 時需要設(shè)置 setLayoutManager(),我們需要重寫它來計算列表的高度。


          代碼如下:

          public?class?WrappingLinearLayoutManager?extends?LinearLayoutManager{
          public WrappingLinearLayoutManager(Context context) { super(context); }
          private int[] mMeasuredDimension = new int[2];
          @Override public boolean canScrollVertically() { return false; }
          @Override public void onMeasure(RecyclerView.Recycler recycler, RecyclerView.State state, int widthSpec, int heightSpec) { final int widthMode = View.MeasureSpec.getMode(widthSpec); final int heightMode = View.MeasureSpec.getMode(heightSpec);
          final int widthSize = View.MeasureSpec.getSize(widthSpec); final int heightSize = View.MeasureSpec.getSize(heightSpec);
          int width = 0; int height = 0; for (int i = 0; i < getItemCount(); i++) { if (getOrientation() == HORIZONTAL) { measureScrapChild(recycler, i, View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED), heightSpec, mMeasuredDimension);
          width = width + mMeasuredDimension[0]; if (i == 0) { height = mMeasuredDimension[1]; } } else { measureScrapChild(recycler, i, widthSpec, View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED), mMeasuredDimension);
          height = height + mMeasuredDimension[1]; if (i == 0) { width = mMeasuredDimension[0]; } } }
          switch (widthMode) { case View.MeasureSpec.EXACTLY: width = widthSize; case View.MeasureSpec.AT_MOST: case View.MeasureSpec.UNSPECIFIED: }
          switch (heightMode) { case View.MeasureSpec.EXACTLY: height = heightSize; case View.MeasureSpec.AT_MOST: case View.MeasureSpec.UNSPECIFIED: }
          setMeasuredDimension(width, height); }
          private void measureScrapChild(RecyclerView.Recycler recycler, int position, int widthSpec, int heightSpec, int[] measuredDimension) {
          View view = recycler.getViewForPosition(position); if (view.getVisibility() == View.GONE) { measuredDimension[0] = 0; measuredDimension[1] = 0; return; } // For adding Item Decor Insets to view super.measureChildWithMargins(view, 0, 0); RecyclerView.LayoutParams p = (RecyclerView.LayoutParams) view.getLayoutParams(); int childWidthSpec = ViewGroup.getChildMeasureSpec( widthSpec, getPaddingLeft() + getPaddingRight() + getDecoratedLeft(view) + getDecoratedRight(view), p.width); int childHeightSpec = ViewGroup.getChildMeasureSpec( heightSpec, getPaddingTop() + getPaddingBottom() + getDecoratedTop(view) + getDecoratedBottom(view), p.height); view.measure(childWidthSpec, childHeightSpec);
          // Get decorated measurements measuredDimension[0] = getDecoratedMeasuredWidth(view) + p.leftMargin + p.rightMargin; measuredDimension[1] = getDecoratedMeasuredHeight(view) + p.bottomMargin + p.topMargin; recycler.recycleView(view); }}


          3、初始化 RecyclerView


          就是按照正常的流程初始化 RecyclerView,只不過在 setLayoutManager() 時,

          使用我們自定的 WrappingLinearLayoutManager。


          至于其中的 DetailCommentAdapter 就是正常的 RecyclerView.Adapter。

          RecyclerView RecyclerHot;RecyclerView RecyclerAll;
          private DetailCommentAdapter mHotCommentAdapter;private DetailCommentAdapter mAllCommentAdapter;
          private List mHotCommentsList = new ArrayList<>();private List mAllCommentsList = new ArrayList<>();
          private void setView() { // 設(shè)置熱門評論列表 WrappingLinearLayoutManager wrappingLinearLayoutManager = new WrappingLinearLayoutManager(mContext); wrappingLinearLayoutManager.setAutoMeasureEnabled(false);// 如果導(dǎo)入的包是 Android Support Library 23.2.0 以上的,需要加這句 RecyclerHot.setLayoutManager(wrappingLinearLayoutManager);
          mHotCommentAdapter = new DetailCommentAdapter(mContext, mHotCommentsList, DetailCommentAdapter.COMMENT_TYPE_HOT); RecyclerHot.setAdapter(mHotCommentAdapter); RecyclerHot.setNestedScrollingEnabled(false);

          // 設(shè)置全部評論列表 WrappingLinearLayoutManager wrappingLinearLayoutManager2 = new WrappingLinearLayoutManager(mContext); wrappingLinearLayoutManager2.setAutoMeasureEnabled(false);// 如果導(dǎo)入的包是 Android Support Library 23.2.0 以上的,需要加這句 RecyclerAll.setLayoutManager(wrappingLinearLayoutManager2);
          mAllCommentAdapter = new DetailCommentAdapter(mContext, mAllCommentsList, DetailCommentAdapter.COMMENT_TYPE_ALL); RecyclerAll.setAdapter(mAllCommentAdapter); RecyclerAll.setNestedScrollingEnabled(true);}


          注意:

          • wrappingLinearLayoutManager.setAutoMeasureEnabled(false);
            如果導(dǎo)入的包是 ?Android Support Library 23.2.0 ?以上的,需要加這句。


          • RecyclerHot.setNestedScrollingEnabled(false);
            在這里setNestedScrollingEnabled(false)禁用滾動為RecyclerView,它不會攔截從NestedScrollView滾動事件。


          • setHasFixedSize(false) (默認(rèn)false)
            確定適配器內(nèi)容中的更改可以更改RecyclerView的大小。


          至此就可以實(shí)現(xiàn)想要達(dá)到的效果。

          源碼地址:

          https://github.com/Wing-Li/DoubleList


          到這里就結(jié)束啦。

          瀏覽 53
          點(diǎn)贊
          評論
          收藏
          分享

          手機(jī)掃一掃分享

          分享
          舉報
          評論
          圖片
          表情
          推薦
          點(diǎn)贊
          評論
          收藏
          分享

          手機(jī)掃一掃分享

          分享
          舉報
          <kbd id="afajh"><form id="afajh"></form></kbd>
          <strong id="afajh"><dl id="afajh"></dl></strong>
            <del id="afajh"><form id="afajh"></form></del>
                1. <th id="afajh"><progress id="afajh"></progress></th>
                  <b id="afajh"><abbr id="afajh"></abbr></b>
                  <th id="afajh"><progress id="afajh"></progress></th>
                  一级黄色AA | 香蕉视频在线观看网站 | 69免费手机视频 | 天天爱天天做天天添少妇 | 欧美三级乱抡 |