1. 程式人生 > >高階控制元件ListView基本運用

高階控制元件ListView基本運用

<?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"
    android:orientation="horizontal" >

    <ImageView
        android:id="@+id/iv_listviewitem_image"
android:layout_width="0dp" android:layout_height="match_parent" android:layout_weight="3" android:padding="10dp" android:scaleType="fitXY" android:src="@drawable/book1" />
<LinearLayout android:layout_width="0dp" android:layout_height
="wrap_content" android:layout_weight="6" android:orientation="vertical" android:padding="10dp" >
<TextView android:id="@+id/tv_listviewitme_title" android:layout_width="match_parent" android:layout_height="wrap_content" android:text
="狂人攝影日記" android:textColor="@color/red" android:textSize="20sp" />
<LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal" > <TextView android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="2" android:text="書本作者:" android:textColor="@color/black" /> <TextView android:id="@+id/tv_listviewitme_author" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="3" android:text="阿劉" /> </LinearLayout> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal" > <TextView android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="2" android:text="書本價格:" android:textColor="@color/black" /> <TextView android:id="@+id/tv_listviewitme_price" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="3" android:text="$123" android:textColor="@color/black" /> </LinearLayout> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal" > <TextView android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="2" android:text=" 出版社:" android:textColor="@color/black" /> <TextView android:id="@+id/tv_listviewitme_publish" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="3" android:text="電子出版社" android:textColor="@color/black" /> </LinearLayout> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal" > <TextView android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="2" android:text="書本簡介:" android:textColor="@color/black" /> <TextView android:id="@+id/tv_listviewitme_remark" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="3" android:ellipsize="end" android:maxLines="2" android:text="很冷的一個冬天很冷的一個冬天很冷的一個冬天很冷的一個冬天很冷的一個冬天很冷的一個冬天很冷的一個冬天很冷的一個冬天很冷的一個冬天很冷的一個冬天" android:textColor="@color/black" /> </LinearLayout> <LinearLayout android:layout_width="match_parent" android:layout_height="20dp" android:orientation="horizontal" > <ImageButton android:id="@+id/bt_listviewitme_btn1" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_marginLeft="5dp" android:layout_marginRight="5dp" android:layout_weight="1" android:src="@drawable/btn_shopping" /> <ImageButton android:id="@+id/bt_listviewitme_btn2" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_marginLeft="5dp" android:layout_marginRight="5dp" android:layout_weight="1" android:src="@drawable/btn_accounts" /> </LinearLayout> </LinearLayout> </LinearLayout>

ListView邏輯介面

package com.example.android_08;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

import java.util.List;

public class MainActivity extends AppCompatActivity {
        private List<Book> data;
        private MyBaseAdapter adapter;
        private ListView lv_main_bookList;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        lv_main_bookList=findViewById(R.id.lv_main_bookList);
        this.data=new BookDao().list();
        adapter=new MyBaseAdapter((LayoutInflater)getSystemService(this.LAYOUT_INFLATER_SERVICE));
        lv_main_bookList.setAdapter(adapter);
    }

    public class MyBaseAdapter extends BaseAdapter{
        private LayoutInflater inflater;//解析器
        public class ViewHolder{
            ImageView iv_listviewitem_image;
            TextView tv_listviewitme_title;
            TextView tv_listviewitme_price;
            TextView tv_listviewitme_publish;
            TextView tv_listviewitme_remark;
        }
        public MyBaseAdapter(LayoutInflater inflater) {
            this.inflater = inflater;
        }

        @Override
        public int getCount() {
            return data.size();
        }

        @Override
        public Object getItem(int position) {
            return data.get(position);
        }

        @Override
        public long getItemId(int position) {
            return position;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            View v=convertView;
            if(v==null){
                ViewHolder vh=new ViewHolder();
                v=inflater.inflate(R.layout.listview_item,null);
                vh.iv_listviewitem_image=v.findViewById(R.id.iv_listviewitem_image);
                vh.tv_listviewitme_title=v.findViewById(R.id.tv_listviewitme_title);
                vh.tv_listviewitme_price=v.findViewById(R.id.tv_listviewitme_price);
                vh.tv_listviewitme_publish=v.findViewById(R.id.tv_listviewitme_publish);
                vh.tv_listviewitme_remark=v.findViewById(R.id.tv_listviewitme_remark);
                v.setTag(vh);
            }
            ViewHolder vh=(ViewHolder)v.getTag();
            Book book=data.get(position);
            vh.iv_listviewitem_image.setImageResource(book.getImage());
            vh.tv_listviewitme_title.setText(book.getTitle());
            vh.tv_listviewitme_price.setText(book.getPrice()+"");
            vh.tv_listviewitme_publish.setText(book.getPublish());
            vh.tv_listviewitme_remark.setText(book.getRemark());
            return v;
        }
    }
}