ListView絕對(duì)可以稱(chēng)得上是Android中最常用的控件之一奢人。
ListView的簡(jiǎn)單用法:
首先listview是用來(lái)展示大量數(shù)據(jù)。那我們就應(yīng)該先將數(shù)據(jù)提供好殷勘。當(dāng)然數(shù)組中的數(shù)據(jù)是無(wú)法直接傳給ListView的此再,這時(shí)候我們就需要適配器。例:ArrayAdapter玲销。
在ArrayAdapter的構(gòu)造函數(shù)中依次傳入當(dāng)前上下文输拇、ListView子項(xiàng)布局的id,
以及要適配的數(shù)據(jù)贤斜。最后調(diào)用ListView的setAdapter()
方法將構(gòu)建好的適配器對(duì)象傳遞進(jìn)去策吠,就這樣ListView和數(shù)據(jù)之間的關(guān)系就建立完成了。
定制ListView的界面
在大多數(shù)情況下瘩绒,系統(tǒng)的ListView無(wú)法滿(mǎn)足業(yè)務(wù)需求猴抹,這時(shí)候我們就需要對(duì)ListView的界面進(jìn)行定制。
舉個(gè)例子:
- 定義一個(gè)實(shí)體類(lèi)锁荔,作為L(zhǎng)istView適配器的適配類(lèi)型
public class Fruit {
private String name;
private int imageId;
...
}
- 為L(zhǎng)istVIew的子項(xiàng)指定一個(gè)我們自定義的布局
<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>
- 創(chuàng)建一個(gè)自定義的適配器蟀给,此適配器繼承ArrayAdapter,并將泛型指向?qū)嶓w類(lèi):
public class FruitAdapter extends ArrayAdapter<Fruit> {
private int resourceId;
LayoutInflater mInflater = null;
public FruitAdapter(Context context , int textViewResourceId , List<Fruit> objects) {
super(context,textViewResourceId,objects);
resourceId = textViewResourceId;
}
@NonNull
@Override
public View getView(int position, View convertView, ViewGroup parent) {
Fruit fruit = getItem(position);//獲取當(dāng)前項(xiàng)的Fruit實(shí)例
//將加載好的布局進(jìn)行緩存,可重用
View view;
ViewHolder viewHolder;
if (convertView == null){
view = mInflater.inflate(resourceId,parent,false);
viewHolder = new ViewHolder();
viewHolder.fruitImage = (ImageView) view.findViewById(R.id.fruit_image);
viewHolder.fruitName = (TextView) view.findViewById(R.id.fruit_name);
view.setTag(viewHolder);
}else {
view = convertView;
viewHolder = (ViewHolder) view.getTag();
}
viewHolder.fruitImage.setImageResource(fruit.getImageId());
viewHolder.fruitName.setText(fruit.getName());
return view;
}
//借助ViewHolder跋理,將控件進(jìn)行緩存拍霜,可重用
class ViewHolder{
ImageView fruitImage;
TextView fruitName;
}
}
- MainActivity繼承BaseActivity
//BaseActivity
public abstract class BaseActivity extends AppCompatActivity{
protected Context mContext;
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
this.mContext = this;
int layoutId = getLayoutId();
View contentView = LayoutInflater.from(mContext).inflate(layoutId,null,false);
setContentView(contentView);
hideActionBar();
initData();
initView();
initList();
}
/**
*初始化數(shù)據(jù)
*/
public abstract void initData();
/**
*初始化界面
*/
public abstract void initView();
/**
*隱藏標(biāo)題欄
*/
public void hideActionBar(){
ActionBar actionBar = getSupportActionBar();
if (actionBar != null){
actionBar.hide();
}
}
/**
* 獲取布局文件的id
*
* @return
*/
public abstract int getLayoutId();
}
//MainActivity
public class MainActivity extends BaseActivity implements AdapterView.OnItemClickListener{
private List<Fruit> fruitList = new ArrayList<>();
@Override
public void initData() {
for (int i = 0 ; i<2 ;i++){
Fruit apple = new Fruit("Apple",R.drawable.apple_pic);
fruitList.add(apple);
Fruit banana = new Fruit("Banana",R.drawable.banana_pic);
fruitList.add(banana);
Fruit orange = new Fruit("Orange",R.drawable.orange_pic);
fruitList.add(orange);
Fruit watermelon = new Fruit("Watermelon",R.drawable.watermelon_pic);
fruitList.add(watermelon);
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("Strawberry",R.drawable.strawberry_pic);
fruitList.add(strawberry);
Fruit cherry = new Fruit("Cherry",R.drawable.cherry_pic);
fruitList.add(cherry);
Fruit mango = new Fruit("Mango",R.drawable.mango_pic);
fruitList.add(mango);
}
}
@Override
public void initView() {
FruitAdapter adapter = new FruitAdapter(MainActivity.this,R.layout.fruit_item,fruitList);
ListView listView = (ListView) findViewById(R.id.list_view);
listView.setAdapter(adapter);
listView.setOnItemClickListener(this);
}
@Override
public int getLayoutId() {
return R.layout.activity_main;
}
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
//點(diǎn)擊listView子項(xiàng),彈出提示框顯示該子項(xiàng)的Name
Fruit fruit = fruitList.get(position);
Toast.makeText(MainActivity.this,fruit.getName(),Toast.LENGTH_SHORT).show();
}
}
好至此就完成這個(gè)小例子了薪介。
更強(qiáng)大的滾動(dòng)控件——RecyclerView:
可以說(shuō)RecyclerView是優(yōu)化和強(qiáng)化過(guò)的ListView祠饺,Android官方也推薦使用RecyclerView。
舉個(gè)例子:
- RecyclerView也屬于新增的控件汁政,所以要先導(dǎo)包道偷。
在app/build.gradle中:
compile 'com.android.support:recyclerview-v7:24.2.1'
- 仿上一個(gè)ListView例子:
同樣準(zhǔn)備一個(gè)Fruit類(lèi)和fruit_item.xml,新建一個(gè)FruitAdapter類(lèi)记劈,讓這個(gè)類(lèi)繼承自RecyclerView.Adatper勺鸦,并將泛型指定為FruitAdapter.ViewHolder。
public class FruitAdapter extends RecyclerView.Adapter<FruitAdapter.ViewHolder>{
private List<Fruit> mFruitList;
/**
*定義一個(gè)ViewHolder的類(lèi)目木,在ViewHolder的構(gòu)造函數(shù)中傳入一個(gè)View參數(shù)换途,
*這個(gè)參數(shù)通常為RecyclerView子項(xiàng)的最外層布局。
*/
static class ViewHolder extends RecyclerView.ViewHolder{
ImageView fruitImage;
TextView fruitName;
public ViewHolder(View itemView) {
super(itemView);
fruitImage = (ImageView) itemView.findViewById(R.id.fruit_image);
fruitName = (TextView) itemView.findViewById(R.id.fruit_name);
}
}
//構(gòu)造函數(shù)刽射,用于取數(shù)據(jù)源
public FruitAdapter(List<Fruit> mFruitList) {
this.mFruitList = mFruitList;
}
//用于創(chuàng)建ViewHolder實(shí)例军拟。在此,我們將子項(xiàng)布局加載進(jìn)來(lái)誓禁,然后傳入構(gòu)造函數(shù)懈息,最后返回viewHolder。
@Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.fruit_item,parent,false);
ViewHolder viewHolder = new ViewHolder(view);
return viewHolder;
}
//用于對(duì)RecyclerView子項(xiàng)的數(shù)據(jù)進(jìn)行賦值摹恰,會(huì)在每個(gè)子項(xiàng)被滾動(dòng)到屏幕內(nèi)執(zhí)行辫继。
@Override
public void onBindViewHolder(ViewHolder holder, int position) {
Fruit fruit = mFruitList.get(position);
holder.fruitImage.setImageResource(fruit.getImageId());
holder.fruitName.setText(fruit.getName());
}
//子項(xiàng)的個(gè)數(shù)
@Override
public int getItemCount() {
return mFruitList.size();
}
}
- MainActivity
@Overridepublic void initView() {
RecyclerView recyclerView = (RecyclerView) findViewById(R.id.recycler_view);
LinearLayoutManager layoutManager = new LinearLayoutManager(this);
recyclerView.setLayoutManager(layoutManager);
FruitAdapter adapter = new FruitAdapter(fruitList);
recyclerView.setAdapter(adapter);
}
初始化界面中,我們先獲取到RecyclerView的實(shí)例俗慈,然后通過(guò)LinearLayoutManager對(duì)象以setLayoutManager()
方法設(shè)置RecyclerView的布局方式(這里使用的LinearLayoutManager是線性布局的意思)
然后實(shí)例一個(gè)adapter姑宽,通過(guò)setAdapter()
完成適配器設(shè)置。
實(shí)現(xiàn)橫向滾動(dòng)和瀑布流布局
其實(shí)實(shí)現(xiàn)橫向滾動(dòng)很簡(jiǎn)單闺阱,就如上面的例子炮车。我們只要設(shè)置layoutManager.setOrientation(LinearLayoutManager.HORIZONTAL)
就行了。
RecyclerView能輕松實(shí)現(xiàn)這些效果的原因是RecyclerView的布局排列是交給LayoutManager管理的馏颂,而LayoutManager中制定了一套可擴(kuò)展的布局排列接口示血,子類(lèi)只要按接口規(guī)范來(lái)實(shí)現(xiàn),就能制定出各種不同的排列方式的布局了救拉。
同樣RecyclerView還提供了GridLayoutManager和StaggeredGridLayoutManager這兩種內(nèi)置的布局排列方式难审。
GridLayoutManager:實(shí)現(xiàn)網(wǎng)格布局
StaggeredGridLayoutManager:實(shí)現(xiàn)瀑布流布局
實(shí)現(xiàn)瀑布流布局只要加這句就可以了StaggeredGridLayoutManager layoutManager = new StaggeredGridLayoutManager(3,StaggeredGridLayoutManager.VERTICAL);
參數(shù)一:指定布局的列數(shù);參數(shù)二:指定布局的排列方向亿絮,這里是縱向告喊。
實(shí)現(xiàn)網(wǎng)格布局加這句:GridLayoutManager layoutManager = new GridLayoutManager(GridLayoutManager.VERTICAL,3);
其參數(shù)與瀑布相似麸拄。
RecyclerView的點(diǎn)擊事件
RecyclerView的所有的點(diǎn)擊事件都由具體的View去注冊(cè)。
舉個(gè)例子:
...
static class ViewHolder extends RecyclerView.ViewHolder{
ImageView fruitImage;
TextView fruitName;
//定義一個(gè)View接收子項(xiàng)的View
View fruitView;
public ViewHolder(View itemView) {
super(itemView);
fruitView = itemView;
fruitImage = (ImageView) itemView.findViewById(R.id.fruit_image);
fruitName = (TextView) itemView.findViewById(R.id.fruit_name);
}
}
...
@Overridepublic
ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.fruit_item,parent,false);
final ViewHolder viewHolder = new ViewHolder(view);
//注冊(cè)子項(xiàng)點(diǎn)擊事件
viewHolder.fruitView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
int position = viewHolder.getAdapterPosition();
Fruit fruit = mFruitList.get(position);
Toast.makeText(v.getContext(),"you clicked view "+fruit.getName(),Toast.LENGTH_SHORT).show();
}
}
);
//注冊(cè)子項(xiàng)中的控件的點(diǎn)擊事件
viewHolder.fruitImage.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
int position = viewHolder.getAdapterPosition();
Fruit fruit = mFruitList.get(position);
Toast.makeText(v.getContext(),"you clicked image "+fruit.getName(),Toast.LENGTH_SHORT).show();
}
}
);
return viewHolder;
}
不得不說(shuō)RecyclerView確實(shí)比ListView強(qiáng)大黔姜。