2017-10-20 58 views
1

我正在開發一個網格cardview使用recyclerview和卡片視圖在android v25,面對Android回收視圖的行爲有一個分隔線,充當兩行之間的分隔符,如下面的圖片如何以編程方式或使用xml屬性刪除Recycler分隔符/分隔線

Screen shot from emulator

我想刪除這條線,我附上我的佈局XML,並在這裏

的Java代碼MainActivity.java

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 
    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar); 
    setSupportActionBar(toolbar); 

    recyclerView = (RecyclerView) findViewById(R.id.recycler_view); 
    categoriesAdapter = new RecyclerViewAdapter(getApplicationContext(),getAllItemList()); 

    recyclerView.setHasFixedSize(true); 

    RecyclerView.LayoutManager mLayoutManager = new LinearLayoutManager(getApplicationContext()); 
    // set a GridLayoutManager with default vertical orientation and 3 number of columns 
    GridLayoutManager gridLayoutManager = new GridLayoutManager(getApplicationContext(),3); 
    recyclerView.setLayoutManager(gridLayoutManager); 
    recyclerView.addItemDecoration(new DividerItemDecoration(this, LinearLayoutManager.VERTICAL)); 
    recyclerView.setItemAnimator(new DefaultItemAnimator()); 
    recyclerView.setAdapter(categoriesAdapter); 

    getAllItemList(); 
} 

main_card_grid.xml

<android.support.v7.widget.CardView 
xmlns:card_view="http://schemas.android.com/apk/res-auto" 
xmlns:android="http://schemas.android.com/apk/res/android" 
android:id="@+id/card_view" 
android:layout_width="120dp" 
android:layout_height="wrap_content" 
card_view:cardUseCompatPadding="true" 
card_view:cardCornerRadius="2dp" 
android:layout_marginBottom="16dp"> 

<RelativeLayout 
    android:layout_width="match_parent" 
    android:layout_height="match_parent"> 

    <ImageView 
     android:id="@+id/country_photo" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 

     android:contentDescription="@string/action_settings" 
     android:src="@drawable/eight" 
     android:scaleType="centerCrop" /> 

    <TextView 
     android:id="@+id/country_name" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:textSize="13sp" 
     android:text="@string/country_name" 
     android:textColor="@color/textColor" 
     android:gravity="center" 
     android:layout_below="@+id/country_photo" 
     android:paddingBottom="4dp" 
     android:paddingTop="4dp" 
     android:layout_alignParentBottom="true" 
     android:background="@color/white"/> 

</RelativeLayout> 

content_main.xml

<?xml version="1.0" encoding="utf-8"?> 
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:app="http://schemas.android.com/apk/res-auto" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    app:layout_behavior="@string/appbar_scrolling_view_behavior" 
    tools:context="groups.wtz.bluewine.com.miyo.MainActivity" 
    tools:showIn="@layout/app_bar_main"> 

    <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" 
     android:background="@color/white" 
     android:orientation="vertical"> 

     <RelativeLayout 
      android:layout_width="match_parent" 
      android:layout_height="match_parent" 
      android:padding="4dp" 
      tools:context=".MainActivity"> 

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

     </RelativeLayout> 

    </LinearLayout> 

</android.support.constraint.ConstraintLayout> 

哪個XML的部分製備本下劃線,我可以更新它

+1

刪除recyclerview itemDecorator –

+0

@AswinPAshok謝謝你的工作 –

回答

0

下線製作麻煩。因爲你正在爲你的物品添加裝飾。這將添加分隔符到您的視圖。只需在下面註釋即可。

recyclerView.addItemDecoration(new DividerItemDecoration(this, LinearLayoutManager.VERTICAL)); 

你可以提供你的DividerItemDecoration.java類嗎?

0

你應該註釋行:

recyclerView.addItemDecoration(new DividerItemDecoration(this, LinearLayoutManager.VERTICAL)); 

你可以參考我的代碼:

RecyclerView.LayoutManager mLayoutManager = new GridLayoutManager(getApplicationContext(), 3); 
    listEbook.setLayoutManager(mLayoutManager); 
    listEbook.addItemDecoration(new GridSpacingItemDecoration(3, dpToPx(3), true)); 
    listEbook.setItemAnimator(new DefaultItemAnimator()); 
    mAdapter = new EbookAdapter(listEbook, lisBooks, DanhsachEbookActivity.this); 
    listEbook.setAdapter(mAdapter); 

/** 
* RecyclerView item decoration - give equal margin around grid item 
*/ 
public class GridSpacingItemDecoration extends RecyclerView.ItemDecoration { 

    private int spanCount; 
    private int spacing; 
    private boolean includeEdge; 

    public GridSpacingItemDecoration(int spanCount, int spacing, boolean includeEdge) { 
     this.spanCount = spanCount; 
     this.spacing = spacing; 
     this.includeEdge = includeEdge; 
    } 

    @Override 
    public void getItemOffsets(Rect outRect, View view, RecyclerView parent, RecyclerView.State state) { 
     int position = parent.getChildAdapterPosition(view); // item position 
     int column = position % spanCount; // item column 

     if (includeEdge) { 
      outRect.left = spacing - column * spacing/spanCount; // spacing - column * ((1f/spanCount) * spacing) 
      outRect.right = (column + 1) * spacing/spanCount; // (column + 1) * ((1f/spanCount) * spacing) 

      if (position < spanCount) { // top edge 
       outRect.top = spacing; 
      } 
      outRect.bottom = spacing; // item bottom 
     } else { 
      outRect.left = column * spacing/spanCount; // column * ((1f/spanCount) * spacing) 
      outRect.right = spacing - (column + 1) * spacing/spanCount; // spacing - (column + 1) * ((1f/ spanCount) * spacing) 
      if (position >= spanCount) { 
       outRect.top = spacing; // item top 
      } 
     } 
    } 
} 

/** 
* Converting dp to pixel 
*/ 
private int dpToPx(int dp) { 
    Resources r = getResources(); 
    return Math.round(TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, dp, r.getDisplayMetrics())); 
} 

我希望它可以幫助您的問題!