Home | 簡體中文 | 繁體中文 | 雜文 | 知乎專欄 | Github | OSChina 博客 | 雲社區 | 雲棲社區 | Facebook | Linkedin | 視頻教程 | 打賞(Donations) | About
知乎專欄多維度架構 | 微信號 netkiller-ebook | QQ群:128659835 請註明“讀者”

35.3. ListView

35.3.1. Array

			
        String[] list = Arrays.asList("Apple", "Banana", "Orange", "Watermelon",
                "Pear", "Grape", "Pineapple", "Strawberry", "Cherry", "Mango";
        ArrayAdapter<String> adapter = new ArrayAdapter<String>(MainActivity.this, android.R.layout.simple_list_item_1,data);
        ListView listView = (ListView) findViewById(R.id.history);
        listView.setAdapter(adapter);	
			
			
			
    <ListView
        android:id="@+id/history"
        android:layout_width="368dp"
        android:layout_height="444dp"
        android:scrollbars="horizontal"
        tools:layout_editor_absoluteX="8dp"
        tools:layout_editor_absoluteY="59dp" />	
			
			

35.3.2. List

			
		List<String> list = Arrays.asList("Apple", "Banana", "Orange", "Watermelon","Pear", "Grape", "Pineapple", "Strawberry", "Cherry", "Mango");
        ArrayAdapter<String> adapter = new ArrayAdapter<String>(MainActivity.this, android.R.layout.simple_list_item_1,list);
        ListView listView = (ListView) findViewById(R.id.history);
        listView.setAdapter(adapter);        		
			
			

35.3.3. setOnItemClickListener()

			
        List<String> list = Arrays.asList("Text 文本", "URL 網址", "電話號碼", "短信","開啟應用", "地址", "日曆", "圖片", "郵箱", "GPS 坐標");
        ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1,list);
        final ListView listView = (ListView) findViewById(R.id.schemaList);
        listView.setAdapter(adapter);


        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

                String text = listView.getItemAtPosition(position)+"";
                Log.e("WRITE","position="+position+", text="+text);
            }
        });	
			
			

35.3.4. 用介面方法實現

		
public class MainActivity extends Activity implements OnItemClickListener, OnScrollListener
		
			
		
		List<String> list = Arrays.asList("Text 文本", "URL 網址", "電話號碼", "短信","開啟應用", "地址", "日曆", "圖片", "郵箱", "GPS 坐標");
        ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1,list);
        final ListView listView = (ListView) findViewById(R.id.schemaList);
        listView.setAdapter(adapter);
        
        listView.setOnItemClickListener(this);
        listView.setOnScrollListener(this);		
		
			
		
	@Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        String text = listView.getItemAtPosition(position)+"";
        Log.e("WRITE","position="+position+", text="+text);
    }		
		
			
		
	@Override
    public void onScrollStateChanged(AbsListView view, int scrollState) {

        switch (scrollState) {
        case SCROLL_STATE_FLING:
            Log.i("tag", "用戶手指離開屏幕後,因慣性繼續滑動");
            Map<String,Object>map = new HashMap<String,Object>();
            map.put("icon", R.drawable.ic_launcher);
            map.put("text", "新增加項目");
            dataList.add(map);  
            adapter.notifyDataSetChanged(); 
            break;
        case SCROLL_STATE_IDLE:
            Log.i("tag","已經停止滑動");
            break;      
        case SCROLL_STATE_TOUCH_SCROLL:
            Log.i("tag", "手指未離開屏幕,屏幕繼續滑動");
            break;
        }   
    }