自定义ListView中实现多列和可变大小的元素:AsymmetricGridView

jopen 10年前

一个Android自定义ListView中实现多列和可变大小的元素。

Screenshots:

screenshot 1screenshot 2screenshot 3screenshot 4

Usage

In your build.gradle file:

repositories {      maven { url 'https://github.com/felipecsl/m2repository/raw/master/' }      // ...  }    dependencies {      // ...      compile 'com.felipecsl:asymmetricgridview:1.0.+'  }

In your layout xml:

<com.felipecsl.asymmetricgridview.library.widget.AsymmetricGridView      xmlns:android="http://schemas.android.com/apk/res/android"      android:id="@+id/listView"      android:layout_width="match_parent"      android:layout_height="match_parent"/>

In your activity class:

@Override  protected void onCreate(Bundle savedInstanceState) {      super.onCreate(savedInstanceState);      setContentView(R.layout.activity_main);      listView = (AsymmetricGridView) findViewById(R.id.listView);        // Choose your own preferred column width      listView.setRequestedColumnWidth(Utils.dpToPx(this, 120));      final List<AsymmetricItem> items = new ArrayList<>();        // initialize your items array      adapter = new ListAdapter(this, listView, items);        listView.setAdapter(adapter);  }

Supports resetting and appending more elements into the adapter:

// Will append more items at the end of the adapter.  listView.getAdapter().appendItems(moreItems);    // resetting the adapter items. Will clear the adapter  // and add the new items.  listView.getAdapter().setItems(items);

Toggle to enable/disable reordering of elements to better fill the grid

// Setting to true will move items up and down to better use the space  // Defaults to false.  listView.setAllowReordering(true);    listView.isAllowReordering(); // true

Works with Android 2.3.x and above.

项目主页:http://www.open-open.com/lib/view/home/1413857075528