自定義控件
第一步:搭建好布局
<?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="wrap_content"
android:background="@drawable/title_bg"
>
<Button
android:id="@+id/title_back"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_margin="5dp"
android:background="@drawable/back_bg"
android:text="Back"
android:textColor="#fff"
/>
<TextView
android:id="@+id/tiitle_text"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_weight="1"
android:gravity="center"
android:text="Title Text"
android:textColor="#fff"
android:textSize="24sp"
/>
<Button
android:id="@+id/title_edit"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_margin="5dp"
android:background="@drawable/edit_bg"
android:text="Edit"
android:textColor="#fff"
/>
</LinearLayout>
第二步,實(shí)現(xiàn)自定義控件的點(diǎn)擊事件等
具體做法:創(chuàng)建一個(gè)類繼承自LinearLayout,然后動(dòng)態(tài)加載第一步搭建的xml文件
public class TitleLayout extends LinearLayout {
//重寫構(gòu)造函數(shù)
public TitleLayout(Context context, AttributeSet attrs) {
super(context, attrs);
//LayoutInflater的from()方法可以構(gòu)造一個(gè)LayoutInflater對(duì)象难咕,然后調(diào)用inflate可以動(dòng)態(tài)加載一個(gè)布局文件
//inflate()方法接收兩個(gè)參數(shù),1.要加載的布局文件的id泛烙,2.加載好的布局添加一個(gè)父布局理卑。
LayoutInflater.from(context).inflate(R.layout.title, this);
//監(jiān)聽返回按鈕的點(diǎn)擊事件
Button titleBack = (Button) findViewById(R.id.title_back);
Button titleEdit = (Button) findViewById(R.id.title_edit);
titleBack.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
//當(dāng)前activity出棧
((Activity) getContext()).finish();
}
});
//Edit按鈕的點(diǎn)擊事件
titleEdit.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(getContext(), "點(diǎn)擊了編輯按鈕", Toast.LENGTH_SHORT).show();
}
});
}
}
第三步:在activity的layout文件中添加自定義控件
<?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">
<com.example.anwser_mac.customviewtest.TitleLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
/>
</LinearLayout>
ListView的簡單用法
類似iOS的TableView
第一步,布局ListView
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<ListView
android:id="@+id/list_view"
android:layout_width="match_parent"
android:layout_height="match_parent">
</ListView>
</LinearLayout>
第二步蔽氨,設(shè)置ListView展示的數(shù)據(jù)
需要注意的是藐唠,ListView的數(shù)據(jù)展示需要借助適配器來實(shí)現(xiàn)帆疟;ArrayAdapter適配器可以通過泛型來指定要適配的數(shù)據(jù)類型。
public class MainActivity extends AppCompatActivity {
private String[] data = {"apple", "apple", "apple", "apple", "apple", "apple", "apple", "apple", "apple", "apple", "apple", "apple", "apple", "apple", "apple"};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.layout);
//ArrayAdapter構(gòu)造函數(shù)宇立,依次傳入當(dāng)前上下文踪宠,ListView子項(xiàng)布局的id,以及要適配的數(shù)據(jù)
//android.R.layout.simple_list_item_1 :Android的一個(gè)內(nèi)置布局文件泄伪,里面只有一個(gè)TextView
ArrayAdapter<String> adapter = new ArrayAdapter<String>(MainActivity.this, android.R.layout.simple_list_item_1, data);
ListView listView = (ListView) findViewById(R.id.list_view);
listView.setAdapter(adapter);
}
}
自定義ListView的界面
第一步:定義一個(gè)實(shí)體類殴蓬,存儲(chǔ)ListView要顯示的數(shù)據(jù)。
新建Fruit實(shí)體類
public class Fruit {
//水果名
private String name;
//顯示的圖片id
private int imageId;
public Fruit(String name, int imageId) {
this.name = name;
this.imageId = imageId;
}
public String getName() {
return name;
}
public int getImageId() {
return imageId;
}
}
第二步:搭建ListViewItem蟋滴,用來展示實(shí)體類存儲(chǔ)的數(shù)據(jù)
新建listItem.xml布局文件
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView
android:id="@+id/fruit_image"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<TextView
android:id="@+id/fruit_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:layout_marginLeft="10dp"/>
</LinearLayout>
第三步染厅,自定義適配器(Adapter)
創(chuàng)建一個(gè)FruitAdapter繼承自ArrayAdapter
public class FruitAdapter extends ArrayAdapter<Fruit> {
private int resourceId;
//重寫構(gòu)造方法,將上下文津函、ListView子項(xiàng)布局的ID和數(shù)據(jù)都傳進(jìn)來
public FruitAdapter(Context context, int textViewResourceId, List<Fruit> objects) {
super(context, textViewResourceId, objects);
resourceId = textViewResourceId;
}
//重寫getView()方法肖粮,這個(gè)方法在子項(xiàng)被滾動(dòng)屏幕內(nèi)時(shí)調(diào)用
@NonNull
@Override
public View getView(int position, View convertView, ViewGroup parent) {
//獲取當(dāng)前項(xiàng)的Fruit實(shí)例
Fruit fruit = getItem(position);
View view;
//創(chuàng)建一個(gè)viewHolder來緩存控件,這樣就不用每次加載的時(shí)候都要調(diào)用findViewById()來獲得控件的實(shí)例了
ViewHodler viewHolder;
// 加載布局
if (convertView == null) {
//創(chuàng)建
view = LayoutInflater.from(getContext()).inflate(resourceId, parent, false);
viewHolder = new ViewHodler();
viewHolder.fruitImage = (ImageView) view.findViewById(R.id.fruit_image);
viewHolder.fruitName = (TextView) view.findViewById(R.id.fruit_name);
//把viewHolder存入view中
view.setTag(viewHolder);
} else {
view = convertView;
//重新取出viewHolder
viewHolder = (ViewHodler) view.getTag();
}
//設(shè)置布局顯示的數(shù)據(jù)
viewHolder.fruitImage.setImageResource(fruit.getImageId());
viewHolder.fruitName.setText(fruit.getName());
return view;
}
}
class ViewHodler {
ImageView fruitImage;
TextView fruitName;
}
第四步:在activity中加載listView
public class MainActivity extends AppCompatActivity {
private List<Fruit> fruitList = new ArrayList<>();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.layout);
//初始化水果數(shù)據(jù)
initFruits();
FruitAdapter adapter = new FruitAdapter(MainActivity.this, R.layout.fruit_item, fruitList);
//設(shè)置listView的數(shù)據(jù)
ListView listView = (ListView) findViewById(R.id.list_view);
listView.setAdapter(adapter);
}
private void initFruits() {
for (int i = 0; i < 2; i++) {
Fruit apple = new Fruit("apple", R.drawable.apple_pic);
fruitList.add(apple);
Fruit bananna = new Fruit("Banana", R.drawable.banana_pic);
fruitList.add(bananna);
Fruit orange = new Fruit("Orange", R.drawable.orange_pic);
fruitList.add(orange);
Fruit watermelo = new Fruit("waterelon", R.drawable.watermelon_pic);
fruitList.add(watermelo);
Fruit pear = new Fruit("Pear", R.drawable.pear_pic);
fruitList.add(pear);
Fruit grape = new Fruit("Grape", R.drawable.grape_pic);
fruitList.add(grape);
Fruit pineapple = new Fruit("Pineapple", R.drawable.pineapple_pic);
fruitList.add(pineapple);
Fruit strawberry = new Fruit("cherry", R.drawable.cherry_pic);
fruitList.add(strawberry);
}
}
}
ListView的點(diǎn)擊事件
使用setOnItemClickLister()方法為ListView注冊(cè)一個(gè)監(jiān)聽器尔苦,當(dāng)用戶點(diǎn)擊ListView的任何一個(gè)子項(xiàng)時(shí)涩馆,就會(huì)回調(diào)onItemClick()方法。
///監(jiān)聽listView的點(diǎn)擊事件
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Fruit fruit = fruitList.get(position);
Toast.makeText(MainActivity.this, fruit.getName(), Toast.LENGTH_SHORT).show();
}
});