{"id":354,"date":"2023-02-25T07:41:04","date_gmt":"2023-02-24T23:41:04","guid":{"rendered":"https:\/\/www.appblog.cn\/?p=354"},"modified":"2023-04-30T14:45:28","modified_gmt":"2023-04-30T06:45:28","slug":"recyclerview-operate-data-and-add-segmentation-line","status":"publish","type":"post","link":"https:\/\/www.appblog.cn\/index.php\/2023\/02\/25\/recyclerview-operate-data-and-add-segmentation-line\/","title":{"rendered":"RecyclerView\u64cd\u4f5c\u6570\u636e\u53ca\u6dfb\u52a0\u5206\u5272\u7ebf"},"content":{"rendered":"<h2>\u64cd\u4f5c\u6570\u636e<\/h2>\n<p>\u5b98\u65b9RecyclerView.Adapter\u4e2d\u63d0\u4f9b\u57fa\u672c\u6dfb\u52a0\u3001\u5220\u9664\u548c\u4fee\u6539\u6570\u636e\u7684\u65b9\u6cd5\u5982\u4e0b\uff1a<\/p>\n<p><!-- more --><\/p>\n<pre><code class=\"language-java\">\/\/\u8be5\u65b9\u6cd5\u7528\u4e8e\u5f53\u589e\u52a0\u4e00\u4e2a\u6570\u636e\u7684\u65f6\u5019\uff0cposition\u8868\u793a\u65b0\u589e\u6570\u636e\u663e\u793a\u7684\u4f4d\u7f6e\nfinal void notifyItemInserted(int position)\n\n\/\/\u8be5\u65b9\u6cd5\u7528\u4e8e\u5220\u9664\u4e00\u4e2a\u6570\u636e\u7684\u65f6\u5019\uff0cposition\u8868\u793a\u6570\u636e\u5220\u9664\u7684\u4f4d\u7f6e\nfinal void notifyItemRemoved(int position)\n\n\/\/\u8be5\u65b9\u6cd5\u8868\u793a\u6240\u5728position\u5bf9\u5e94\u7684item\u4f4d\u7f6e\u4e0d\u4f1a\u6539\u53d8\uff0c\u4f46\u662f\u8be5item\u5185\u5bb9\u53d1\u751f\u53d8\u5316\nfinal void notifyItemChanged(int position)\n\n\/\/\u8be5\u65b9\u6cd5\u4e00\u822c\u7528\u4e8e\uff1a\u9002\u914d\u5668\u4e4b\u524d\u88c5\u8f7d\u7684\u6570\u636e\u5927\u90e8\u5206\u5df2\u7ecf\u8fc7\u65f6\uff0c\u9700\u8981\u91cd\u65b0\u66f4\u65b0\u6570\u636e\n\/\/\u8c03\u7528\u8be5\u65b9\u6cd5\u7684\u65f6\u5019\uff0cRecyclerView\u4f1a\u91cd\u65b0\u8ba1\u7b97\u5b50Item\u53ca\u6240\u6709\u5b50Item\u91cd\u65b0\u5e03\u5c40\n\/\/\u51fa\u4e8e\u6548\u7387\u8003\u8651\uff0c\u5b98\u65b9\u5efa\u8bae\u7528\u66f4\u52a0\u7cbe\u786e\u7684\u65b9\u6cd5\u6765\u53d6\u4ee3\u8be5\u65b9\u6cd5\nfinal void notifyDataSetChanged()<\/code><\/pre>\n<p>\u9996\u5148\u5bf9MyAdapter.java\u4fee\u6539\uff0c\u65b0\u589e\u65b9\u6cd5\uff1a<\/p>\n<pre><code class=\"language-java\">\/\/\u79fb\u9664\u6570\u636e\npublic void removeData(int position) {\n    mDataSet.remove(position);\n    notifyItemRemoved(position);\n}\n\/\/\u65b0\u589e\u6570\u636e\npublic void addData(int position) {\n    mDataSet.add(position, &quot;Add One&quot;);\n    notifyItemInserted(position);\n}\n\/\/\u66f4\u6539\u67d0\u4e2a\u4f4d\u7f6e\u7684\u6570\u636e\npublic void changeData(int position) {\n    mDataSet.set(position, &quot;Item has changed &quot; + count++);\n    notifyItemChanged(position);\n}<\/code><\/pre>\n<p>\u6bcf\u4e00\u4e2aItem\u53d8\u5316\u7684\u65f6\u5019\u90fd\u4e0d\u662f\u77ac\u95f4\u7684\uff0c\u90fd\u4f1a\u6709\u4e00\u4e2a\u52a8\u753b\u6548\u679c\uff0c\u4f7f\u5f97\u7528\u6237\u4f53\u9a8c\u5f88\u597d\uff0c\u5176\u5b9e\u8fd9\u91cc\u9762\u4f7f\u7528\u4e86RecyclerView\u9ed8\u8ba4\u63d0\u4f9b\u7684\u52a8\u753b\u6548\u679c\uff1a<\/p>\n<pre><code class=\"language-java\">\/\/\u975e\u5fc5\u8981\uff0cRecyclerView\u4f1a\u9ed8\u8ba4\u4f7f\u7528\nmRecyclerView.setItemAnimator(new DefaultItemAnimator());<\/code><\/pre>\n<p>RecyclerView.ItemAnimator\u62bd\u8c61\u7c7b\u7528\u4e8e\u63a7\u5236Item\u7684\u52a8\u753b\u6548\u679c\uff0c\u800cDefaultItemAnimator()\u6b63\u662f\u5176\u9ed8\u8ba4\u5b9e\u73b0\u7c7b\u3002\u6211\u4eec\u53ef\u4ee5\u81ea\u5b9a\u4e49\u5b9e\u73b0\u5f88\u591a\u7279\u6548\u52a8\u753b\uff0c\u81f3\u4e8e\u5b83\u7684\u5404\u79cd\u52a8\u753b\u6548\u679c\uff0c\u53ef\u4ee5\u53c2\u8003GitHub\u4e0a\u7684\u5f00\u6e90\u9879\u76eeRecyclerViewItemAnimators\uff1a<a target=\"_blank\" rel=\"noopener\" href=\"https:\/\/github.com\/gabrielemariotti\/RecyclerViewItemAnimators\">https:\/\/github.com\/gabrielemariotti\/RecyclerViewItemAnimators<\/a>\u3002<\/p>\n<h2>\u6dfb\u52a0\u5206\u5272\u7ebf<\/h2>\n<p>RecycleView\u6ca1\u6709\u50cfListView\u53ef\u4ee5\u76f4\u63a5\u5728xml\u4e2d\u6dfb\u52a0android:divider\u5c5e\u6027\uff0c\u53ef\u80fd\u662f\u8003\u8651\u5230RecycleView\u7075\u6d3b\u591a\u53d8\u7684\u7279\u70b9\uff0c\u4e0d\u8fc7\u53ef\u4ee5\u624b\u52a8\u6dfb\u52a0\u3002RecyclerView.ItemDecoration\u62bd\u8c61\u7c7b\u5373\u7528\u4e8e\u6dfb\u52a0\u5206\u5272\u7ebf\uff1a<\/p>\n<pre><code class=\"language-java\">public static abstract class ItemDecoration {\n\n    public void onDraw(Canvas c, RecyclerView parent, State state) {\n        onDraw(c, parent);\n    }\n\n    public void onDrawOver(Canvas c, RecyclerView parent, State state) {\n        onDrawOver(c, parent);\n    }\n\n    public void getItemOffsets(Rect outRect, View view, RecyclerView parent, State state) {\n        getItemOffsets(outRect, ((LayoutParams) view.getLayoutParams()).getViewLayoutPosition(), parent);\n    }\n}<\/code><\/pre>\n<p>\u7701\u7565\u4e86\u4e09\u4e2a\u5b98\u65b9\u4e0d\u63d0\u5021\u7684\u65b9\u6cd5\uff0c\u8fd9\u91cc\u7b80\u5355\u8bf4\u660e\u4e00\u4e0b\u4ee5\u4e0a\u5404\u4e2a\u65b9\u6cd5\u7684\u4f5c\u7528\uff1a<\/p>\n<ul>\n<li>onDraw\u548conDrawOver\uff1a\u8fd9\u4e24\u4e2a\u65b9\u6cd5\u7528\u4e8e\u7ed8\u5236\uff0c\u7ed8\u5236\u5206\u5272\u7ebf\u7684\u903b\u8f91\u53ef\u4ee5\u653e\u5728\u8fd9\u91cc\u9762\uff0c\u5b83\u4eec\u4e8c\u8005\u7684\u5177\u4f53\u533a\u522b\u662f\uff1aonDraw\u662f\u5728Item View\u7ed8\u5236\u4e4b\u524d\u8c03\u7528\uff0c\u800conDrawOver\u662f\u5728Item View\u7ed8\u5236\u4e4b\u540e\u8c03\u7528\uff0c\u56e0\u6b64\u6211\u4eec\u4e00\u822c\u9009\u62e9\u91cd\u5199\u5176\u4e2d\u4e00\u4e2a\u65b9\u6cd5\u5373\u53ef\u3002<\/li>\n<li>getItemOffsets\uff1a\u8fd9\u4e2a\u65b9\u6cd5\u662f\u544a\u8bc9RecyclerView\u5728\u7ed8\u5236\u5b8c\u4e00\u4e2aItem View\u7684\u65f6\u5019\uff0c\u5e94\u8be5\u7559\u4e0b\u591a\u5c11\u7a7a\u4f4d\uff0c\u4ee5\u4fbf\u4e8e\u7ed8\u5236\u5206\u5272\u7ebf\u3002<\/li>\n<\/ul>\n<p>\u6211\u4eec\u6765\u5199\u4e00\u4e2a\u5b9e\u73b0\u7c7b\uff0c\u65b0\u5efaDividerItemDecoration\uff08\u6ce8\uff1a\u8be5\u7c7b\u53c2\u8003\u81eaAndroid\u5b98\u65b9\uff09\uff1a<\/p>\n<pre><code class=\"language-java\">public class DividerItemDecoration extends RecyclerView.ItemDecoration {\n\n    \/\/\u4f7f\u7528\u7cfb\u7edf\u81ea\u5e26\u7684listDivider\n    private static final int[] ATTRS = new int[] {\n        android.R.attr.listDivider\n    };\n\n    public static final int ORIENTATION_HORIZONTAL = LinearLayoutManager.HORIZONTAL;\n    public static final int ORIENTATION_VERTICAL = LinearLayoutManager.VERTICAL;\n\n    private Drawable mDivider;\n    private int mOrientation;\n\n    public DividerItemDecoration(Context context,int orientation) {\n        \/\/\u4f7f\u7528TypeArray\u52a0\u8f7d\u8be5\u7cfb\u7edf\u8d44\u6e90\n        final TypedArray ta = context.obtainStyledAttributes(ATTRS);\n        mDivider = ta.getDrawable(0);\n        \/\/\u7f13\u5b58\n        ta.recycle();\n        setOrientation(orientation);\n    }\n\n    public void setOrientation(int orientation) {\n        if (orientation != ORIENTATION_HORIZONTAL &amp;&amp; orientation != ORIENTATION_VERTICAL) {\n            throw new IllegalArgumentException(&quot;invalid orientation&quot;);\n        }\n        mOrientation = orientation;\n    }\n\n    @Override\n    public void onDraw(Canvas c, RecyclerView parent, RecyclerView.State state) {\n        if (mOrientation == ORIENTATION_VERTICAL) {\n            drawVertical(c, parent);\n        } else {\n            drawHorizontal(c,parent);\n        }\n    }\n\n    public void drawVertical(Canvas c, RecyclerView parent) {\n        \/\/\u83b7\u53d6\u5206\u5272\u7ebf\u7684\u5de6\u8fb9\u8ddd\uff0c\u5373RecyclerView\u7684padding\u503c\n        final int left = parent.getPaddingLeft();\n        \/\/\u5206\u5272\u7ebf\u53f3\u8fb9\u8ddd\n        final int right = parent.getWidth() - parent.getPaddingRight();\n        final int childCount = parent.getChildCount();\n        \/\/\u904d\u5386\u6240\u6709item view\uff0c\u4e3a\u5b83\u4eec\u7684\u4e0b\u65b9\u7ed8\u5236\u5206\u5272\u7ebf\n        for (int i=0; i &lt; childCount; i++) {\n            final View child = parent.getChildAt(i);\n            final RecyclerView.LayoutParams params = (RecyclerView.LayoutParams) child.getLayoutParams();\n            final int top = child.getBottom() + params.bottomMargin;\n            final int bottom = top + mDivider.getIntrinsicHeight();\n            mDivider.setBounds(left, top, right, bottom);\n            mDivider.draw(c);\n        }\n    }\n\n    public void drawHorizontal(Canvas c, RecyclerView parent) {\n        final int top = parent.getPaddingTop();\n        final int bottom = parent.getHeight() - parent.getPaddingBottom();\n\n        final int childCount = parent.getChildCount();\n        for (int i = 0; i &lt; childCount; i++) {\n            final View child = parent.getChildAt(i);\n            final RecyclerView.LayoutParams params = (RecyclerView.LayoutParams) child.getLayoutParams();\n            final int left = child.getRight() + params.rightMargin;\n            final int right = left + mDivider.getIntrinsicHeight();\n            mDivider.setBounds(left, top, right, bottom);\n            mDivider.draw(c);\n        }\n    }\n\n    @Override\n    public void getItemOffsets(Rect outRect, View view, RecyclerView parent, RecyclerView.State state) {\n        if (mOrientation == ORIENTATION_VERTICAL) {\n            \/\/\u8bbe\u7f6e\u504f\u79fb\u7684\u9ad8\u5ea6\u662fmDivider.getIntrinsicHeight\uff0c\u8be5\u9ad8\u5ea6\u6b63\u662f\u5206\u5272\u7ebf\u7684\u9ad8\u5ea6\n            outRect.set(0,0,0,mDivider.getIntrinsicHeight());\n        } else {\n            outRect.set(0,0,mDivider.getIntrinsicWidth(), 0);\n        }\n    }\n}<\/code><\/pre>\n<p>\u63a5\u7740\u5728MainActivity.java\u6dfb\u52a0\u5982\u4e0b\u4ee3\u7801\uff1a<\/p>\n<pre><code class=\"language-java\">mRecyclerView.addItemDecoration(new DividerItemDecoration(this, DividerItemDecoration.ORIENTATION_VERTICAL));<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>\u64cd\u4f5c\u6570\u636e \u5b98\u65b9RecyclerView.Adapter\u4e2d\u63d0\u4f9b\u57fa\u672c\u6dfb\u52a0\u3001\u5220\u9664\u548c\u4fee\u6539\u6570\u636e\u7684\u65b9\u6cd5\u5982\u4e0b\uff1a \/\/\u8be5\u65b9\u6cd5\u7528 [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[119],"tags":[65],"class_list":["post-354","post","type-post","status-publish","format-standard","hentry","category-android-ui","tag-recyclerview"],"_links":{"self":[{"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/posts\/354","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/comments?post=354"}],"version-history":[{"count":0,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/posts\/354\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/media?parent=354"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/categories?post=354"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/tags?post=354"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}