2017-10-04 172 views
0

大家好,我很新的android和我有一個recyclerview的問題。我試圖在回收站視圖中的圖像視圖之間添加空間,但我不成功。android recyclerView水平和垂直間距

我想要什麼

enter image description here

發生了什麼

enter image description here

下面是我實現 ItemOffsetDecoration.java

public class ItemOffsetDecoration extends RecyclerView.ItemDecoration { 
    private int itemOffset; 

    public ItemOffsetDecoration(int itemOffset) { 
     itemOffset = itemOffset; 
    } 

    public ItemOffsetDecoration(@NonNull Context context, @DimenRes int itemOffsetId) { 
     this(context.getResources().getDimensionPixelSize(itemOffsetId)); 
    } 

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

shopping.xml

<?xml version="1.0" encoding="utf-8"?> 
<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:background="@drawable/shopping_bg" 
    tools:context=".activities.HomepageActivity$PlaceholderFragment"> 
     <android.support.v7.widget.RecyclerView 
      android:id="@+id/shoppingRV" 
      android:layout_width="match_parent" 
      android:layout_height="match_parent" 
      android:clipToPadding="false" 
      android:padding="@dimen/grid_horizontal_spacing"/> 
</RelativeLayout> 

Shopping.java

public class Shopping extends Fragment implements InstaPukkeiRecyclerViewAdapter.ItemClickListener { 
    InstaPukkeiRecyclerViewAdapter adapter; 
    @Override 
    public View onCreateView(LayoutInflater inflater, ViewGroup container, 
          Bundle savedInstanceState) { 
     View rootView = inflater.inflate(R.layout.tab_shopping, container, false); 
     processRV(rootView); 
     return rootView; 
    } 
    private void processRV(View layout) { 
     RecyclerView recyclerView = (RecyclerView) layout.findViewById(R.id.shoppingRV); 
     int noOfColumns = 3; 
     recyclerView.setLayoutManager(new GridLayoutManager(getContext(), noOfColumns)); 
     adapter = new InstaPukkeiRecyclerViewAdapter(getContext(), LogoIds.SHOPPING_SITE_LOGOS); 
     adapter.setClickListener(this); 
     recyclerView.setAdapter(adapter); 
     recyclerView.addItemDecoration(new ItemOffsetDecoration(getContext(), R.dimen.grid_horizontal_spacing)); 
    } 
} 

請幫我在這裏。提前致謝。

編輯 item_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"> 

    <ImageView 
     android:id="@+id/logoImageView" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:scaleType="fitXY" 
     android:adjustViewBounds="true" /> 
</LinearLayout> 
+2

份額烏爾項目佈局 –

+0

在您的項目佈局文件'tab_shopping.xml'儘量設置一些容限屬性到根佈局 –

+0

嘗試使用線性layouy與水平方向,而不是相對 –

回答

1
<?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="wrap_content"> 

<ImageView 
    android:marginLeft="18dp" 
    android:marginRight="18dp" 
    android:id="@+id/logoImageView" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:scaleType="fitXY" 
    android:adjustViewBounds="true" /> 
    </LinearLayout> 

與此更改您的項目佈局一個在你的項目的XML中。你讓家長(的LinearLayout)高度匹配父,這就是爲什麼你所得到的垂直全屏幕空間

+0

這工作。非常感謝 – MrG

0

使用wrap_content在RecyclerView也行layout.And還增加利潤

<android.support.v7.widget.RecyclerView 
      android:id="@+id/shoppingRV" 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:clipToPadding="false" 
      android:padding="@dimen/grid_horizontal_spacing"/> 
0

嗯,你可以添加保證金您image view或使用捫設置高度和寬度是這樣的

android:layout_width="@dimen/dp_130" 
android:layout_height="@dimen/dp_120" 

<ImageView 
    android:id="@+id/logoImageView" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:scaleType="fitXY" 
    android:layout_margin="@dimen/dp_15" 

    android:adjustViewBounds="true" />