Android簡單的ListViewDemo及每個控制項的點擊事件,listview控制項點擊事件

來源:互聯網
上載者:User

Android簡單的ListViewDemo及每個控制項的點擊事件,listview控制項點擊事件

 

main.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"    android:gravity="center">    <ListView        android:id="@+id/lv"        android:layout_width="fill_parent"        android:layout_height="fill_parent"        /></LinearLayout>
list_item.xml每個item的排布
<?xml version="1.0" encoding="utf-8"?><LinearLayout    xmlns:android="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;                    }                }                                    });                }}
以上就是全部代碼,下面是

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.