Android-day program: GridView achieves album effect, androidgridview
This chapter uses the GridView control to create an album effect.
The image effect is as follows:
In response to the click event, the page displays the pictures of the current chapter, from left to right, top to bottom.
Click the first image, and 1 is displayed.
Steps:
1. Create a new project and copy the image resources to the res/drawable/folder. It doesn't matter if there is no drawable folder. You can directly create this folder and refresh it in the Eclipse project, it will be displayed in the project.
2. Enter the following code in the xml file activity_main.xml (you can also name it as needed) on the Layout interface:
<?xml version="1.0" encoding="utf-8"?><GridView xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/gridview" android:layout_width="match_parent" android:layout_height="match_parent" android:columnWidth="90dp" android:numColumns="auto_fit" android:verticalSpacing="10dp" android:horizontalSpacing="10dp" android:stretchMode="columnWidth" android:gravity="center"/>
Nothing special, that is, setting the row width and automatically arranging the number of rows numColumns = "auto_fit", which seems to be automatically arranged by Android.
The java file MainActivity. java corresponding to activity_main.xml enters the logic code, which is mainly used to modify the onCreate function. The other functions can be used by default:
@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);GridView gridview = (GridView) findViewById(R.id.gridview);gridview.setAdapter(new ImageAdapter(this));gridview.setOnItemClickListener(new OnItemClickListener() {public void onItemClick(AdapterView<?> parent, View v,int position, long id) {Toast.makeText(MainActivity.this, "" + (position+1),Toast.LENGTH_SHORT).show();}});}
You can set the ItemClickListener for listening to the GridView object. Toast can be understood as a lightweight prompt dialog box, that is, a small dialog box displaying 1 will automatically disappear after about 1 second.
TIPS:
I don't know what header files the object belongs? For example, what file library does OnItemClickListener belong?
This is sometimes a headache, especially when using VC to write C ++, You Need To Do It Yourself. The headache is that after finding this function or class, I didn't even notice what header files need to be included, but in Java, it seems that this is not the case. A Shortcut Key is completed: ctrl + shift + o; it will automatically include all the required libraries.
4. Create a Java class, store it in the MainActivity. java directory, and add the Code:
package su.gridview.hellogridview;import android.content.Context;import android.view.View;import android.view.ViewGroup;import android.widget.BaseAdapter;import android.widget.GridView;import android.widget.ImageView;public class ImageAdapter extends BaseAdapter {private Context mContext;public ImageAdapter(Context c) {mContext = c;}public int getCount() {return mThumbIds.length;}public Object getItem(int position) {return null;}public long getItemId(int position) {return 0;}// create a new ImageView for each item referenced by the Adapterpublic View getView(int position, View convertView, ViewGroup parent) {ImageView imageView;if (convertView == null) { // if it's not recycled, initialize some// attributesimageView = new ImageView(mContext);imageView.setLayoutParams(new GridView.LayoutParams(85, 85));imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);imageView.setPadding(8, 8, 8, 8);} else {imageView = (ImageView) convertView;}imageView.setImageResource(mThumbIds[position]);return imageView;}// references to our imagesprivate Integer[] mThumbIds = { R.drawable.x0, R.drawable.x1,R.drawable.x2, R.drawable.x3, R.drawable.x4, R.drawable.x5,R.drawable.x10, R.drawable.x9, R.drawable.x8, R.drawable.x7,R.drawable.x6, R.drawable.x5, R.drawable.x11, R.drawable.x20,R.drawable.x13, R.drawable.x14, R.drawable.x15, R.drawable.x16,R.drawable.x17, R.drawable.x18, R.drawable.x19, R.drawable.x20 };}
The useless functions in this example are all because the BaseAdapter class must be rewritten.
The getCount and getView functions are mainly used. You can implement it according to his style. The lower-level work is automatically called by the Adapter class. When getCount is called, the Adapter will know how many resource objects need to be displayed, call getView to display the image resources. The convertView is used to accelerate the conversion and reuse the previously used View.
This is my understanding. For more detailed principles, please refer to the source code.
References: http://developer.android.com/guide/topics/ui/layout/gridview.html
The second chapter is complete. It seems that it is too convenient for Android to write programs. It is no wonder that Android is quite popular. Compared with the usage of DirectX APIs, it is just silly. In particular, it reminds me of the fact that Microsoft was really stupid when we used Direct2D. After all these years, we still had an API that was so difficult to use Direct2D. Well, let alone I am; recently I heard that VC is going to support Android. I feel that Microsoft has lowered its head and supported competitors; at the same time, we also want to keep up with the pace of Android, instead of sacrificing colors. Strategic needs should be lowered.