Professional Documents
Culture Documents
CUSTOM LAYOUT
(LƯU HÀNH NỘI BỘ)
NỘI DUNG
1. Giới thiệu
ListView
Custom ListView
Vì sao phải Costum layout
Custom ListView
layout.simple_list_item_1
Data
LayoutListView.xml
(Image + String)
DATA DATA
ADAPTER ADAPTER
1. Khởi tạo dữ liệu cho mảng arr (còn gọi là data source)
String arr[]={"Teo","Ty","Bin","Bo"};
2. Khai báo Array Adapter và Gán Data source vào ArrayAdapter
ArrayAdapter<String>adapter=new ArrayAdapter<>(MainActivity.this,
android.R.layout.simple_list_item_1, arr);
3. Khai báo Listview và Đưa Adapter vào ListView
ListView lv;
lv.setAdapter(adapter);
2. Thiết kế ứng dụng với Costum – Listview
6
5
4
7
1
2
3
Các bước thực hiện:
B1: Xây dựng Layout cho activity_main.xml, Layout đơn giản, chỉ gồm 1
ListView
Các bước thực hiện:
B2: Xây dựng Layout activity_for_linear.xml, Layout này sẽ được gọi đến khi
ta click vào một hàng trong ListView
Các bước thực hiện:
EditText edtdthoai;
@Override
setContentView(R.layout.activity_for_linear);
edtdthoai = (EditText)findViewById(R.id.edtdthoai);
extra = getIntent().getExtras();
ArrayList<Phone>myArray = null;
int LayoutId;
Các bước thực hiện:
super(context, LayoutId,arr);
this.context = context;
this.LayoutId = LayoutId;
this.myArray = arr; }
Các bước thực hiện:
* hàm dùng để custom layout, ta phải override lại hàm này
* từ MainActivity truyền vào
* @param position : là vị trí của phần tử trong danh sách điện thoại
* @param convertView: convertView, dùng nó để xử lý Item
* @param parent : Danh sách điện thoại truyền từ Main
* @return View: trả về chính convertView
*/
@Override
public View getView(int position, View convertView, ViewGroup parent)
{
// TODO Auto-generated method stub
LayoutInflater inflater = context.getLayoutInflater();
convertView = inflater.inflate(LayoutId, null);
final Phone myphone = myArray.get(position);
//dòng lệnh lấy ImageView ra để hiển thị hình ảnh điện thoại
final ImageView imgitem =
(ImageView)convertView.findViewById(R.id.imgAvatar);
imgitem.setImageResource(myphone.getImg());
//dòng lệnh lấy TextView ra để hiển thị tên điện thoại
final TextView myname =
(TextView)convertView.findViewById(R.id.tvTitle);
myname.setText(myphone.getNamePhone());
return convertView;
3. Thiết kế ứng dụng với Costum - Gridview
Yêu cầu: Xây dựng ứng dụng có giao diện như sau, sử dụng Custom
GridView. Mỗi Ô trong GridView sẽ có 2 đối tượng: ImageView (Chứa hình
ảnh) và TextView (chứa tên ảnh). Khi chúng ta click vào một dòng trong
GridView, chương trình sẽ gọi đến một Activity mới có dạng như sau