ListView使用的时候遇到的一些问题
昨天在做项目时,请求服务器的好友动态后,将好友动态和评论显示到界面上,用ListView显示,发现一进这个界面时,listView的适配器的getVIew()方法就会执行6次,后来发现原来是ListView的宽高写的有问题,开始写的是宽为match_parent,高为包裹内容,这样就出现了问题,之后把ListView的宽高都改为了match_parent,getView()方法就正常执行了,界面布局改为了这样:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingStart="5dp"
android:paddingEnd="5dp"
android:background="@color/base_background_color"> <ListView
android:id="@+id/lv_fragment_friends"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:clipToPadding="false"
android:divider="@android:color/transparent"
android:dividerHeight="10dp"
android:paddingBottom="10dp"
android:scrollbarStyle="outsideOverlay"> </ListView> </LinearLayout>
然后我要把分享信息的评论动态用代码添加到ListView的每个item里面,item布局如下:

部分代码:
//messageFragment的二级fragment
public class Message_Friends_Fragment extends Fragment {
/**
* 自定义的观察者
*/
public MyObserver mObserver;
/**
* 从服务器获取的所有的好友分享的动态的集合
*/
private List<Resquest_friends_info.EveryShareInfo> mResults_list;
/**
* ListVIew当前的条目
*/
public static int CURRENT_ITEM = 0;
@Bind(R.id.lv_fragment_friends)
ListView mListView;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_message_firends, null);//ViewGroup ?
ButterKnife.bind(this, view);
mObserver = new MyObserver();//创建一个观察者对象
//创建一个访问网络的Control
Message_Friends_NetControl control = new Message_Friends_NetControl(this);
control.getFriendsShareFromServer();//访问网络并且解析Json
mListView.setDivider(null);
return view;
}
class FrendsAdapter extends BaseAdapter {
@Override
public int getCount() {
return mResults_list.size();
}
@Override
public Object getItem(int i) {
return mResults_list.get(i);
}
@Override
public long getItemId(int i) {
return i;
}
@Override
public View getView(int i, View convertView, ViewGroup viewGroup) {
Log.d("msg", "getView()");
ViewHolder holder;
if (convertView != null) {
holder = (ViewHolder) convertView.getTag();
} else {
convertView = View.inflate(UIUtils.getContext(), R.layout.lv_item_message_friends, null);
holder = new ViewHolder(convertView);
convertView.setTag(holder);
}
//获取某条分享的动态
Resquest_friends_info.EveryShareInfo info = mResults_list.get(i);
// Log.d("msg","现在是第几个动态?"+"--->"+i);
//获取当前分享动态的所有回复的集合
ArrayList<Resquest_friends_info.EveryShareInfo.Reply> replys = info.getPub_com();
// Log.d("msg","评论总数--->"+replys.size());
for (Resquest_friends_info.EveryShareInfo.Reply reply : replys) {
Log.d("msg",reply.getPc_txt()+"--->"+reply.getPc_name());//评论的内容以及顺序全是正确的
}
Log.d("msg", "第几个条目?++------------------------------------------------" + i);
//动态添加评论之前先移除评论线性布局里的所有评论,因为这个线性布局可能是复用的之前item的,所以要先移除线性布局里原来的评论
holder.ll_comment_message_friends.removeAllViews();
for(int j=0;j<replys.size();j++){
TextView textView = new TextView(UIUtils.getContext());//new 一条评论
textView.setText(replys.get(j).getPc_name()+": "+replys.get(j).getPc_txt());//设置评论人的名字和评论的内容
textView.setTextColor(Color.BLACK);
holder.ll_comment_message_friends.addView(textView);
}
holder.tvUserName.setText(info.getPub_frd_name());//设置好友动态分享者的名字
Log.d("msg","tvUserName是否同一对象?---->"+holder.tvUserName.toString());
holder.tvTime.setText(info.getPub_datetime());//设置分享这条动态的时间
holder.tvSaySth.setText(info.getPub_context());//设置分享动态的内容
//显示用户头像
ImageLoader.getInstance().displayImage(GlobalConstant.SERVER_URL + "/" + info.getPub_frd_head(), holder.ivUserFace);
return convertView;
}
}
//holder里面的对象,每个item都是这个对象,只不过内容不同
static class ViewHolder {
@Bind(R.id.iv_friends_face)
ImageView ivUserFace;//用户头像
@Bind(R.id.tv_user_name)
TextView tvUserName;//用户名字
@Bind(R.id.tvTime_friends)
TextView tvTime;//时间
@Bind(R.id.tvSaySth)
TextView tvSaySth;//说点什么
@Bind(R.id.iv_friends_friends_pic)
ImageView iv_friends_friends_pic;//发的具体图片
/**
* 用来放评论的线性布局
*/
@Bind(R.id.ll_comment_message_friends)
LinearLayout ll_comment_message_friends;
public ViewHolder(View view) {
ButterKnife.bind(this, view);
}
}
ListView使用的时候遇到的一些问题的更多相关文章
- 张高兴的 UWP 开发笔记:横向 ListView
ListView 默认的排列方向是纵向 ( Orientation="Vertical" ) ,但如果我们需要横向显示的 ListView 怎么办? Blend for Visua ...
- Android—万能ListView适配器
ListView是开发中最常用的控件了,但是总是会写重复的代码,浪费时间又没有意义. 最近参考一些资料,发现一个万能ListView适配器,代码量少,节省时间,总结一下分享给大家. 首先有一个自定义的 ...
- Android—ListView条目背景为图片时,条目间距问题解决
ListView是android开发中使用最普遍的控件了,可有的listView条目的内容颇为丰富,甚至为了美观,背景用指定图片,如下图:
- Android中ListView实现图文并列并且自定义分割线(完善仿微信APP)
昨天的(今天凌晨)的博文<Android中Fragment和ViewPager那点事儿>中,我们通过使用Fragment和ViewPager模仿实现了微信的布局框架.今天我们来通过使用Li ...
- listview下拉刷新和上拉加载更多的多种实现方案
listview经常结合下来刷新和上拉加载更多使用,本文总结了三种常用到的方案分别作出说明. 方案一:添加头布局和脚布局 android系统为listview提供了addfootview ...
- Android listview和gridview以及view的区别
GridView 可以指定显示的条目的列数. listview一般显示的条目的列数都是一列 如果是列表(单列多行形式)的使用ListView,如果是多行多列网状形式的优先使用GridView andr ...
- mono for android Listview 里面按钮 view Button click 注册方法 并且传值给其他Activity 主要是context
需求:为Listview的Item里面的按钮Button添加一个事件,单击按钮时通过事件传值并跳转到新的页面. 环境:mono 效果: 布局代码 主布局 <?xml version=" ...
- 【腾讯Bugly干货分享】跨平台 ListView 性能优化
本文来自于腾讯Bugly公众号(weixinBugly),未经作者同意,请勿转载,原文地址:https://mp.weixin.qq.com/s/FbiSLPxFdGqJ00WgpJ94yw 导语 精 ...
- android内部培训视频_第三节(3)_常用控件(ViewPager、日期时间相关、ListView)
第三节(2):常用控件之ViewPager.日期时间相关.ListView 一.ViewPager 实例:结合PagerAdapter滑动切换图片 二.日期时间相关:AnalogClock\Dig ...
- 父ListView嵌套子ListView时点击事件没有响应
转发请备注出处:http://www.cnblogs.com/LT5505/p/5972999.html 问题: 在ListView中嵌套ListView之后,子ListView会把父ListView ...
随机推荐
- 设计模式4 外观模式 FACADE
一个外观是一个类,其提供的功能介于工具箱的功能和完整系统的功能之间,并为一个包或者一个子系统中的类提供了简化的使用方式.
- java代理的深入浅出(三)-JavaAssist,ASM
简介 类似字节码操作方法还有ASM.几种动态编程方法相比较,在性能上Javassist高于反射,但低于ASM,因为Javassist增加了一层抽象.在实现成本上Javassist和反射都很低,而ASM ...
- PAT 天梯赛 L2-007 家庭房产
建图+DFS 题目链接:https://www.patest.cn/contests/gplt/L2-007 题解 在热身赛的时候没有做出来,用的并查集的思想,但是敲残了,最后也没整出来.赛后听到别人 ...
- 《Windows驱动开发技术详解》之驱动程序调用驱动程序——通过设备指针调用其他驱动程序
本节介绍“手动”构造各个IRP,然后将IRP传递到相应驱动程序的派遣函数里. 获得设备指针 每个内核中的句柄都会和一个内核对象的指针联系起来.ZwCreateFile内核函数可以通过设备名打开设备句柄 ...
- MySQL 不允许从远程访问的解决方法
解决方法: 1. 改表法. 可能是你的帐号不允许从远程登陆,只能在localhost.这个时候只要在localhost的那台电脑,登入mysql后,更改 "mysql" 数据库里的 ...
- inflate的使用注意事项
public View inflate (int resource, ViewGroup root, boolean attachToRoot) 我们在使用这个方法时,要清楚原理,下面是这个方法的文档 ...
- MySQL查看索引、表信息、触发器
查看索引: select * FROM information_schema.TABLE_CONSTRAINTS ; select * FROM information_schema.TABLE_CO ...
- hdu_3001_Travelling(状压DP)
题目连接:http://acm.hdu.edu.cn/showproblem.php?pid=3001 题意:给你N个点,M条边,每个点最多走两次,问走完N个点最短的路程为多少. 题解:注意这题有重边 ...
- uva 156 (map)
暑假培训习题 1.用vector<string>储存string类型的输入单词: 2.将vector中的元素逐一标准化后映射进map中,并给map值加一: 3.新建一个空的vector 4 ...
- python2.6.6在centos6.4下安装
1.wget http://www.python.org/ftp/python/2.6.6/Python-2.6.6.tar.bz2 2. tar xvjf Python-2.6.6.tar.bz2 ...