scrollView中实现listView的解决方法
1.自定义view,采用继承LinearLayout的方式
2.继承Adapter,本例中采用两种方式进行演示:
1.ArrayAdapter 2.BaseAdapter
先是第一种ArrayAdapter:
package com.terry.widget;
import java.util.List;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.TextView;
public class ArrayAdapterForLinearLayout extends ArrayAdapter<String> {
private LayoutInflater mInflater;
private int resource;
private List<String> data;
private int to;
public ArrayAdapterForLinearLayout(Context context, int resource,
int textViewResourceId, List<String> objects) {
super(context, resource, textViewResourceId, objects);
this.resource = resource;
this.data = objects;
this.to = textViewResourceId;
this.mInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
@Override
public int getCount() {
// TODO Auto-generated method stub
return data.size();
}
@Override
public String getItem(int position) {
// TODO Auto-generated method stub
return data.get(position);
}
@Override
public long getItemId(int position) {
// TODO Auto-generated method stub
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
convertView = mInflater.inflate(resource, null);
String content = data.get(position);
View view = convertView.findViewById(to);
if (view instanceof TextView) {
((TextView) view).setText(content == null ? "" : content);
}
convertView.setTag(position);
return convertView;
}
}
package com.terry.widget;
import android.content.Context;
import android.util.AttributeSet;
import android.view.View;
import android.widget.LinearLayout;
public class LinearLayoutForListView extends LinearLayout {
private ArrayAdapterForLinearLayout adapter = null;
private OnClickListener onClickListener = null;
public LinearLayoutForListView(Context context) {
super(context);
}
public LinearLayoutForListView(Context context, AttributeSet attrs) {
super(context, attrs);
// TODO Auto-generated constructor stub
}
/**
* 绑定布局
*/
public void bindLinearLayout() {
int count = adapter.getCount();
for (int i=0;i<count;i++) {
View view = adapter.getView(i, null, null);
view.setOnClickListener(this.onClickListener);
//用来消除最后一个下划线
if (i == count-1) {
LinearLayout ly = (LinearLayout) view;
ly.removeViewAt(1);
}
addView(view,i);
}
}
public ArrayAdapterForLinearLayout getAdapter() {
return adapter;
}
public void setAdapter(ArrayAdapterForLinearLayout adapter) {
this.adapter = adapter;
bindLinearLayout();
}
public OnClickListener getOnClickListener() {
return onClickListener;
}
public void setOnClickListener(OnClickListener onClickListener) {
this.onClickListener = onClickListener;
}
}
package com.terry.widget;
import java.util.ArrayList;
import java.util.List;
import android.app.Activity;
import android.os.Bundle;
import android.widget.ScrollView;
public class Main extends Activity {
private LinearLayoutForListView lv = null;
private List<String> list = new ArrayList<String>();
private ScrollView scrollView = null;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
scrollView = (ScrollView) findViewById(R.id.MAIN_VIEW_DETAIL);
scrollView.setSmoothScrollingEnabled(true);
scrollView.fullScroll(ScrollView.FOCUS_UP);
scrollView.fullScroll(ScrollView.FOCUS_DOWN);
lv = (LinearLayoutForListView) findViewById(R.id.ListView01);
for (int i=0;i<40;i++) {
list.add("name"+i);
}
ArrayAdapterForLinearLayout layoutadapter = new ArrayAdapterForLinearLayout(this, R.layout.test, R.id.TextView01, list);
lv.setAdapter(layoutadapter);
}
}
main.xml 主布局xml
<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_height="fill_parent" android:layout_width="fill_parent"
android:id="@+id/MAIN_VIEW_DETAIL" android:scrollbars="vertical">
<com.terry.widget.LinearLayoutForListView
android:orientation="vertical" android:layout_width="450px"
android:layout_height="fill_parent" android:id="@+id/ListView01">
</com.terry.widget.LinearLayoutForListView>
</ScrollView>
test.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent" android:orientation="vertical"
android:layout_height="fill_parent">
<TextView android:id="@+id/TextView01"
android:layout_marginLeft="10px"
android:layout_width="wrap_content" android:layout_height="wrap_content">
</TextView>
<View android:layout_height="1px" android:background="#FFFFFF"
android:layout_width="fill_parent"></View>
</LinearLayout>
第二种BaseAdapter:
package com.terry.widget;
import java.util.List;
import java.util.Map;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;
public class AdapterForLinearLayout extends BaseAdapter {
private LayoutInflater mInflater;
private int resource;
private List<? extends Map<String, ?>> data;
private String[] from;
private int[] to;
public AdapterForLinearLayout(Context context, List<? extends Map<String, ?>> data, int resouce, String[] from, int[] to) {
this.data = data;
this.resource = resouce;
this.data = data;
this.from = from;
this.to = to;
this.mInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
@Override
public int getCount() {
// TODO Auto-generated method stub
return data.size();
}
@Override
public Object getItem(int arg0) {
// TODO Auto-generated method stub
return data.get(arg0);
}
@SuppressWarnings("unchecked")
public String get(int position,Object key) {
Map<String, ?> map = (Map<String, ?>) getItem(position);
return map.get(key).toString();
}
@Override
public long getItemId(int arg0) {
// TODO Auto-generated method stub
return arg0;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
convertView = mInflater.inflate(resource, null);
Map<String, ?> item = data.get(position);
int count = to.length;
for (int i=0;i<count;i++) {
View view = convertView.findViewById(to[i]);
bindView(view, item, from[i]);
}
convertView.setTag(position);
return convertView;
}
private void bindView(View view,Map<String, ?> item,String from) {
Object data = item.get(from);
if (view instanceof TextView) {
((TextView) view).setText(data == null ? "" : data.toString());
}
}
}
package com.terry.widget;
import android.content.Context;
import android.util.AttributeSet;
import android.view.View;
import android.widget.LinearLayout;
public class LinearLayoutForListView extends LinearLayout {
private AdapterForLinearLayout adapter = null;
private OnClickListener onClickListener = null;
public LinearLayoutForListView(Context context) {
super(context);
}
public LinearLayoutForListView(Context context, AttributeSet attrs) {
super(context, attrs);
// TODO Auto-generated constructor stub
}
/**
* 绑定布局
*/
public void bindLinearLayout() {
int count = adapter.getCount();
for (int i=0;i<count;i++) {
View view = adapter.getView(i, null, null);
view.setOnClickListener(this.onClickListener);
if (i == count-1) {
LinearLayout ly = (LinearLayout) view;
ly.removeViewAt(2);
}
addView(view, i);
}
}
public AdapterForLinearLayout getAdapter() {
return adapter;
}
public void setAdapter(AdapterForLinearLayout adapter) {
this.adapter = adapter;
bindLinearLayout();
}
public OnClickListener getOnClickListener() {
return onClickListener;
}
public void setOnClickListener(OnClickListener onClickListener) {
this.onClickListener = onClickListener;
}
}
package com.terry.widget;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import android.app.Activity;
import android.os.Bundle;
import android.widget.ScrollView;
public class Main extends Activity {
private LinearLayoutForListView lv = null;
private List<HashMap<String, Object>> list = new ArrayList<HashMap<String,Object>>();
private ScrollView scrollView = null;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
scrollView = (ScrollView) findViewById(R.id.MAIN_VIEW_DETAIL);
scrollView.setSmoothScrollingEnabled(true);
scrollView.fullScroll(ScrollView.FOCUS_UP);
scrollView.fullScroll(ScrollView.FOCUS_DOWN);
lv = (LinearLayoutForListView) findViewById(R.id.ListView01);
for (int i = 0; i < 10; i++) {
HashMap<String, Object> map = new HashMap<String, Object>();
map.put("key_name", "name" + i);
map.put("value_name", "value" + i);
list.add(map);
}
AdapterForLinearLayout layoutadapter = new AdapterForLinearLayout(this, list, R.layout.test, new String[] { "key_name",
"value_name" }, new int[] { R.id.TextView01,R.id.TextView02 });
lv.setAdapter(layoutadapter);
}
}
主布局xml man.xml
<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_height="fill_parent" android:layout_width="fill_parent"
android:id="@+id/MAIN_VIEW_DETAIL" android:scrollbars="vertical">
<com.terry.widget.LinearLayoutForListView
android:orientation="vertical" android:layout_width="450px"
android:layout_height="fill_parent" android:id="@+id/ListView01">
</com.terry.widget.LinearLayoutForListView>
</ScrollView>
test.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent" android:orientation="vertical"
android:layout_height="fill_parent">
<TextView android:id="@+id/TextView01"
android:layout_marginLeft="10px" android:textAppearance="?android:attr/textAppearanceLarge"
android:layout_width="wrap_content" android:layout_height="wrap_content">
</TextView>
<TextView android:id="@+id/TextView02" android:layout_width="wrap_content"
android:textAppearance="?android:attr/textAppearanceSmall"
android:layout_marginLeft="10px" android:layout_height="wrap_content">
</TextView>
<View android:layout_height="1px" android:background="#FFFFFF"
android:layout_width="fill_parent"></View>
</LinearLayout>
事件操作,并通过下标得到数据源:
lv.setOnclickLinstener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
Toast.makeText(
BlueToothActivity.this,
Layoutadpater.get(Integer.parseInt(v.getTag()
.toString()), "key_name"), 1000).show();
}
});
lv.setAdapter(Layoutadpater);