Andriod【自定义适配器】适配器②

//BaseAdapter:是所有适配器类的父类,可以对列表项进行最大限度的定制

与数组适配器,简单适配器不同的是自定义适配器解析的项资源可以复杂化(可以布局里面套布局)

//示例:

//MainActivity

package com.example.a02;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
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.List;
import java.util.zip.Inflater;

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{
        //使用内部类ViewHolder+ConvertView.setTag()保存控件,而不用每次查找
        public class ViewHolder{
            ImageView iv_listviewitem_image;
            TextView tv_listviewitme_title;
            TextView tv_listviewitme_author;
            TextView tv_listviewitme_price ;
            TextView tv_listviewitme_publish;
            TextView tv_listviewitme_remark;
        }
        //解析器
        private LayoutInflater inflater;



    //绑定解析器,把它写进适配器的构造方法                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                              `
        public MyBaseAdapter(LayoutInflater inflater) {
            this.inflater = inflater;
        }
        //获取了多少条数据
        @Override
        public int getCount() {
            return data.size();
        }

        //获取项资源(一条数据对应一个项),让适配器解析第几条数据
        @Override
        public Object getItem(int i) {
            return null;
        }

        //获取第几条数据的索引(下标)
        @Override
        public long getItemId(int i) {
            return i;
        }

        //解析完数据,填充到view才能展示数据
        @Override
        public View getView(int i, View view, ViewGroup parent) {
            //view的视图在不断创建
            // View v = inflater.inflate(R.layout.listview_item,null);//null表示没有父控件
            //优化创建视图
            View v = view;
            if(v == null){
                ViewHolder vh = new ViewHolder();
                Log.i("test","position");
                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_author = v.findViewById(R.id.tv_listviewitme_author);
                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);
            }

             Object Tag= v.getTag();
          /*
            ImageView iv_listviewitem_image= v.findViewById(R.id.iv_listviewitem_image);
            //获取项资源里所有控件
            TextView tv_listviewitme_title = v.findViewById(R.id.tv_listviewitme_title);
            TextView tv_listviewitme_author = v.findViewById(R.id.tv_listviewitme_author);
            TextView tv_listviewitme_price = v.findViewById(R.id.tv_listviewitme_price);
            TextView tv_listviewitme_publish = v.findViewById(R.id.tv_listviewitme_publish);
            TextView tv_listviewitme_remark = v.findViewById(R.id.tv_listviewitme_remark);*/

            //赋值
            ViewHolder vh = (ViewHolder) v.getTag();
            Book book =  data.get(i);
            vh.iv_listviewitem_image.setImageResource(book.getImage());
            vh.tv_listviewitme_title.setText(book.getTitle());
            vh. tv_listviewitme_author.setText(book.getAuthor());
            vh.tv_listviewitme_price.setText(book.getPublish());
            vh. tv_listviewitme_publish.setText(book.getPublish());
            vh.tv_listviewitme_remark.setText(book.getRemark());

            return v;
        }
    }
}
//项资源文件
<?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>
//main.xml
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">

    <ListView
        android:id="@+id/lv_main_bookList"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Hello World!"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

</android.support.constraint.ConstraintLayout>

猜你喜欢

转载自blog.csdn.net/qq_40979551/article/details/82726681