Android ListView控件

2017-10-18  本文已影响0人  百里漫步

首先,明确新建列表流程:
新建适配器--》添加数据源到适配器--》加载适配器到视图

数据适配器的作用:把复杂的数据(数组、链表、数据库、集合等)填充在指定视图界面上。
ArrayAdapter:绑定单一数据,数据源可以是集合或数组
SimpleAdapter:用于绑定格式复杂的数据,数据源是特定泛型的集合

数据适配器是连接数据源和视图界面的桥梁。

下面给出一个例子,实例中有两个列表和点击事件:
activity_main.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <ListView
        android:id="@+id/ListView_1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" 
        >
    </ListView>
    
    <Button 
        android:id="@+id/Button_1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="跳转"
        android:textColor="#000000" />

</LinearLayout>

MainActivity.java

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewTreeObserver.OnScrollChangedListener;
import android.widget.AbsListView;
import android.widget.AbsListView.OnScrollListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ListView;
import android.widget.Toast;

public class MainActivity extends Activity {

    private ListView  listView01;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        String[] str = {"a","b","c","d"};
       listView01 = (ListView) findViewById(R.id.ListView_1);
       Button button01 = (Button) findViewById(R.id.Button_1);
       //上下文,布局文件,数据源
       ArrayAdapter<String> arr_adapter = new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1 ,str);
       listView01.setAdapter(arr_adapter);
       
       button01.setOnClickListener(new OnClickListener() {
        
        @Override
        public void onClick(View arg0) {
            Intent intent = new Intent(MainActivity.this,SimpleItemActivity.class);
            MainActivity.this.startActivity(intent);
        }
    });
       
    }
}

activity_simple_item.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="horizontal" >
<ListView
        android:id="@+id/ListView_02"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" 
        >
    </ListView>
    
</LinearLayout>

SimpleItemActivity.java

package com.example.day01;

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

import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.AbsListView;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.Toast;
import android.widget.AbsListView.OnScrollListener;
import android.widget.AdapterView.OnItemClickListener;

public class SimpleItemActivity extends Activity implements OnItemClickListener,OnScrollListener{

    private ListView listView;
    private SimpleAdapter sim_adapter;
    private List<Map<String,Object>> list;
    
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_simple_item);

        //页面的初始化ListView控件
        listView = (ListView) findViewById(R.id.ListView_02);
        //新建SimpleAdapter,上下文,布局文件,键值对键名,键值对值
        sim_adapter = new SimpleAdapter(this, getData(),R.layout.item ,new String[]{"pic","text"} ,new int[]{R.id.ImageView_01,R.id.TextView_01});
        //视图加载适配器
        listView.setAdapter(sim_adapter);
        listView.setOnItemClickListener(this);
        listView.setOnScrollListener(this);
    }

    private List<Map<String, Object>> getData() {
        list = new ArrayList<Map<String,Object>>();
        for(int i=0;i<20;i++){
            Map<String,Object> map = new HashMap<String,Object>();
            map.put("pic", R.drawable.ic_launcher);
            map.put("text", "小鬼"+i);            
            list.add(map);
        }
        
        return list;
    }
    @Override
    public void onItemClick(AdapterView<?> adapter, View v, int position, long id) {
        String text = listView.getItemAtPosition(position)+"";
        Toast.makeText(this, text, Toast.LENGTH_SHORT).show();;
    }

    @Override
    public void onScroll(AbsListView arg0, int arg1, int arg2, int arg3) {
        
        
    }

    @Override
    public void onScrollStateChanged(AbsListView view, int state) {
        switch(state){
        case SCROLL_STATE_TOUCH_SCROLL:
            Log.i("tag","手指正在滑动");
            Map<String,Object> map = new HashMap<String,Object>();
            map.put("pic", R.drawable.ic_launcher);
            map.put("text", "大鬼");
            list.add(map);
            sim_adapter.notifyDataSetChanged();
            break;
        case SCROLL_STATE_FLING:
            Log.i("tag","手指移开,自动滑动中。。。");
            break;
        case SCROLL_STATE_IDLE:
            Log.i("tag","停止滑动");
            break;
        }
        
    }
    

}

item.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="horizontal" >
    <ImageView
        android:id="@+id/ImageView_01"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginLeft="20dp"
        android:background="@drawable/ic_launcher" />

    <TextView 
        android:id="@+id/TextView_01"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="20dp"
        android:text="@string/xiaogui"
        android:textColor="#000000"
        />
    

</LinearLayout>

效果演示:

item.gif
上一篇下一篇

猜你喜欢

热点阅读