今天在公司碰到的问题,伤透了我的脑筋。
在我的Acitivity中有两个fragment(f1和f2),两个fragment布局很类似,但是也有不同,使用的是不同的初始化代码。每个fragment中都有ListView,每一个ListView都增加了一个footerView,然后在我的smartisan t1上两个ListView尾部都显示正常,在魅族mx3,以及vivo的一款手机上f2显示不了footerview。没有任何异常报错信息,对调两个fragment的显示位置,发现依旧是该fragment上的该ListView的footer无法显示,另一个依然正常。
f1使用以下代码初始化的
@Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { View view = inflater.inflate(findLayoutId(), null); mListView = (ListView) view.findViewById(R.id.lv_list); if (mListView == null) { throw new IllegalArgumentException("can not find R.id.lv_list."); } mTipsLayout = (TipsLayout) view.findViewById(R.id.custom_tipslayout); if (mTipsLayout == null) { throw new IllegalArgumentException("can not find R.id.custom_tipslayout."); } mListView.setOnScrollListener(this); mListView.setOnItemClickListener(this); mFooterView = getFooterView(); if (mFooterView == null) { mFooterView = inflater.inflate(R.layout.game_view_footer_loading, null); mFooterView.findViewById(R.id.custom_loading_view).setVisibility(View.GONE); mFooterView.findViewById(R.id.tv_loading_tips).setOnClickListener( new OnClickListener() { @Override public void onClick(View v) { if (getView().findViewById(R.id.custom_loading_view).getVisibility() == View.VISIBLE) { return; } if (mHasLoadAllData) { return; } sendEmptyBackgroundMessage(MSG_BACK_LOAD_DATA); } }); } mListView.addFooterView(mFooterView); mAdapter = createAdapter(); mListView.setAdapter(mAdapter); mTipsLayout.setOnRefreshButtonClickListener(new OnClickListener() { @Override public void onClick(View v) { sendEmptyBackgroundMessage(MSG_BACK_LOAD_DATA); } }); if (!mDelayLoad) { sendEmptyBackgroundMessage(MSG_BACK_LOAD_DATA); } // 非ViewPager时因为不会调用 setUserVisibleHint,所以要在这设定 if (!mHaveSetUserVisibleHint) { setDobuleClickCallback(); } return view; }
<pre name="code" class="java">@Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { View view = inflater.inflate(findLayoutId(), null); mListView = (ListView) view.findViewById(R.id.lv_list); if (mListView == null) { throw new IllegalArgumentException("can not find R.id.lv_list."); } mAdapter = createAdapter(); mListView.setAdapter(mAdapter); mListView.setOnScrollListener(this); mTipsLayout = (TipsLayout) view.findViewById(R.id.custom_tipslayout); if (mTipsLayout == null) { throw new IllegalArgumentException("can not find R.id.custom_tipslayout."); } mFooterView = getFooterView(inflater); if (mFooterView == null) { throw new IllegalArgumentException("footer view can not be null"); } mListView.addFooterView(mFooterView); mTipsLayout.setOnRefreshButtonClickListener(new View.OnClickListener() { @Override public void onClick(View v) { sendEmptyBackgroundMessage(MSG_BACK_LOAD_DATA); } }); if (!mDelayLoad) { sendEmptyBackgroundMessage(MSG_BACK_LOAD_DATA); } addHeaderView(mListView); // 非ViewPager时因为不会调用 setUserVisibleHint,所以要在这设定 if (!mHaveSetUserVisibleHint) { setDobuleClickCallback(); } return view; }
经过几个小时对代码的仔细对照,推敲后,问题终于浮出水面。
结论:
在给ListView设置footerView的代码,要放在setAdapter之前,在部分手机的部分情况下(具体场景我也没推敲出来)如果将addFooterView放在setAdapter后,尽管你调用了notifyDataSetChanged但是并不能够让ListView加载出这个footerView