Android practice simple tutorial-21st gun (GridView dynamic add Item), androidgridview

Source: Internet
Author: User

Android practice simple tutorial-21st gun (GridView dynamic add Item), androidgridview

In this example, an added image is displayed at the end of the GridView, And the content item is dynamically added by clicking the image.

1. main. xml:

<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" >    <GridView        android:id="@+id/gv_test"        android:numColumns="3"        android:layout_width="match_parent"        android:layout_height="match_parent" /></RelativeLayout>

2. grid_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="match_parent"    android:orientation="vertical" >    <ImageView        android:id="@+id/item"        android:layout_width="wrap_content"        android:layout_height="wrap_content" /></LinearLayout>

3. GridViewAdapter. java:

Package com. yayun. gridviewdemo; import java. util. list; import android. content. context; import android. view. layoutInflater; import android. view. view; import android. view. viewGroup; import android. widget. baseAdapter; import android. widget. imageView; public class GridViewAdapter extends BaseAdapter {private Context context; private List <Integer> list; LayoutInflater layoutInflater; private ImageView mImageView; public GridViewAdapter (Context context, List <Integer> list) {this. context = context; this. list = list; layoutInflater = LayoutInflater. from (context) ;}@ Overridepublic int getCount () {return list. size () + 1; // note here} @ Overridepublic Object getItem (int position) {return list. get (position) ;}@ Overridepublic long getItemId (int position) {return position ;}@ Overridepublic View getView (int position, View convertView, ViewGroup parent) {convertView = layoutInflater. inflate (R. layout. grid_item, null); mImageView = (ImageView) convertView. findViewById (R. id. item); if (position <list. size () {mImageView. setBackgroundResource (list. get (position);} else {mImageView. setBackgroundResource (R. drawable. pic3); // The Last plus image displayed} return convertView ;}}

4. MainActivity. java:

Package com. yayun. gridviewdemo; import java. util. arrayList; import java. util. list; import android. OS. bundle; import android. support. v7.app. actionBarActivity; import android. view. view; import android. widget. adapterView; import android. widget. adapterView. onItemClickListener; import android. widget. gridView; import android. widget. toast; public class MainActivity extends ActionBarActivity {private List <Integer> m Datas; private GridView mGridView; private GridViewAdapter adapter; @ Overrideprotected void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); setContentView (R. layout. activity_main); mGridView = (GridView) findViewById (R. id. gv_test); initDatas (); adapter = new GridViewAdapter (MainActivity. this, mDatas); mGridView. setAdapter (adapter); mGridView. setOnItemClickListener (new OnItemClickListener () {@ Overridepublic void onItemClick (AdapterView <?> Parent, View view, int position, long id) {if (position = parent. getChildCount ()-1) {mDatas. add (R. drawable. pic1); Toast. makeText (MainActivity. this, "you clicked to add", 1 ). show (); adapter = new GridViewAdapter (MainActivity. this, mDatas); mGridView. setAdapter (adapter); adapter. notifyDataSetChanged () ;}});} private void initDatas () {mDatas = new ArrayList <> (); mDatas. add (R. drawable. pic1); mDatas. add (R. drawable. pic1); mDatas. add (R. drawable. pic1); mDatas. add (R. drawable. pic1); mDatas. add (R. drawable. pic1 );}}

5. Run the project:

You can use the delete function of the previous blog to add and delete dynamically.

My favorite friends follow me!

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.