安卓开发:自定义ListView布局,并实现监听事件

实现的功能:为ListView自定义了界面,并实现了监听点击事件。
效果如下:
这里写图片描述
这里写图片描述

工程结构如下:
这里写图片描述

Language.java:

package com.example.aiden.example;

/**
 * Created by Aiden on 2016/2/18.
 */
public class Language {
    private String name; // 编程语言的名字
    private int imageId; // 编程语言的图像id

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

    public String getName() {
        return name;
    }

    public int getImageId() {
        return imageId;
    }

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

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

MyAdapter.java:

package com.example.aiden.example;

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 Aiden on 2016/2/18.
 */
public class MyAdapter extends ArrayAdapter<Language> {
    private int resourceId;

    // 初始化
    // 第一个参数是上下文,第二个参数是每个子项目的布局文件,第三个是每个子项目的内容
    public MyAdapter(Context context, int textViewResourceId, List<Language> list) {
        super(context, textViewResourceId, list);
        // 得到加载子项目的资源文件
        resourceId = textViewResourceId;
    }

    // 重写getView()方法,用于将每个子项目都加载响应的资源
    @Override
    public View getView(int position, View convertView, ViewGroup viewGroup) {
        // 得到被选中的子项目
        Language language = this.getItem(position);
        // 得到被选中的资源文件
        View view = LayoutInflater.from(this.getContext()).inflate(resourceId, null);
        // 加载图片
        ImageView imageView = (ImageView) view.findViewById(R.id.imageId);
        imageView.setImageResource(language.getImageId());
        // 加载文字
        TextView name = (TextView) view.findViewById(R.id.name);
        name.setText(language.getName());
        return view;
    }
}

MainActivity.java:

package com.example.aiden.example;

import android.app.AlertDialog;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;

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

public class MainActivity extends AppCompatActivity implements AdapterView.OnItemClickListener {
    private List<Language> list = new ArrayList<Language>();
    ListView listView;

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

        init();
        // 获得适配器
        MyAdapter myAdapter = new MyAdapter(this, R.layout.item, list);
        // 为ListView设置适配器
        listView = (ListView) this.findViewById(R.id.listView);
        listView.setAdapter(myAdapter);
        // 注册监听事件
        listView.setOnItemClickListener(this);
    }

    // 用于向list增加Language对象
    public void init() {
        Language java = new Language("Java", R.drawable.java);
        list.add(java);

        Language c = new Language("C++", R.drawable.c);
        list.add(c);

        Language python = new Language("Python", R.drawable.python);
        list.add(python);

        Language swift = new Language("swift", R.drawable.swift);
        list.add(swift);

        Language php = new Language("php", R.drawable.php);
        list.add(php);
    }

    // 重写监听事件
    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        // 如果是listView对象被点击
        if (parent == listView) {
            // 获取show.xml布局文件的对象
            // 并分别拿到imageView和textView控件
            LinearLayout linearLayout = (LinearLayout) this.getLayoutInflater().inflate(R.layout.show, null);
            ImageView imageView = (ImageView) linearLayout.findViewById(R.id.showImage);
            TextView textView = (TextView) linearLayout.findViewById(R.id.showText);

            // 拿到被选中的子项目的图片以及文字
            ImageView imageId = (ImageView) view.findViewById(R.id.imageId);
            TextView name = (TextView) view.findViewById(R.id.name);

            // 将被选中的子项目图片以及文字加载到imageView和textView中
            imageView.setImageDrawable(imageId.getDrawable());
            textView.setText(name.getText().toString());

            // 新建弹出窗类对象,并将linearLayout加载到该对象中
            AlertDialog.Builder builder = new AlertDialog.Builder(this);
            builder.setTitle("您选择了");
            builder.setView(linearLayout);
            builder.create();
            builder.show();
        }
    }
}

上述三个java类的代码注释已经挺详细的了,这里就不解释了。
activity_main.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"
    android:orientation="vertical">

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

</LinearLayout>

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"
    android:orientation="horizontal">

    <ImageView
        android:id="@+id/imageId"
        android:layout_width="100dp"
        android:layout_height="100dp" />

    <TextView
        android:id="@+id/name"
        android:textSize="50dp"
        android:gravity="center_vertical"
        android:layout_width="match_parent"
        android:layout_height="100dp" />
</LinearLayout>

show.xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="horizontal">

    <ImageView
        android:id="@+id/showImage"
        android:layout_width="100dp"
        android:layout_height="100dp" />

    <TextView
        android:id="@+id/showText"
        android:layout_width="match_parent"
        android:layout_height="100dp"
        android:gravity="center_vertical"
        android:textSize="50dp" />

</LinearLayout>

以及五张图片:
分别是这里写图片描述

遇到的几个问题:
1、在适配器中一定要实现getView方法。该方法的作用是:将资源加载到View中,并显示出来。
2、在实现监听点击事件的时候,onItemClick(AdapterView<?> parent, View view, int position, long id) 这里有四个参数。第一个就是ListView对象,第二个就是被点击的子项目。如果我们在弹出窗口将被点击的子项目显示出来的时候,调用new Builder().setView(view);则会报错。因为每个View只能被加到一个View上。
3、获取layout文件对象的方法:
在Activity类中:this.getLayoutInflater().inflater(R.layout.布局文件名,null);
在非Activity类中:
Context.getSystemService(Context.LAYOUT_INFLATER_SERVICE).inflater(R.layout.布局文件名,null);

4、这里提供该工程的下载:
点击下载

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值