2017-06-30 21 views
6

Dodaję przekrój ukośny do RecyclerView, ale nie otrzymam oczekiwanego wyniku. mój drugi widok start with end of first view, and thats obvious. ale chcę, żeby każdy widok był taki.Tworzenie dynamicznej listy przekątnej widoku

Moja wyjściowa:

enter image description here

i to, co chciałem:

enter image description here

CutLayout.class:

public class CutLayout extends FrameLayout { 
    private Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG); 
    private Xfermode pdMode = new PorterDuffXfermode(PorterDuff.Mode.CLEAR); 
    private Path path = new Path(); 

    public CutLayout(Context context) { 
     super(context); 
    } 

    public CutLayout(Context context, AttributeSet attrs) { 
     super(context, attrs); 
    } 

    public CutLayout(Context context, AttributeSet attrs, int defStyleAttr) { 
     super(context, attrs, defStyleAttr); 
    } 

    @TargetApi(Build.VERSION_CODES.LOLLIPOP) 
    public CutLayout(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) { 
     super(context, attrs, defStyleAttr, defStyleRes); 
    } 

    @Override 
    protected void dispatchDraw(Canvas canvas) { 
     int saveCount = canvas.saveLayer(0, 0, getWidth(), getHeight(), null, Canvas.ALL_SAVE_FLAG); 
     super.dispatchDraw(canvas); 

     paint.setXfermode(pdMode); 
     path.reset(); 
     path.moveTo(0, getHeight()); 
     path.lineTo(getWidth(), getHeight()); 
     path.lineTo(getWidth(), getHeight() - TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 50, getResources().getDisplayMetrics())); 
     path.close(); 
     canvas.drawPath(path, paint); 

     canvas.restoreToCount(saveCount); 
     paint.setXfermode(null); 
    } 
} 

item.xml

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout 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" 
    xmlns:app="http://schemas.android.com/apk/res-auto" 
    android:orientation="vertical" 
    app:layout_behavior="@string/appbar_scrolling_view_behavior" 
    tools:context=".SampleActivity"> 

    <com.yarolegovich.slidingrootnav.sample.helper.CutLayout 
     android:layout_width="match_parent" 
     android:layout_height="200dp"> 

     <ImageView 
      android:scaleType="centerCrop" 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:src="@drawable/homebulding" /> 
    </com.yarolegovich.slidingrootnav.sample.helper.CutLayout> 
</LinearLayout> 
+0

https://stackoverflow.com/questions/39185299/color-overlay-on- drawable-android –

+0

Już to widziałem, to nie mój problem .. to co chcę to połączenie dynamicznego widoku .. –

+0

każde łącze jest już otwarte w mojej przeglądarce .., jestem również w stanie połączyć statycznie widok. ale jak dynamiczny? –

Odpowiedz

6

zmienić CutLayout mieć tę samą ścieżkę na górze tak:

public class CutLayout extends FrameLayout { 
    private Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG); 
    private Xfermode pdMode = new PorterDuffXfermode(PorterDuff.Mode.CLEAR); 
    private Path path = new Path(); 

    public CutLayout(Context context) { 
     super(context); 
    } 

    public CutLayout(Context context, AttributeSet attrs) { 
     super(context, attrs); 
    } 

    public CutLayout(Context context, AttributeSet attrs, int defStyleAttr) { 
     super(context, attrs, defStyleAttr); 
    } 

    @TargetApi(Build.VERSION_CODES.LOLLIPOP) 
    public CutLayout(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) { 
     super(context, attrs, defStyleAttr, defStyleRes); 
    } 

    @Override 
    protected void dispatchDraw(Canvas canvas) { 
     int saveCount = canvas.saveLayer(0, 0, getWidth(), getHeight(), null, Canvas.ALL_SAVE_FLAG); 
     super.dispatchDraw(canvas); 

     paint.setXfermode(pdMode); 
     path.reset(); 

     path.moveTo(0, 0); 
     path.lineTo(getWidth(), 0); 
     path.lineTo(0, TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 50, getResources().getDisplayMetrics())); 
     path.close(); 
     canvas.drawPath(path, paint); 

     path.reset(); 

     path.moveTo(0, getHeight()); 
     path.lineTo(getWidth(), getHeight()); 
     path.lineTo(getWidth(), getHeight() - TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 50, getResources().getDisplayMetrics())); 
     path.close(); 
     canvas.drawPath(path, paint); 

     canvas.restoreToCount(saveCount); 
     paint.setXfermode(null); 
    } 
} 

Następnie wszystko co musisz zrobić, to utworzyć dekorator artykuł na swojej Widok Recycler, który może przyjąć ujemny górny margines

public class NegativeTopItemDecorator extends RecyclerView.ItemDecoration{ 
    private final int mTopMargin; 

    public NegativeTopItemDecorator(int topMargin) { 
     this.mTopMargin = topMargin; 
    } 


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

i dodać go do swojej RecyclerView

int topMargin = - (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 50, getResources().getDisplayMetrics()); 
NegativeTopItemDecorator itemDecorator = new NegativeTopItemDecorator(topMargin); 
mRecyclerView.addItemDecoration(itemDecorator); 

to daje coś następnego wyjścia

sample diagonal cut recycler

+0

W moim przypadku na końcu karty zawsze jest biała spacja prawy dolny. każdy pomysł, jak to rozwiązać. –

+0

Czy zastosowałeś 'NegativeTopItemDecorator'? – linakis

+0

tak, to dlatego otrzymuję wynik podobny do twojego ... ale na ostatnim obrazie indeksu jest również pozostawić nagative space. –