Android Learning Tutorial Dynamic GridView Control use (6) _android

Source: Internet
Author: User

This example for you to share the Android dynamic GridView control the use of specific code for your reference, the specific content as follows

Mainactivity.java Code:

Package Siso.haha;

Import android.content.Intent;
Import android.support.v7.app.AppCompatActivity;
Import Android.os.Bundle;
Import Android.view.View;
Import Android.widget.Button;

public class Mainactivity extends Appcompatactivity {
 private Button btnstaggeredgridview;

 @Override
 protected void onCreate (Bundle savedinstancestate) {
 super.oncreate (savedinstancestate);
 Setcontentview (r.layout.activity_main);

 btnstaggeredgridview= (Button) Findviewbyid (R.id.btnstaggeredgridview);
 Btnstaggeredgridview.setonclicklistener (New View.onclicklistener () {
  @Override public
  void OnClick (View V ) {
  Intent Intent = new Intent ();
  Intent.setclass (mainactivity.this,staggeredgridviewactivity.class);
  Direct start of an activity
  startactivity (intent);}}
 )


 ;
}

Staggeredgridviewactivity.java Code:

Package Siso.haha;
Import android.app.Activity;
Import Android.content.Context;
Import Android.os.Bundle;
Import Android.util.Log;
Import Android.view.LayoutInflater;
Import Android.view.Menu;
Import Android.view.View;
Import Android.view.ViewGroup;

Import Android.widget.BaseAdapter;

Import Java.util.Random;
Import Viewhelper.staggeredgridview;

Import ViewHelper.StaggeredGridView.LayoutParams; public class Staggeredgridviewactivity extends activity {Private final static String TAG = Staggeredgridviewactivity.cla
 Ss.getsimplename ();
 Private Staggeredgridview MSGV;

 Private Sgvadapter Madapter; Private Staggeredgridview.onscrolllistener Mscrolllistener = new Staggeredgridview.onscrolllistener () {@Override Publ IC void onscrollstatechanged (viewgroup view, int scrollstate) {log.d (TAG, "[Onscrollstatechanged] scrollstate:" + Scrol
  Lstate);
   Switch (scrollstate) {case Scroll_state_idle:settitle ("Scroll_state_idle");

  Break Case Scroll_state_fling:settitle ("SCROll_state_fling ");

  Break
   Case Scroll_state_touch_scroll:settitle ("Scroll_state_touch_scroll");

  Break
  Default:break;
  @Override public void Onscroll (viewgroup view, int firstvisibleitem, int visibleitemcount, int totalitemcount) { LOG.D (TAG, "[Onscroll] Firstvisibleitem:" + Firstvisibleitem + "VisibleItemCount:" +visibleitemcount + "TotalItemCount:

 "+ Totalitemcount);

 }
 };
 @Override protected void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate);

 Setcontentview (R.layout.activity_staggeredgridview);
 Madapter = new Sgvadapter (this);
 MSGV = (Staggeredgridview) Findviewbyid (R.id.grid);
 Msgv.setcolumncount (4);
 Msgv.setadapter (Madapter);
 Msgv.setonscrolllistener (Mscrolllistener);  @Override public boolean Oncreateoptionsmenu (Menu menu) {//Inflate the menu; This adds the items to the action Bar if
 It is present.
 Getmenuinflater (). Inflate (R.menu.activity_main, menu);
 return true; Private Final Class Sgvadapter extends Baseadapter {layoutinflater minflater;
 Public Sgvadapter (Context ctx) {minflater = Layoutinflater.from (CTX);
 @Override public int GetCount () {return 30;
 @Override public Object getitem (int position) {return null;
 @Override public long getitemid (int position) {return 0;

 } Random r = new Random (); @Override public View getview (int position, View Convertview, ViewGroup parent) {//layoutparams is equivalent to a layout packet that encapsulates LA Yout position, height, width and other information.
  Assuming that an area on the screen is occupied by a layout, if you add a view to a layout, it is best to tell layout the desired layout, that is, to pass a recognized layoutparams.
  /* can be described as Layoutparams, chess on the chessboard, each piece occupies a position, that is, each piece has a position of information, such as this piece in 4 rows of 4 columns, where the "4 Rows 4 Columns" is the layoutparams of the pieces. But the Layoutparams class also simply describes the width and height can be set to three values: 1, a certain value, 2,fill_parent, that is, fill (as the parent container size), 3,wrap_content, that is, wrap the assembly.
  * Final Layoutparams LP;
  Final View v;
   Switch (position) {case 0:case 29:v = Minflater.inflate (R.layout.element_header, parent, false);
   LP = new Layoutparams (V.getlayoutparams ());Lp.span = Msgv.getcolumncount ();
  Break
   Case 8:case 9:case 18:case 19:v = Minflater.inflate (R.layout.element_item_small, parent, false);
   LP = new Layoutparams (V.getlayoutparams ());
   Lp.span = 1;
  Break
   Case 10:case 20:v = Minflater.inflate (R.layout.element_item_large, parent, false);
   LP = new Layoutparams (V.getlayoutparams ());
   Lp.span = 4;
  Break
   Default:v = Minflater.inflate (R.layout.element_item, parent, false);
   LP = new Layoutparams (V.getlayoutparams ());
   Lp.span = 2;
  Break
  } v.setlayoutparams (LP);
 return v;

 }
 }

}

Activity_main.xml content:

<?xml version= "1.0" encoding= "Utf-8"?> <relativelayout xmlns:android= "http://schemas.android.com/apk/res/" Android "xmlns:tools=" Http://schemas.android.com/tools "android:layout_width=" Match_parent "android:layout_height = "Match_parent" android:paddingleft= "@dimen/activity_horizontal_margin" android:paddingright= "@dimen/activity_ Horizontal_margin "android:paddingtop=" @dimen/activity_vertical_margin "android:paddingbottom=" @dimen/activity_ Vertical_margin "tools:context=" siso.haha.MainActivity > <button android:layout_width= "match_parent" Android : layout_height= "wrap_content" android:text= "Dynamic GridView" android:id= "@+id/btnstaggeredgridview" Android:layout_
 Alignparenttop= "true" android:layout_centerhorizontal= "true"/> <button android:layout_width= "Match_parent" android:layout_height= "Wrap_content" android:text= "New button" android:id= "@+id/button2" android:layout_below= "@+" Id/btnstaggeredgridview "android:layout_centerhorizontal=" true "/> &LT Button android:layout_width= "match_parent" android:layout_height= "wrap_content" android:text= "New button" Android:
 Id= "@+id/button3" android:layout_below= "@+id/button2" android:layout_centerhorizontal= "true"/> <Button Android:layout_width= "Match_parent" android:layout_height= "wrap_content" android:text= "New button" android:id= "@+ Id/button4 "android:layout_below=" @+id/button3 "android:layout_centerhorizontal= true"/> Android: Layout_width= "Match_parent" android:layout_height= "wrap_content" android:text= "New button" android:id= "@+id/ Button5 "android:layout_below=" @+id/button4 "android:layout_centerhorizontal=" true "/> <button
 _width= "Match_parent" android:layout_height= "wrap_content" android:text= "New button" android:id= "@+id/button6" android:layout_below= "@+id/button5" android:layout_centerhorizontal= "true"/> <button android:layout_width= " Match_parent "android:layout_height=" wrap_content "android:text=" NEW Button "android:id=" @+id/button7 "android:layout_below=" @+id/button6 "android:layout_centerhorizontal=" true "/ > <button android:layout_width= "match_parent" android:layout_height= "wrap_content" android:text= "New button" a Ndroid:id= "@+id/button8" android:layout_below= "@+id/button7" android:layout_centerhorizontal= "true"/> < Button android:layout_width= "match_parent" android:layout_height= "wrap_content" android:text= "New button" Android:
 Id= "@+id/button9" android:layout_below= "@+id/button8" android:layout_centerhorizontal= "true"/> <Button Android:layout_width= "Match_parent" android:layout_height= "wrap_content" android:text= "New button" android:id= "@+ Id/button10 "android:layout_alignparentbottom=" true "android:layout_centerhorizontal=" true "/> </

 Relativelayout>

Activity_staggeredgridview.xml content:

<?xml version= "1.0" encoding= "Utf-8"?> <relativelayout xmlns:android=

 "http://schemas.android.com/apk" /res/android "
 xmlns:tools=" Http://schemas.android.com/tools "
 android:layout_width=" Match_parent
 " android:layout_height= "Match_parent"
 tools:context= "staggeredgridviewactivity" >

 < Viewhelper.staggeredgridview
  android:id= "@+id/grid"
  android:layout_width= "Match_parent"
  android: layout_height= "Match_parent"/>

 </RelativeLayout>

Other:

Element_header.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=" 56DP "
 android:background=" @drawable/bg_white_box "
 android:gravity=" center_vertical "
 android:orientation=" Horizontal ">

 <button
 android:id= "@+id/button1"
 android:layout_width= "Wrap_content"
 Wrap_content "
 android:layout_weight=" 1 "
 android:text=" to do something ... "/>

 <progressbar
 Android:id= "@+id/progressbar1"
 android:layout_width= "wrap_content"
 android:layout_height= "WRAP_" Content "
 android:layout_weight=" 1 "/>

</LinearLayout>

Element_item.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=" Wrap_content "
 android: background= "@drawable/bg_white_box"
 android:orientation= "vertical"
 android:padding= "2DP" >

 < ImageView
 android:id= "@+id/imageview1"
 android:layout_width= "match_parent"
 android:layout_height= "100DP"
 android:src= "@android: Color/holo_green_light"/>

 <textview
 android:id= "@+id/ TextView1 "
 android:layout_width=" match_parent "
 android:layout_height=" 56DP "
 android:layout_ Margin= "8DP"
 android:drawableright= "@android:d rawable/ic_menu_info_details"
 android:gravity= "Center_ Vertical "
 android:lines=" 2 "
 android:text=" list item text here, the image above "
 android:textappearance="? android:attr/ Textappearance "/>

</LinearLayout>

Element_item_large.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=" Wrap_content "
 android: background= "@drawable/bg_white_box"
 android:orientation= "vertical"
 android:padding= "2DP" >

 < ImageView
 android:id= "@+id/imageview1"
 android:layout_width= "match_parent"
 android:layout_height= "160DP"
 android:src= "@android: Color/holo_orange_light"/>

 <textview
 android:id= "@+id/ TextView1 "
 android:layout_width=" match_parent "
 android:layout_height=" Wrap_content "
 android: Layout_margin= "8DP"
 android:drawableright= "@android:d rawable/ic_menu_info_details"
 android:gravity= " Center_vertical "
 android:lines=" 2 "
 android:text=" list item text here, the image above "
 android:textappearance="? Android:attr/textappearance "/>

</LinearLayout>

Element_item_small.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=" Wrap_content "
 android: background= "@drawable/bg_white_box"
 android:orientation= "vertical"
 android:padding= "2DP" >

 < ImageView
 android:id= "@+id/imageview1"
 android:layout_width= "match_parent"
 android:layout_height= "100DP"
 android:src= "@android: Color/holo_red_light"/>

 <textview
 android:id= "@+id/ TextView1 "
 android:layout_width=" match_parent "
 android:layout_height=" 56DP "
 android:layout_ Margin= "8DP"
 android:drawableright= "@android:d rawable/ic_menu_info_details"
 android:gravity= "Center_ Vertical "
 android:lines=" 2 "
 android:text=" list item text here, the image above "
 android:textappearance="? android:attr/ Textappearance "/>

</LinearLayout>

Bg_white_box.xml

<?xml version= "1.0" encoding= "Utf-8"?> <shape xmlns:android=
"http://schemas.android.com/apk/res/" Android "
 android:shape=" Rectangle ">

 <solid android:color=" @android:d rawable/screen_background_ Light "/>

 <stroke
 android:width=" 1DP "
 android:color=" @android: Color/holo_blue_dark "

/> </shape>

Run the results as shown in figure:

The above is the entire content of this article, I hope to help you learn, but also hope that we support the cloud habitat community.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.