Last active
December 14, 2015 14:08
-
-
Save rajivnarayana/5098090 to your computer and use it in GitHub Desktop.
Demonstrates MyGridView a custom GridView that can be placed inside a Scrollview. Used in ResolutionTweet android app.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android" | |
android:layout_width="match_parent" | |
android:layout_height="match_parent" > | |
<LinearLayout | |
android:layout_width="match_parent" | |
android:layout_height="wrap_content" | |
android:orientation="vertical" > | |
<TextView | |
android:layout_width="wrap_content" | |
android:layout_height="200dp" | |
android:background="@android:color/darker_gray" | |
android:gravity="center" | |
android:text="@string/hello_world" /> | |
<com.webileapps.hellogridview.MyGridView | |
android:id="@+id/grid" | |
android:layout_width="wrap_content" | |
android:layout_height="wrap_content" | |
android:numColumns="7" /> | |
</LinearLayout> | |
</ScrollView> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class MainActivity extends Activity { | |
@Override | |
protected void onCreate(Bundle savedInstanceState) { | |
super.onCreate(savedInstanceState); | |
setContentView(R.layout.activity_main); | |
GridView gridView = (GridView) findViewById(R.id.grid); | |
gridView.setAdapter(new MyArrayAdapter()); | |
} | |
private class MyArrayAdapter extends ArrayAdapter<String> { | |
MyArrayAdapter() { | |
super(MainActivity.this, android.R.layout.simple_list_item_1, android.R.id.text1); | |
} | |
@Override | |
public int getCount() { | |
return 20; | |
} | |
@Override | |
public String getItem(int position) { | |
return "Cell "+position; | |
} | |
} | |
@Override | |
public boolean onCreateOptionsMenu(Menu menu) { | |
// Inflate the menu; this adds items to the action bar if it is present. | |
getMenuInflater().inflate(R.menu.main, menu); | |
return true; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.webileapps.hellogridview; | |
import android.content.Context; | |
import android.util.AttributeSet; | |
import android.widget.GridView; | |
public class MyGridView extends GridView { | |
public MyGridView(Context context, AttributeSet attrs) { | |
super(context, attrs); | |
} | |
@Override | |
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { | |
super.onMeasure(widthMeasureSpec, heightMeasureSpec); | |
measureChildren(widthMeasureSpec, heightMeasureSpec); | |
if(getChildCount() == 0) { | |
super.onMeasure(widthMeasureSpec, heightMeasureSpec); | |
return; | |
} | |
else { | |
//I know my adapter has 7 columns. getNumColumns() works from API level 11. | |
int heightOfContent = ((int) Math.ceil(getAdapter().getCount()/7.0)) * getChildAt(0).getMeasuredHeight(); | |
setMeasuredDimension(getMeasuredWidth(), Math.max(getMeasuredHeight(), heightOfContent)); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment