大家好,我很新的android和我有一個recyclerview的問題。我試圖在回收站視圖中的圖像視圖之間添加空間,但我不成功。android recyclerView水平和垂直間距
我想要什麼
發生了什麼
下面是我實現 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>
份額烏爾項目佈局 –
在您的項目佈局文件'tab_shopping.xml'儘量設置一些容限屬性到根佈局 –
嘗試使用線性layouy與水平方向,而不是相對 –