转载自:http://blog.csdn.net/l_serein/article/details/6404407
需要在一个界面里放置超出屏幕容量的内容,需要用到ScrollView实现滚屏;想要将内容以列表的形式动态展示,需要用到ListView。但是如果想要在一个界面中使用多个列表,并实现整体滚屏,那么ScrollView和ListView共用的时候会出现UI问题,无法实现想要的效果。具体原因就不清楚了,至少在2.1以下是有问题的,应该算是Android的一个bug吧。
其实要实现这样的一个效果也并不难,不过不能直接使用ListView,而需要自己定义一个列表布局,并实现其Adapter设置相关的方法来实现。于是我实现了一个继承自LinearLayout的类LinearLayoutForListView,并添加Adapter相关方法,实现这一功能。
public class LinearLayoutForListView extends LinearLayout {
private ListAdapter adapter;
private OnClickListener onClickListener = null;
private OnTouchListener onTouchListener = null;
/**
* 绑定布局
*/
public void bindLinearLayout() {
int count = adapter.getCount();
for (int i = 0; i < count; i++) {
View v = adapter.getView(i, null, null);
v.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));
v.setOnTouchListener(this.onTouchListener);
v.setOnClickListener(this.onClickListener);
v.setId(i);
addView(v, i);
}
Log.v("countTAG", "" + count);
}
public LinearLayoutForListView(Context context) {
super(context);
}
public LinearLayoutForListView(Context context, AttributeSet attrs) {
super(context, attrs);
// TODO Auto-generated constructor stub
}
/**
* 获取Adapter
*
* @return adapter
*/
public ListAdapter getAdpater() {
return adapter;
}
/**
* 设置数据
*
* @param adpater
*/
public void setAdapter(ListAdapter adpater) {
this.adapter = adpater;
bindLinearLayout();
}
/**
* 获取点击事件
*
* @return
*/
public OnClickListener getOnclickListner() {
return onClickListener;
}
/**
* 设置点击事件
*
* @param onClickListener
*/
public void setOnclickLinstener(OnClickListener onClickListener) {
this.onClickListener = onClickListener;
}
public OnTouchListener getOnTouchListener() {
return onTouchListener;
}
public void setOnTouchListener(OnTouchListener onTouchListener) {
this.onTouchListener = onTouchListener;
}
}
使用的时候其实跟ListView的使用方式一样,同样是用一个继承自BaseAdapter的类对象作为数据源。
private void fillData()
{
LinearLayoutForListView mSetupList = (LinearLayoutForListView) findViewById(R.id.linear_list);
mSetupList.setOnclickLinstener(SetupListClickEvent);
mSetupList.setOnTouchListener(SetupListTouchEvent);
mSetupList.setAdapter(new ListAdapter(this));
}
public class ListAdapter extends BaseAdapter
{
private LayoutInflater mInflater;
public ListAdapter(Context context)
{
// Cache the LayoutInflate to avoid asking for a new one each time.
mInflater = LayoutInflater.from(context);
}
/**
* The number of items in the list is determined by the number of
* speeches in our array.
*
* @see android.widget.ListAdapter#getCount()
*/
public int getCount()
{
return this.itemList.length;
}
/**
* Since the data comes from an array, just returning the index is
* sufficent to get at the data. If we were using a more complex data
* structure, we would return whatever object represents one row in the
* list.
*
* @see android.widget.ListAdapter#getItem(int)
*/
public Object getItem(int position)
{
return position;
}
/**
* Use the array index as a unique id.
*
* @see android.widget.ListAdapter#getItemId(int)
*/
public long getItemId(int position)
{
return position;
}
/**
* Make a view to hold each row.
*
* @see android.widget.ListAdapter#getView(int, android.view.View,
* android.view.ViewGroup)
*/
public View getView(int position, View convertView, ViewGroup parent)
{
// Do something or create convertView
return convertView;
}
}
View.OnTouchListener SetupListTouchEvent = new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
// TODO Auto-generated method stub
return false;
}
};
View.OnClickListener SetupListClickEvent = new View.OnClickListener()
{
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
int position = v.getId();
...
}
};