Android課程---最佳化ListView列表視圖(2)

來源:互聯網
上載者:User

標籤:

layout_simple.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="wrap_content">    <ImageView        android:layout_width="70dp"        android:layout_height="70dp"        android:src="@drawable/yuantu"        android:id="@+id/lv2"/>    <LinearLayout        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:orientation="vertical"        android:layout_gravity="center_vertical"        android:layout_marginLeft="20dp">        <TextView            android:layout_width="wrap_content"            android:layout_height="wrap_content"            android:text="美食圖片"            android:id="@+id/tv1"/>        <TextView            android:layout_width="wrap_content"            android:layout_height="wrap_content"            android:text="很好吃啊"            android:id="@+id/tv2"/>    </LinearLayout></LinearLayout>

activity_ui5.xml

<?xml version="1.0" encoding="utf-8"?><ListView xmlns:android="http://schemas.android.com/apk/res/android"    android:layout_width="match_parent"    android:layout_height="match_parent"    android:id="@+id/lv_2"></ListView>

UIActity.java

package com.hanqi.test5;import android.os.Bundle;import android.support.v7.app.AppCompatActivity;import android.util.Log;import android.view.LayoutInflater;import android.view.View;import android.view.ViewGroup;import android.widget.BaseAdapter;import android.widget.ImageView;import android.widget.ListView;import android.widget.TextView;import java.util.ArrayList;import java.util.List;public class UIActivity5 extends AppCompatActivity{    //資料來源    //List<Map<String,Object>> lm;    List<Food> lf;    @Override    protected void onCreate(Bundle savedInstanceState)    {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_ui5);        //擷取ListView        ListView lv_2 =(ListView)findViewById(R.id.lv_2);        //準備資料來源        lf = new ArrayList<Food>();        lf.add(new Food(R.drawable.yuantu,"內容1","美食1"));        lf.add(new Food(R.drawable.yuantu,"內容2","美食2"));        lf.add(new Food(R.drawable.yuantu,"內容3","美食3"));        lf.add(new Food(R.drawable.yuantu,"內容4","美食4"));       /* lm =new ArrayList<Map<String,Object>>();        Map<String,Object> map = new Hashtable<String, Object>();        map.put("image",R.drawable.yuantu);        map.put("name","美食圖片01");        map.put("content","很好吃啊01");        lm.add(map);        map = new Hashtable<String, Object>();        map.put("image",R.drawable.yuantu);        map.put("name","美食圖片02");        map.put("content","很好吃啊02");        lm.add(map);        map = new Hashtable<String, Object>();        map.put("image",R.drawable.yuantu);        map.put("name","美食圖片03");        map.put("content","很好吃啊03");        lm.add(map);        map = new Hashtable<String, Object>();        map.put("image",R.drawable.yuantu);        map.put("name","美食圖片04");        map.put("content","很好吃啊04");        lm.add(map);*/        lv_2.setAdapter(new MyBaseAdapter());    }    class MyBaseAdapter extends BaseAdapter    {        //擷取要顯示的行數        @Override        public int getCount()        {            return lf.size();        }        //擷取對應下標的行        @Override        public Object getItem(int position)        {            return lf.get(position);        }        //擷取對應行的id        @Override        public long getItemId(int position)        {            return position;        }        //給視圖適配資料,返回視圖        //3個參數:1.position(資料的下標)        //2.convertView(可重複使用的視圖)        //3.parent(父視圖)        @Override        public View getView(int position, View convertView, ViewGroup parent)        {            //如果convertView為空白,就載入layout檔案            if (convertView == null)            {            Log.e("TAG","position = "+position);            //1.擷取樣式檔案(layout檔案)            LayoutInflater layoutInflater = getLayoutInflater();                convertView = layoutInflater.inflate(R.layout.layout_simple, null);            }            //2.資料和layout檔案的視圖組件進行綁定            //1)從資料來源裡擷取一行資料            //Map<String,Object> map = lm.get(position);            Food food = lf.get(position);            //2)擷取layout檔案的視圖組件            ImageView iv_2 = (ImageView)convertView.findViewById(R.id.lv2);            TextView tv1 = (TextView)convertView.findViewById(R.id.tv1);            TextView tv2 = (TextView)convertView.findViewById(R.id.tv2);            //3)綁定(資料適配)//            iv_2.setImageResource((int) map.get("image"));//            tv1.setText(map.get("name").toString());//            tv2.setText(map.get("content").toString());            iv_2.setImageResource((int) food.getImage());            tv1.setText(food.getName());            tv2.setText(food.getContent());            //3.返回視圖            return convertView;        }    }}

 

Android課程---最佳化ListView列表視圖(2)

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.