ප්‍රතිචක්‍රීකරණ දර්ශනයේ අයිතම අතර බෙදුම්කරුවන් සහ අවකාශයන් එකතු කරන්නේ කෙසේද?


955

බෙදුම්කරු සහ බෙදුම්කරු හයිට් පරාමිතීන් ListViewභාවිතා කරමින් පන්තියේදී මීට පෙර එය කළ හැකිව තිබූ බවට මෙය නිදසුනකි :

<ListView
    android:id="@+id/activity_home_list_view"
    android:layout_width="match_parent" 
    android:layout_height="match_parent"
    android:divider="@android:color/transparent"
    android:dividerHeight="8dp"/>

කෙසේ වෙතත්, RecyclerViewපන්තියේ එවැනි හැකියාවක් මා දකින්නේ නැත .

<android.support.v7.widget.RecyclerView
    android:id="@+id/activity_home_recycler_view"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:scrollbars="vertical"/>

එවැනි අවස්ථාවක, ආන්තිකයන් නිර්වචනය කිරීම සහ / හෝ අභිරුචි බෙදුම්කරු දර්ශනයක් ලැයිස්තු අයිතමයේ පිරිසැලසුමට කෙලින්ම එකතු කිරීම හරිද? නැතහොත් මගේ ඉලක්කය සපුරා ගැනීමට වඩා හොඳ ක්‍රමයක් තිබේද?



YeesClear අයිතම <TextView /> තවත් xml එකතු කර ලැයිස්තුවේ එකම ක්‍රියාකාරකමක් භාවිතා කරන්න.
අමිත්ශර්මා

8
ආධාරක ලිබ් හි පන්තියක් ඇති com.homeretailgroup.argos.android.view.decorators.DividerItemDecorationඅතර එය එසේ භාවිතා කරන්න:mRecyclerView.addItemDecoration(new DividerItemDecoration(activity, LinearLayoutManager.VERTICAL));
fada21

සිරස් ලැයිස්තු සඳහා ඔබේ ලැයිස්තු අයිතමයට පහළ ආන්තිකය එකතු කළ හැකි අතර සමහර විට එය බෙදුම්කරු ලෙස භාවිතා කළ හැකිද?
resw67

සරලම ක්‍රමය නම් ඇඩැප්ටරයේ පේළියේ පළමු අයිතමය වටා ඉහළ / පහළ ආන්තිකයන් එකතු කිරීමයි. android: layout_marginBottom = "4dp". (මව් පිරිසැලසුමට ආන්තිකය එකතු කිරීමෙන් එය කපා හරිනු නොලැබේ.)
pstorli

Answers:


1254

ඔක්තෝබර් 2016 යාවත්කාලීන කිරීම

ඇන්ඩ්‍රොයිඩ් ආධාරක පුස්තකාලයේ 25.0.0 අනුවාදය DividerItemDecorationපන්තිය හඳුන්වා දුන්නේය :

DividerItemDecoration යනු ප්‍රතිචක්‍රීකරණ දසුනකි LinearLayoutManager. එය දෙකටම HORIZONTALසහ VERTICALදිශානතියට සහාය වේ.

භාවිතය:

DividerItemDecoration dividerItemDecoration = new DividerItemDecoration(recyclerView.getContext(),
    layoutManager.getOrientation());
recyclerView.addItemDecoration(dividerItemDecoration);

පෙර පිළිතුර

සමහර පිළිතුරු එක්කෝ එතැන් සිට ඉවත් කර ඇති ක්‍රම භාවිතා කරයි, නැතහොත් සම්පූර්ණ විසඳුමක් ලබා නොදේ, එබැවින් මම කෙටි, යාවත්කාලීන එතුමක් කිරීමට උත්සාහ කළෙමි.


මෙන් නොව ListView, RecyclerViewපන්තියට බෙදුම්කරු හා සම්බන්ධ පරාමිතීන් නොමැත. ඒ වෙනුවට, ඔබ දීර්ඝ කිරීමට අවශ්ය ItemDecoration, එය RecyclerView's අභ්යන්තර පන්ති:

ItemDecorationඅයදුම් ඇඩප්ටරය දත්ත කට්ටලයක් සිට විශේෂිත අයිතමය අදහස් කිරීමට හිලව් විශේෂ චිත්ර හා සැලැස්ම එකතු කිරීමට ඉඩ දෙයි. අයිතම, උද්දීපනයන්, දෘශ්‍ය කණ්ඩායම් මායිම් සහ තවත් දේ අතර බෙදුම්කරුවන් ඇඳීමට මෙය ප්‍රයෝජනවත් වේ.

සියලු ItemDecorations(අයිතමය අදහස් පෙර, ඔවුන් එකතු විය පිළිවෙළ ඇද ගනී onDraw()) සහ onDrawOver ඇති ද්රව්ය ((පසු Canvas, RecyclerView, RecyclerView.State).

Vertical පරතරය ItemDecoration

විස්තාරණය කරන්න ItemDecoration, heightපරාමිතියක් ලෙස ඉඩ ලබාගෙන අභිබවා යන අභිරුචි ඉදිකිරීම්කරු එක් කරන්න getItemOffsets():

public class VerticalSpaceItemDecoration extends RecyclerView.ItemDecoration {

    private final int verticalSpaceHeight;

    public VerticalSpaceItemDecoration(int verticalSpaceHeight) {
        this.verticalSpaceHeight = verticalSpaceHeight;
    }

    @Override
    public void getItemOffsets(Rect outRect, View view, RecyclerView parent,
            RecyclerView.State state) {
        outRect.bottom = verticalSpaceHeight;
    }
}

අවසාන අයිතමයට පහළින් ඉඩක් ඇතුළු කිරීමට ඔබට අවශ්‍ය නැතිනම්, පහත දැක්වෙන කොන්දේසිය එක් කරන්න:

if (parent.getChildAdapterPosition(view) != parent.getAdapter().getItemCount() - 1) {
            outRect.bottom = verticalSpaceHeight;
}

සටහන: ඔබට වෙනස් කළ හැකිය outRect.top, outRect.leftසහ outRect.rightඅපේක්ෂිත බලපෑම සඳහා ගුණාංග.

බෙදුම්කරු ItemDecoration

ක්‍රමය දීර් ItemDecorationand කිරීම සහ අභිබවා යාම onDraw():

public class DividerItemDecoration extends RecyclerView.ItemDecoration {

    private static final int[] ATTRS = new int[]{android.R.attr.listDivider};

    private Drawable divider;

    /**
     * Default divider will be used
     */
    public DividerItemDecoration(Context context) {
        final TypedArray styledAttributes = context.obtainStyledAttributes(ATTRS);
        divider = styledAttributes.getDrawable(0);
        styledAttributes.recycle();
    }

    /**
     * Custom divider will be used
     */
    public DividerItemDecoration(Context context, int resId) {
        divider = ContextCompat.getDrawable(context, resId);
    }

    @Override
    public void onDraw(Canvas c, RecyclerView parent, RecyclerView.State state) {
        int left = parent.getPaddingLeft();
        int right = parent.getWidth() - parent.getPaddingRight();

        int childCount = parent.getChildCount();
        for (int i = 0; i < childCount; i++) {
            View child = parent.getChildAt(i);

            RecyclerView.LayoutParams params = (RecyclerView.LayoutParams) child.getLayoutParams();

            int top = child.getBottom() + params.bottomMargin;
            int bottom = top + divider.getIntrinsicHeight();

            divider.setBounds(left, top, right, bottom);
            divider.draw(c);
        }
    }
}

සුපුරුදු ඇන්ඩ්‍රොයිඩ් බෙදුම්කරු ගුණාංග භාවිතා කරන පළමු ඉදිකිරීම්කරු හෝ ඔබේම ඇඳිය ​​හැකි දෙවැන්න භාවිතා කළ හැකිය, උදාහරණයක් ලෙස drawable / diver.xml

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
       android:shape="rectangle">
    <size android:height="1dp" />
    <solid android:color="#ff992900" />
</shape>

සටහන: ඔබේ අයිතම මත බෙදුම්කරු ඇද ගැනීමට ඔබට අවශ්‍ය නම් , onDrawOver()ඒ වෙනුවට ක්‍රමය ඉක්මවා යන්න.

භාවිතය

ඔබගේ නව පංතියේ එකතු කිරීම VerticalSpaceItemDecorationහෝ භාවිතා DividerSpaceItemDecorationකිරීම RecyclerViewසඳහා, උදාහරණයක් ලෙස ඔබේ කැබැල්ලේ onCreateView()ක්‍රමයට:

private static final int VERTICAL_ITEM_SPACE = 48;
private RecyclerView recyclerView;
private LinearLayoutManager linearLayoutManager;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {
    View rootView = inflater.inflate(R.layout.fragment_feed, container, false);

    recyclerView = (RecyclerView) rootView.findViewById(R.id.fragment_home_recycler_view);
    linearLayoutManager = new LinearLayoutManager(getActivity());
    recyclerView.setLayoutManager(linearLayoutManager);
    
    //add ItemDecoration
    recyclerView.addItemDecoration(new VerticalSpaceItemDecoration(VERTICAL_ITEM_SPACE));
    //or
    recyclerView.addItemDecoration(new DividerItemDecoration(getActivity()));
    //or
    recyclerView.addItemDecoration(
            new DividerItemDecoration(getActivity(), R.drawable.divider));

    recyclerView.setAdapter(...);

    return rootView;
}

ද තිබෙන ලූකස් රෝචා පුස්ථකාලය අයිතමය සැරසිලි ක්රියාවලිය සරල කිරීම සඳහා යැයි කියනු ලබන. එය උත්සාහ කර නැත.

එහි ලක්ෂණ අතර:

  • කොටස් අයිතම සැරසිලි එකතුවක් ඇතුළුව:
  • අයිතම පරතරය තිරස් / සිරස් බෙදුම්කරුවන්.
  • ලැයිස්තු අයිතමය

3
roproppin_science මම වැරදියි නම් මාව නිවැරදි කරන්න, නමුත් මම කිසිම වස්තුවක් නිර්මාණය කරන්නේ නැහැ onDraw(). මම දැනටමත් සඳහන් කර ඇති අවස්ථා ගැන සඳහන් කරමි.
අයිස්ක්ලෙයාර්

1
ඇඳිය හැකි දේ නිර්මාණය කරනවා වෙනුවට තීන්ත භාවිතා කිරීම හොඳ අදහසක් දැයි මට සිතේ. එවිට මම කතා කරන්න canvas.drawLine(startX, startY, stopX, stopY, mPaint)දී onDrawOver? කාර්ය සාධන වෙනසක් තිබේද?
ආර්ස්ට්

1
තොරතුරු දැනගැනීමේ අදහසක්: ඔබ පසුව ලැයිස්තුවේ අයිතම එකතු කිරීමට අදහස් කරන්නේ නම් සෑම විටම අවසාන අයිතමයට අවකාශය එක් කරන්න. ඔබ එය නොකරන්නේ නම්, අයිතමයක් එකතු කිරීමේදී එයට ඉඩක් නොලැබේ. සිරස් අවකාශයට ස්තූතියි!
සුහාරේසු

27
ඉහත පෙන්වා ඇති පරිදි DividerItemDecoration අයිතම සම්පුර්ණයෙන්ම පාරදෘශ්‍ය නම් ක්‍රියා නොකරනු ඇත. එවැනි අවස්ථාවකදී ඔබට getItemOffsets () ද අභිබවා ගොස් පහළ ඕෆ්සෙට් එක පිටතට ගෙන ඒමට අවශ්‍ය වන අතර එමඟින් බෙදුම්කරු අයිතමයෙන් පිටත අවසන් වේ. විකල්පයක් ලෙස, ඔබට අයිතමය දුරින් බෙදන්නා ඇද ගැනීමට onDraw () වෙනුවට onDrawOver () අභිබවා යා හැකිය.
jpop

129
ප්‍රතිචක්‍රීකරණ දර්ශනයට බෙදුම්කරු එක් කිරීම සඳහා කේතයේ සම්පූර්ණ පිටුවක් හොඳම පිළිතුරයි. ඔබට ලැජ්ජයි, ගූගල්.
ප්‍රවේශම් 7j

491

එකතු කරන්න

recyclerView.addItemDecoration(new DividerItemDecoration(getContext(),
                DividerItemDecoration.VERTICAL));

එසේම ඔබට පරායත්තතාවය එකතු කිරීමට අවශ්‍ය විය හැකිය
compile 'com.android.support:recyclerview-v7:27.1.0'

සංස්කරණය කරන්න:

එය ටිකක් අභිරුචිකරණය කිරීම සඳහා ඔබට අභිරුචි ඇඳිය ​​හැකි දෑ එකතු කළ හැකිය:

DividerItemDecoration itemDecorator = new DividerItemDecoration(getContext(), DividerItemDecoration.VERTICAL);
itemDecorator.setDrawable(ContextCompat.getDrawable(getContext(), R.drawable.divider));

නිදසුනක් ලෙස ඇඳිය ​​හැකි ඕනෑම අභිරුචි භාවිතා කිරීමට ඔබට නිදහස තිබේ:

<shape xmlns:android="http://schemas.android.com/apk/res/android"
       android:shape="rectangle">
    <solid android:color="@color/colorPrimary"/>
    <size android:height="0.5dp"/>
</shape>

ක්‍රියාකාරකම් අවශ්‍ය නොවේ. සන්දර්භය ප්‍රමාණවත්
mac229

මෙය නිවැරදි පිළිතුර විය යුතුය. Plz, getActivity වෙනස් කරන්න සන්දර්භයට.
ජෝන් ෆ්‍රෙඩි ටෘජිලෝ ඔර්ටෙගා

ඔබේ LayoutManager වෙතින් දිශානතිය ලබා ගැනීම වඩා හොඳය.
lsrom

ඔබට ස්තුතියි! Configurationසිරස් බෙදුම්කරු සඳහාද ඔබට භාවිතා කළ හැකිය :if (orientation == Configuration.ORIENTATION_LANDSCAPE) { recyclerView.addItemDecoration(new DividerItemDecoration(this, DividerItemDecoration.HORIZONTAL)); } else { recyclerView.addItemDecoration(new DividerItemDecoration(this, DividerItemDecoration.VERTICAL));}
ඇලෙක්ස්

4
නියම පිළිතුර, නමුත් එය අවසාන අයිතමයෙන් පසුව බෙදුම්කරුවෙකු එකතු කරයි.
CoolMind

259

ඇලෙක්ස් ෆු විසින් ගිතූබ් හි මෙම විශේෂිත ගොනුව වෙත ඔබේ අවධානය යොමු කළ හැකිය: https://gist.github.com/alexfu/0f464fc3742f134ccd1e

එය DividerItemDecoration.java නිදර්ශන ගොනුව "ආධාරක නිරූපණ වලින් කෙළින්ම ඇද ගන්නා ලදි". ( Https://plus.google.com/103498612790395592106/posts/VVEB3m7NkSS )

මගේ ව්‍යාපෘතියේ මෙම ගොනුව ආනයනය කිරීමෙන් පසුව බෙදුම් රේඛා මනාව ලබා ගැනීමටත් එය ප්‍රතිචක්‍රීකරණ දර්ශනයට අයිතම සැරසිලි ලෙස එකතු කිරීමටත් මට හැකි විය.

ප්‍රතිචක්‍රීකරණ දර්ශනය අඩංගු මගේ කැබැල්ලේ මගේ onCreateView පෙනුම කෙබඳුදැයි මෙන්න:

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    View rootView = inflater.inflate(R.layout.fragment_recycler_view, container, false);

    mRecyclerView = (RecyclerView) rootView.findViewById(R.id.my_recycler_view);
    mRecyclerView.addItemDecoration(new DividerItemDecoration(getActivity(), DividerItemDecoration.VERTICAL_LIST));

    mRecyclerView.setHasFixedSize(true);
    mLayoutManager = new LinearLayoutManager(getActivity());
    mRecyclerView.setLayoutManager(mLayoutManager);
    mRecyclerView.setItemAnimator(new DefaultItemAnimator());

    return rootView;
}

අතිරේක මෝස්තර කිරීම කළ හැකි බව මට විශ්වාසයි, නමුත් එය ආරම්භක ස්ථානයක්. :)


ඒවා වෙනුවට ඔබ එකතු කරන්නේ කෙසේද: "footerDividersEnabled", "headerDividersEnabled", "listSelector", "fastScrollEnabled", "smoothScrollbar", "textFilterEnabled"?
ඇන්ඩ්‍රොයිඩ් සංවර්ධක

මෝස්තරය දැමිය යුතු ආකාරය පිළිබඳ කිසියම් යෙදවුම්
nizam.sp

මෙම විසඳුම සැකසීමට ඔබේ තේමාවේ "android: listDivider" ගුණාංගය අභිබවා යා යුතුය
Pavel Dudka

1
බෙදුම්කරු ප්‍රතිචක්‍රීකරණ දර්ශනය සමඟ ක්‍රියා නොකරයි. ඔබ RecyclerView.itemDecoration භාවිතා කළ යුතුය. මෙම පිළිතුර බලන්න: stackoverflow.com/a/27664023/2311451
කොකෝරිකෝ

3
බෙදුම්කරු අයිතමයේ සම්පූර්ණ පළල පුළුල් කරන්නේ ඇයි? මෙම උපැස් වගේ ප්රදර්ශනය කිරීමට ආකාරය google.com/design/spec/components/lists.html#lists-specs
චිප්

158

ItemDecorationසියලුම අයිතම අතර සමාන අවකාශයන් සඳහා සරල ක්‍රියාත්මක කිරීම.

public class SpacesItemDecoration extends RecyclerView.ItemDecoration {
    private int space;

    public SpacesItemDecoration(int space) {
        this.space = space;
    }

    @Override
    public void getItemOffsets(Rect outRect, View view, RecyclerView parent, RecyclerView.State state) {
        outRect.left = space;
        outRect.right = space;
        outRect.bottom = space;

        // Add top margin only for the first item to avoid double space between items
        if(parent.getChildAdapterPosition(view) == 0) {
            outRect.top = space;
        }
    }
}

මට අවකාශය ලැබෙනවා, නමුත් මම
බෙදීමක්

28
getChildPositionදැන් අතහැර දමා ඇත, getChildAdapterPositionඒ වෙනුවට භාවිතා කළ හැකිය.
අයිස්ක්ලෙයාර්

4
ඇමතුම ඉවත් කිරීමට (මා කළ පරිදි) අමතක නොකරන්න super.getItemOffsets, නැතහොත් ඔබගේ ඕෆ්සෙට් නැවත ලියනු ලැබේ.
jayeffkay

Y අයිස්ක්ලෙයාර් getChildLayoutPositionභාවිතා නොකළ යුතුද?
අවිනාශ් ආර්

4
මෙය පරතරය පික්සෙල් වලින් ක්‍රියාත්මක කරයිද?
අපිරිසිදු_ විශාරද

109

සරලම දෙය නම් ප්‍රතිචක්‍රීකරණ දර්ශනය සඳහා පසුබිම් වර්ණය සහ අයිතම සඳහා විවිධ පසුබිම් වර්ණය සැකසීමයි . මෙන්න උදාහරණයක් ...

<android.support.v7.widget.RecyclerView
    android:background="#ECEFF1"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:scrollbars="vertical"/>

සහ පහළ ආන්තිකය සහිත "x" dp හෝ px සමඟ පෙළ දර්ශන අයිතමය (එය ඕනෑම දෙයක් විය හැකිය).

<TextView
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_marginBottom="1dp"
    android:background="#FFFFFF"/>

ප්‍රතිදානය ...

රූප විස්තරය මෙහි ඇතුළත් කරන්න


2
මොනතරම් උපක්‍රමයක්ද! පූරණය කිරීමේදී ලැයිස්තුව සුදු ලෙස තබා ගැනීම පමණක් අවශ්‍ය වේ.
හම්සේ සොබෝ

36
අයිරා වලින් පරිස්සම් වන්න!
ෂෙම්

ඔබට විස්තර කළ හැකිද?
රොමිනා වී

7
ඇන්ඩ්‍රොයිඩ් ස්ථර කිහිපයක් එකිනෙක ඉහළින් ඇඳීමේදී (ක්‍රියාකාරකම් පසුබිම, ප්‍රතිචක්‍රීකරණ දර්ශන පසුබිම සහ අයිතම බැලීමේ පසුබිම) - ඇන්ඩ්‍රොයිඩ් ඒවා සියල්ලම ඇඳීම, පරිශීලකයින්ට නොපෙනෙන ඒවා. එය අයිරා ලෙස හැඳින්වෙන අතර එය ඔබගේ ක්‍රියාකාරිත්වයට බලපානු ඇත, ඒ ගැන වැඩි විස්තර මෙතැනින්: youtube.com/watch?v=T52v50r-JfE
shem

41

සරල බෙදුම්කරු භාවිතා කිරීමෙන් ඔබට

එක් එක් අයිතමයට බෙදුම්කරු එක් කිරීමට උපකාරී වනු ඇතැයි මම සිතමි :
1- මෙය ඇද ගත හැකි ඩිරෙක්ටරියට එකතු කරන්න line_divider.xml

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<size
    android:width="1dp"
    android:height="1dp" />
<solid android:color="#999999" />
</shape>

2- SimpleDividerItemDecoration පන්තිය සාදන්න
මෙම පන්තිය අර්ථ දැක්වීම සඳහා මම මෙම උදාහරණය භාවිතා කළෙමි:
https://gist.github.com/polbins/e37206fbc444207c0e92

package com.example.myapp;
import android.content.Context;
import android.content.res.Resources;
import android.graphics.Canvas;
import android.graphics.drawable.Drawable;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import com.example.myapp.R;

public class SimpleDividerItemDecoration extends RecyclerView.ItemDecoration{
private Drawable mDivider;

public SimpleDividerItemDecoration(Resources resources) {
    mDivider = resources.getDrawable(R.drawable.line_divider);
}

public void onDrawOver(Canvas c, RecyclerView parent, RecyclerView.State state) {
    int left = parent.getPaddingLeft();
    int right = parent.getWidth() - parent.getPaddingRight();

    int childCount = parent.getChildCount();
    for (int i = 0; i < childCount; i++) {
        View child = parent.getChildAt(i);

        RecyclerView.LayoutParams params = (RecyclerView.LayoutParams) child.getLayoutParams();

        int top = child.getBottom() + params.bottomMargin;
        int bottom = top + mDivider.getIntrinsicHeight();

        mDivider.setBounds(left, top, right, bottom);
        mDivider.draw(c);
    }
  }
}


3- ප්‍රතිචක්‍රීකරණ දර්ශනය භාවිතා කරන ක්‍රියාකාරීත්වයේ හෝ කැබැල්ලේ, onCreateView තුළ මෙය එක් කරන්න:

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
 RecyclerView myRecyclerView = (RecyclerView) layout.findViewById(R.id.my_recycler_view);
 myRecyclerView.addItemDecoration(new SimpleDividerItemDecoration(getResources()));
 ....
 }


4- අයිතම අතර පරතරය
එකතු කිරීම සඳහා ඔබේ අයිතම දර්ශනයට පෑඩින් දේපල එකතු කළ යුතුය

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent" android:layout_height="match_parent"
android:padding="4dp"
>
..... item structure
</RelativeLayout>

සෛල අතර සිරස් බෙදීම් පෙන්වීම සඳහා ග්‍රිඩ්ලයිට් කළමණාකරු සඳහා එය ක්‍රියා කරන්නේ කෙසේද?
ඇන්ඩ්‍රොයිඩ් සංවර්ධක

2
resources.getDrawable () දැන් ඉවත් කර ඇත. ඔබට සන්දර්භය තුළ සමත් විය හැකි අතර ContextCompat.getDrawable (සන්දර්භය, R.drawable.line_divider) භාවිතා කළ හැකිය
එරික් බී.

37

මම නියම කර ඇති පරිදි ItemAnimators. මෙම ItemDecoratorසජීවිකරණ සමග ඇතුළු හෝ පිටවීමේ නැහැ.

එක් එක් අයිතමයේ මගේ අයිතම දර්ශන පිරිසැලසුම් ගොනුවේ දර්ශන රේඛාවක් තිබීම මම අවසන් කළෙමි. එය මගේ නඩුව විසඳීය. DividerItemDecorationසරල බෙදුම්කරුවකුට මන්ත්‍ර ගුරුකම් වැඩියි.

<View
    android:layout_width="match_parent"
    android:layout_height="1px"
    android:layout_marginLeft="5dp"
    android:layout_marginRight="5dp"
    android:background="@color/lt_gray"/>

ඔයා හරි. අයිතම අලංකරණය සමඟ සජීවිකරණ ක්‍රියා නොකරයි. එයට හේතුව කුමක්දැයි මට විශ්වාස නැත, නමුත් මා කිසිවක් සඳහන් නොකර, මට සජීවීකරණ ලැබෙන අතර, අයිතම අලංකරණය විසින් නිර්මාණය කරන ලද රේඛා අනුගමනය නොකිරීම ඉතා අවධානය වෙනතකට හා කැත ලෙස මට පෙනේ. එබැවින් මම ඔබ වැනි විසඳුමක් භාවිතා කරමි.
මයිකල්

අවසාන අයිතමය සමඟ ඔබ කටයුතු කළේ කෙසේද?
oldgod

ඕල්ඩර්ගොඩ්. ඔබ නිවැරදි වේදනා ලක්ෂ්‍යය පෙන්වා දුන්නා. අන්තිම අයිතමය මත බෙදීමක් ඇති කිරීම සඳහා මම මුලින්ම එකඟ වෙමි. නමුත් ඔබට එය අවශ්‍ය නැතිනම්. මෙම දර්ශනයට හැඳුනුම්පතක් ලබා දී ස්ථානය අවසාන නම් බින්ඩ්වීව් හි සඟවන්න.
ජවනේටර්

Av ජවනේටර් මම හරි, මම ගත් ප්‍රවේශයම හොඳයි. ස්තූතියි.
oldgod

සරලම හොඳම දේ
hyyou2010

29

මෙය සරලයි, ඔබට එවැනි සංකීර්ණ කේතයක් අවශ්‍ය නොවේ

DividerItemDecoration divider = new 
DividerItemDecoration(mRVMovieReview.getContext(), 
DividerItemDecoration.VERTICAL);
divider.setDrawable(
    ContextCompat.getDrawable(getBaseContext(), R.drawable.line_divider)
);
mRVMovieReview.addItemDecoration(divider);

ඔබේ ඇඳිය ​​හැකි දේට මෙය එක් කරන්න: line_divider.xml

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" 
  android:shape="rectangle">
    <size android:height="1dp" />
    <solid android:color="@android:color/black" />
</shape>

27

මම ඩිවයිඩර් දර්ශනය සහ ඩිවයිඩර් ඉන්සෙට් හසුරුවන ආකාරය ප්‍රතිචක්‍රීකරණ දර්ශන දිගුවක් එක් කිරීමකි.

1.

View හෝ RecyclerView නම් කිරීමෙන් නව දිගු ගොනුවක් එක් කරන්න:

RecyclerViewExtension.kt

setDividerRecyclerViewExtension.kt ගොනුව තුළ දීර් extension කිරීමේ ක්‍රමය එක් කරන්න .

/*
* RecyclerViewExtension.kt
* */
import androidx.annotation.DrawableRes
import androidx.core.content.ContextCompat
import androidx.recyclerview.widget.DividerItemDecoration
import androidx.recyclerview.widget.RecyclerView


fun RecyclerView.setDivider(@DrawableRes drawableRes: Int) {
    val divider = DividerItemDecoration(
        this.context,
        DividerItemDecoration.VERTICAL
    )
    val drawable = ContextCompat.getDrawable(
        this.context,
        drawableRes
    )
    drawable?.let {
        divider.setDrawable(it)
        addItemDecoration(divider)
    }
}

2.

drawableපැකේජයේ ඇතුළත ඇඳිය ​​හැකි සම්පත් ගොනුවක් සාදන්න recycler_view_divider.xml:

<inset xmlns:android="http://schemas.android.com/apk/res/android"
    android:insetLeft="10dp"
    android:insetRight="10dp">

    <shape>
        <size android:height="0.5dp" />
        <solid android:color="@android:color/darker_gray" />
    </shape>

</inset>

එහිදී ඔබට වම් හා දකුණු සඳහන් කළ හැකිය ආන්තිකය මත android:insetLeftහා android:insetRight.

3.

ප්‍රතිචක්‍රීකරණ දර්ශනය ආරම්භ කර ඇති ඔබගේ ක්‍රියාකාරකම් හෝ කොටස් මත, ඔබට ඇමතීමෙන් අභිරුචි ඇඳිය ​​හැකි දේ සැකසිය හැකිය:

recyclerView.setDivider(R.drawable.recycler_view_divider)

4.

චියර්ස්

බෙදුම්කරු සමඟ ප්‍රතිචක්‍රීකරණ පේළිය.


21

ද්‍රව්‍යමය සැලසුම භාවිතයෙන් මෙය තවමත් නිසි ලෙස ක්‍රියාත්මක කිරීමට නිවැරදි ක්‍රමයක් නොමැති බැවින්, ලැයිස්තු අයිතමයට කෙලින්ම බෙදීමක් එක් කිරීමට මම පහත උපක්‍රමය කළෙමි:

<View
android:layout_width="match_parent"
android:layout_height="1dp"
android:background="@color/dividerColor"/>

ද්‍රව්‍යමය සැලසුම පිළිබඳ යම් උන්නතාංශ තොරතුරු මා සතුව ඇති පසු (ඉන්බොක්ස් හි මෙන් සමාන බලපෑමක් ලබා ගැනීම සඳහා) ඩිවයිඩර් අයිටම් ඩෙකොරේෂන් වැඩ කිරීම නවත්වන්න; එය සරල දෙයකට වඩා සංකීර්ණ වී ඇත. මෙම විසඳුම සරල වන අතර ක්රියා කරයි.
ඩෙනිස් ජීඑල්

17

යමෙක් අයිතම අතර 10dp පරතරයක් එක් කිරීමට පමණක් කියන්නේ නම්, ඔබට මෙය ඇඳිය ​​හැකි පරිදි සැකසීමෙන් කළ හැකිය DividerItemDecoration:

DividerItemDecoration dividerItemDecoration = new DividerItemDecoration(
    recyclerView.getContext(),
    layoutManager.getOrientation()
);

dividerItemDecoration.setDrawable(
    ContextCompat.getDrawable(getContext(), R.drawable.divider_10dp)
); 

divider_10dpඇද ගත හැකි සම්පතක් අඩංගු වන්නේ කොහේද :

<shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle">
    <size android:height="10dp"/>
    <solid android:color="@android:color/transparent"/>
</shape>


12
  • බෙදුම්කරු එකතු කිරීම සඳහා සරල හැක් එකක් මෙන්න
  • පහත සඳහන් පරිදි ඔබේ ප්‍රතිචක්‍රීකරණ අයිතමයේ පිරිසැලසුමට පසුබිමක් එක් කරන්න

    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="@drawable/shape_border"
        android:gravity="center"
        android:orientation="horizontal"
        android:padding="5dp">
    
    <ImageView
        android:id="@+id/imageViewContactLogo"
        android:layout_width="60dp"
        android:layout_height="60dp"
        android:layout_marginRight="10dp"
        android:src="@drawable/ic_user" />
    
    <LinearLayout
        android:id="@+id/linearLayout"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_weight="0.92"
        android:gravity="center|start"
        android:orientation="vertical">
    
    <TextView
        android:id="@+id/textViewContactName"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:singleLine="true"
        android:text="Large Text"
        android:textAppearance="?android:attr/textAppearanceLarge" />
    
    <TextView
        android:id="@+id/textViewStatusOrNumber"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="5dp"
        android:singleLine="true"
        android:text=""
        android:textAppearance="?android:attr/textAppearanceMedium" />
    </LinearLayout>
    
    <TextView
        android:id="@+id/textViewUnreadCount"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginRight="10dp"
        android:padding="5dp"
        android:text=""
        android:textAppearance="?android:attr/textAppearanceMedium"
        android:textColor="@color/red"
        android:textSize="22sp" />
    
    <Button
        android:id="@+id/buttonInvite"
        android:layout_width="54dp"
        android:layout_height="wrap_content"
        android:background="@drawable/ic_add_friend" />
    </LinearLayout>

ඇද ගත හැකි ෆෝල්ඩරයේ පහත දැක්වෙන shape_border.xml සාදන්න

  <?xml version="1.0" encoding="utf-8"?>
    <shape xmlns:android="http://schemas.android.com/apk/res/android"
      android:shape="rectangle" >
       <gradient
        android:angle="270"
        android:centerColor="@android:color/transparent"
        android:centerX="0.01"
        android:startColor="#000" />
    </shape>

මෙන්න අවසාන ප්‍රති result ලය - බෙදුම්කරු සමඟ ප්‍රතිචක්‍රීකරණ දසුනක්.

මෙන්න අවසාන ප්‍රති result ලය - බෙදුම්කරු සමඟ ප්‍රතිචක්‍රීකරණ දසුනක්.


1
මෙය වඩාත් කැමති ප්‍රවේශයක් නොවේ. නමුත් @EyesClear පිළිතුර කෙ ඇති onDraw දී වන අතර int parent.getChildAdapterPosition(view) != parent.getAdapter().getItemCount() - 1බොහෝ විට විය යුතු parent.getChildAdapterPosition(view) > 0සමඟ outRect.bottom = mVerticalSpaceHeightබවට outRect.top = mVerticalSpaceHeightඑය පිළිගත් පිළිතුර විය යුතුය.
droppin_science

roproppin_science - මෙය වඩාත් කැමති ප්‍රවේශයක් නොවන බව පැවසීමෙන් ඔබට එය නොසලකා හැරිය නොහැකිය, එය අපේක්ෂා කළ පරිදි නිවැරදි ප්‍රති results ල ලබා දෙයි, මමත් අයිස්ක්ලෙයාර්ගේ පිළිතුර දෙස බලමි, නමුත් සරල බෙදුම්කරුවෙකුට එම පිළිතුර ඉතා සංකීර්ණ වේ, කෙසේ වෙතත් අවශ්‍ය නම් අයිතමය සමඟ අමතර සැරසිලි කරන්න, එවිට එය පිළිගත් පිළිතුර විය හැකිය.
turbandroid

පහල ඡන්දදායකයින් සඳහා, මෙම පිළිතුර ලබා දී ඇත්තේ ඩිවයිඩර් අයිටම් ඩෙකොරේෂන් සඳහා නිල පන්ති නොතිබූ අවස්ථාවේ දී ය, එබැවින් මෙම පිළිතුර සහ ලියෝ ඩ්‍රොයිඩ්කෝඩර් විසින් දෙන ලද පිළිතුර අතර කාල පරතරය සසඳා බලන්න. :)
turbandroid

11

මෙය ඇත්ත වශයෙන්ම ගැටළුව විසඳන්නේ නැත, නමුත් තාවකාලික විසඳුමක් ලෙස, ඔබේ XML පිරිසැලසුමෙහි ඇති කාඩ්පතේ ඇති භාවිත කොම්පැට් පැඩින් දේපල සැකසිය හැකි අතර එය පෙර-ලොලිපොප් අනුවාදවල මෙන් ම මැනිය හැකිය.

card_view:cardUseCompatPadding="true"


11

අයිතම අතර ඇති අවකාශයන් ගැන පමණක් සොයන අය සඳහා, RecyclerViewමගේ ප්‍රවේශය බලන්න, ඔබ විශාල අයිතමයක් ලබා දුන් පළමු හා අවසාන අයිතම හැර, සියලු අයිතම අතර සමාන ඉඩක් ලබා ගන්නා ස්ථානය. මම පෑඩින් යෙදීම තිරස් අතට වමට / දකුණට LayoutManagerසහ ඉහළ / පහළ සිරස් අතට පමණි LayoutManager.

public class PaddingItemDecoration extends RecyclerView.ItemDecoration {

    private int mPaddingPx;
    private int mPaddingEdgesPx;

    public PaddingItemDecoration(Activity activity) {
        final Resources resources = activity.getResources();
        mPaddingPx = (int) resources.getDimension(R.dimen.paddingItemDecorationDefault);
        mPaddingEdgesPx = (int) resources.getDimension(R.dimen.paddingItemDecorationEdge);
    }

    @Override
    public void getItemOffsets(Rect outRect, View view, RecyclerView parent, RecyclerView.State state) {
        super.getItemOffsets(outRect, view, parent, state);

        final int itemPosition = parent.getChildAdapterPosition(view);
        if (itemPosition == RecyclerView.NO_POSITION) {
            return;
        }
        int orientation = getOrientation(parent);
        final int itemCount = state.getItemCount();

        int left = 0;
        int top = 0;
        int right = 0;
        int bottom = 0;

        /** HORIZONTAL */
        if (orientation == LinearLayoutManager.HORIZONTAL) {
            /** all positions */
            left = mPaddingPx;
            right = mPaddingPx;

            /** first position */
            if (itemPosition == 0) {
                left += mPaddingEdgesPx;
            }
            /** last position */
            else if (itemCount > 0 && itemPosition == itemCount - 1) {
                right += mPaddingEdgesPx;
            }
        }
        /** VERTICAL */
        else {
            /** all positions */
            top = mPaddingPx;
            bottom = mPaddingPx;

            /** first position */
            if (itemPosition == 0) {
                top += mPaddingEdgesPx;
            }
            /** last position */
            else if (itemCount > 0 && itemPosition == itemCount - 1) {
                bottom += mPaddingEdgesPx;
            }
        }

        if (!isReverseLayout(parent)) {
            outRect.set(left, top, right, bottom);
        } else {
            outRect.set(right, bottom, left, top);
        }
    }

    private boolean isReverseLayout(RecyclerView parent) {
        if (parent.getLayoutManager() instanceof LinearLayoutManager) {
            LinearLayoutManager layoutManager = (LinearLayoutManager) parent.getLayoutManager();
            return layoutManager.getReverseLayout();
        } else {
            throw new IllegalStateException("PaddingItemDecoration can only be used with a LinearLayoutManager.");
        }
    }

    private int getOrientation(RecyclerView parent) {
        if (parent.getLayoutManager() instanceof LinearLayoutManager) {
            LinearLayoutManager layoutManager = (LinearLayoutManager) parent.getLayoutManager();
            return layoutManager.getOrientation();
        } else {
            throw new IllegalStateException("PaddingItemDecoration can only be used with a LinearLayoutManager.");
        }
    }
}

මාන. xml

<resources>
    <dimen name="paddingItemDecorationDefault">10dp</dimen>
    <dimen name="paddingItemDecorationEdge">20dp</dimen>
</resources>

11

ඔබේ දෘෂ්ටියට ආන්තිකයක් එක් කරන්න, එය මට වැඩ කළේය.

android:layout_marginTop="10dp"

ඔබ එකතු කිරීමට අවශ්ය නම්, සමාන පරතර ලෑම හා එය කිරීමට අවශ්ය XML , හුදෙක් සකස් paddingඔබේ RecyclerViewහා සමාන මුදලක් layoutMarginඔබට ඔබේ බවට තත්වය් අයිතමය කිරීමට RecyclerView, සහ පසුබිම් වර්ණය වන පරතරය වර්ණ තීරණය කරමු.


3
මෙය ක්‍රියාත්මක වුවද, මෙය නිවැරදි පිළිතුර නොවේ, උදාහරණයක් ලෙස, පේළි පිරිසැලසුමට අමතර දේ නොකර ගැටළුව විසඳන්නේ නැති නිසා, ඉහළින් ආන්තිකය x1 දිස්වනු ඇත, පේළි අතර ආන්තිකය x2 දිස්වනු ඇත.
ශ්‍රීකාන්ත් කරුණානගට්

මෙය හොඳ අදහසක් නොවේ, මන්ද overscrollලැයිස්තුවේ අවසානයේ අදින විට ඇතිවන බලපෑම අනවශ්‍ය RecyclerView
පෑඩින් කිරීම සඳහා පෑඩින්

ආධාරක පුස්තකාල කාඩ්පතක අයිතමයේ පිරිසැලසුම එතීම වඩා හොඳය, එවිට ඔබට වෙනත් ගුණාංග පාලනය කළ හැකිය. උදා: උන්නතාංශය / සෙවනැල්ල යනාදිය: <?xml version="1.0" encoding="utf-8"?> <android.support.v7.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android" xmlns:card_view="http://schemas.android.com/apk/res-auto" android:id="@+id/card_view" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginBottom="10dp" card_view:cardElevation="4dp" <!-- your item's XML here --> </android.support.v7.widget.CardView>
kip2

9

මම පැරණි සාරාංශයකින් DividerItemDecoration වෙත යොමු කර එය මගේ භාවිතයට සරිලන පරිදි සරල කර ඇති අතර, අවසාන ලැයිස්තු අයිතමයට පසුව බෙදුම්කරුවෙකු ද ඇතුළුව, ලැයිස්තු දර්ශනයේ බෙදුම් කරුවන් ඇද ගන්නා ආකාරයට එය ඇද ගැනීමට මම එය වෙනස් කළෙමි. මෙය සිරස් ItemAnimator සජීවිකරණ ද හසුරුවනු ඇත:

1) ඔබේ ව්‍යාපෘතියට මෙම පන්තිය එක් කරන්න:

public class DividerItemDecoration extends RecyclerView.ItemDecoration {
    private static final int[] ATTRS = new int[]{android.R.attr.listDivider};
    private Drawable divider;

    public DividerItemDecoration(Context context) {
        try {
            final TypedArray a = context.obtainStyledAttributes(ATTRS);
            divider = a.getDrawable(0);
            a.recycle();
        } catch (Resources.NotFoundException e) {
            // TODO Log or handle as necessary.
        }
    }

    @Override
    public void getItemOffsets(Rect outRect, View view, RecyclerView parent, RecyclerView.State state) {
        super.getItemOffsets(outRect, view, parent, state);
        if (divider == null) return;
        if (parent.getChildAdapterPosition(view) < 1) return;

        if (getOrientation(parent) == LinearLayoutManager.VERTICAL)
            outRect.top = divider.getIntrinsicHeight();
        else
            throw new IllegalArgumentException("Only usable with vertical lists");
    }

    @Override
    public void onDrawOver(Canvas c, RecyclerView parent, RecyclerView.State state) {
        if (divider == null) {
            super.onDrawOver(c, parent, state);
            return;
        }

        final int left = parent.getPaddingLeft();
        final int right = parent.getWidth() - parent.getPaddingRight();
        final int childCount = parent.getChildCount();

        for (int i = 0; i < childCount; ++i) {
            final View child = parent.getChildAt(i);
            final RecyclerView.LayoutParams params = (RecyclerView.LayoutParams) child.getLayoutParams();
            final int size = divider.getIntrinsicHeight();
            final int top = (int) (child.getTop() - params.topMargin - size + child.getTranslationY());
            final int bottom = top + size;
            divider.setBounds(left, top, right, bottom);
            divider.draw(c);

            if (i == childCount - 1) {
                final int newTop = (int) (child.getBottom() + params.bottomMargin + child.getTranslationY());
                final int newBottom = newTop + size;
                divider.setBounds(left, newTop, right, newBottom);
                divider.draw(c);
            }
        }
    }

    private int getOrientation(RecyclerView parent) {
        if (!(parent.getLayoutManager() instanceof LinearLayoutManager))
            throw new IllegalStateException("Layout manager must be an instance of LinearLayoutManager");
        return ((LinearLayoutManager) parent.getLayoutManager()).getOrientation();
    }
}

2) ඔබේ ප්‍රතිචක්‍රීකරණ දර්ශනයට සැරසිලි කරන්නා එක් කරන්න:

recyclerView.addItemDecoration(new DividerItemDecoration(getActivity()));

නිවැරදි, එය රේඛීය ලේයූට් මැනේජර් සඳහා අදහස් කෙරේ. GridLayoutManager සඳහා එය අනුවර්තනය කිරීම සඳහා ඔබට එය පිටුපස ඇති අදහස ගත හැකිය.
OpenGL ES

9

shape xmlබෙදුම්කරුගේ උස සහ වර්ණය වෙනස් කිරීම සඳහා a නිර්මාණය කරනවා වෙනුවට . ඔබට ක්‍රමලේඛ ලෙස වැනි නිර්මාණය කළ හැකිය

val divider = DividerItemDecoration(context,
        DividerItemDecoration.VERTICAL)

divider.setDrawable(ShapeDrawable().apply {
    intrinsicHeight = resources.getDimensionPixelOffset(R.dimen.dp_15)
    paint.color = Color.RED // note: currently (support version 28.0.0), we can not use tranparent color here, if we use transparent, we still see a small divider line. So if we want to display transparent space, we can set color = background color or we can create a custom ItemDecoration instead of DividerItemDecoration. 
})

recycler_devices.addItemDecoration(divider)

මෙය ප්‍රයෝජනවත් පිළිතුරකි
taha

7

ගූගල් සෙවුමකින් ගත් විට, මෙම අයිතම අලංකරණය ඔබගේ RecyclerView:

public class DividerItemDecoration extends RecyclerView.ItemDecoration {

private Drawable mDivider;
private boolean mShowFirstDivider = false;
private boolean mShowLastDivider = false;


public DividerItemDecoration(Context context, AttributeSet attrs) {
    final TypedArray a = context
            .obtainStyledAttributes(attrs, new int[]{android.R.attr.listDivider});
    mDivider = a.getDrawable(0);
    a.recycle();
}

public DividerItemDecoration(Context context, AttributeSet attrs, boolean showFirstDivider,
        boolean showLastDivider) {
    this(context, attrs);
    mShowFirstDivider = showFirstDivider;
    mShowLastDivider = showLastDivider;
}

public DividerItemDecoration(Drawable divider) {
    mDivider = divider;
}

public DividerItemDecoration(Drawable divider, boolean showFirstDivider,
        boolean showLastDivider) {
    this(divider);
    mShowFirstDivider = showFirstDivider;
    mShowLastDivider = showLastDivider;
}

@Override
public void getItemOffsets(Rect outRect, View view, RecyclerView parent,
        RecyclerView.State state) {
    super.getItemOffsets(outRect, view, parent, state);
    if (mDivider == null) {
        return;
    }
    if (parent.getChildPosition(view) < 1) {
        return;
    }

    if (getOrientation(parent) == LinearLayoutManager.VERTICAL) {
        outRect.top = mDivider.getIntrinsicHeight();
    } else {
        outRect.left = mDivider.getIntrinsicWidth();
    }
}

@Override
public void onDrawOver(Canvas c, RecyclerView parent, RecyclerView.State state) {
    if (mDivider == null) {
        super.onDrawOver(c, parent, state);
        return;
    }

    // Initialization needed to avoid compiler warning
    int left = 0, right = 0, top = 0, bottom = 0, size;
    int orientation = getOrientation(parent);
    int childCount = parent.getChildCount();

    if (orientation == LinearLayoutManager.VERTICAL) {
        size = mDivider.getIntrinsicHeight();
        left = parent.getPaddingLeft();
        right = parent.getWidth() - parent.getPaddingRight();
    } else { //horizontal
        size = mDivider.getIntrinsicWidth();
        top = parent.getPaddingTop();
        bottom = parent.getHeight() - parent.getPaddingBottom();
    }

    for (int i = mShowFirstDivider ? 0 : 1; i < childCount; i++) {
        View child = parent.getChildAt(i);
        RecyclerView.LayoutParams params = (RecyclerView.LayoutParams) child.getLayoutParams();

        if (orientation == LinearLayoutManager.VERTICAL) {
            top = child.getTop() - params.topMargin;
            bottom = top + size;
        } else { //horizontal
            left = child.getLeft() - params.leftMargin;
            right = left + size;
        }
        mDivider.setBounds(left, top, right, bottom);
        mDivider.draw(c);
    }

    // show last divider
    if (mShowLastDivider && childCount > 0) {
        View child = parent.getChildAt(childCount - 1);
        RecyclerView.LayoutParams params = (RecyclerView.LayoutParams) child.getLayoutParams();
        if (orientation == LinearLayoutManager.VERTICAL) {
            top = child.getBottom() + params.bottomMargin;
            bottom = top + size;
        } else { // horizontal
            left = child.getRight() + params.rightMargin;
            right = left + size;
        }
        mDivider.setBounds(left, top, right, bottom);
        mDivider.draw(c);
    }
}

private int getOrientation(RecyclerView parent) {
    if (parent.getLayoutManager() instanceof LinearLayoutManager) {
        LinearLayoutManager layoutManager = (LinearLayoutManager) parent.getLayoutManager();
        return layoutManager.getOrientation();
    } else {
        throw new IllegalStateException(
                "DividerItemDecoration can only be used with a LinearLayoutManager.");
    }
}
}

මෙය ලීනියර් ලේයූට් මැනේජර් සඳහා පමණක් හොඳය. GridLayoutManager සඳහා කළ යුත්තේ කුමක්ද?
ඇන්ඩ්‍රොයිඩ් සංවර්ධක

6

මෙම සබැඳිය මා සිත් ඇදගන්නා සුළු ලෙස ක්‍රියා කළේය:

https://gist.github.com/lapastillaroja/858caf1a82791b6c1a36

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.Rect;
import android.graphics.drawable.Drawable;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.AttributeSet;
import android.view.View;

public class DividerItemDecoration extends RecyclerView.ItemDecoration {

    private Drawable mDivider;
    private boolean mShowFirstDivider = false;
    private boolean mShowLastDivider = false;


    public DividerItemDecoration(Context context, AttributeSet attrs) {
        final TypedArray a = context
                .obtainStyledAttributes(attrs, new int[]{android.R.attr.listDivider});
        mDivider = a.getDrawable(0);
        a.recycle();
    }

    public DividerItemDecoration(Context context, AttributeSet attrs, boolean showFirstDivider,
            boolean showLastDivider) {
        this(context, attrs);
        mShowFirstDivider = showFirstDivider;
        mShowLastDivider = showLastDivider;
    }

    public DividerItemDecoration(Drawable divider) {
        mDivider = divider;
    }

    public DividerItemDecoration(Drawable divider, boolean showFirstDivider,
            boolean showLastDivider) {
        this(divider);
        mShowFirstDivider = showFirstDivider;
        mShowLastDivider = showLastDivider;
    }

    @Override
    public void getItemOffsets(Rect outRect, View view, RecyclerView parent,
            RecyclerView.State state) {
        super.getItemOffsets(outRect, view, parent, state);
        if (mDivider == null) {
            return;
        }
        if (parent.getChildPosition(view) < 1) {
            return;
        }

        if (getOrientation(parent) == LinearLayoutManager.VERTICAL) {
            outRect.top = mDivider.getIntrinsicHeight();
        } else {
            outRect.left = mDivider.getIntrinsicWidth();
        }
    }

    @Override
    public void onDrawOver(Canvas c, RecyclerView parent, RecyclerView.State state) {
        if (mDivider == null) {
            super.onDrawOver(c, parent, state);
            return;
        }

        // Initialization needed to avoid compiler warning
        int left = 0, right = 0, top = 0, bottom = 0, size;
        int orientation = getOrientation(parent);
        int childCount = parent.getChildCount();

        if (orientation == LinearLayoutManager.VERTICAL) {
            size = mDivider.getIntrinsicHeight();
            left = parent.getPaddingLeft();
            right = parent.getWidth() - parent.getPaddingRight();
        } else { //horizontal
            size = mDivider.getIntrinsicWidth();
            top = parent.getPaddingTop();
            bottom = parent.getHeight() - parent.getPaddingBottom();
        }

        for (int i = mShowFirstDivider ? 0 : 1; i < childCount; i++) {
            View child = parent.getChildAt(i);
            RecyclerView.LayoutParams params = (RecyclerView.LayoutParams) child.getLayoutParams();

            if (orientation == LinearLayoutManager.VERTICAL) {
                top = child.getTop() - params.topMargin;
                bottom = top + size;
            } else { //horizontal
                left = child.getLeft() - params.leftMargin;
                right = left + size;
            }
            mDivider.setBounds(left, top, right, bottom);
            mDivider.draw(c);
        }

        // show last divider
        if (mShowLastDivider && childCount > 0) {
            View child = parent.getChildAt(childCount - 1);
            RecyclerView.LayoutParams params = (RecyclerView.LayoutParams) child.getLayoutParams();
            if (orientation == LinearLayoutManager.VERTICAL) {
                top = child.getBottom() + params.bottomMargin;
                bottom = top + size;
            } else { // horizontal
                left = child.getRight() + params.rightMargin;
                right = left + size;
            }
            mDivider.setBounds(left, top, right, bottom);
            mDivider.draw(c);
        }
    }

    private int getOrientation(RecyclerView parent) {
        if (parent.getLayoutManager() instanceof LinearLayoutManager) {
            LinearLayoutManager layoutManager = (LinearLayoutManager) parent.getLayoutManager();
            return layoutManager.getOrientation();
        } else {
            throw new IllegalStateException(
                    "DividerItemDecoration can only be used with a LinearLayoutManager.");
        }
    }
}

ඔබගේ ක්‍රියාකාරකම තුළ:

mCategoryRecyclerView.addItemDecoration(
    new DividerItemDecoration(this, null));

නැතහොත් ඔබ කැබැල්ලක් භාවිතා කරන්නේ නම් මෙය:

mCategoryRecyclerView.addItemDecoration(
    new DividerItemDecoration(getActivity(), null));

1
මෙය හොඳින් ක්‍රියාත්මක වේ, නමුත් එය ලැයිස්තුවේ අවසාන අයිතමය යටතේ බෙදුම්කරු නොපෙන්වයි. මම මේ වගේ එය අවශ්ය: mShowFirstDivider = false, mShowLastDivider = true, නමුත් එය ක්රියා නොකරනු ඇත. කිසියම් අදහසක් ඇයි?
රාත්‍රී

මෙය GridLayoutManager හොඳින් හැසිරවිය නොහැක.
ඇන්ඩ්‍රොයිඩ් සංවර්ධක

6

DividerItemDecoration වැනි ප්‍රතිචක්‍රීකරණ දර්ශනයට අමුණා ඇති විවිධ සැරසිලි කරුවන් භාවිතා කර අපට අයිතම අලංකාර කළ හැකිය.

පහත දැක්වෙන දෑ භාවිතා කරන්න ... අයිස්ක්ලෙයාර් විසින් පිළිතුරෙන් ලබා ගන්නා ලදි

public class DividerItemDecoration extends RecyclerView.ItemDecoration {

private static final int[] ATTRS = new int[]{android.R.attr.listDivider};

private Drawable mDivider;

/**
 * Default divider will be used
 */
public DividerItemDecoration(Context context) {
    final TypedArray styledAttributes = context.obtainStyledAttributes(ATTRS);
    mDivider = styledAttributes.getDrawable(0);
    styledAttributes.recycle();
}

/**
 * Custom divider will be used
 */
public DividerItemDecoration(Context context, int resId) {
    mDivider = ContextCompat.getDrawable(context, resId);
}

@Override
public void onDraw(Canvas c, RecyclerView parent, RecyclerView.State state) {
    int left = parent.getPaddingLeft();
    int right = parent.getWidth() - parent.getPaddingRight();

    int childCount = parent.getChildCount();
    for (int i = 0; i < childCount; i++) {
        View child = parent.getChildAt(i);

        RecyclerView.LayoutParams params = (RecyclerView.LayoutParams) child.getLayoutParams();

        int top = child.getBottom() + params.bottomMargin;
        int bottom = top + mDivider.getIntrinsicHeight();

        mDivider.setBounds(left, top, right, bottom);
        mDivider.draw(c);
    }
}

} ඉන්පසු ඉහත සඳහන් දෑ පහත පරිදි භාවිතා කරන්න

RecyclerView.ItemDecoration itemDecoration = new DividerItemDecoration(this, DividerItemDecoration.VERTICAL_LIST);
recyclerView.addItemDecoration(itemDecoration);

පහත දැක්වෙන පරිදි මෙය ලැයිස්තුවේ ඇති එක් එක් අයිතමය අතර බෙදුම්කරුවන් පෙන්වනු ඇත:

රූප විස්තරය මෙහි ඇතුළත් කරන්න

වැඩි විස්තර සොයන අයට ප්‍රතිචක්‍රීකරණ දර්ශනය _ කෝඩ්පාත් ඇන්ඩ්‍රොයිඩ් ක්ලිෆ්නෝට්ස් භාවිතා කරමින් මෙම මාර්ගෝපදේශය බලන්න.

මෙහි සමහර පිළිතුරු මඟින් ආන්තිකයන් භාවිතා කිරීමට යෝජනා කරන නමුත් අල්ලා ගැනීම නම්: ඔබ ඉහළ සහ පහළ ආන්තික දෙකම එකතු කළහොත්, ඒවා දෙකම අයිතම අතර එකතු වී ඇති බව පෙනේ, ඒවා විශාල වනු ඇත. ඔබ එක්කෝ පමණක් එකතු කරන්නේ නම්, සම්පූර්ණ ලැයිස්තුවේ ඉහළ හෝ පහළ ආන්තිකයක් නොමැත. ඔබ දුරින් අඩක් ඉහළින්, අඩක් පතුලේ එකතු කළහොත් පිටත ආන්තිකය ඉතා කුඩා වනු ඇත.

මේ අනුව, එකම සෞන්දර්යාත්මක නිවැරදි විසඳුම වන්නේ පද්ධතිය නිසි ලෙස ඉල්ලුම් කළ යුතු ස්ථානය දන්නා බෙදුම්කරු ය: අයිතම අතර නමුත් අයිතමවලට ඉහළින් හෝ පහළින් නොවේ.

පහත දැක්වෙන අදහස් දැක්වීමේ කිසියම් සැකයක් කරුණාකර මට දන්වන්න :)


1
මෙය DividerItemDecorationකේතය මොන වගේද යන්න නිරූපණය නොකරයි .
ඉගෝර්ගනාපොල්ස්කි

1
එහි AOSP පන්තියක්, මම ඔබ වෙනුවෙන් කේතය හාරා ..... gist.githubusercontent.com/alexfu/0f464fc3742f134ccd1e/raw/…
අනුදීප් සමයියා

එය හොඳින් ක්‍රියා නොකරයි: එය විවිධ උස පේළි හසුරුවන්නේ නැත, එය ග්‍රිඩ් සඳහා සිරස් බෙදීමක් නොපෙන්වයි
ඇන්ඩ්‍රොයිඩ් සංවර්ධක

6

XML භාවිතා නොකරන සරල, කේත පදනම් කරගත් පිළිතුරක් අවශ්‍ය බව මට හැඟේ

DividerItemDecoration dividerItemDecoration = new DividerItemDecoration(recyclerView.getContext(), DividerItemDecoration.VERTICAL);

ShapeDrawable shapeDrawableForDivider = new ShapeDrawable(new RectShape());

int dividerThickness = // (int) (SomeOtherView.getHeight() * desiredPercent);
shapeDrawableForDivider.setIntrinsicHeight(dividerThickness);
shapeDrawableForDivider.setAlpha(0);

dividerItemDecoration.setDrawable(shapeDrawableForDivider);

recyclerView.addItemDecoration(dividerItemDecoration);

මම මෙම පිළිතුරට බෙහෙවින් ඇලුම් කරමි, මම එය තනි ප්‍රකාශනයකින් නැවත ලිවීමි.

    recyclerView.addItemDecoration(DividerItemDecoration(this,DividerItemDecoration.VERTICAL).also { deco ->
        with (ShapeDrawable(RectShape())){
            intrinsicHeight = (resources.displayMetrics.density * 24).toInt()
            alpha = 0
            deco.setDrawable(this)
        }
    })

මෙය er නර්ඩිගේ මුල් පිළිතුරට සමාන වේ, එය වෙනත් දර්ශනයේ උසක ප්‍රතිශතයක් වෙනුවට බෙදුම්කරුගේ උස 24dp ලෙස සකසයි.


5

ප්‍රමාද වැඩියි නමුත් GridLayoutManagerමම මෙය භාවිතා කරන්නේ:

public class GridSpacesItemDecoration : RecyclerView.ItemDecoration
{
    private int space;

    public GridSpacesItemDecoration(int space) {
        this.space = space;
    }

    public override void GetItemOffsets(Android.Graphics.Rect outRect, View view, RecyclerView parent, RecyclerView.State state)
    {
        var position = parent.GetChildLayoutPosition(view);

        /// Only for GridLayoutManager Layouts
        var manager = parent.GetLayoutManager() as GridLayoutManager;

        if (parent.GetChildLayoutPosition(view) < manager.SpanCount)
            outRect.Top = space;

        if (position % 2 != 0) {
            outRect.Right = space;
        }

        outRect.Left = space;
        outRect.Bottom = space;
    }
}

ඔබ සතුව ඇති ඕනෑම පරතරයක් සඳහා මෙම කාර්යය.

ඔලී.


ඉහළ අවකාශය ගැන, ඔබ එය සහය දැක්වීම සඳහා වෙනස් කරන්නේ FlexboxLayoutManagerකෙසේද?
ඇන්ඩ්‍රොයිඩ් සංවර්ධක

5

ඔබට ක්‍රමලේඛනගතව පහසුවෙන් එකතු කළ හැකිය.

ඔබේ පිරිසැලසුම් කළමණාකරු රේඛීය සැකැස්ම නම් ඔබට භාවිතා කළ හැකිය:

DividerItemDecoration යනු ප්‍රතිචක්‍රීකරණ දසුනකි. රේඛීය සැකසුම් කළමණාකරුගේ අයිතම අතර බෙදුම්කරුවෙකු ලෙස භාවිතා කළ හැකිය. එය HORIZONTAL සහ VERTICAL දිශානතියට සහාය දක්වයි.

 mDividerItemDecoration = new DividerItemDecoration(recyclerView.getContext(),
         mLayoutManager.getOrientation());
 recyclerView.addItemDecoration(mDividerItemDecoration);

මූලාශ්රය


4

ඔබට අයිතම සඳහා එකම ඉඩක් එක් කිරීමට අවශ්‍ය නම්, සරලම ක්‍රමය වන්නේ ප්‍රතිචක්‍රීකරණ දර්ශනය සඳහා ඉහළ + වම් පෑඩින් සහ කාඩ් අයිතම සඳහා දකුණු + පහළ ආන්තිකය එකතු කිරීමයි.

මාන. xml

<resources>
    <dimen name="divider">1dp</dimen>
</resources>

list_item.xml

<CardView
 android:layout_marginBottom="@dimen/divider"
 android:layout_marginRight="@dimen/divider">
 ...
</CardView>

list.xml

<RecyclerView
 android:paddingLeft="@dimen/divider"
 android:paddingTop="@dimen/divider"
/>

4

මම පහත වැනි ලැයිස්තු අයිතමයේ පේළියක් එක් කර ඇත්තෙමි

<View
android:id="@+id/divider"
android:layout_width="match_parent"
android:layout_height="1px"
android:background="@color/dividerColor"/>

1px තුනී රේඛාව අඳිනු ඇත.

ඔබට අවසාන පේළිය සඳහා බෙදුම්කරු සැඟවීමට අවශ්‍ය නම් divider.setVisiblity(View.GONE);අවසාන ලැයිස්තු අයිතමය සඳහා onBindViewHolder හි.


1
මම මේකට කැමතියි, අනෙක් අය ඉතා සංකීර්ණයි.
සෑම් චෙන්

4

1. එක් මාර්ගයක් වන්නේ කාඩ් දර්ශනය සහ ප්‍රතිචක්‍රීකරණ දර්ශනය එකට භාවිතා කිරීමෙන් අපට පහසුවෙන් බෙදුම්කරු වැනි බලපෑමක් එක් කළ හැකිය. උදා. https://developer.android.com/training/material/lists-cards.html

2. සහ අනෙක් කාරණය වන්නේ ප්‍රතිචක්‍රීකරණ දර්ශනයේ list_item_layout වෙත බෙදුම්කරු ලෙස දර්ශනය එකතු කිරීමයි .

        <View
            android:id="@+id/view1"
            android:layout_width="match_parent"
            android:layout_height="1dp"
            android:background="@color/colorAccent" />

3

මෙම RecyclerViewසිට ටිකක් වෙනස් වේ ListView. ඇත්ත වශයෙන්ම, ඒ RecyclerViewසඳහා ListViewසමාන ව්යුහයක් අවශ්ය වේ. උදාහරණයක් ලෙස, අ LinearLayout. මෙම LinearLayoutබෙදීම සඳහා එක් එක් මූලද්රව්යය සඳහා පරාමිතීන්. පහත කේතයේ මා සතුව ඇත්තේ “පෑඩින්” සහිත වස්තුවක් වන RecyclerViewඅතර එමඟින් CardViewඅයිතම LinearLayoutඅතර යම් ඉඩක් ලැබේ. එම අවකාශය සැබවින්ම කුඩා කර ඔබට රේඛාවක් ලැබෙනු ඇත.

මෙන්න ප්‍රතිචක්‍රීකරණ දර්ශනය recyclerview_layout.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
    android:layout_height="match_parent" android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    android:paddingBottom="@dimen/activity_vertical_margin" tools:context=".ToDoList">

    <!-- A RecyclerView with some commonly used attributes -->
    <android.support.v7.widget.RecyclerView
        android:id="@+id/todo_recycler_view"
        android:scrollbars="vertical"
        android:layout_width="match_parent"
        android:layout_height="match_parent"/>

</RelativeLayout>

මෙන්න සෑම අයිතමයක්ම පෙනෙන්නේ (සහ එය ඇන්ඩ්‍රොයිඩ් නිසා බෙදී ඇති බව පෙන්නුම් කරයි: සෑම දෙයක්ම වටා ඇති රේඛීය සැකැස්මෙහි පැඩින් කිරීම.) වෙනත් ගොනුවක: cards_layout.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="match_parent"
    android:layout_height="match_parent"
    **android:padding="@dimen/activity_vertical_margin"**>
    <!-- A CardView that contains a TextView -->
    <android.support.v7.widget.CardView
        xmlns:card_view="http://schemas.android.com/apk/res-auto"
        android:id="@+id/card_view"
        android:layout_gravity="center"
        android:layout_width="match_parent"
        android:layout_height="100dp"
        android:elevation="30dp"
        card_view:cardElevation="3dp">
            <TextView
                android:id="@+id/info_text"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                />
    </android.support.v7.widget.CardView>
</LinearLayout>

3

ඇත්තෙන්ම පහසු විසඳුමක් වන්නේ ප්‍රතිචක්‍රීකරණ-නම්‍යශීලී ඩිවයිඩර් භාවිතා කිරීමයි

යැපීම එක් කරන්න:

compile 'com.yqritc:recyclerview-flexibledivider:1.4.0'

ඔබේ ප්‍රතිචක්‍රීකරණ දර්ශනයට එක් කරන්න:

recyclerView.addItemDecoration(new HorizontalDividerItemDecoration.Builder(context).build());

ඔබ ඉවරයි!


චමත්කාරජනක ලෙස ක්‍රියා කරයි ... විවෘත මූලාශ්‍ර බෙදා ගැනීමට ආදරය කළ යුතුය.
බිලී

3
public class CommonItemSpaceDecoration extends RecyclerView.ItemDecoration {

        private int mSpace = 0;
        private boolean mVerticalOrientation = true;

    public CommonItemSpaceDecoration(int space) {
        this.mSpace = space;
    }

    public CommonItemSpaceDecoration(int space, boolean verticalOrientation) {
        this.mSpace = space;
        this.mVerticalOrientation = verticalOrientation;
    }

    @Override
    public void getItemOffsets(Rect outRect, View view, RecyclerView parent, RecyclerView.State state) {
        outRect.top = SizeUtils.dp2px(view.getContext(), mSpace);
        if (mVerticalOrientation) {
            if (parent.getChildAdapterPosition(view) == 0) {
                outRect.set(0, SizeUtils.dp2px(view.getContext(), mSpace), 0, SizeUtils.dp2px(view.getContext(), mSpace));
            } else {
                outRect.set(0, 0, 0, SizeUtils.dp2px(view.getContext(), mSpace));
            }
        } else {
            if (parent.getChildAdapterPosition(view) == 0) {
                outRect.set(SizeUtils.dp2px(view.getContext(), mSpace), 0, 0, 0);
            } else {
                outRect.set(SizeUtils.dp2px(view.getContext(), mSpace), 0, SizeUtils.dp2px(view.getContext(), mSpace), 0);
            }
        }
    }
}

මෙය සෑම අයිතමයකම ඉහළ සහ පහළ (හෝ වමේ සහ දකුණේ) අවකාශය එක් කරයි .එවිට ඔබට එය ඔබට සැකසිය හැකිය recyclerView.

recyclerView.addItemDecoration(new CommonItemSpaceDecoration(16));

SizeUtils.java

public class SizeUtils {
    public static int dp2px(Context context, float dpValue) {
        final float scale = context.getResources().getDisplayMetrics().density;
        return (int) (dpValue * scale + 0.5f);
    }
}
By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.