[Android]listview图文混排

简介: 一、效果图:二、代码实现2.1ListViewActivitypackage cn.ac.ucas.yp.uiwidgettest;import android.

一、效果图:

这里写图片描述

二、代码实现

2.1ListViewActivity

package cn.ac.ucas.yp.uiwidgettest;

import android.support.v7.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 java.util.ArrayList;
import java.util.List;

public class ListViewActivity extends AppCompatActivity {

    private String[] data = {"Apple", "Orange", "Watermelon", "Pear"};
    private List<Fruit> fruitList = new ArrayList<Fruit>();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.listview);

        initFruits();
        ListView listview1 = (ListView) findViewById(R.id.listview1);
        FruitAdapter fruitAdapter = new FruitAdapter(ListViewActivity.this, R.layout.fruit_item, fruitList);
        listview1.setAdapter(fruitAdapter);
        /*
        ArrayAdapter<String> adapter=new ArrayAdapter<String>(ListViewActivity.this,android.R.layout.simple_list_item_1,data);
        listview1.setAdapter(adapter);
        */
        listview1.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                Fruit fruit = fruitList.get(position);
                Toast.makeText(ListViewActivity.this, fruit.getName(), Toast.LENGTH_SHORT).show();
            }
        });
    }

    private void initFruits() {
        Fruit apple = new Fruit("Apple", R.drawable.apple, "治下痢、降血脂、滋润皮肤。");
        fruitList.add(apple);
        Fruit banana = new Fruit("banana", R.drawable.bananas, "香蕉被称为百果之冠,具有润肠通便,清热解毒,健脑益智,通血脉,填精髓,降血压等功效。");
        fruitList.add(banana);
        Fruit cherry = new Fruit("cherry", R.drawable.cherry, "樱桃补中益气,祛风胜湿");
        fruitList.add(cherry);
        Fruit grape = new Fruit("grape", R.drawable.grape, "葡萄营养丰富、酸甜可口,因此成为世界四大水果(苹果、葡萄、柑橘和香蕉)之一");
        fruitList.add(grape);
        Fruit kiwi = new Fruit("kiwi", R.drawable.kiwi, "能促使血液循环顺畅,增进性能力。");
        fruitList.add(kiwi);
        Fruit lemon = new Fruit("lemon", R.drawable.lemon, "可增强消化、出汗过多、食欲不振、体力倦怠、减肥解酒。");
        fruitList.add(lemon);
        Fruit mango = new Fruit("mango", R.drawable.mango, "芒果果实营养价值极高,维生素A的含量比杏子还要多出1倍 ");
        fruitList.add(mango);
        Fruit orange = new Fruit("orange", R.drawable.orange, "桔子含水量高、营养丰富");
        fruitList.add(orange);
        Fruit pear = new Fruit("pear", R.drawable.pear, "清解热毒、镇咳化痰。");
        fruitList.add(pear);
        Fruit persimmon = new Fruit("persimmon", R.drawable.persimmon, "柿子营养丰富,但要吃得健康,不过要注意一些与柿子相克的食物 ");
        fruitList.add(persimmon);
        Fruit pineapple = new Fruit("pineapple", R.drawable.pineapple, "菠萝清香宜人,汁多味甜,广受人们喜爱。");
        fruitList.add(pineapple);
        Fruit strawberry = new Fruit("strawberry", R.drawable.strawberry,
                "去火、解暑、清热;促进胃肠蠕动、帮助消化、改善便秘;预防痔疮、肠癌 ");
        fruitList.add(strawberry);
        Fruit watermelon = new Fruit("watermelon", R.drawable.watermelon, "西瓜味道甘味多汁,清爽解渴,是盛夏佳果。");
        fruitList.add(watermelon);


    }
}

2.2ListViewActivity对应的布局文件listview.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="cn.ac.ucas.yp.uiwidgettest.ListViewActivity">
    <ListView
        android:id="@+id/listview1"
        android:layout_width="match_parent"
        android:layout_height="match_parent"></ListView>

</RelativeLayout>

2.3fruit.java

package cn.ac.ucas.yp.uiwidgettest;

/**
 * Created by yp on 16/4/15.
 */
public class Fruit {
    private String name;
    private int imageId;
    private String introduction;

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

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getIntroduction() {
        return introduction;
    }

    public void setIntroduction(String introduction) {
        this.introduction = introduction;
    }

    public int getImageId() {
        return imageId;
    }

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

2.4FruitAdapter.java

package cn.ac.ucas.yp.uiwidgettest;

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 java.util.List;

/**
 * Created by yp on 16/4/15.
 */
public class FruitAdapter extends ArrayAdapter {

    private int resourceId;

    public FruitAdapter(Context context, int textViewResourceId, List<Fruit> objects) {
        super(context, textViewResourceId, objects);
        resourceId = textViewResourceId;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        Fruit fruit = (Fruit) getItem(position);
        View view;
        if (convertView == null) {
            view = LayoutInflater.from(getContext()).inflate(resourceId, null);
        } else {
            view = convertView;
        }
        ImageView fruitImage = (ImageView) view.findViewById(R.id.fruit_img);
        TextView fruitName = (TextView) view.findViewById(R.id.fruit_name);
        TextView fruitIntroduction = (TextView) view.findViewById(R.id.fruit_introduction);

        fruitImage.setImageResource(fruit.getImageId());
        fruitName.setText(fruit.getName());
        fruitIntroduction.setText(fruit.getIntroduction());
        return view;

    }
}

2.4fruit_item.xml

<?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="match_parent">

    <LinearLayout
        android:layout_width="0dip"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:orientation="vertical">

        <ImageView
            android:id="@+id/fruit_img"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center" />

        <TextView
            android:id="@+id/fruit_name"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_gravity="center"
            android:layout_marginLeft="10dip"
            android:gravity="center"
            android:paddingLeft="0dp" />

    </LinearLayout>

    <LinearLayout
        android:layout_width="0dip"
        android:layout_height="wrap_content"
        android:layout_weight="2">

        <TextView
            android:id="@+id/fruit_introduction"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_gravity="center" />
    </LinearLayout>


</LinearLayout>
目录
相关文章
|
7月前
|
API Android开发 开发者
Android UI设计: 什么是RecyclerView?为什么它比ListView更好?
Android UI设计: 什么是RecyclerView?为什么它比ListView更好?
98 2
|
6月前
|
API Android开发 开发者
`RecyclerView`是Android API 21引入的UI组件,用于替代ListView和GridView
【6月更文挑战第26天】`RecyclerView`是Android API 21引入的UI组件,用于替代ListView和GridView。它提供高效的数据视图复用,优化的布局管理,支持多种布局(如线性、网格),并解耦数据、适配器和视图。RecyclerView的灵活性、性能(如局部刷新和动画支持)和扩展性使其成为现代Android开发的首选,特别是在处理大规模数据集时。
72 2
|
7月前
|
XML Java Android开发
Android Studio App入门之列表视图ListView的讲解及实战(附源码 超详细必看)
Android Studio App入门之列表视图ListView的讲解及实战(附源码 超详细必看)
744 1
|
6月前
|
前端开发 API Android开发
25. 【Android教程】列表控件 ListView
25. 【Android教程】列表控件 ListView
219 2
|
7月前
|
XML Java Android开发
如何美化android程序:自定义ListView背景
如何美化android程序:自定义ListView背景
70 2
|
7月前
|
XML 编解码 Java
Android控件之高级控件——ListView、cardView、屏幕适配
Android控件之高级控件——ListView、cardView、屏幕适配
|
XML Android开发 数据格式
安卓-无敌解决ListView添加标题头无法正常显示的问题(歪门邪道)
安卓-无敌解决ListView添加标题头无法正常显示的问题(歪门邪道)
81 0
|
Java Android开发
[笔记]Android 学习一之转场动画+ViewPager+ListView简单Demo
[笔记]Android 学习一之转场动画+ViewPager+ListView简单Demo
|
XML Java Android开发
Android优化版ListView(附源代码)
本文是博主对Adapter(适配器)的一些理解,为了加深对Adapter的理解以及记录自己的阶段学习而写,同时也适合初学者阅读,参考本条博客的逻辑进行学习。
149 0
|
Android开发
安卓中listview点击每一条进入不同界面
安卓中listview点击每一条进入不同界面
113 0