【Android】ListView控件总结
? ? ? ?ListView控件是列表視圖展示,排列方式是縱向。其中ListView控件有自己默認(rèn)的布局,當(dāng)默認(rèn)布局不能滿足我們要求時(shí),我們可以進(jìn)行自定義控件,下面我們?cè)敿?xì)分析ListView控件的使用。
一、默認(rèn)效果
思路:在layout下創(chuàng)建一個(gè)布局,添加ListView控件,在Activity中對(duì)ListView控件賦值,其中ListView的方法setAdapter起了舉足輕重的作用,而setAdapter()方法中需要傳入一個(gè)adapter類(lèi)型的數(shù)據(jù),從此可以進(jìn)入了童話世界。
其中官方解釋文檔是這樣說(shuō)的:
/*** Sets the data behind this ListView.** The adapter passed to this method may be wrapped by a {@link WrapperListAdapter},* depending on the ListView features currently in use. For instance, adding* headers and/or footers will cause the adapter to be wrapped.** @param adapter The ListAdapter which is responsible for maintaining the* data backing this list and for producing a view to represent an* item in that data set.** @see #getAdapter()*/其大意是:這個(gè)適配器通過(guò)這個(gè)方法可以封裝成你具有你自己特點(diǎn)的ListView控件
? ? ? Activity詳解
package com.first.tglistviewdemo.ListViewDefault;import android.os.Bundle; import android.support.annotation.Nullable; import android.support.v7.app.ActionBar; import android.support.v7.app.AppCompatActivity; import android.view.View; import android.widget.Adapter; import android.widget.AdapterView; import android.widget.ArrayAdapter; import android.widget.ListView; import android.widget.Toast;import com.first.tglistviewdemo.R;public class ListViewDefaultActivity extends AppCompatActivity {private ListView lv;private String[] fruit = {"apple","pear","Orange","Cherry","Mango","apple","pear","Orange","Cherry","Mango","apple","pear","Orange","Cherry","Mango","apple","pear","Orange","Cherry","Mango"};@Overrideprotected void onCreate(@Nullable Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_listview_default);lv = findViewById(R.id.lv_1);ArrayAdapter arrayAdapter = new ArrayAdapter(ListViewDefaultActivity.this, android.R.layout.simple_list_item_1, fruit);lv.setAdapter(arrayAdapter); ////修改標(biāo)題欄ActionBar actionBar = getSupportActionBar();if (actionBar != null) {actionBar.setTitle("默認(rèn)的ListView樣式");}//點(diǎn)擊事件lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {@Overridepublic void onItemClick(AdapterView<?> parent, View view, int position, long id) {Toast.makeText(ListViewDefaultActivity.this,"position"+position+":"+fruit[position]+",id"+id,Toast.LENGTH_SHORT).show();}});lv.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {@Overridepublic boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id) {Toast.makeText(ListViewDefaultActivity.this,"position"+position+":"+fruit[position]+",id"+id,Toast.LENGTH_SHORT).show();return true; //fasle 長(zhǎng)按點(diǎn)擊回調(diào)函數(shù)不會(huì)銷(xiāo)毀,會(huì)繼續(xù)執(zhí)行click true:長(zhǎng)按回調(diào)函數(shù)將銷(xiāo)毀}});} }?
二、自定義效果
自定義效果只不過(guò)在setAdapter()方法中的參數(shù)動(dòng)了下手腳
思路:我們創(chuàng)建一個(gè)adapter的子類(lèi),其所有的adapter都是繼承自BaseAdapter的,同樣的我們只要繼承一個(gè)其adapter類(lèi)即可。
/*** Get a View that displays the data at the specified position in the data set. You can either* create a View manually or inflate it from an XML layout file. When the View is inflated, the* parent View (GridView, ListView...) will apply default layout parameters unless you use* {@link android.view.LayoutInflater#inflate(int, android.view.ViewGroup, boolean)}* to specify a root view and to prevent attachment to the root.* * @param position The position of the item within the adapter's data set of the item whose view* we want.* @param convertView The old view to reuse, if possible. Note: You should check that this view* is non-null and of an appropriate type before using. If it is not possible to convert* this view to display the correct data, this method can create a new view.* Heterogeneous lists can specify their number of view types, so that this View is* always of the right type (see {@link #getViewTypeCount()} and* {@link #getItemViewType(int)}).* @param parent The parent that this view will eventually be attached to* @return A View corresponding to the data at the specified position.*/官方是這樣解釋的:在數(shù)據(jù)集中得到一個(gè)子列,你同樣可以創(chuàng)建一個(gè)視圖或者填充這個(gè)視圖來(lái)之XML布局,當(dāng)這個(gè)視圖控件被填充,將會(huì)提供一個(gè)默認(rèn)的布局樣式,除非你用了一個(gè)自己的布局
package com.first.tglistviewdemo.ListViewDefault;import android.content.Context; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.ArrayAdapter; import android.widget.TextView; import android.widget.Toast;import com.first.tglistviewdemo.R;import java.util.List;public class LvdAdapter extends ArrayAdapter<Fruit> {private int resourceid;public LvdAdapter(Context context, int resourceViewId, List<Fruit> data) {super(context, resourceViewId,data);resourceid=resourceViewId;//當(dāng)前自己構(gòu)造的Layout布局的ID}static class ViewHolder{private TextView fname;private TextView price;private TextView protime;}@Overridepublic View getView(int position, View convertView, ViewGroup parent) {Fruit fruit=getItem(position);Toast.makeText(getContext(),fruit.getFname(),Toast.LENGTH_SHORT).show();ViewHolder viewHolder;if(convertView==null){convertView= LayoutInflater.from(getContext()).inflate(resourceid,null);viewHolder=new ViewHolder();viewHolder.fname= convertView.findViewById(R.id.txt_fname);viewHolder.price=convertView.findViewById(R.id.txt_price);viewHolder.protime=convertView.findViewById(R.id.txt_protime);convertView.setTag(viewHolder);}else{convertView=convertView;viewHolder= (ViewHolder) convertView.getTag();}viewHolder.fname.setText(fruit.getFname());viewHolder.protime.setText(fruit.getPrice());viewHolder.price.setText(fruit.getPrice());return convertView;} }?
總結(jié)
以上是生活随笔為你收集整理的【Android】ListView控件总结的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: java学习(126):throw向上抛
- 下一篇: 第二十期:想吃透监控系统,就这一篇够不够