ListView

ListView是安卓之前常用的列表View组件,现在用的最多RecylerView不过还是要了解个大概, 代码如下

package com.example.xyz.baseadapterlearn;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.ListView;
import android.widget.SimpleAdapter;

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

public class test1Activity extends AppCompatActivity {
    public List<HashMap<String,String>> data;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_test1);
        mmmset();
        SimpleAdapter simpleAdapter = new SimpleAdapter(test1Activity.this,data,android.R.layout.simple_list_item_2,new String[] {"text1","text2"},
                new int[] {android.R.id.text1,android.R.id.text2});
        ListView listView = findViewById(R.id.listview_1);
        listView.setAdapter(simpleAdapter);
    }

    public void mmmset(){
        data = new ArrayList<>();
        for(int i = 0;i<15;i++){
            HashMap<String,String> map = new HashMap<>();
            map.put("text1","李");
            map.put("text2","业成");
            data.add(map);
        }
    }




}

首先定义List集合data,HashMap是一个散列表,它存储的内容是键值对(key-value)映射。定义泛型String

定义方法mmmset实例化给data数据,定义Hashmap map 保存键值后添加到list中

后面通过listView.setAdapter,构建视图

SimpleAdapter的参数说明

第一个参数 表示访问整个android应用程序接口,基本上所有的组件都需要

第二个参数表示生成一个Map(String ,Object)列表选项

第三个参数表示界面布局的id 表示该文件作为列表项的组件

第四个参数表示该Map对象的哪些key对应value来生成列表项

第五个参数表示来填充的组件 Map对象key对应的资源一依次填充组件 顺序有对应关系

注意的是map对象可以key可以找不到 但组件的必须要有资源填充 因为 找不到key也会返回null 其实就相当于给了一个null资源

下面的程序中如果 new String[] { “name”, “head”, “desc”,“name” } new int[] {R.id.name,R.id.head,R.id.desc,R.id.head}

这个head的组件会被name资源覆盖

RecyclerView

使用RecyclerView需要在\app\build.gradle的dependencies到入包

implementation 'com.android.support:recyclerview-v7:24.2.1'

在布局文件设置RecyclerView

    <LinearLayout 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"
        >

        <android.support.v7.widget.RecyclerView
            android:id="@+id/recycler_view"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            />
    </LinearLayout>

还有设置每个item的样式文件

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

    >
    <ImageView
        android:layout_width="100dp"
        android:layout_height="100dp"
        android:id="@+id/fruit_image"
        android:layout_gravity="center"
        />

    <TextView
        android:id="@+id/fruitname"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginLeft="10dp"
        android:text="测试" />

    <ImageView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginLeft="200dp"
        android:src="@mipmap/ic_launcher" />

</LinearLayout>

建个Fruit类,方便统一控制

package com.example.xyz.recyclerview;

public class Fruit {
        private String name;
        private int imageId;

        public Fruit(String name, int imageId){
            this.name = name;
            this.imageId = imageId;

        }

        public String getName() {
            return name;
        }

        public int getImageId() {
            return imageId;
        }

}

建适配器类

package com.example.xyz.recyclerview;

import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import java.util.List;



public class FruitAdapter extends RecyclerView.Adapter<FruitAdapter.ViewHolder> {   //这里的ViewHolder为内部类
    //private static final String TAG = "测试";
    private List<Fruit> mFruitList;
    
    static class ViewHolder extends RecyclerView.ViewHolder {
        ImageView fruitImage;
        TextView fruitName;

        public ViewHolder(View view) {
            super(view);
            fruitImage = (ImageView) view.findViewById(R.id.fruit_image);
            fruitName = (TextView) view.findViewById(R.id.fruitname);
        }

    }

    public FruitAdapter(List<Fruit> fruitList) {
        mFruitList = fruitList;
    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.fruit_item, parent, false);
        final ViewHolder holder = new ViewHolder(view);
        holder.fruitImage.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                int position = holder.getAdapterPosition();   //获取点击位置索引
                Fruit fruit = mFruitList.get(position);   //List的方法get获取对象
                Toast.makeText(v.getContext(),"图片为" + fruit.getName(),Toast.LENGTH_SHORT).show();
            }
        });
        return holder;
    }

    @Override
    public void onBindViewHolder(ViewHolder holder, int position) {

        Fruit fruit = mFruitList.get(position);
        holder.fruitImage.setImageResource(fruit.getImageId());
        holder.fruitName.setText(fruit.getName());
    }

    @Override
    public int getItemCount() {
        //Log.d(TAG,String.valueOf(mFruitList.size()));
        return mFruitList.size();
    }

}

主程序

package com.example.xyz.recyclerview;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;

import java.util.ArrayList;
import java.util.List;

public class MainActivity extends AppCompatActivity {
    private RecyclerView mrvMain;
    private List<Fruit> fruitlist = new ArrayList<>();
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        mrvMain = (RecyclerView) findViewById(R.id.recycler_view);
        initFruit();
        //mrvMain.setLayoutManager(new LinearLayoutManager(MainActivity.this));
        LinearLayoutManager layoutManager = new LinearLayoutManager(MainActivity.this);
        //layoutManager.setOrientation(LinearLayoutManager.HORIZONTAL);
        mrvMain.setLayoutManager(layoutManager);
        FruitAdapter adapter = new FruitAdapter(fruitlist);
        mrvMain.setAdapter(adapter);
    }


    private void initFruit() {
        for (int i =0;i<2;i++){
            Fruit apple = new Fruit("Apple",R.drawable.imgtest);
            fruitlist.add(apple);
            Fruit banana = new Fruit("banana",R.drawable.imgtest);
            fruitlist.add(banana);
            Fruit orange = new Fruit("orange",R.drawable.imgtest);
            fruitlist.add(orange);
            Fruit pear = new Fruit("Apple",R.drawable.imgtest);
            fruitlist.add(pear);
            Fruit pineapple = new Fruit("Apple",R.drawable.imgtest);
            fruitlist.add(pineapple);
        }
    }
}

这里开始新建两个mrvMain,fruitlist集合集合存放Fruit用于存放Fruit实例,initFruit()方法使用add存放Fruit在fruitlist集合中、

LinearLayoutManager layoutManager = new LinearLayoutManager(MainActivity.this);

mrvMain.setLayoutManager(layoutManager);

设置布局方式为线性布局 FruitAdapter adapter = new FruitAdapter(fruitlist);实例化适配器,FruitAdapter类会从FruitAdapter构成方法中调用重写的方法

FruitAdapter继承RecyclerView.Adapter。因为必须重写onCreateViewHolder(),onBindViewHolder()和getItemCount()三个方法

mrvMain.setAdapter(adapter);设置适配器,生成视图

效果如下 avatar