Android: ListView 和重写后 MyListView 的简单使用

本文介绍了Android中ListView的基本使用,强调了在处理多个相似内容时ListView的便捷性,并提供了一个简单的ListView实例。此外,针对ScrollView与ListView结合导致的问题,文章详细解释了如何重写ListView为MyListView来解决只显示一行数据的异常,确保在实际设备上能正常工作。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

什么是 ListView ?它有什么用?


如这个页面,多个新闻信息:,一个个在xml写是不可能的!

所以,我们需要用ListView循环写!

一个LinearLayout 布局中,

就一个图片,一个标题,一个时间,一个评论数量!是不是很少啊?




一、普通的ListView 例子

为了方便大家理解,我就写个超级简单的例子吧:

代码的组成:一个方法,2个xml页面,图片若张(自备)




1、EsotericActivity.java 

(我这里的xml 很多重复,所以我用了循环,但是有时候还是不能偷懒)

package com.open_open.android_plantest;

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

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

public class EsotericActivity extends Activity {
    private ListView listOne;
    //定义一个集合存放所有学生信息
    private SimpleAdapter adapter;
    private List<Map<String,Object>> data=null;
    private String planArray[]={"训练须知","训练须知2","训练须知3","训练须知4","训练须知5","训练须知6"};

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

        listOne = (ListView) findViewById(R.id.listOne);
//构建适配器【首先定义好自己的布局】
        getData();  //获取数据
        String[] from = {"Photo1", "Title1", "Title2", "Photo2"};
        int[] to = {R.id.photo1, R.id.title1, R.id.title2, R.id.photo2};
        adapter = new SimpleAdapter(this, data, R.layout.activity_plan, from, to);
        listOne.setAdapter(adapter);

    }
    private void getData() {
        data = new ArrayList<Map<String, Object>>();
//构建一项内容
        Map<String, Object> item = new HashMap<String, Object>();

        for(int i=0;i<planArray.length;i++){
            item = new HashMap<String, Object>();
            item.put("Photo1", R.drawable.plan1);
            item.put("Title1", planArray[i]);
            item.put("Title2", "");
            item.put("Photo2", R.drawable.go_in);
            data.add(item);
        }
    }

}


正常的情况:getDate() 的写法

  private void getData() {
        data = new ArrayList<Map<String,Object>>();
//构建一项内容
        Map<String,Object> item=new HashMap<String, Object>();

        item=new HashMap<String,Object>();
        item.put("Photo",R.drawable.intel);
        item.put("Title","Intel/英特尔 I7-4790K LGA 1150 4.0GHZ");
        item.put("Test","英特尔官方旗舰店");
        item.put("Comment","成交:14");
        data.add(item);

        item=new HashMap<String,Object>();
        item.put("Photo",R.drawable.intel);
        item.put("Title","英特尔至强/Xeon E5-2630v2 6核12线程2.6GHzDell/戴尔 E5-2630v2");
        item.put("Test","酷睿专卖店");
        item.put("Comment","成交:7");
        data.add(item);

        item=new HashMap<String,Object>();
        item.put("Photo",R.drawable.jijia);
        item.put("Title","GIGABYTE/技嘉GTX1080 Xtreme W-8G萤火虫 水之力GTX1080游戏显卡");
        item.put("Test","技嘉旗舰店");
        item.put("Comment","成交:13");
        data.add(item);

        item=new HashMap<String,Object>();
        item.put("Photo",R.drawable.shandi);
        item.put("Title","Sandisk/闪迪 SDSSDA-120G SSD笔记本 台式机电脑固态硬盘非128G");
        item.put("Test","和瑟数码专营店");
        item.put("Comment","成交:1711");
        data.add(item);

        item=new HashMap<String,Object>();
        item.put("Photo",R.drawable.sanxing);
        item.put("Title","Kingston/金士顿 SV300S37A/120G");
        item.put("Test","易华旗舰店");
        item.put("Comment","成交:65");
        data.add(item);

        item=new HashMap<String,Object>();
        item.put("Photo",R.drawable.sanxing);
        item.put("Title","发布在即:三星Galaxy C5 Pro获Wi-Fi认证");
        item.put("Test","三星旗舰店");
        item.put("Comment","成交:142");
        data.add(item);

        item=new HashMap<String,Object>();
        item.put("Photo",R.drawable.leishe);
        item.put("Title","Razer/雷蛇 炼狱蝰蛇 3500dpi 激光 5个");
        item.put("Test","雷蛇亿果专卖店  ");
        item.put("Comment","成交:2w");
        data.add(item);
    }



2、activity_esoteric.xml




<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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:orientation="vertical"
    tools:context="com.open_open.android_plantest.EsotericActivity">

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="50dp"
            android:background="@color/cblue"
            android:orientation="horizontal"
            android:gravity="center">

            <RelativeLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content">
                <ImageView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:src="@drawable/go_back"
                    android:gravity="left"
                    android:layout_margin="5dp"/>
                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="训练秘籍"
                    android:textSize="20dp"
                    android:textColor="@android:color/white"
                    android:textStyle="bold"
                    android:layout_margin="10dp"
                    android:layout_centerHorizontal="true" />
            </RelativeLayout>
        </LinearLayout>

        <ListView
            android:id="@+id/listOne"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:divider="@color/cbrown"
            android:dividerHeight="0.3dp"
            android:listSelector="@android:color/holo_blue_light"/>

    </LinearLayout>


3.activity_plan.xml

我们有一个头像photo1,一个标题title1, 一个隐藏的标题title 2(占位,把图标顶向右边),

一个向右的图标photo2!

 



<?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"
    android:orientation="vertical"
    android:layout_margin="8dp">

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal">
    <ImageView
        android:id="@+id/photo1"
        android:layout_width="55dp"
        android:layout_height="55dp"
        android:layout_margin="5dp"
        android:src="@drawable/plan1"/>
    <TextView
        android:id="@+id/title1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="训练须知"
        android:textSize="25dp"
        android:textColor="@color/cbrown"
        android:layout_gravity="center"
        android:layout_marginLeft="10dp"/>

    <TextView
        android:id="@+id/title2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_weight="1"/>
    <ImageView
        android:id="@+id/photo2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@drawable/go_in"
        android:layout_gravity="center"/>
</LinearLayout>

</LinearLayout>

结果:



二、重写ListView 的例子!


有时候,我们的布局不是LinearLayout 那么简单,

由于页面比较长,我们使用了 ScrollView 可滑动布局

但是ScrollView 布局中,ListView 只显示一行数据啊!

这个时候,我们就需要用到MyListView




MyListView : 如图,好像一行都不显示了? #滑稽




别担心!虽然在studio 中不显示,但是在手机中,虚拟机中还是正常的!


我们重写一下ListView !

1、MyListView.java  (新建一个activity ,但是没有xml )


package com.open_open.android_plantest;

import android.content.Context;
import android.util.AttributeSet;
import android.widget.ListView;

/**
 * Created by Administrator on 2016/12/28.
 */

public class MyListView extends ListView {

    public MyListView(Context context) {
        // TODO Auto-generated method stub
        super(context);
    }

    public MyListView(Context context, AttributeSet attrs) {
        // TODO Auto-generated method stub
        super(context, attrs);
    }

    public MyListView(Context context, AttributeSet attrs, int defStyle) {
        // TODO Auto-generated method stub
        super(context, attrs, defStyle);
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        // TODO Auto-generated method stub
        int expandSpec = MeasureSpec.makeMeasureSpec(Integer.MAX_VALUE >> 2,
                MeasureSpec.AT_MOST);
        super.onMeasure(widthMeasureSpec, expandSpec);
    }
}


2、ListView 改为 MyListView 




改完后,看看自己的项目是不是照样可以用呢?


PS : ListView 也自带可 滑动的效果!


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值