首页 > 代码库 > android-ListView 最后一行添加控件

android-ListView 最后一行添加控件

今天在做一个功能的时候,要求必须是在一个listview下,有一段提示行的文字,自己的那个listview的adapter用的是cursoradapter,这样的话,处理布局的灵活性就大打折扣了。最开始的想法是改变item的布局,然后在adapter中处理,保证在listview加载到最后一行的时候,控制item中添加的textview显示,实现提示效果。但是,这时候会面临很多的问题。比如,最后一行点击事件,最后一行的下划线等。

后来,想到了另外一种方法,就是不再adapter中添加,而是直接在listview中添加,这个很好的解决了问题。代码如下

/* Vanzo:zhangshuli on: Sat, 24 Jan 2015 16:21:40 +0000 */    private View mAddSlideItem;    private TextView mSearchResult;// End of Vanzo: zhangshuli    public void onCreate1(Bundle icicle) {        MmsLog.d(TAG,"onCreate"); 
initPlugin(this, getIntent()); 
sNeedRequery = true
  final Uri u = getIntent().getData(); if (u != null && u.getQueryParameter("source_id") != null) {
 
gotoComposeMessageActivity(u)
return; } ContentResolver cr = getContentResolver(); 
mLayoutSearch = (RelativeLayout) findViewById(R.id.conv_second); mLayoutSearch.setVisibility(View.GONE);
//得到想要添加的控件
mAddSlideItem
= ((LayoutInflater) getSystemService( Context.LAYOUT_INFLATER_SERVICE)).inflate(R.layout.search_mms_result, null);        
        mSearchResult
= (TextView) mAddSlideItem.findViewById(R.id.search_result); 
mSearchListView = (ListView) findViewById(R.id.list2); mSearchListView.setItemsCanFocus(true); mSearchListView.setFocusable(true); mSearchListView.setClickable(true);//在末尾添加控件
        mSearchListView.addFooterView(mAddSlideItem);
mTvEmpty
= (TextView)findViewById(android.R.id.empty); 
setTitle(""); Contact.addListener(mContactListener); 
mQueryHandler2 = new AsyncQueryHandler(cr) { protected void onQueryComplete(int token, Object cookie, Cursor c) { 
try { if (searchProgressDialog != null && searchProgressDialog.isShowing()) { searchProgressDialog.setDismiss(true); searchProgressDialog.dismiss(); } } catch (IllegalArgumentException ex) { MmsLog.d(TAG,"Dialog.dismiss() IllegalArgumentException"); } if (mIsContentChanged) { sWaitSaveDraft = false; mIsContentChanged = false; } mIsQueryComplete = true; /// @} if (c == null) { /// M: Add for OP09: @{

                mSearchListView.setFocusable(true); mSearchListView.setFocusableInTouchMode(true); mSearchListView.requestFocus(); // Remember the query if there are actual results if (cursorCount > 0) { SearchRecentSuggestions recent = ((MmsApp)getApplication()).getRecentSuggestions(); if (recent != null) { recent.saveRecentQuery(searchString, getString(R.string.search_history, cursorCount, searchString)); } }

 虽然你在这里增加了自己的控件,但是,如果你想刷新这个控件的状态,这时候你需要跟listview刷新类似,也就是每次都要

android-ListView 最后一行添加控件