ScrollView套嵌ListView

时间 : 15-11-09 栏目 : Android, 移动开发 作者 : noway 评论 : 0 点击 : 526 次

这是我最近遇到的问题,想实现全屏滚动大家都知道在最外层嵌套一个scrollview,但是如果里面有listview就操蛋了,listview只能显示大约两行的大小。我找了找资料,解决的办法一种是:不要在scrollview里嵌套listview(我操,你这不废话么,老子的设计就是这样的,能怎么办?这种回答纯属欠抽),第二种办法是重写listview的onMeasure重新计算显示行数,还有一种办法是给listview加上头和尾,即调用丫的addHeaderView和addFooterView,这种办法也是不错滴,不过如果你的listview想实现某种背景效果的话这种办法就做不到了。哪哪,现在我就推出我的终极解决办法:也就是第一种欠抽的办法,既然listview不能用,那就不用listview了,用一个linearlayout,然后调用addView的方法来解决之。

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

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

<com.bengxin.utils.LinearLayoutForListView

android:id="@+id/list_myself" android:orientation="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);这个点击事件类似于listviewitemOnClick事件。需要注意的是这个事件里面你需要用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);//
设置适配器

试一下吧

本文标签

除非注明,文章均为( noway )原创,转载请保留链接: http://blog-old.z3a105.com/?p=786

ScrollView套嵌ListView:等您坐沙发呢!

发表评论





       ==QQ:122320466==

 微信    QQ群


0

Aujourd’hui, une partie avec le développement du e-commerce, achats en ligne est devenu une partie de la vie pour beaucoup de gens. La mariage a commencé achats en ligne. Si vous choisissez achats les mariages en ligne, il peut être beaucoup moins cher que le salon de la Robe de mariée pas chermariée local, réduisant le budget de mariage. vous pouvez avoir beaucoup de choix si acheter de mariage en ligne. vous pouvez ramasser une robe de mariée bon marché sur Internet.
Piercing fascinerande figur, och nu tittar vi på 2016 senast brudklänning, kan du vara den vackraste bruden det!2016 senaste Bra brudklänning, söt temperament Bra design, romantiska spetsar blomma kjol, som du lägger till en elegant och charmig temperament.Kvinnan tillbaka mjuka linjer, människor brudklänningofta få en känsla av oändlig frestelse. Fall 2016 mässan, lämnar uppgifter om ditt bröllop charmig.
Yesterday afternoon, the Chinese team was training in the Guangzhou Gymnasium, when the reporter asked Zhao Yunlei the feeling of wearing the new cheap jersey , cheap jerseys online shopshe readily took a shirt from the bag crumpled ball to reporters, and she said with a smile: ” This shirt is light. ”Zhao Yunlei said: “Our material is very light like with the clothes of the tennis King Nadal, Federer, after the sweat, sweat does not drip down to the ground, when we do move, it is easy pace slipping if the sweat drip on the floor.”Tennis players Zhang Yawen, told reporters: “You might think the clothes attached to the body, fearing we swing will be affected, in fact, we do not feel anything, because the clothes are very light, very soft, put on quite comfortable. And it’s particularly good clothes to dry, washing and will dry in 15 minutes. ”
China’s sports enthusiasts NFL sweatshirt with mad love and the pursuit of, and therefore, NFL jerseys have a good market in China and development. China is a populous country, is the consumer, the economic momentum is so good, the sales prospects sportswear is immeasurable. With hot sales sweatshirt, but also to promote the importance of sports fans, on health, on the other hand is a matter of concern for the World Cup, fans wearing NFL jerseys and also can express themselves more fully love and obsession Therefore, NFL jerseys Wholesale jerseys online shopwholesale has good prospects and development in China.
ANTA-ANTA Sports Products Limited, referred to as ANTA Sports, Anta, is China’s leading sporting goods companies, mainly engaged in the design, development, manufacture and marketing of ANTA brand sporting goods, including sports footwear, apparel and accessories. Anta sweatshirt design advantages, warm stretch knit fabric, using Slim version of model, more personal fit, bid farewell to bloated, so wearing more stylish.GUIRENNIAO-This logo is a spiritual totem, smooth graphics implication unstoppable force; flexible deliver an elegant arc Wholesale jerseys china shop movement, strength and speed of the United States, a symbol of passion and rationality publicity “Heart” and “meaning”, “concept” unity; pass the fearless and enterprising mind, showing beyond the realm of self, to unstoppable force to create the future.XTEP-Xtep (China) Co., Ltd. is a comprehensive development wholesale jerseys china shop, production and marketing of Xtep brand (XTEP) sports shoes, clothing, bags, caps, balls, socks mainly large sporting goods industry enterprises.
There are a lot of fans in identifying the authenticity of the above cheap jerseys have great distress, so here to i will show you some methods to definitely affordable inexpensive cheap jerseys : Firstly, we should look at if it is working fine. China has been called the world’s factory, a lot cheap jerseys factories in China have foundries, but our cheap jerseys are all from here! Secondly, should to see whether it is the , we all know that it is difficult to get out of print once a genuine cheap cheap jerseys free shipping jersey was print. and we have all kind of stocka on the whole website, in other words, we have all you want ! Finally, look at the price, our price is not necessarily the lowest in the whole website but it must be most fair on the whole website, we certainly you will not regret later when you buy it. Of course, except that cheap jerseys, we also have the other products, such as socks, leggings and some other related products, everyone can enjoy the best services of here!

KUBET