Android ListView好友列表展示

Android ListView好友列表展示_第1张图片

工程文件

https://download.csdn.net/download/qq_45688164/51976770
前端


<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:ignore="ExtraText">

    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="好友列表"
        android:textSize="20dp" />

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <ListView
            android:id="@+id/listview"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_alignParentRight="true"
            android:layout_centerVertical="true"
            android:spinnerMode="dialog" />
    LinearLayout>
LinearLayout>
《item list》文件

```html

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal">

    <LinearLayout
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_weight="1">

        <ImageView
            android:id="@+id/iv_icon"
            android:layout_width="80dp"
            android:layout_height="80dp"
            android:gravity="center" />
    LinearLayout>

    <LinearLayout
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_weight="3"
        android:orientation="vertical">

        <TextView
            android:id="@+id/tv_name"
            android:textStyle="bold"
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:layout_weight="1"
            android:textColor="#000000"
            android:textSize="30dp" />

        <TextView
            android:id="@+id/neirong"
            android:textColor="#969696"
            android:layout_width="match_parent"
            android:layout_weight="1"
            android:layout_height="0dp"
            android:textSize="17sp"/>
    LinearLayout>
LinearLayout>

Java代码

```java

import androidx.appcompat.app.AppCompatActivity;

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

import java.util.ArrayList;
import java.util.HashMap;

public class MainActivity extends AppCompatActivity {

    private ArrayList datas;
    private ListView listview;
    private int[] iconArray =
            {R.mipmap.hope, R.mipmap.meili, R.mipmap.brightness,
                    R.mipmap.banye, R.mipmap.bianfeng, R.mipmap.feitian,R.mipmap.guilaixi};
    private String[] starArray = {"hope", "美丽心情", "brightness", "半叶", "彼岸风", "飞天", "归来兮"};
    private String str="这个家伙很懒";

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

        listview = findViewById(R.id.listview);
        datas = new ArrayList();

        for (int i = 0; i < starArray.length; i++) {
            HashMap listData = new HashMap();
            listData.put("image", iconArray[i]);
            listData.put("name", starArray[i]);
            listData.put("neirong",str);
            datas.add(listData);
        }
        //创建适配器对象
        SimpleAdapter starAdapter = new SimpleAdapter(this, datas,
                R.layout.item_list, new String[]{"image", "name","neirong"},
                new int[]{R.id.iv_icon, R.id.tv_name,R.id.neirong});
        //设置适配器
        listview.setAdapter(starAdapter);

        listview.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView adapterView, View view,
                                    int position, long l) {
                Toast.makeText(MainActivity.this,
                        "好友" + starArray[position], Toast.LENGTH_LONG).show();
            }
        });
    }

}

你可能感兴趣的:(android,java)