安卓学习之ListView

 简单的用法

ListView是一个非常强大的控件,先看看它的简单用法。

首先,我们在主界面的xml代码中添加这段代码

<ListView
       android:id="@+id/list_item"
       android:layout_width="match_parent"
       android:layout_height="match_parent"
       ></ListView>

然后我们在MainActivity中使用适配器来生成界面。

package com.example.listviewtext01;

import androidx.appcompat.app.AppCompatActivity;

import android.app.Activity;
import android.os.Bundle;
import android.widget.ArrayAdapter;
import android.widget.ListView;

public class MainActivity extends AppCompatActivity {

    private String[] data={
            "aa","bb","cc" ,"aa","bb","cc", "aa","bb","cc",
    };
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
         //第一个参数上下文环境,第二个参数默认的适配布局,第三个参数是数据
        ArrayAdapter<String> adapter=new ArrayAdapter<String>(getApplicationContext(),
               android.R.layout.simple_expandable_list_item_1,data);
        ListView listView=(ListView)findViewById(R.id.list_view);
        listView.setAdapter(adapter);
    }
}

效果如下

安卓学习之ListView

 自定义ListView

1.创建在ListView中的子布局界面

<?xml version="1.0" encoding="utf-8"?>
<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"
   >
    <ImageView
        android:id="@+id/image"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        ></ImageView>
    <TextView
        android:id="@+id/text"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        ></TextView>



</LinearLayout>

创建要添加的数据类型

package com.example.listviewtext01;

public class Fruit {


    private String name;
    private int imageId;
    public Fruit(String name,int imageId)
    {
        this.imageId=imageId;
        this.name=name;
    }
    public int getImageId()
    {
        return imageId;
    }
    public String getName()
    {
        return name;
    }

}

创建自己的适配器

package com.example.listviewtext;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import java.util.List;

public class FruitAdapter extends ArrayAdapter<Fruit> {
    private int resouleId;

    public FruitAdapter(Context context, int textViewResouleID, List<Fruit> object)
    {
        super(context,textViewResouleID,object);
        resouleId=textViewResouleID;
    }
    @NonNull
    @Override
    public View getView(int position, @Nullable View convertView, @NonNull ViewGroup parent) {
       Fruit fruit=getItem(position);//获取实例
        View view= LayoutInflater.from(getContext()).inflate(resouleId,parent,false);
        ImageView fruitimage=(ImageView)view.findViewById(R.id.fruit_imag);
        TextView fruitName=(TextView)view.findViewById(R.id.fruit_name);
        fruitimage.setImageResource(fruit.getImageid());
        fruitName.setText(fruit.getName());
        return view;

    }
}

使用界面

package com.example.listviewtext;

import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.ArrayAdapter;
import android.widget.ListView;

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

public class MainActivity extends AppCompatActivity {



    private String data[]=
            {
                    "hello","jasbd","asnbd","sdhnambd","Sdhabd","sndba"
            };
    private List<Fruit>fruitList=new ArrayList<>();
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
       /* ArrayAdapter<String> adapter = new ArrayAdapter<String>(MainActivity.this
        ,android.R.layout.simple_list_item_1,data);*/
       initFruits();
       FruitAdapter adapter=new FruitAdapter(MainActivity.this,R.layout.activity_fruit_layout
       ,fruitList);
        ListView listView=(ListView)findViewById(R.id.list_View);
        listView.setAdapter(adapter);
    }
    public void initFruits()
    {
      for(int i=0;i<15;i++)
      {
          Fruit apple=new Fruit("apple",R.drawable.ic_launcher_background);
          fruitList.add(apple);
      }
    }

}

 

安卓学习之ListView安卓学习之ListView 不学好学习不改网名 发布了135 篇原创文章 · 获赞 9 · 访问量 7041 私信 关注
上一篇:Django之视图 ListView


下一篇:数据绑定控件简介