1. 程式人生 > >android--ListView(控制元件+二種介面卡+監聽事件)

android--ListView(控制元件+二種介面卡+監聽事件)

介面卡就是大量資料來源顯示到檢視上
介面卡分為簡單介面卡和陣列介面卡
陣列介面卡:先在佈局檔案新建一個ListVi控制元件
1.新建一個數組介面卡 arrAdapter=new ArrAdapter<String>(this,android.layout.simple_list_item_1,string)//string[] string={""""""}
2.新建一個listview,並把它給初始化
3.listview啟動介面卡List.setAdpter(simpleAdapter);
簡單介面卡
1. simpleAdapter=new SimpleAdapter(this,getdata(),R.layout.item, new String[]{"pic","text"},new int[]{R.id.pic,R.id.text});
(1)getdata()得到資料來源的方法listdata是有許多個map組成,每一個map都會對應ListView列表中的一行,map由鍵值對組成,
(2)R.layout.item 佈局檔案ID就是你構建的一個個列表
(3)new String[]{"pic","text"} map的鍵名,就是你要建立map,必須構建鍵值對。
(4)new int[]{R.id.pic,R.id.text} 你佈局時構建的控制元件,要和鍵值對繫結。
2.構建資料來源 private List<Map<String,Object>> datalist;  
         datalist=new ArrayList<Map<String, Object>>();
3.構建map
在得到資料來源發法裡面定義map,構建map裡元素具體的數值
把自己構建的map加到資料來源裡
Map<String,Object> map= new HashMap<String, Object>();
datalist.add(map);


列表監聽屬於介面監聽
為了響應某個動作(手指滑動)
OnItemClickListener:
可以處理檢視中單個條目的點選事件;
OnScrollListener:
監聽滾動變化,可以用於檢視在滾動中載入資料

監聽器是程式和使用者互動的橋樑

MainActivity

package com.example.wuzuo.listview;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.AbsListView;
import android.widget.Adapter;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.Toast;

import java.util.AbstractList;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;

public class MainActivity extends AppCompatActivity implements AdapterView.OnItemClickListener,AbsListView.OnScrollListener{
    private ListView listView;
    private SimpleAdapter simpleAdapter;
    private ArrayAdapter<String> arrayAdapter;
    private List<Map<String,Object>> datalist;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        listView= (ListView) findViewById(R.id.listView1);
        String[] strings={"賽客網","慕克網","網易雲課堂"};
        datalist=new ArrayList<Map<String, Object>>();
        arrayAdapter=new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1,strings);
        simpleAdapter=new SimpleAdapter(this,getdata(),R.layout.item, new String[]{"pic","text"},new int[]{R.id.pic,R.id.text});
        listView.setAdapter(simpleAdapter);
        listView.setOnItemClickListener(this);
        listView.setOnScrollListener(this);
    }
private List<Map<String,Object>> getdata()
{
    for (int i=0;i<20;i++)
    {
        Map<String,Object> map= new HashMap<String, Object>();
        map.put("pic",R.mipmap.ic_launcher);
        map.put("text","慕克網"+i);
        datalist.add(map);
    }
    return datalist;
}

    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
    String text= listView.getItemIdAtPosition(position)+"";
        Toast.makeText(this,"postion= "+position+"text= "+text,Toast.LENGTH_LONG).show();

    }


    @Override
    public void onScrollStateChanged(AbsListView view, int scrollState) {
        switch (scrollState)
        {
            case SCROLL_STATE_FLING:
                Log.i("Main","手指離開了,可是檢視繼續滑動");
                Map<String,Object> map=new HashMap<String, Object>();
                map.put("pic",R.mipmap.ic_launcher);
                map.put("text","增加項");
                datalist.add(map);
                simpleAdapter.notifyDataSetChanged();
                break;
            case SCROLL_STATE_IDLE:
                Log.i("Main","檢視停止滑動");
                break;
            case SCROLL_STATE_TOUCH_SCROLL:
                Log.i("Main","手指按著滑動");
                break;
        }

    }

    @Override
    public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) {


    }
}



activity_main
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="com.example.wuzuo.listview.MainActivity">


    <ListView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:id="@+id/listView1"
       />
</RelativeLayout>

item.xlm
<?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="horizontal">
<ImageView
    android:id="@+id/pic"
    android:layout_marginLeft="15dp"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:src="@mipmap/ic_launcher"/>
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/text"
        android:textColor="#000000"
        android:text="demo"
        android:textSize="40dp"
        />

</LinearLayout>