How to give space between gridview in through java

ぐ巨炮叔叔 提交于 2019-12-20 04:37:26

问题


How do i get perfect aligning between the gridView items, i have added the gridview using java and it's base adapter is set in which i added the image and textview.

Xml Code:

    <android.support.v7.widget.CardView 
        android:layout_width="180dp"
        android:layout_height="200dp"
        android:layout_margin="5dp"
        android:orientation="vertical"
        app:cardCornerRadius="5dp"
        app:cardElevation="5dp">

 <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:orientation="vertical">

            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:layout_weight="1"
                android:orientation="vertical">

                <ImageView
                    android:id="@+id/ImageWomenItem"
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:clickable="true"
                    android:scaleType="fitXY"
                    android:src="@drawable/womenone" />
            </LinearLayout>


                <TextView
                    android:id="@+id/itemName"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="Red Top"
                    android:textColor="#8e8d8d"
                    android:textSize="15dp" />

        </LinearLayout>

    </android.support.v7.widget.CardView>

This is Activity code in which i declared the recylerview and set it's adapter

Java code:

recyclerView = (RecyclerView) view.findViewById(R.id.recyclerViewClothes);
        recyclerView.setHasFixedSize(true);
             mLayoutManager = new GridLayoutManager(getActivity(), 2);
        recyclerView.setLayoutManager(mLayoutManager);

        mAdapter = new GridAdapter();
        recyclerView.setAdapter(mAdapter);

回答1:


Add padding to your root layout of adapter xml.

<LinearLayout     
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:padding="8dp">

<android.support.v7.widget.CardView
    app:cardCornerRadius="5dp"
    app:cardElevation="5dp"
    android:layout_width="180dp"
    android:layout_height="200dp"
    android:layout_margin="5dp"
    android:orientation="vertical">.......
</android.support.v7.widget.CardView>

 </LinearLayout>



回答2:


Use Space Decoration Class For That

public class SpacesItemDecoration extends RecyclerView.ItemDecoration {
    private int space;

    public SpacesItemDecoration(int space) {
        this.space = space;
    }

    @Override
    public void getItemOffsets(Rect outRect, View view,
                               RecyclerView parent, RecyclerView.State state) {
        outRect.left = space;
        outRect.right = space;
        outRect.bottom = space;

        // Add top margin only for the first item to avoid double space between items
        if (parent.getChildLayoutPosition(view) == 0) {
            outRect.top = space;
        } else {
            outRect.top = 0;
        }
    }
}

Your Activity Where You Set Your Adapter

int spacing = getResources().getDimensionPixelSize(R.dimen.spacing);

  recyclerview.addItemDecoration(new SpacesItemDecoration(spacing));

Specify in dimen.xml

 <dimen name="spacing">5px</dimen>

Xml Code:

    <android.support.v7.widget.CardView 
        android:layout_width="175dp"
        android:layout_height="200dp"
        android:orientation="vertical"
        app:cardCornerRadius="5dp"
        app:cardElevation="5dp">

 <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:orientation="vertical">

            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:layout_weight="1"
                android:orientation="vertical">

                <ImageView
                    android:id="@+id/ImageWomenItem"
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:clickable="true"
                    android:scaleType="fitXY"
                    android:src="@drawable/womenone" />
            </LinearLayout>


                <TextView
                    android:id="@+id/itemName"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="Red Top"
                    android:textColor="#8e8d8d"
                    android:textSize="15dp" />

        </LinearLayout>

    </android.support.v7.widget.CardView>



回答3:


Try this,

    recyclerView  = (RecyclerView) findViewById(R.id.recyclerView );
    recyclerView .setLayoutManager(new GridLayoutManager(getActivity(), 2));
    int spanCount = 2; 
    int spacing_left = 10; 
    int spacing_top=15;

    recyclerView.addItemDecoration(new GridSpacingItemDecoration(spanCount, spacing_left, spacing_top));

GridSpacingItemDecoration:

    /* set spacing for grid view */
    public class GridSpacingItemDecoration extends RecyclerView.ItemDecoration {
        final private int spanCount, spacing, spacing_top;
        final private boolean includeEdge;

        public GridSpacingItemDecoration(int spanCount, int spacing_left, int spacing_top) {
            this.spanCount = spanCount;
            this.spacing = spacing_left;
            this.includeEdge = true;
            this.spacing_top = spacing_top;
        }

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

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

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


来源:https://stackoverflow.com/questions/44543234/how-to-give-space-between-gridview-in-through-java

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!