
说明
下面的讲解中,只创建自定义的适配器类,如何使用请参考android中常用控件的使用之ListView
1.创建类MyListViewAdapter
创建类MyListViewAdapter,该类继承BaseAdapter,并实现其抽象方法:
1
2
3
4
int getCount()
Object getItem(int position)
long getItemId(int position)
View getView(int position,View convertView,ViewGroup parent)
getCount需要返回有多少个item,也就是说最会在listview中展示这么多行
getItem需要返回参数position位置的数据
getItemId返回position就行了
2.给MyListViewAdapter类添加成员变量和构造方法
两个成员变量
1
2
List<String>list
Context context
list表示要显示的数据,context变量在生成View对象时需要用到
构造方法:构造方法是为了给两个成员变量赋值
1
2
3
4
public MyListViewAdapter(List<String>list , Context context) {
this.list = list
this.context = context
}
3.给getCount,getItem,getItemId方法添加代码
getCount需要返回有多少个item,也就是说最会在listview中展示这么多行,所以返回list.size
getItem需要返回参数position位置的数据,也就是list中第position项的值list.get(position)
getItemId返回position就行了
1
2
3
4
5
6
7
8
9
10
11
12
13
14
@Override
public int getCount() {
return list.size()
}
@Override
public Object getItem(int position) {
return list.get(position)
}
@Override
public long getItemId(int position) {
return position
}
4.给getView方法添加代码
getView方法是返回位置为position的View对象,第二个参数convertView考虑到资源重用问题,在上下滑动的过程中,需要显示某项的时候才会调用getView方法,而如果有某项被隐藏不显示,就会把不显示那一行的View作为convertView参数传入,如果没有某项被隐藏,convertView值为null。可以通过下面代码中的if(convertView!=null)中的输出来看哪一行被隐藏了。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
@Override
public View getView(int position, View convertView, ViewGroup parent) {
System.out.println("调用getView方法,显示position="+position+"项")
if(convertView!=null){
TextView t = (TextView) convertView.findViewById(R.id.firstTextView)
System.out.println(t.getText())
}else{
LayoutInflater layoutInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE)
convertView = layoutInflater.inflate(R.layout.item_mylistviewadapter, null)
}
TextView t = (TextView)(convertView.findViewById(R.id.firstTextView))
t.setText(list.get(position))
if(position%2==0)
{
t.setBackgroundColor(Color.WHITE)
}
else{
t.setBackgroundColor(Color.GRAY)
}
return convertView
}
补充:通过xml生成View对象
通过Context对象生成一个LayoutInflater对象
调用LayoutInflater对象的inflate方法生成控件对象,inflate方法的第一个参数为xml文件,第二个参数一般为null。返回值为该xml文件最外层的标签对象。
1
2
LayoutInflater layoutInflater =(LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE)
LinearLayout convertView =(LinearLayout)layoutInflater.inflate(R.layout.item_mylistvie
源代码下载
pan.baidu.com/s/1ntuQDdv
package com.testimport android.app.ListActivity
import android.os.Bundle
import android.os.Handler
import android.view.Gravity
import android.view.View
import android.view.ViewGroup
import android.widget.AbsListView
import android.widget.BaseAdapter
import android.widget.ImageView
import android.widget.LinearLayout
import android.widget.ProgressBar
import android.widget.TextView
import android.widget.AbsListView.OnScrollListener
import android.widget.LinearLayout.LayoutParams
public class test extends ListActivity implements OnScrollListener {
Aleph0 adapter = new Aleph0()
int lastItem = 0
int mProgressStatus = 0
private Handler mHandler = new Handler()
ProgressBar progressBar
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState)
LinearLayout searchLayout = new LinearLayout(this)
searchLayout.setOrientation(LinearLayout.HORIZONTAL)
progressBar = new ProgressBar(this)
progressBar.setPadding(0, 0, 15, 0)
searchLayout.addView(progressBar, new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.WRAP_CONTENT,
LinearLayout.LayoutParams.WRAP_CONTENT))
TextView textView = new TextView(this)
textView.setText("加载中...")
textView.setGravity(Gravity.CENTER_VERTICAL)
searchLayout.addView(textView, new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.FILL_PARENT,
LinearLayout.LayoutParams.FILL_PARENT))
searchLayout.setGravity(Gravity.CENTER)
LinearLayout loadingLayout = new LinearLayout(this)
loadingLayout.addView(searchLayout, new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.WRAP_CONTENT,
LinearLayout.LayoutParams.WRAP_CONTENT))
loadingLayout.setGravity(Gravity.CENTER)
getListView().addFooterView(loadingLayout)
// Start lengthy operation in a background thread
// new Thread(new Runnable() {
// public void run() {
// while (mProgressStatus <100) {
//
// // Update the progress bar
// mHandler.post(new Runnable() {
// public void run() {
// progressBar.setProgress(mProgressStatus)
// }
// })
// }
// }
// }).start()
registerForContextMenu(getListView())
setListAdapter(adapter)
getListView().setOnScrollListener(this)
}
public void onScroll(AbsListView v, int firstVisibleItem,
int visibleItemCount, int totalItemCount) {
//lastItem = firstVisibleItem + visibleItemCount - 1
//System.out.println("lastItem:" + lastItem)
}
public void onScrollStateChanged(AbsListView v, int state) {
//if (lastItem == adapter.count
//&&state == OnScrollListener.SCROLL_STATE_IDLE) {
//adapter.count += 10
//adapter.notifyDataSetChanged()
//}
if (state == OnScrollListener.SCROLL_STATE_IDLE) {
adapter.count += 10
adapter.notifyDataSetChanged()
}
}
class Aleph0 extends BaseAdapter {
int count = 10
public int getCount() {
return count
}
public Object getItem(int pos) {
return pos
}
public long getItemId(int pos) {
return pos
}
public View getView(int pos, View v, ViewGroup p) {
TextView view = new TextView(test.this)
view.setText("entry " + pos)
view.setHeight(90)
return view
}
}
}
androidstudio拉长listview的方法:1、在布局文件中添加ListView。
2、divider属性表示ListView中视图之间的分割线,dividerHeight属性表示分割线的高度,即粗细程序,将ListView的分割线设置为红色,高度设置为5dp。
3、通过数组资源或者使用适配器(Adapter)来设置ListView显示的内容即可。
欢迎分享,转载请注明来源:内存溢出
微信扫一扫
支付宝扫一扫
评论列表(0条)