ListView用法(第一行代码练习)

Posted William_Tao(攻城狮)

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了ListView用法(第一行代码练习)相关的知识,希望对你有一定的参考价值。

ListView用法

简单用法

1.创建数据

2.创建适配器

3.ListView组件关联适配器

4.创建ListView布局以及ListView单个组件的布局

activity_main

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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/listview"
    android:layout_width="match_parent"
    android:layout_height="match_parent"/>
</RelativeLayout>

simple_list_item

<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

</TextView>

MainActivity

package com.example.firstcodeandrod;

import androidx.appcompat.app.ActionBar;
import androidx.appcompat.app.AppCompatActivity;

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

public class MainActivity extends AppCompatActivity 
  //定义数据
    private String[] data="1","2","3","4","5","6","7","8","9","10";
    @Override
    protected void onCreate(Bundle savedInstanceState) 
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        //创建适配器
        ArrayAdapter<String> adapter = new ArrayAdapter<String>(MainActivity.this,R.layout.simple_list_item,data);
        //将ListView与适配器关联
        ListView listView=(ListView) findViewById(R.id.listview);
        listView.setAdapter(adapter);


    

文字➕图片版

MainActivity

package com.example.firstcodeandrod;

import androidx.appcompat.app.ActionBar;
import androidx.appcompat.app.AppCompatActivity;

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

import com.example.firstcodeandrod.dao.Fruit;

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

public class MainActivity extends AppCompatActivity 
  //定义数据
    private List<Fruit> fruitList = new ArrayList<>();
    @Override
    protected void onCreate(Bundle savedInstanceState) 
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initFruit();
        //创建适配器
        FruitAdapter fruitAdapter =new FruitAdapter(MainActivity.this,R.layout.fruit_item,fruitList);
        //将ListView与适配器关联
        ListView listView=(ListView) findViewById(R.id.listview);
        listView.setAdapter(fruitAdapter);


    

    private void initFruit() 
        for(int i=0;i<10;i++)
            Fruit apple = new Fruit("Apple",R.drawable.apple_pic);
            fruitList.add(apple);
            Fruit Banana =new Fruit("Banana",R.drawable.banana_pic);
            fruitList.add(Banana);
            Fruit watermelon = new Fruit("watermelon",R.drawable.watermelon_pic);
            fruitList.add(watermelon);
            Fruit orange = new Fruit("orange",R.drawable.orange_pic);
            fruitList.add(orange);
        
    

Fruit

package com.example.firstcodeandrod.dao;

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 void setName(String name) 
        this.name = name;
    

    public int getImageId() 
        return imageId;
    

    public void setImageId(int imageId) 
        this.imageId = imageId;
    

Adapter

package com.example.firstcodeandrod;

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 com.example.firstcodeandrod.dao.Fruit;

import java.util.List;

public class FruitAdapter extends ArrayAdapter<Fruit> 
    private int resouceId;
    public FruitAdapter( Context context, int textViewResourceId, List<Fruit> objects) 
        super(context, textViewResourceId, objects);
        resouceId = textViewResourceId;
    
   //该方法是为了让每一个子项被滚动到屏幕内的时候会被调用
    @NonNull
    @Override
    public View getView(int position, @Nullable View convertView, @NonNull ViewGroup parent) 
        Fruit fruit =getItem(position);//获取当前项的Fruit实例
        View view = LayoutInflater.from(getContext()).inflate(resouceId,parent,false);
        ImageView fruitImage = view.findViewById(R.id.fruit_img);
        TextView fruitName = view.findViewById(R.id.fruit_name);
        fruitImage.setImageResource(fruit.getImageId());
        fruitName.setText(fruit.getName());
        return view;
    

fruit_item

<?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="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/fruit_img"/>
    <TextView
        android:id="@+id/fruit_name"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center_vertical"
        android:layout_marginLeft="10dp"/>

</LinearLayout>

activity_main

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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/listview"
    android:layout_width="match_parent"
    android:layout_height="match_parent"/>
</RelativeLayout>

优化+点击事件

除了下面代码不一样,其余和上面的一样

Adapter

package com.example.firstcodeandrod;

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 androidx.recyclerview.widget.RecyclerView;

import com.example.firstcodeandrod.dao.Fruit;

import org.w3c.dom.Text;

import java.util.List;

public class FruitAdapter extends ArrayAdapter<Fruit> 
    private int resouceId;
    public FruitAdapter( Context context, int textViewResourceId, List<Fruit> objects) 
        super(context, textViewResourceId, objects);
        resouceId = textViewResourceId;
    
   //该方法是为了让每一个子项被滚动到屏幕内的时候会被调用
    @NonNull
    @Override
    public View getView(int position, @Nullable View convertView, @NonNull ViewGroup parent) 
        Fruit fruit =getItem(position);//获取当前项的Fruit实例
        View view;
        ViewHolder viewHolder;
        if(convertView ==null)
            view = LayoutInflater.from(getContext()).inflate(resouceId,parent,false);
            viewHolder = new ViewHolder();
            viewHolder.fruitImage= view.findViewById(R.id.fruit_img);
            viewHolder.fruitName = view.findViewById(R.id.fruit_name);
            view.setTag(viewHolder);//将ViewHolder存储在View当中
            
        else
            view = convertView;
            viewHolder = (ViewHolder) view.getTag();//重新获取ViewHolder
        

        viewHolder.fruitImage.setImageResource(fruit.getImageId());
        viewHolder.fruitName.setText(fruit.getName());
        return view;
    
    class ViewHolder
        ImageView fruitImage;
        TextView fruitName;
    

MainActivity

package com.example.firstcodeandrod;

import androidx.appcompat.app.ActionBar;
import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.Toast;

import com.example.firstcodeandrod.dao.Fruit;

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

public class MainActivity extends AppCompatActivity 
  //定义数据
    private List<Fruit> fruitList = new ArrayList<>();
    @Override
    protected void onCreate(Bundle savedInstanceState) 
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initFruit();
        //创建适配器
        FruitAdapter fruitAdapter =new FruitAdapter(MainActivity.this,R.layout.fruit_item,fruitList);
        //将ListView与适配器关联
        ListView listView=(ListView) findViewById(R.id.listview);
        listView.setAdapter(fruitAdapter);
        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() 
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) 
                Fruit fruit=fruitList.get(position);
                Toast.makeText(MainActivity.this,fruit.getName(),Toast.LENGTH_SHORT).show();
            
        );

    

    private void initFruit() 
        for(int i=0;i<10;i++)
            Fruit apple = new Fruit("Apple",R.drawable.apple_pic);
            fruitList.add(apple);
            Fruit Banana =new Fruit("Banana",R.drawable.banana_pic以上是关于ListView用法(第一行代码练习)的主要内容,如果未能解决你的问题,请参考以下文章

最常用和最难用的控件--ListView(Android第一行代码)

3.1课堂练习

c# listview 如何改变某一行的位置

c# listView1写入第一行第一列

在 Listview 的第一行添加 ViewFlipper

突出显示 ListView 和 ListBox 控件中的第一行