代码如下:
MainActivity
package com.ss.hsx
import android.app.Activity
import android.os.Bundle
import android.widget.ArrayAdapter
public class MainActivity extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState)
setContentView(R.layout.main)
MyListView listView = (MyListView) findViewById(R.id.listView)
String[] data = new String[] { "第一个", "第二个", "第三个" }
ArrayAdapter<String>adapter = new ArrayAdapter<String>(MainActivity.this, android.R.layout.simple_list_item_1, data)
listView.setAdapter(adapter)
}
}
MyListView
package com.ss.hsx
import android.content.Context
import android.util.AttributeSet
import android.view.LayoutInflater
import android.view.View
import android.widget.ListView
public class MyListView extends ListView {
View one
public MyListView(Context context) {
super(context)
initHeaderView(context)
}
public MyListView(Context context, AttributeSet attrs) {
super(context, attrs)
initHeaderView(context)
}
public MyListView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle)
initHeaderView(context)
}
private void initHeaderView(Context context) {
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE)
one = inflater.inflate(R.layout.one, null)
View two = inflater.inflate(R.layout.two, null)
addHeaderView(one)
addHeaderView(two)
}
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
super.onMeasure(widthMeasureSpec, heightMeasureSpec)
//控制第一个头部布局的位置
one.setPadding(0, -1 * one.getHeight()/2, 0, 0)
}
}
1.加头尾部(1)定自义一个xml布局 my_headview.xml
在Activity类里找到这个自定义布局,并添加到头部
LinearLayout hearderViewLayout = (LinearLayout) LayoutInflater.(
xwActivity.this).inflate(R.layout.my_headview, null)
listview.addHeaderView(hearderViewLayout)
(2)定自义一个xml布局 my_footview.xml
LinearLayout footViewLayout = (LinearLayout) LayoutInflater.(
xwActivity.this).inflate(R.layout.my_footview, null)
listview.addFooterView(footerViewLayout)
(3)重视事务
listview必然会有Adapter,参加头尾部,必须在参加Adapter之前!
listview.addHeaderView(hearderViewLayout)
listview.addFooterView(footerViewLayout)
listview.setAdapter(myAdapter)
1.加上拉到尾部的监听件件
listview.setOnScrollListener(new OnScrollListener() {
public void onScrollStateChanged(AbsListView view, int scrollState) {
switch (scrollState) {
case OnScrollListener.SCROLL_STATE_IDLE: // 当不迁移转变时
// 断定迁移转变到底部
if (view.getLastVisiblePosition() == (view.getCount() - 1)) {
//在这里添加 *** 纵
}
break
}
}
public void onScroll(AbsListView view, int firstVisibleItem,
int visibleItemCount, int totalItemCount) {
}
})
你是的意思是在listView的每一项里都加一个固定的按钮或图片吗,那就将它写到一个xml中,然后inflate到listView中,这样listView的每一项的布局就都是xml中的布局了。欢迎分享,转载请注明来源:内存溢出
评论列表(0条)