2017-10-04 1 views
0

Hallo all ich bin sehr neu zu Android und ich habe ein Problem mit Recyclerview. Ich versuche, Platz zwischen den Bildansichten in einer Recyclerview hinzuzufügen, aber ich bin nicht erfolgreich.android recyclerView horizontalen und vertikalen Abstand

Was ich will

enter image description here

Was

enter image description here

unten geschieht, sind meine Implementierungen 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)); 
    } 
} 

Bitte helfen Sie mir hier. Danke im Voraus.

EDIT 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

share ur Artikel Layout –

+0

In Ihrem Artikel Layout-Datei 'tab_shopping.xml' versuchen, einige Marge Attribut an die Wurzel-Layout zu –

+0

versuchen Linear layouy mit horizontaler Ausrichtung zu verwenden, statt Relative –

Antwort

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> 

ändern Ihre Artikel Layout mit diesem eine in der XML-Datei Ihres Artikels. Sie machen die Eltern (Linearlayout) Höhe Match Elternteil, das ist, warum Sie vertikale Vollbildraum

+0

Das hat funktioniert. Danke vielmals – MrG

0

Verwenden wrap_content in RecyclerView und auch in Reihe layout.And auch Ränder hinzufügen

<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

hmmm Sie Rand Dimen zu Ihrem image view oder verwenden Sie hinzufügen können, werden immer Höhe einstellen und Breite so etwas wie dieses

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

oder

<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" /> 
Verwandte Themen