In this case, the GridView is used to implement a grid similar to the following. Only two items are displayed in each row:
650) this. width = 650; "src =" http://www.bkjia.com/uploads/allimg/131228/1055311053-0.png "title =" 001.png"/>
For the symmetry of the layout, some layout parameters are dynamically set here, such as padding:
Public class KnowledgeFragment extends Fragment {private static final String TAG = "KnowledgeFragment"; private int tileWidthPx; // The width of each tile private int tileHeightPx; // The height of each tile private int gvSpacingPx; // interval between tile @ Override public void onCreate (Bundle savedInstanceState) {Log. v (TAG, "onCreate"); super. onCreate (savedInstanceState); int screenWidthDp = DensityUtil. px2dip (getActivity (), D IsplayUtil. getScreenWidth (getActivity (); int gvSpacingDp = (int) getResources (). getDimension (R. dimen. knowledge_first_cat_space); int tileSideDp = (screenWidthDp-gvSpacingDp * 3)/2; // dp to convert px gvSpacingPx = DensityUtil. dip2px (getActivity (), gvSpacingDp); tileWidthPx = DensityUtil. dip2px (getActivity (), tileSideDp); // here, set tile height to 2/3 tileHeightPx = (int) (tileWidthPx * (2.0/3);} @ Overr Ide public View onCreateView (LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {Log. v (TAG, "onCreateView"); View v = inflater. inflate (R. layout. knowledge, null); assert v! = Null; GridView gvFirstCat = (GridView) v. findViewById (R. id. knowledge_gv); // sets the layout parameter gvFirstCat of the gridview. setPadding (gvSpacingPx, 0); gvFirstCat. setHorizontalSpacing (gvSpacingPx); gvFirstCat. setVerticalSpacing (gvSpacingPx); gvFirstCat. setAdapter (new FirstCatAdapter (getActivity (); gvFirstCat. setOnItemClickListener (new AdapterView. onItemClickListener () {@ Override public Void onItemClick (AdapterView <?> Parent, View view, int position, long id) {Log. v (TAG, "click" + position) ;}}); return v ;}
GridView is also derived from AbsListView. The layout of each list item and the corresponding adapter are also provided here, as follows:
Public class FirstCatAdapter extends BaseAdapter {private Context mContext; private LayoutInflater mInflater; private KnowledgeCat [] mCats; public FirstCatAdapter (Context context) {mContext = context; mInflater = getActivity (). getLayoutInflater (); Resources res = mContext. getResources (); int [] catsId = res. getIntArray (R. array. first_cats_id); String [] catsName = res. getStringArray (R. array. first_cat S_name); mCats = new KnowledgeCat [catsName. length]; TypedArray taDrawables = res. obtainTypedArray (R. array. first_cats_icon); for (int idx = 0; idx <taDrawables. length (); ++ idx) {mCats [idx] = new KnowledgeCat (); mCats [idx]. setCatName (catsName [idx]); mCats [idx]. setCatIcon (taDrawables. getDrawable (idx); mCats [idx]. setCatId (catsId [idx]) ;}@ Override public int getCount () {return mCats. length;} @ O Verride public Object getItem (int position) {return mCats [position] ;}@ Override public long getItemId (int position) {return position ;}@ Override public View getView (int position, view convertView, ViewGroup parent) {if (convertView = null) {convertView = mInflater. inflate (R. layout. cat_tile, null);} KnowledgeCat kc = (KnowledgeCat) getItem (position); assert convertView! = Null; AbsListView. layoutParams lp; lp = new AbsListView. layoutParams (ViewGroup. layoutParams. MATCH_PARENT, tileHeightPx, 0); // set the tile height to convertView. setLayoutParams (lp); ImageView ivIcon = (ImageView) convertView. findViewById (android. r. id. icon); ivIcon. setImageDrawable (kc. getCatIcon (); TextView tvTitle = (TextView) convertView. findViewById (android. r. id. title); tvTitle. setText (kc. getCatName (); return convertView ;}}
Layout file knowledge. xml:
<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical"> <GridView android:id="@id/knowledge_gv" android:layout_width="match_parent" android:layout_height="match_parent" android:drawSelectorOnTop="false" android:scrollbarStyle="outsideOverlay" android:scrollingCache="true" android:cacheColorHint="@android:color/transparent" android:listSelector="@android:color/transparent" android:numColumns="auto_fit" android:stretchMode="columnWidth" android:gravity="center"/></LinearLayout>
The layout file cat_tile.xml of each item
<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:id="@id/k_cat_tile_layout" android:orientation="vertical" android:layout_width="wrap_content" android:layout_height="wrap_content" android:gravity="center" android:background="@drawable/k_cat_tile_bg"> <ImageView android:id="@android:id/icon" android:layout_width="wrap_content" android:layout_height="wrap_content" android:scaleType="centerInside" android:contentDescription="@string/default_string"/> <TextView android:id="@android:id/title" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginTop="4.0dip" android:textSize="@dimen/SmallTextSize"/></LinearLayout>
This article is from the "quiet Madman" blog, please be sure to keep this source http://quietmadman.blog.51cto.com/3269500/1282216