0

嗨,我想通過SearchView部件搜索我的應用程序中的用戶。當SearchWidget處於活動狀態時,我想在同一XML佈局的ViewPager之上顯示建議用戶的姓名ListView。 我用下面的XMLAndroid顯示搜索查看結果ListView above ViewPager

<?xml version="1.0" encoding="utf-8"?><RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
android:layout_width="fill_parent" 
android:layout_height="fill_parent" >  
<ListView 
    android:id="@+id/searched_friends_list" 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:divider="@color/list_divider" 
    android:dividerHeight="1dp" 
    android:layout_marginLeft="15dp" 
    android:layout_marginRight="15dp" 
    android:visibility="gone" 
    android:listSelector="@drawable/list_row_selector" />  
<android.support.v4.view.ViewPager 
    android:id="@+id/pager" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" > 
</android.support.v4.view.ViewPager> 

而在代碼我visibled ListView向用戶展示。

@Override 
public boolean onQueryTextChange(String newText) { 
    friendListView.setVisibility(View.VISIBLE); 
    if (TextUtils.isEmpty(newText)) { 
     friendListView.clearTextFilter(); 
    } else { 
     friendListView.setFilterText(newText.toString()); 
    } 
    return true; 
} 

它並不顯示ListView enter image description here

,但是當我添加以下代碼

@Override 
public boolean onQueryTextChange(String newText) { 
    friendListView.setVisibility(View.VISIBLE); 
    viewPager.setVisibility(View.GONE); 
    if (TextUtils.isEmpty(newText)) { 
     friendListView.clearTextFilter(); 
    } else { 
     friendListView.setFilterText(newText.toString()); 
    } 
    return true; 
} 

它顯示下面的輸出 enter image description here

但我想ListView以上ViewPager。提前致謝。

回答

0

如果您希望您的ListView實時顯示哪些用戶在searchView中建議的結果 - 請在您的適配器中使用Filter。

添加到適配器:

ArrayList<full_data> adapterDataList = new ArrayList<full_data>(); 
ArrayList<filtered_data> mFilteredDataList = new ArrayList<filtered_data>(listOR); 
Filter mFilter = new Filter() { 
@Override 
protected FilterResults performFiltering(CharSequence searchTerm) { 
Locale locale = Locale.getDefault(); 
FilterResults results = new FilterResults(); 
if ((searchTerm null) || (searchTerm.length() 0)) { 
ArrayList<String> searchList = new ArrayList<String>(adapterDataList); 
results.values = searchList; 
results.count = searchList.size(); 
} else { 
final String searchTermString = searchTerm.toString().toLowerCase(locale); 
final String[] words = searchTermString.split(" "); 
final int wordCount = words.length; 
final ArrayList<String> newValues = new ArrayList<String>(); 

for (final String value : adapterDataList) { 
        if (value == null) { 
         continue; 
        } 
        final String valueText = value.toLowerCase(locale); 
for (int k = 0; k < wordCount; k++) { 
         if (valueText.contains(words[k])) { 
          newValues.add(value); 
          break; 
         } 
        } 
       } 
       results.values = newValues; 
       results.count = newValues.size(); 
      } 
      return results; 
     } 
@Override 
     protected void publishResults(CharSequence constraint, FilterResults results) { 
      mFilteredDataList = (ArrayList&lt;String&gt;)results.values; 
      notifyDataSetChanged(); 
     } 
    }; 
public Filter getFilter(){ return mFilter; } 

to SearchView's TextChangedListener: 
@Override 
public void afterTextChanged(Editable searchText) { 
mAdapter.getFilter().filter(searchText); 
} 

我認爲它應該工作的罰款。

+0

感謝您的回答。我添加了'受保護的FilterResults performFiltering(CharSequence約束){',它可以過濾數據。但我的問題是在'ViewPager'上方顯示'ListView'。 – user3384985 2014-10-23 04:12:37

+0

@ user3384985在這裏隱藏您的ViewPager:'viewPager.setVisibility(View.GONE);' 嘗試將其刪除並將您的佈局從RelativeLayout更改爲LinearLayout。 之後,所有必須罰款。 – QArea 2014-10-23 13:51:39