ListView
Function: 1. Fill data to the layout. 2. Handle the user's selection, click and other operations.
According to the adapter type of the list, the list is divided into three types, ArrayAdapter, SimpleAdapter and SimpleCursorAdapter
Example:
listview.xml <LinearLayoutxmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" android:descendantFocusability="afterDescendants"> <!-- android:cacheColorHint="#00000000" background transparent --> <!-- android:dividerHeight="1dp" item limit width--> <!-- android:cacheColorHint="#00000000" item boundary color--> <ListView android:id="@+id/listview" android:layout_width="match_parent" android:layout_height="wrap_content" android:cacheColorHint="#00000000" android:dividerHeight="1dp" android:divider="#FF0000" /> </LinearLayout> item.xml <LinearLayoutxmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:gravity="center_vertical" android:orientation="horizontal"> <!-- List item layout --> <ImageView android:id="@+id/img" android:layout_width="100dp" android:layout_height="100dp" android:layout_marginLeft="15dp" android:src="@drawable/a2"/> <TextView android:id="@+id/txtname" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginLeft="25dp"/> </LinearLayout>
ActivityListView.Java
public class ActivityListView extends Activity { private ListView mListView; protected void onCreate(android.os.Bundle savedInstanceState) { super.onCreate (savedInstanceState); //Load the layout file where the listview control is located setContentView(R.layout.listview_layout); //Get the object of the listview control mListView = (ListView)findViewById(R.id.listview); /** *Define SimpleAdapter (and load the corresponding item layout file, get the corresponding keY, get the id of the component in the corresponding item layout file *The second parameter data: data source The third parameter resource: the layout file corresponding to each item of the listView The fourth parameter from: *The fifth parameter to: new String[]{corresponding key}v */ SimpleAdapter simpleAdapter =new SimpleAdapter(this, get_data(), R.layout.item_listview,new String[]{"name","img" }, newint[] { R.id.txtname, R.id.img }); //Bind the adapter to the listView control via setAdapter mListView.setAdapter(simpleAdapter); }; /** *data source */ String[] name = { "Zhang San", "Li Si", "Little Star", "Quiet", "Mingming", "Xiao Cui" }; private ArrayList<Map<String, Object>> get_data() { //Define a collection of ArrayList (the collection encapsulates a collection of map type) ArrayList<Map<String, Object>> data_list =new ArrayList<Map<String,Object>>(); for (int i = 0; i<name.length; i++) { // key,valueintstring map不能new map Map<String, Object> data_map =new HashMap<String, Object>(); data_map.put("name",name[i]); data_map.put("img",R.drawable.a1); //Add (encapsulate) the map collection to the ArrayList collection data_list.add(data_map); } //return ArrayList collection return data_list; } }
Gallery
Function: Realize the timing and scrolling display of pictures
Example:
gallery.xml <LinearLayoutxmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:gravity="center_vertical" android:orientation="vertical"> <Gallery android:id="@+id/gallery" android:layout_width="match_parent" android:layout_height="wrap_content" android:spacing="0dip"/> </LinearLayout>
public class ActivityGrelly extends Activity { /** Called when the activity isfirst created. */ private int index; private Gallery g; private Handler handler; public void onCreate(Bundle savedInstanceState) { super.onCreate (savedInstanceState); setContentView(R.layout.gallery_layout); initView(); } private void initView() { // TODOAuto-generated method stub // Get the Gallery object g = (Gallery)findViewById(R.id.gallery); // Add ImageAdapter to Gallery object g.setAdapter(newImageAdapter(this)); // Set the background of the Gallery g.setBackgroundResource(R.drawable.bg); TimerTask task = new TimerTask() { @Override public void run() { Message message = new Message(); message.what = 2; index = g.getSelectedItemPosition(); index++; handler.sendMessage(message); } }; Timer timer = new Timer (); timer.schedule(task, 3000, 3000); handler = new Handler () { @Override public void handleMessage(Message msg) { super.handleMessage(msg); switch (msg.what) { case 2: g.setSelection(index); break; default: break; } } }; // Set the event listener for Gallery g.setOnItemClickListener(new OnItemClickListener() { public void onItemClick(AdapterView<?> parent, View v, int position,long id) { Toast.makeText(ActivityGrelly.this, "You selected" + (position + 1) + "Image", Toast.LENGTH_SHORT) .show(); } }); } }
Effect:
Source code download:
Eclipse download: http://download.csdn.net/detail/dickyqie/9620303
AndroidStudio download: https://github.com/DickyQie/android-list-control/tree/list-use