涉及知识点:ListView、SimpleAdapter和ListView点击事件等

文章目录

一、运行图

二、代码

文章内容

一、运行图

在这里插入图片描述
运行效果:
在这里插入图片描述

二、代码

1.activity_main.xml

<?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"
    android:orientation="vertical"
    tools:context=".MainActivity">
    <ListView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:id="@+id/listView"
        android:divider="@color/cardview_shadow_start_color"
        android:dividerHeight="5dp"/>

</LinearLayout>

2.logic.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/imageView"
        android:layout_width="200dp"
        android:layout_height="150dp" />

    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginLeft="20dp"
        android:orientation="vertical">

        <TextView
            android:id="@+id/textView1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textSize="26sp"
            android:textColor="@color/design_default_color_secondary_variant"/>

        <TextView
            android:id="@+id/textView2"
            android:textColor="@color/design_default_color_primary_variant"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textSize="25sp" />
    </LinearLayout>

</LinearLayout>

3.MySimpleAdapter.java

package com.example.test2;

import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.SimpleAdapter;

import java.util.List;
import java.util.Map;

public class MySimpleAdapter extends SimpleAdapter {
    private Context context;

    public void setIndex(int index) {
        this.index = index;
    }
    private int index=-1;
    public MySimpleAdapter(Context context, List<? extends Map<String, ?>> data, int resource, String[] from, int[] to) {
        super(context, data, resource, from, to);
        this.context=context;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        View view = super.getView(position, convertView, parent);
        if (position%2 == 0) {
            view.setBackgroundColor(this.context.getResources().getColor(R.color.purple_200));
        }
        else {
            view.setBackgroundColor(this.context.getResources().getColor(R.color.teal_200));
        }
        if(position==index){
            view.setBackgroundColor(this.context.getResources().getColor(R.color.white));
        }
        return view;
    }
}

package com.example.test2;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.Toast;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


public class MainActivity extends AppCompatActivity {
    private ListView listView;
    private String[] titles={"一尾","二尾","三尾","四尾","五尾","六尾","七尾","八尾","九尾"};
    private String [] infos={"守鹤","又旅","矶抚","孙悟空","穆王","犀犬","重明","牛鬼","九喇嘛"};
    private int []images={R.drawable.yiwei, R.drawable.erwei,
    R.drawable.sanwei,R.drawable.siwei,R.drawable.wuwei,R.drawable.liuwei,R.drawable.qiwei,
    R.drawable.bawei,R.drawable.jiuwei};
    private List<Map<String,Object>> list=new ArrayList<>();

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

        for(int i=0;i<titles.length;i++){
            Map<String,Object> item=new HashMap<>();
            item.put("images",images[i]);
            item.put("titles",titles[i]);
            item.put("infos",infos[i]);
            list.add(item);
        }
       MySimpleAdapter mySimpleAdapter=new MySimpleAdapter(this,list,R.layout.logic
                , new String[]{"images","titles","infos"},
                new int[]{R.id.imageView,R.id.textView2,R.id.textView1});
        listView.setAdapter(mySimpleAdapter);

        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                Map<String ,Object>temp=list.get(i);
                mySimpleAdapter.setIndex(i);
                mySimpleAdapter.notifyDataSetChanged();
                Toast.makeText(MainActivity.this, temp.get("titles")+"-"+temp.get("infos"),Toast.LENGTH_SHORT)
                        .show();
            }
        });

    }
}