Добавление разделителя между строками GridView

Я создал таблицу данных с помощью GridView и хотел бы применить горизонтальный разделитель под каждой строкой, как показано в рекомендациях по дизайну материалов Google. Однако я не могу найти никакой информации о его реализации. Есть идеи?

введите здесь описание изображения


person Luke Allison    schedule 30.05.2016    source источник
comment
stackoverflow.com/questions/7132030/   -  person Damini Mehra    schedule 30.05.2016


Ответы (3)


Попробуйте задать цвет фона для GridView следующим образом.

<GridView
    android:id="@+id/gridView1"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="#e5e5e5"
    android:horizontalSpacing="1dp"
    android:numColumns="auto_fit"
    android:stretchMode="columnWidth"
    android:verticalSpacing="1dp" >

Укажите размер horizontalSpacing и verticalSpacing в dp и укажите цвет фона, который вы хотите использовать в качестве разделителя, например Grey.

person Bhoomit_BB    schedule 30.05.2016
comment
Это просто делает весь gridView цветом, указанным в качестве фона. - person Luke Allison; 31.05.2016

Используйте атрибут android:horizontalSpacing GridView.

person earthw0rmjim    schedule 30.05.2016

Просто создайте собственное представление сетки, подобное этому

package net.nightwhistler.pageturner.view;

import net.nightwhistler.pageturner.R;
import net.nightwhistler.pageturner.library.LibraryBook;
import net.nightwhistler.pageturner.library.QueryResult;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Rect;
import android.util.AttributeSet;
import android.view.KeyEvent;
import android.widget.GridView;

public class BookCaseView extends GridView {

private Bitmap background;

private int mShelfWidth;
private int mShelfHeight;   

private QueryResult<LibraryBook> result;    

private LibraryBook selectedBook;   

public BookCaseView(Context context, AttributeSet attributes) {
    super(context, attributes);

    this.setFocusableInTouchMode(true);
    this.setClickable(false);

    final Bitmap shelfBackground = BitmapFactory.decodeResource(context.getResources(),
            R.drawable.shelf_single);
    setBackground(shelfBackground);
    this.setFocusable(true);
}

public void setBackground(Bitmap background) {
    this.background = background;

    mShelfWidth = background.getWidth();
    mShelfHeight = background.getHeight();
}   

protected void onClick( int bookIndex ) {
    LibraryBook book = this.result.getItemAt(bookIndex);

    this.selectedBook = book;
    invalidate();
}

@Override
protected void dispatchDraw(Canvas canvas) {
    final int count = getChildCount();
    final int top = count > 0 ? getChildAt(0).getTop() : 0;
    final int shelfWidth = mShelfWidth;
    final int shelfHeight = mShelfHeight;
    final int width = getWidth();
    final int height = getHeight();
    final Bitmap background = this.background;

    for (int x = 0; x < width; x += shelfWidth) {
        for (int y = top; y < height; y += shelfHeight) {
            canvas.drawBitmap(background, x, y, null);
        }

        //This draws the top pixels of the shelf above the current one

        Rect source = new Rect(0, mShelfHeight - top, mShelfWidth, mShelfHeight);
        Rect dest = new Rect(x, 0, x + mShelfWidth, top );              

        canvas.drawBitmap(background, source, dest, null);            
    }        


    super.dispatchDraw(canvas);
}

@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
    if ( keyCode == KeyEvent.KEYCODE_BACK && this.selectedBook != null ) {
        this.selectedBook = null;
        invalidate();
        return true;
    }

    return false;
}   

}

Я получил его ответ от ее фоновой прокрутки с элементом в gridview

person Mohamed Atef    schedule 30.05.2016