Skip to content

Commit

Permalink
Merge pull request #57 from jenzz/feature/DynamicColumns
Browse files Browse the repository at this point in the history
Merged pull request #57 into develop - Add support for setColumnCount(int, int) - Issue #26
  • Loading branch information
denizmveli committed Feb 22, 2014
2 parents d4d921b + c7882ed commit 1f5a8bd
Show file tree
Hide file tree
Showing 5 changed files with 71 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -229,7 +229,11 @@ public void onWindowFocusChanged(boolean hasWindowFocus) {

@Override
protected void onSizeChanged(int w, int h, int oldw, int oldh) {
if (getChildCount() > 0) {
onSizeChanged(w, h);
}

protected void onSizeChanged(int w, int h) {
if (getChildCount() > 0) {
mDataChanged = true;
rememberSyncState();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -208,6 +208,25 @@ public void setGridPadding(int left, int top, int right, int bottom) {
mGridPaddingRight = right;
mGridPaddingBottom = bottom;
}

public void setColumnCountPortrait(int columnCountPortrait) {
mColumnCountPortrait = columnCountPortrait;
onSizeChanged(getWidth(), getHeight());
requestLayoutChildren();
}

public void setColumnCountLandscape(int columnCountLandscape) {
mColumnCountLandscape = columnCountLandscape;
onSizeChanged(getWidth(), getHeight());
requestLayoutChildren();
}

public void setColumnCount(int columnCountPortrait, int columnCountLandscape) {
mColumnCountPortrait = columnCountPortrait;
mColumnCountLandscape = columnCountLandscape;
onSizeChanged(getWidth(), getHeight());
requestLayoutChildren();
}

// //////////////////////////////////////////////////////////////////////////////////////////
// MEASUREMENT
Expand Down Expand Up @@ -314,6 +333,14 @@ protected void onChildCreated(final int position, final boolean flowDown) {
setPositionIsHeaderFooter(position);
}
}

private void requestLayoutChildren() {
final int count = getChildCount();
for (int i = 0; i < count; i++) {
final View v = getChildAt(i);
if (v != null) v.requestLayout();
}
}

@Override
protected void layoutChildren() {
Expand Down Expand Up @@ -817,7 +844,13 @@ protected boolean hasSpaceUp() {
@Override
protected void onSizeChanged(final int w, final int h, final int oldw, final int oldh) {
super.onSizeChanged(w, h, oldw, oldh);
boolean isLandscape = w > h;
onSizeChanged(w, h);
}

@Override
protected void onSizeChanged(int w, int h) {
super.onSizeChanged(w, h);
boolean isLandscape = w > h;
int newColumnCount = isLandscape ? mColumnCountLandscape : mColumnCountPortrait;
if (mColumnCount != newColumnCount) {
mColumnCount = newColumnCount;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,11 +47,6 @@ public SampleAdapter(final Context context, final int textViewResourceId) {
mBackgroundColors.add(R.color.grey);
}

@Override
public void notifyDataSetChanged() {
super.notifyDataSetChanged();
}

@Override
public View getView(final int position, View convertView, final ViewGroup parent) {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.AbsListView;
import android.widget.AdapterView;
Expand Down Expand Up @@ -66,6 +68,28 @@ protected void onCreate(Bundle savedInstanceState) {
mGridView.setOnItemClickListener(this);
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.menu_sgv_dynamic, menu);
return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.col1:
mGridView.setColumnCount(1, 1);
break;
case R.id.col2:
mGridView.setColumnCount(2, 2);
break;
case R.id.col3:
mGridView.setColumnCount(3, 3);
break;
}
return true;
}

@Override
protected void onSaveInstanceState(final Bundle outState) {
super.onSaveInstanceState(outState);
Expand Down
8 changes: 8 additions & 0 deletions sample/src/main/res/menu/menu_sgv_dynamic.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">

<item android:id="@+id/col1" android:title="1 Column" android:showAsAction="always|withText" />
<item android:id="@+id/col2" android:title="2 Columns" android:showAsAction="always|withText" />
<item android:id="@+id/col3" android:title="3 Columns" android:showAsAction="always|withText" />

</menu>

0 comments on commit 1f5a8bd

Please sign in to comment.