您好,登錄后才能下訂單哦!
ListView的使用
ListView
是一個用來展示處于垂直的滾動列表中的items的View。這些Items來自與其相關(guān)聯(lián)的ListAdapter。ListAdapter繼承自Adapter
,它是一個鏈接ListView與 列表后面的數(shù)據(jù)的橋梁。Frequently that data comes from a Cursor, but that is not required.The ListView can display any data provided that it is wrapped in a ListAdapter.An Adapter object acts as a bridge between an AdapterView
and the underlying data for that view. The Adapter provides access to the dataitems. The Adapter is also responsible for making a View
for each item in the data set.
使用ListView
1.創(chuàng)建ListView
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" tools:context="com.cqjtu.yanguifeng.listview.MainActivity"> <ListView android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/lv_test"></ListView> </RelativeLayout>
2.創(chuàng)建一個類,該類的對象包含了ListView中的子項中的數(shù)據(jù)。我們要創(chuàng)建的案例中,一個ListView中的子項包含了兩個TextView,一個TextView是大標(biāo)題,另一個TextView是對大標(biāo)題的詳細(xì)描述,如Memory標(biāo)題下有一個4GB的描述。我們創(chuàng)建一個類,這個類有兩個成員變量,分別是描述ListView子項大標(biāo)題和詳細(xì)描述的字符串。分別是title和content。如下所示
publicclass MyData { private String title; private String content; public MyData(String title, String content) { this.title = title; this.content = content; } public String getContent() { returncontent; } public String getTitle() { returntitle; } }
3.為ListView中的子項創(chuàng)建布局文件
<?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" android:orientation="vertical"> <!-- 用來顯示子項數(shù)據(jù)之一的title --> <TextView android:id="@+id/tv_title" android:layout_width="wrap_content" android:layout_height="wrap_content" android:textSize="20sp" android:paddingTop="10dp" android:paddingLeft="5dp"/> <!-- 用來顯示子項數(shù)據(jù)之二的content --> <TextView android:id="@+id/tv_content" android:layout_width="wrap_content" android:layout_height="wrap_content" android:paddingBottom="10dp" android:paddingLeft="5dp" android:textColor="#BFBFBF"/> </LinearLayout>
4.創(chuàng)建自定義的Adapter類來加工自定義的類的對象
//創(chuàng)建適配器,使之能夠加工ListView子項背后的數(shù)據(jù)成為ListView的子項 publicclass MyDataAdapter extendsArrayAdapter<MyData> { private int itemLayoutId; // ListView子項的布局文件ID public MyDataAdapter(Context context, intresource,List<MyData> objects) { super(context, resource, objects); itemLayoutId = resource; } //當(dāng)ListView中的子項滾動到屏幕中時會調(diào)用這個方法來產(chǎn)生ListView的子項view,然后ListView顯示之 @Override public View getView(intposition, View convertView, ViewGroup parent) { // 根據(jù)position參數(shù)得到ListView子項后面的數(shù)據(jù) MyData myData = getItem(position); View view; // ListView的子項view ViewHolder viewHolder; // 存儲ListView子項view中的view if (convertView == null) // 如果沒有緩存 { view = LayoutInflater.from(getContext()).inflate(itemLayoutId, null); viewHolder = new ViewHolder(); // 將得到的view的ID保存在viewHolder的域中,方便以后的使用 // 這種方法是為了優(yōu)化ListView的顯示而作的 viewHolder.tv_title = (TextView) view.findViewById(R.id.tv_title); viewHolder.tv_content = (TextView) view.findViewById(R.id.tv_content); view.setTag(viewHolder); } else { view = convertView; viewHolder = (ViewHolder) view.getTag(); } viewHolder.tv_title.setText(myData.getTitle()); viewHolder.tv_content.setText(myData.getContent()); returnview; } class ViewHolder { TextView tv_title; TextView tv_content; } }
5.在代碼中顯式ListView
import java.util.ArrayList; import java.util.List; import android.app.Activity; import android.os.Bundle; import android.view.Menu; import android.view.MenuItem; import android.view.View; import android.widget.AdapterView; import android.widget.AdapterView.OnItemClickListener; import android.widget.ListView; import android.widget.Toast; public class MainActivity extends Activity { private ListView lv_test; private List<MyData> items = new ArrayList<MyData>(); // 初始化數(shù)據(jù) private void init() { MyData item1 = new MyData("手機型號", "MyPhone"); items.add(item1); MyData item2 = new MyData("Android version", "4.4.4"); items.add(item2); MyData item3 = new MyData("Processor", "MSM8974 four cores"); items.add(item3); MyData item4 = new MyData("Memory", "4GB"); items.add(item4); MyData item5 = new MyData("version", "123456"); items.add(item5); MyData item6 = new MyData("Phone Number", "18523415974"); items.add(item6); MyData item7 = new MyData("Name", "Yanguifeng"); items.add(item7); } @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); init(); lv_test = (ListView) findViewById(R.id.lv_test); MyDataAdapter adapter = new MyDataAdapter(this, R.layout.listview_item_layout, items); lv_test.setAdapter(adapter); lv_test.setOnItemClickListener(new OnItemClickListener() { @Override public void onItemClick(AdapterView<?> parent, View view, int position, long id) { MyData myData = (MyData) parent.getItemAtPosition(position); Toast.makeText(view.getContext(), myData.getTitle(), Toast.LENGTH_LONG).show(); } }); } @Override public boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu; this adds items to the action bar if it is present. getMenuInflater().inflate(R.menu.main, menu); return true; } @Override public boolean onOptionsItemSelected(MenuItem item) { // Handle action bar item clicks here. The action bar will // automatically handle clicks on the Home/Up button, so long // as you specify a parent activity in AndroidManifest.xml. int id = item.getItemId(); if (id == R.id.action_settings) { return true; } return super.onOptionsItemSelected(item); } }
2015-6-17日 創(chuàng)建
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。
億速云公眾號
手機網(wǎng)站二維碼
Copyright ? Yisu Cloud Ltd. All Rights Reserved. 2018 版權(quán)所有
廣州億速云計算有限公司粵ICP備17096448號-1 粵公網(wǎng)安備 44010402001142號增值電信業(yè)務(wù)經(jīng)營許可證編號:B1-20181529