android recyclerView水平和垂直间距

时间:2017-10-04 16:31:01

标签: android

大家好我对android很新,我遇到了recyclerview的问题。我试图在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>

3 个答案:

答案 0 :(得分:1)

{{1}}

使用项目的XML更改项目布局。您使父(LinearLayout)高度匹配父级,这就是您获得垂直全屏空间的原因

答案 1 :(得分:0)

在RecyclerView中以及行布局中使用wrap_content。还可以添加边距

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

答案 2 :(得分: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" />
相关问题