ListViewd的相关问题

Android 码拜 9年前 (2016-04-30) 1146次浏览
用Fragment实现Tag换页,在第一个Fragment中嵌入了一个ViewPager和ListView。 都写好了,然后运行的时候弹出界面是ListView是不显示本人载入的内容,要下拉才显示。本人截个图给你们看看

一打开界面是ListViewd的相关问题
然后本人向下滑动,listView的内容才显示出来!
ListViewd的相关问题

代码如下:

View view = inflater.inflate(R.layout.home_pager, container, false);
		Notice_ItemBeanlist = new ArrayList<Notice_ItemBean>();
		context = MyApplication.getInstance();
		ListView listView = (ListView) view.findViewById(R.id.id_listView_notice);
		for (int i = 0; i < 20; i++) {
			Notice_ItemBeanlist.add(new Notice_ItemBean(
					R.drawable.ic_launcher, 
					"本人是标题" + i, 
					"本人是内容" + i));
		}
		listView.setAdapter(new MyNoticeAdapter(context, Notice_ItemBeanlist));

适配器代码如下:

public class MyNoticeAdapter extends BaseAdapter{
	private List<Notice_ItemBean> mlist;
	private LayoutInflater mInflater;
	 public MyNoticeAdapter(Context home_page,List<Notice_ItemBean> mlist) {
		// TODO Auto-generated constructor stub
		 this.mlist=mlist;
		 mInflater=LayoutInflater.from(home_page);
		 }
	@Override
	public int getCount() {
		// TODO Auto-generated method stub
		return mlist.size();
	}
	@Override
	public Object getItem(int position) {
		// TODO Auto-generated method stub
		return mlist.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) {
		// TODO Auto-generated method stub
		ViewHolder viewHolder;
		if(convertView==null){
			viewHolder=new ViewHolder();
			convertView=mInflater.inflate(R.layout.item_notice, null);
			viewHolder.noitce_imag=(ImageView) convertView.findViewById(R.id.notice_image);
			viewHolder.noitce_title=(TextView) convertView.findViewById(R.id.notice_title);
			viewHolder.noitce_content=(TextView) convertView.findViewById(R.id.notice_content);
			convertView.setTag(viewHolder);

		}else {
			viewHolder=(ViewHolder) convertView.getTag();
			Notice_ItemBean bean =mlist.get(position);
			viewHolder.noitce_imag.setImageResource(bean.ItemImageResid);
			viewHolder.noitce_content.setText(bean.ItemContent);
			viewHolder.noitce_title.setText(bean.ItemTitle);
		}
		return convertView;
	}
class ViewHolder{
	private ImageView noitce_imag;
	private TextView  noitce_title;
	private TextView  noitce_content;
}
}
解决方案

10

viewHolder.noitce_imag=(ImageView) convertView.findViewById(R.id.notice_image);
viewHolder.noitce_title=(TextView) convertView.findViewById(R.id.notice_title);
viewHolder.noitce_content=(TextView) convertView.findViewById(R.id.notice_content);
先声明viewHolder = null,这几行操作放在if(){}else{}外面

15

没显示的原因在于,你在if(convertView==null)中并没有为这些控件设置setText,所以在前面这几行就不会有数据,而后面那几行由于是进入了else的判断,而else的判断中题主已经为它们添加了setText了,所以滑下来才有数据
可以将你那几行setText同样放在if(convertView==null)中,如下:

 @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        // TODO Auto-generated method stub
        ViewHolder viewHolder;
        if(convertView==null){
            viewHolder=new ViewHolder();
            convertView=mInflater.inflate(R.layout.item_notice, null);
            viewHolder.noitce_imag=(ImageView) convertView.findViewById(R.id.notice_image);
            viewHolder.noitce_title=(TextView) convertView.findViewById(R.id.notice_title);
            viewHolder.noitce_content=(TextView) convertView.findViewById(R.id.notice_content);
             viewHolder.noitce_imag.setImageResource(bean.ItemImageResid);
            viewHolder.noitce_content.setText(bean.ItemContent);
            viewHolder.noitce_title.setText(bean.ItemTitle);
            convertView.setTag(viewHolder);
             
        }else {
            viewHolder=(ViewHolder) convertView.getTag();
            Notice_ItemBean bean =mlist.get(position);
            viewHolder.noitce_imag.setImageResource(bean.ItemImageResid);
            viewHolder.noitce_content.setText(bean.ItemContent);
            viewHolder.noitce_title.setText(bean.ItemTitle);
        }
        return convertView;
    }

15

更正一下:
应该是一开始设置ViewHolder为null,
然后将setText部分从else中拿出来:

@Override
    public View getView(int position, View convertView, ViewGroup parent) {
        // TODO Auto-generated method stub
        ViewHolder viewHolder = null;
        Notice_ItemBean bean;
        if(convertView==null){
            viewHolder=new ViewHolder();
            convertView=mInflater.inflate(R.layout.item_notice, null);
            viewHolder.noitce_imag=(ImageView) convertView.findViewById(R.id.notice_image);
            viewHolder.noitce_title=(TextView) convertView.findViewById(R.id.notice_title);
            viewHolder.noitce_content=(TextView) convertView.findViewById(R.id.notice_content);
            convertView.setTag(viewHolder);
              
        }else {
            viewHolder=(ViewHolder) convertView.getTag();
            bean =mlist.get(position);
           
        }
            viewHolder.noitce_imag.setImageResource(bean.ItemImageResid);
            viewHolder.noitce_content.setText(bean.ItemContent);
            viewHolder.noitce_title.setText(bean.ItemTitle);
        return convertView;
    }

CodeBye 版权所有丨如未注明 , 均为原创丨本网站采用BY-NC-SA协议进行授权 , 转载请注明ListViewd的相关问题
喜欢 (0)
[1034331897@qq.com]
分享 (0)