TwoWay GridView library doesn't work properly.

2019-06-03 10:40发布

问题:

I was trying to achieve the following task: Setting an horizontal GridView of images instead of the normal vertical GridView.
I found out that most StackOverflow answers were targetting this library:
https://github.com/jess-anders/two-way-gridview

I first ran the sample project and it resulted in the following:

Here's the xml File of the TwoWayGridView layout:

 <?xml version="1.0" encoding="utf-8"?>
<com.jess.ui.TwoWayGridView
    xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:background="#E8E8E8"
    android:id="@+id/gridview"
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent"
    app:cacheColorHint="#E8E8E8"
    app:columnWidth="80dp"
    app:rowHeight="80dp"
    app:numColumns="auto_fit"
    app:numRows="auto_fit"
    app:verticalSpacing="16dp"
    app:horizontalSpacing="16dp"
    app:stretchMode="spacingWidthUniform"
    app:scrollDirectionPortrait="vertical"
    app:scrollDirectionLandscape="horizontal"
    app:gravity="center"/>

I also tried to link this library into my project, but the results are the same. Each image takes the whole row and the scrolling is vertical instead of horizontal.

What could be the reason for that problem? (Note: I haven't changed anything in the sample project).

EDIT: Here's a ScreenShot when importing the project:

回答1:

Make sure you are using my version of it and not one of the forked versions. I just downloaded a fresh copy from github and it works fine.



回答2:

If you are having problems getting the spacing correct using the values in xml, you could try looking at the following tutorial:

http://spragucm.wordpress.com/2013/11/17/android-horizontal-and-vertical-gridview-tutorial/

I specifically wrote it because the two-way-gridview items wouldn't space evenly and they wouldn't fill the row/column. The example code in my tutorial lets you set column and row number and everything else is done for you so that the child fills a row/column with some padding between items.



回答3:

  app:stretchMode="columnWidth"
  app:scrollDirectionPortrait="horizontal"
  app:scrollDirectionLandscape="horizontal"