你的位置:首页 > 操作系统

[操作系统]Android简单的ListViewDemo及每个控件的点击事件


 

main.
<?  android:layout_width="match_parent"  android:layout_height="match_parent"  android:orientation="vertical"  android:gravity="center">  <ListView    android:id="@+id/lv"    android:layout_width="fill_parent"    android:layout_height="fill_parent"    /></LinearLayout>

list_item.
<?LinearLayout  ="http://schemas.android.com/apk/res/android"  android:layout_width="fill_parent"  android:layout_height="60dp"  android:orientation="horizontal">  <LinearLayout    android:layout_width="60dp"    android:layout_height="60dp">    <ImageView      android:id="@+id/image"      android:layout_width="fill_parent"      android:layout_height="fill_parent"/>  </LinearLayout>  <LinearLayout    android:layout_width="fill_parent"    android:layout_height="60dp"    android:orientation="vertical"    android:gravity="center">    <TextView      android:id="@+id/title"      android:layout_width="wrap_content"      android:layout_height="wrap_content"      android:textSize="25sp"/>    <TextView      android:id="@+id/context"      android:layout_width="fill_parent"      android:layout_height="wrap_content"      android:textSize="15sp"/>  </LinearLayout></LinearLayout>

MainActivity.java核心代码

import android.app.Activity;import android.os.Bundle;import android.widget.ListView;import java.util.ArrayList;import java.util.List;import java.util.Map;import java.util.HashMap;import android.widget.SimpleAdapter;import android.view.View;import android.widget.AdapterView.OnItemClickListener;import android.widget.AdapterView;import android.widget.Adapter;import android.widget.Toast;public class MainActivity extends Activity{    @Override  protected void onCreate(Bundle savedInstanceState)  {    super.onCreate(savedInstanceState);    setContentView(R.layout.main);        List<Map<String,Object>>datalist=new ArrayList<Map<String,Object>>();    ListView lv=(ListView) findViewById(R.id.lv);        Map<String,Object> map1=new HashMap<String,Object>();    map1.put("image",R.mipmap.ic_launcher);    map1.put("title","AIDE资源");    map1.put("context","AIDE  资源 分享");    Map<String,Object> map2=new HashMap<String,Object>();    map2.put("image",R.mipmap.ic_launcher);    map2.put("title","了解AIDE");    map2.put("context","AIDE  了解 分享");    Map<String,Object> map3=new HashMap<String,Object>();    map3.put("image",R.mipmap.ic_launcher);    map3.put("title","玩转AIDE");    map3.put("context","AIDE  玩转 分享");    Map<String,Object> map4=new HashMap<String,Object>();    map4.put("image",R.mipmap.ic_launcher);    map4.put("title","控件之TextView");    map4.put("context","AIDE  TextView 分享");        datalist.add(map1);    datalist.add(map2);    datalist.add(map3);    datalist.add(map4);        lv.setAdapter(new SimpleAdapter(this,datalist,R.layout.list_item,new String[]{"image","title","context"},new int[]{R.id.image,R.id.title,R.id.context}));    lv.setOnItemClickListener(new OnItemClickListener(){      //list点击事件        @Override        public void onItemClick(AdapterView<?> p1, View p2, int p3, long p4)        {          // TODO: Implement this method          switch(p3){            case 0://第一个item              Toast.makeText(MainActivity.this,"AIDE  资源 分享",Toast.LENGTH_SHORT).show();              break;              case 1://第二个item              Toast.makeText(MainActivity.this,"AIDE  了解 分享",Toast.LENGTH_SHORT).show();                break;                case 2://第三个item              Toast.makeText(MainActivity.this,"AIDE  玩转 分享",Toast.LENGTH_SHORT).show();                  break;          }        }                  });        }}

以上就是全部代码,下面是效果图