日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

ListView详解 (ListView图文混排)

發布時間:2025/6/15 编程问答 26 豆豆
生活随笔 收集整理的這篇文章主要介紹了 ListView详解 (ListView图文混排) 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

ListView類

Class Overview


A view that shows items in a vertically scrolling list. The items come from the?ListAdapter?associated with this view.

一、使用數據 適配器, 創建ListView: 1、布局文件ListView 2、創建數據適配器 3、ListView與適配器關聯,數據加載到ListView上 ? ? ?lv.setAdapter(ada);
1 activity_main.xml

<LinearLayout?xmlns:android="http://schemas.android.com/apk/res/android"

? ??android:layout_width="fill_parent"

? ??android:layout_height="fill_parent"

? ??android:orientation="vertical"?>


? ??<ListView

? ? ? ??android:id="@+id/listView1"

? ? ? ??android:layout_width="match_parent"

? ? ? ??android:layout_height="wrap_content"?>

? ??</ListView>

? ?

</LinearLayout>


2 數據適配器ListAdapter及實現類

public interface

ListAdapter

implements?Adapter
android.widget.ListAdapter
Known Indirect Subclasses ArrayAdapter<T>,?BaseAdapter,?CursorAdapter,?HeaderViewListAdapter,?ResourceCursorAdapter,?SimpleAdapter,?SimpleCursorAdapter,?WrapperListAdapter

Class Overview


Extended?Adapter?that is the bridge between a?ListView?and the data that backs the list. 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.

創建數據適配器:

//getView,getCount方法需要覆寫,才會顯示List記錄

BaseAdapter?adapter?=?new?BaseAdapter() {

? ? ? ? ? ? ? ? ? ? ? ? //List中所存放的View,如果是只是一個TextView(簡單數據),則返回的是TextView.


TextView tv =?new?TextView(AdapterActivity.this);

tv.setPadding(8, 8, 8, 8);

tv.setTextSize(20);

tv.setText(arrs[position]);


//如果是一個自定義的復雜的布局,如微博的列表的布局,一行中包含有圖片,擺放不同位置的TextView,返回的就是一個layout

@Override

public?View getView(int?position, View convertView, ViewGroup parent) {

//?TODO?Auto-generated method stub

// 1、獲得行布局 用LayoutInflater


// 2、更新行布局?

return?null;

}

//顯示List條數

@Override

public?int?getCount() {

//?TODO?Auto-generated method stub

return?0;

}

@Override

public?long?getItemId(int?position) {

//?TODO?Auto-generated method stub

return?0;

}

@Override

public?Object getItem(int?position) {

//?TODO?Auto-generated method stub

return?null;

}


};


//只有一個TextView的行的布局,listitem.xml (如果是復雜的布局,用布局加控件)

<?xml?version="1.0"?encoding="utf-8"?>

<TextView?xmlns:android="http://schemas.android.com/apk/res/android"

? ??android:layout_width="match_parent"

? ??android:layout_height="match_parent"?>


</TextView>



二、如果ListView一行是TextView,可以采用ArrayAdapter來實現,不用創建BaseAdapter 1、布局文件activity_main.xml一樣 2、Activity

public?class?ArrayAdapterActivity?extends?Activity {

ListView?lv?;

@Override

protected?void?onCreate(Bundle savedInstanceState) {

//?TODO?Auto-generated method stub

super.onCreate(savedInstanceState);

setContentView(R.layout.activity_main);

lv?= (ListView) findViewById(R.id.listView1);

String[] arr ={"aaa","bbb","ccc","ddd","eee"};

ArrayAdapter<String> arrayAdapter =?new?ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, arr);

lv.setAdapter(arrayAdapter);

}


}



simple_list_item_1是android系統提供的一個layout,每個列表項都是一個普通的TextView




? 三、自定義多控件的行布局: 1、效果 ? 2、行布局文件custom_list.xml(activity_main.xml同前)

<?xml?version="1.0"?encoding="utf-8"?>

<RelativeLayout?xmlns:android="http://schemas.android.com/apk/res/android"

? ??android:layout_width="match_parent"

? ??android:layout_height="match_parent"?>


? ??<ImageView

? ? ? ??android:id="@+id/imageView1"

? ? ? ??android:layout_width="wrap_content"

? ? ? ??android:layout_height="wrap_content"

? ? ? ??android:layout_alignParentLeft="true"

? ? ? ??android:layout_alignParentTop="true"

? ? ? ??android:layout_marginLeft="20dp"

? ? ? ??android:layout_marginTop="20dp"

? ? ? ??android:src="@drawable/libai"?/>


? ??<TextView

? ? ? ??android:id="@+id/textView2"

? ? ? ??android:layout_width="wrap_content"

? ? ? ??android:layout_height="wrap_content"

? ? ? ??android:layout_above="@+id/textView1"

? ? ? ??android:layout_marginLeft="16dp"

? ? ? ??android:layout_toRightOf="@+id/textView1"

?? ? ??android:textSize="20dp"/>


? ??<TextView

? ? ? ??android:id="@+id/textView1"

? ? ? ??android:layout_width="wrap_content"

? ? ? ??android:layout_height="wrap_content"

? ? ? ??android:layout_alignParentLeft="true"

? ? ? ??android:paddingLeft="20dp"

? ? ? ??android:layout_below="@+id/imageView1"

? ? ? ??android:text="TextView"?/>


</RelativeLayout>


3、 Activity ?public?class?MainActivity?extends?Activity {

private?String[]?names?=?new?String[]{"虎頭","李白","弄玉","清照"};

private?int[]?imagesIds?=?new?int[]{R.drawable.tiger,R.drawable.libai,R.drawable.nongyu,R.drawable.qingzhao};

private?String[]?contents?=?new?String[]{

"很可愛的小男孩的名字",

"唐代著名詩人,有一首詩,叫舉頭望明月,啊,故鄉。。。。",

"一個小小女孩",

"尋尋覓覓,冷冷清清,凄凄慘慘戚戚。"

};

ListView?lv?;


@Override

protected?void?onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.activity_main);

lv?= (ListView)findViewById(R.id.listView1);

//LayoutInflater inflater = getLayoutInflater();?

BaseAdapter ada =?new?BaseAdapter() {

//獲取每一行的布局

@Override

public?View getView(int?position, View view, ViewGroup viewGroup) {

//?TODO?Auto-generated method stub

//獲取行布局

LayoutInflater inflater = getLayoutInflater();

RelativeLayout layout = (RelativeLayout)inflater.inflate(R.layout.custom_list,?null);

//行布局中的控件

ImageView image = (ImageView)layout.findViewById(R.id.imageView1);

TextView? tv1 = (TextView)layout.findViewById(R.id.textView1);

TextView? tv2 = (TextView)layout.findViewById(R.id.textView2);

//更新行布局中的內容

image.setImageResource(imagesIds[position]);

tv1.setText(names[position]);

tv2.setText(contents[position]);

return?layout;

}

@Override

public?int?getCount() {

//?TODO?Auto-generated method stub

return?names.length;

}




@Override

public?Object getItem(int?position) {

//?TODO?Auto-generated method stub

return?null;

}




@Override

public?long?getItemId(int?position) {

//?TODO?Auto-generated method stub

return?0;

}

};

lv.setAdapter(ada);

}


}



四、使用SimpleAdapter來實現上面的ListView的布局 SimpleAdapter構造方法需要5個參數:

Public Constructors


public?SimpleAdapter?(Context?context,?List<??extends?Map<String,??>> data, int resource,?String[]?from, int[] to)

Added in?API level 1

Constructor

Parameters
context data resource from to
The context where the View associated with this SimpleAdapter is running
A List of Maps. Each entry in the List corresponds to one row in the list. The Maps contain the data for each row, and should include all the entries specified in "from"
Resource identifier of a view layout that defines the views for this list item. The layout file should include at least those named views defined in "to"
A list of column names that will be added to the Map associated with each item.
The views that should display column in the "from" parameter. These should all be TextViews. The first N views in this list are given the values of the first N columns in the from parameter.

public?class?MainActivity?extends?Activity {

private?String[]?names?=?new?String[]{"虎頭","李白","弄玉","清照"};

private?int[]?imagesIds?=?new?int[]{R.drawable.tiger,R.drawable.libai,R.drawable.nongyu,R.drawable.qingzhao};

private?String[]?contents?=?new?String[]{

"很可愛的小男孩的名字",

"唐代著名詩人,有一首詩,叫舉頭望明月,啊,故鄉。。。。",

"一個小小女孩",

"尋尋覓覓,冷冷清清,凄凄慘慘戚戚。"

};

ListView?lv?;


@Override

protected?void?onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.activity_main);

lv?= (ListView)findViewById(R.id.listView1);

List<Map<String,Object>> listItems =?new?ArrayList<Map<String,Object>>();

for?(int?i = 0; i <?names.length; i++) {

Map<String,Object> listItem =?new?HashMap<String,Object>();

listItem.put("header",?imagesIds[i]);

listItem.put("personName",?names[i]);

? ?? listItem.put("content",contents[i]);

listItems.add(listItem);

}

SimpleAdapter?adapter?=?new?SimpleAdapter(this, listItems, R.layout.custom_list,?

new?String[]{"personName","header","content"},?

new?int[]{R.id.name,R.id.header,R.id.content});


lv.setAdapter(adapter);

}



}


五、ListView事件
OnItemClick

public abstract void?onItemClick?(AdapterView<?> parent,?View?view, int position, long id)

Added in?API level 1

Callback method to be invoked when an item in this AdapterView has been clicked.

Implementers can call getItemAtPosition(position) if they need to access the data associated with the selected item.

Parameters
parent view position id
The AdapterView where the click happened.
The view within the AdapterView that was clicked (this will be a view provided by the adapter)
The position of the view in the adapter.
The row id of the item that was clicked.

Item長按事件

public abstract boolean?onItemLongClick?(AdapterView<?> parent,?View?view, int position, long id)

Added in?API level 1

Callback method to be invoked when an item in this view has been clicked and held. Implementers can call getItemAtPosition(position) if they need to access the data associated with the selected item.

Parameters
parent view position id
The AbsListView where the click happened
The view within the AbsListView that was clicked
The position of the view in the list
The row id of the item that was clicked
Returns
  • true if the callback consumed the long click, false otherwise

總結

以上是生活随笔為你收集整理的ListView详解 (ListView图文混排)的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。