10 January 2017

Android: Hiển thị ListView có hình ảnh (Android Studio)

Hiển thị ListView có hình ảnh 
Bằng việc tạo Adapter riêng kế thừa từ BaseAdapter
Lưu ý: Ở ví dụ này sẽ dùng rất nhiều.
item_contact.xml
Java Android 2017
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="match_parent"
    android:layout_height="match_parent">
    <ImageView
        android:id="@+id/iv_avatar"
        android:src="@drawable/avatar1"
        android:layout_width="70dp"
        android:layout_height="70dp" />
    <TextView
        android:id="@+id/tv_name"
        android:text="Duong Huu Dai"
        android:textSize="15sp"
        android:layout_marginTop="13dp"
        android:layout_toRightOf="@id/iv_avatar"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />
    <TextView
        android:id="@+id/tv_phone"
        android:text="098372323"
        android:textColor="#ff0000"
        android:textSize="19sp"
        android:layout_toRightOf="@id/iv_avatar"
        android:layout_marginTop="5dp"
        android:layout_below="@id/tv_name"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />
</RelativeLayout>
activity_main.xml
Java Android 2017
<?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:id="@+id/activity_main"
    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="com.example.lonely.demo.MainActivity">

    <ListView
        android:id="@+id/lv_contact"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        />
</RelativeLayout>
ContactModel.java
Java Android 2017
package com.example.lonely.demo;

/**
 * Created by Lonely on 1/9/2017.
 */

public class ContactModel {
    private String name;
    private String phone;
    private int image;

    public ContactModel(String name, String phone, int image) {
        this.phone = phone;
        this.image = image;
        this.name = name;
    }

    public String getName() {
        return name;
    }

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

    public String getPhone() {
        return phone;
    }

    public void setPhone(String phone) {
        this.phone = phone;
    }

    public int getImage() {
        return image;
    }

    public void setImage(int image) {
        this.image = image;
    }
}
ContactAdapter.java
Java Android 2017
package com.example.lonely.demo;

import android.app.Activity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import java.util.List;

/**
 * Created by Lonely on 1/9/2017.
 */

public class ContactAdapter extends BaseAdapter {
    private List<ContactModel> listContact;
    private Activity activity;

    public ContactAdapter(List<ContactModel> listContact, Activity activity) {
        this.listContact = listContact;
        this.activity = activity;
    }

    @Override
    public int getCount() {
        return listContact.size();
    }

    @Override
    public Object getItem(int position) {
        return null;
    }

    @Override
    public long getItemId(int position) {
        return 0;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        if (convertView == null) {
            LayoutInflater inflater = activity.getLayoutInflater();
            convertView = inflater.inflate(R.layout.item_contact, parent, false);
            ViewHolder holder = new ViewHolder();
            holder.tvName = (TextView) convertView.findViewById(R.id.tv_name);
            holder.tvPhone = (TextView) convertView.findViewById(R.id.tv_phone);
            holder.ivAvatar = (ImageView) convertView.findViewById(R.id.iv_avatar);
            convertView.setTag(holder);
        }
        ViewHolder holder = (ViewHolder) convertView.getTag();
        ContactModel model = listContact.get(position);
        holder.tvName.setText(model.getName());
        holder.tvPhone.setText(model.getPhone());
        holder.ivAvatar.setImageResource(model.getImage());

        return convertView;
    }

    //Tạo một lần duy nhất để tránh việc tạo nhiều lần làm chậm ứng dụng
    static class ViewHolder {
        TextView tvName;
        TextView tvPhone;
        ImageView ivAvatar;
    }
}
MainActivity.java
Java Android 2017
package com.example.lonely.demo;

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

public class MainActivity extends AppCompatActivity {
    private ListView lvContact;
    private List<ContactModel> listContact = new ArrayList<>();
    //Khi làm việc với ListView thường sẽ có 3 bước chính
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        //Bước 1: Tạo data
        initData();

        //Bước 2: Tạo adapter ở ví dụ này chúng ta tự tạo một Adapter không phụ thuộc vào Adapter có sẵn
        ContactAdapter adapter = new ContactAdapter(listContact, this);

        //Bước 3: Tạo ListView Sét adapter vào ListView
        lvContact = (ListView) findViewById(R.id.lv_contact);
        lvContact.setAdapter(adapter);

        //Bắt sự kiện click vào ListView  dòng bao nhiêu nhận biết thông qua position
        lvContact.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                ContactModel contactModel = listContact.get(position);
                Toast.makeText(MainActivity.this, contactModel.getName(), Toast.LENGTH_SHORT).show();
            }
        });
    }

    public void initData() {
        listContact.add(new ContactModel("Nguyen Van A", "098987923521", R.drawable.avatar1));
        listContact.add(new ContactModel("Nguyen Van B", "098987923522", R.drawable.avatar2));
        listContact.add(new ContactModel("Nguyen Van D", "098987923524", R.drawable.avatar3));
        listContact.add(new ContactModel("Nguyen Van E", "098987923525", R.drawable.avatar4));
        listContact.add(new ContactModel("Nguyen Van A", "098987923521", R.drawable.avatar1));
        listContact.add(new ContactModel("Nguyen Van B", "098987923522", R.drawable.avatar2));
        listContact.add(new ContactModel("Nguyen Van D", "098987923524", R.drawable.avatar3));
        listContact.add(new ContactModel("Nguyen Van E", "098987923525", R.drawable.avatar4));
        listContact.add(new ContactModel("Nguyen Van A", "098987923521", R.drawable.avatar1));
        listContact.add(new ContactModel("Nguyen Van B", "098987923522", R.drawable.avatar2));
        listContact.add(new ContactModel("Nguyen Van D", "098987923524", R.drawable.avatar3));
        listContact.add(new ContactModel("Nguyen Van E", "098987923525", R.drawable.avatar4));
        listContact.add(new ContactModel("Nguyen Van A", "098987923521", R.drawable.avatar1));
        listContact.add(new ContactModel("Nguyen Van B", "098987923522", R.drawable.avatar2));
        listContact.add(new ContactModel("Nguyen Van D", "098987923524", R.drawable.avatar3));
        listContact.add(new ContactModel("Nguyen Van E", "098987923525", R.drawable.avatar4));
    }
}

0 nhận xét:

Post a Comment

 

BACK TO TOP

Xuống cuối trang