GridView control
(1) Overview The
GridView control is called a grid list, and it is used frequently in android. The control can display information in multiple rows and columns, and has strong performance capabilities.
(2) Commonly used attribute
attribute name Java method Function
columnWidth setColumnWidth(int) Set the column width gravity setGravity(int) set the alignment horizontalSpacing setHorizontalSpacing(int) Set the horizontal spacing between elements numColumns setNumColumns(int) Set the number of columns stretchMode setStrechMode(int) Set stretch mode verticalSpacing setVerticalSpacing(int) Sets the vertical spacing between elements
in,
stretchMode has the following optional property values NO_STRETCH do not stretch STRETCH_SPACING stretches only the space between elements STRETCH_SPACING_UNIFORM The table element itself and the space between elements are stretched together STRETCH_COLUMN_WIDTH stretches only the table element itself
Gallery Control
(1) Overview The
Gallery control is similar to the Spinner class, and both are subclasses of the AbsSpinner class. The difference between them is: Spinner shows a vertical list select box, while Gallery shows a horizontal list select box, and Gallery allows the user to drag the list box horizontally.
ListView is a vertical list, Gallery can be understood as a horizontal list.
(2) Common attributes
XML attribute related method description Gravity setGravity(int) Set the alignment spacing setSpacing(int) sets the spacing between list items unselectedAlpha setUnselectedAlpha(float) Sets the transparency of unselected list items
(3) Important interfaces and methods
1. OnItemClickListener interface: monitor the click event of a list item. The interface defines the following methods:
1) void onItemClick(AdapterView<?> parent, View view, int position, long id)
Function: Respond to the event that the user clicks the list item.
Parameter description:
the first parameter -parent: the current Gallery object
The second parameter -view: the clicked list item
The third parameter -position: the position (index value) of the clicked list item in the
adapter Four parameters -id: the index value of the column where the clicked list item is located in the list.
2) void setOnItemClickListener(OnItemClickListener listener) Function: Set the method parameter description
for responding to the user's click event : Listener: An object that implements the OnItemClickListener interface. Example:
package com.jxust.day_05_01_listviewdemo; import java.util.ArrayList; import java.util.List; import android.app.Activity; import android.os.Bundle; import android.view.Menu; import android.view.MenuItem; import android.view.View; import android.view.View.OnClickListener; import android.view.ViewGroup; import android.widget.AdapterView; import android.widget.AdapterView.OnItemClickListener; import android.widget.AdapterView.OnItemLongClickListener; import android.widget.BaseAdapter; import android.widget.Gallery; import android.widget.GridView; import android.widget.ImageView; import android.widget.ListView; import android.widget.TextView; import android.widget.Toast; public class MainActivity extends Activity { Gallery mGallery; List<GeneralBean> mGenerals; // Represents a collection of ten military strategists GeneralAdapter mAdapter; int[] resid = { R.drawable.baiqi, R.drawable.caocao, R.drawable.chengjisihan, R.drawable.hanxin, R.drawable.lishimin, R.drawable.nuerhachi, R.drawable.sunbin, R.drawable.sunwu, R.drawable.yuefei, R.drawable.zhuyuanzhang }; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate (savedInstanceState); setContentView(R.layout.activity_main); initData(); // initialize data initView(); setListener(); //Set the listener } private void setListener() { setOnItemClickListener(); setOnItemLongClickListener(); } // long press operation private void setOnItemLongClickListener() { mGallery.setOnItemLongClickListener(new OnItemLongClickListener() { @Override public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id) { Toast.makeText(MainActivity.this, mGenerals.get(position).getName()+"被长按", 2000).show(); //The function of false is to tell the system that it has not processed it and hand it over to other listeners, so that the listener for the short press operation is reached. //The role of true is to tell the system that it has been processed return true; } }); } // short press private void setOnItemClickListener() { mGallery.setOnItemClickListener(new OnItemClickListener() { @Override public void onItemClick(AdapterView<?> parent, View view, int position, long id) { Toast.makeText(MainActivity.this, mGenerals.get(position).getName()+"被短按", 2000).show(); } }); } private void initView() { mGallery = (Gallery) findViewById(R.id.galleryGeneral); mAdapter = new GeneralAdapter(); // create the adapter // Associate with ListView mGallery.setAdapter(mAdapter); } private void initData() { // Convert the string array in the resource to a Java array String[] names = getResources().getStringArray(R.array.generals); mGenerals = new ArrayList<GeneralBean>(); for (int i = 0; i < names.length; i++) { GeneralBean bean = new GeneralBean(resid[i], names[i]); mGenerals.add(bean); } } // Define the adapter class that represents the military strategist class GeneralAdapter extends BaseAdapter { @Override public int getCount() { // return mGenerals.size(); return Integer.MAX_VALUE; } @Override public Object getItem(int position) { return mGenerals.get(position); } @Override public long getItemId(int position) { return position; } @Override public View getView(int position, View convertView, ViewGroup parent) { // Get the layout of the ListView and convert it to an object of type View View layout = View.inflate(MainActivity.this, R.layout.item_generals, null); // root represents the parent container // Find the ImageView that displays the fighter's avatar // The role of layout is to convert the target to layout.item_generals ImageView ivThumb = (ImageView) layout.findViewById(R.id.ivThumb); // Find the TextView that displays the military strategist's name TextView tvName = (TextView) layout.findViewById(R.id.tvName); // Get the strategist object whose subscript is position in the strategist GeneralBean bean = (GeneralBean) getItem(position%mGenerals.size()); // Display the fighter's avatar ivThumb.setImageResource(bean.getResid()); // Display the military strategist's name tvName.setText(bean.getName()); return layout; } } }
<?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:orientation="vertical" > <ImageView android:id="@+id/ivThumb" android:layout_width="80dp" android:layout_height="80dp" android:scaleType="fitXY" android:src="@drawable/baiqi" /> <TextView android:id="@+id/tvName" android:layout_width="80dp" android:layout_height="wrap_content" android:gravity="center_horizontal" android:text="白起" android:textSize="20sp" /> </LinearLayout>
<?xml version="1.0" encoding="utf-8"?> <resources> <string name="app_name">Day_05_01_ListViewDemo</string> <string name="hello_world">Hello world!</string> <string name="action_settings">Settings</string> <string-array name="generals"> <item >白起</item> <item >曹操</item> <item >Genghis Khan</item> <item>Han Xin</item> <item >Li Shimin</item> <item >Nurhaci</item> <item >孙膑</item> <item >Sun Wu</item> <item >Yue Fei</item> <item >Zhu Yuanzhang</item> </string-array> </resources>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" > <Gallery android:id="@+id/galleryGeneral" android:layout_width="match_parent" android:layout_height="wrap_content" android:spacing="2dp" /> </RelativeLayout>