中三A105论坛

注册 登录
查看: 835|回复: 0

ScrollView套嵌ListView

[复制链接]

11

主题

21

帖子

77

积分

注册会员

Rank: 2

积分
77
发表于 2018-12-4 16:59:03 | 显示全部楼层 |阅读模式

我们可以重写一个adapter,然后重写一个linearlayout,就像listview调用adapter一样一样的。用adapter还是主要用它里面的getView和bindview。然后在自定义的linearlayout里自己写个方法循环的把子项添加到当前的linearlayout就搞定了。下面,贴代码:

首先是:XML也就是我们要用到的linearlayout。

<com.bengxin.utils.LinearLayoutForListView

android:id="@+id/list_myself" androidrientation="vertical"

android:layout_height="wrap_content" android:layout_width="fill_parent"

android:layout_gravity="center_horizontal" android:background="@drawable/list_bg"

android:paddingTop="3dp" android:paddingBottom="3dp" />

<com.bengxin.utils.LinearLayoutForListView   这个东西就是自己写的linearlayout,注意一下路径名称就行了。接下来我们看一下自定义的这个linearlayoutforlistview是怎么写的。

public class LinearLayoutForListView extends LinearLayout {//当然为了省事我们直接继承LinearLayout就行了

private AdapterForLinearLayout adapter;

private OnClickListener onClickListener = null;

//注意这两个构造函数一定要继承,否则后果自负

public LinearLayoutForListView(Context context) {

super(context);

}

public LinearLayoutForListView(Context context, AttributeSet attrs) {

super(context, attrs);

}

//我们把所有的子项加到当前这个linearlayout中去,注意一下,这里面要为每个子项添加上触发事件,我这里只是简单的实现功能,异常处理什么的统统没加

public void bindLinearLayout() {

for (int i = 0; i < adapter.getCount(); i++) {

View v = adapter.getView(i, null, null);

v.setOnClickListener(this.onClickListener);

this.addView(v, i);

}

}

//下面的这四个就不用解释了,get,set方法

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;

}

好了,我们的linearlayout就准备好了,接下来我们就该准备适配器adapter了。前面说了,我们主要是用到adapter的getView和bindview,那么好,我们就重写一下这两个方法就是了。看代码:

public class AdapterForLinearLayout extends BaseAdapter {//二话不说,继承先

private LayoutInflater mInflater;

private int resource;

private List<? extends Map<String, ?>> data;

private String[] from;

private int[] to;

//这个我是仿照simpleAdapter搞的

public AdapterForLinearLayout(Context context,

List<? extends Map<String, ?>> data,int resource, String[] from,

int[] to) {

this.data = data;

this.resource = resource;

this.from = from;

this.to = to;

this.mInflater = LayoutInflater.from(context);

}

public int getCount() {

return data.size();

}

public Object getItem(int position) {

return data.get(position);

}

public long getItemId(int position) {

return position;

}

//亮点在这里,getView方法呈现

@SuppressWarnings("unchecked")

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

convertView = mInflater.inflate(this.resource, null);

Map item = data.get(position);

int count = to.length;

for (int i = 0; i < count; i++) {

View v = convertView.findViewById(to);

bindView(v, item, from);

}

convertView.setTag(position);//一定要注意这个东西,这个可是我们判断哪一个子项被点击的重要依据,你可以往里面set任何对象的

return convertView;

}

//接下来是bindview,我在这里面实现了imageview的功能,simpleAdapter没有的东西哟

@SuppressWarnings("unchecked")

private void bindView(View v, Map item, String from) {

Object data = item.get(from);

if (v instanceof Checkable) {

if (data instanceof Boolean) {

((Checkable) v).setChecked((Boolean) data);

} else {

throw new IllegalStateException(v.getClass().getName()

+ " should be bound to a Boolean, not a "

+ data.getClass());

}

} else if (v instanceof TextView) {

((TextView) v).setText(data == null ? "" : data.toString());

} else if (v instanceof ImageView) {

if (data != null && !data.equals("")) {

if (data instanceof Integer) {

((ImageView) v).setImageResource((Integer) data);

} else {

Bitmap bm = ImageCache.getInstance().get(data.toString(),//这方法是自己写的

(ImageView) v);

((ImageView) v).setImageBitmap(bm);

((ImageView) v).setPadding(2, 2, 2, 2);

}

}

} else {

throw new IllegalStateException(v.getClass().getName()

+ " is not a "

+ " view that can be bounds by this SimpleAdapter");

}

}
}

好了,一切准备就绪,我们就把他们组合起来用一下来得到我们想要的结果吧。用法很简单:首先我们要先把linearlayout弄出来呀,
声明:private LinearLayoutForListView mListView = null;然后获取:mListView = (LinearLayoutForListView) findViewById(R.id.list_myself);
给丫声明一个点击事件mListView.setOnClickListener(testListener);这个点击事件类似于listview的itemOnClick事件。需要注意的是这个事件里面你需要用view.getTag()来获取你在adapter里设置的东西来判断。类似:int id = Integer.parseInt(v.getTag().toString());

好了,万事大吉祥如意。一切搞定,运行看效果吧。哦,对了,把adapter忘了,
adapter = new AdapterForLinearLayout(ListViewTest.this,

(List<? extends Map<String, ?>>) res, resource//这个是你子项的layout,from, to);

调用的时候一定要注意顺序如下:mListView = (LinearLayoutForListView) findViewById(R.id.list_hotcity);//声明mListView.setOnClickListener(itemClickListener);//点击事件

mListView.setAdapter(adapter);//设置适配器


回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

快速回复 返回列表 返回顶部