您好,登錄后才能下訂單哦!
在Android開發中,GridView是一種常用的列表視圖控件,用于展示大量數據。為了提高性能和用戶體驗,我們通常需要對GridView進行分頁顯示和數據過濾。下面是一個簡單的示例,展示了如何實現GridView的分頁顯示和數據過濾。
List<String> dataList = new ArrayList<>();
for (int i = 1; i <= 20; i++) {
dataList.add("Item " + i);
}
public class MyAdapter extends BaseAdapter {
private List<String> dataList;
private Context context;
public MyAdapter(Context context, List<String> dataList) {
this.context = context;
this.dataList = dataList;
}
@Override
public int getCount() {
return dataList.size();
}
@Override
public Object getItem(int position) {
return dataList.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
if (convertView == null) {
convertView = LayoutInflater.from(context).inflate(R.layout.item_layout, parent, false);
}
TextView textView = convertView.findViewById(R.id.text_view);
textView.setText(dataList.get(position));
return convertView;
}
}
public class MainActivity extends AppCompatActivity {
private GridView gridView;
private MyAdapter adapter;
private List<String> dataList;
private int currentPage = 1;
private final int pageSize = 5;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
gridView = findViewById(R.id.gridview);
dataList = new ArrayList<>();
// 填充數據
for (int i = 1; i <= 20; i++) {
dataList.add("Item " + i);
}
adapter = new MyAdapter(this, dataList);
gridView.setAdapter(adapter);
// 設置分頁監聽器
gridView.setOnScrollListener(new AbsListView.OnScrollListener() {
@Override
public void onScrollStateChanged(AbsListView view, int scrollState) {
}
@Override
public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) {
if (firstVisibleItem + visibleItemCount >= totalItemCount && !isLoadingMore) {
isLoadingMore = true;
currentPage++;
loadMoreData();
}
}
});
}
private void loadMoreData() {
// 模擬異步加載數據
new Handler().postDelayed(new Runnable() {
@Override
public void run() {
List<String> newData = new ArrayList<>();
for (int i = (currentPage - 1) * pageSize + 1; i <= currentPage * pageSize; i++) {
newData.add("Item " + i);
}
dataList.addAll(newData);
adapter.notifyDataSetChanged();
isLoadingMore = false;
}
}, 1000);
}
}
public void filterData(String keyword) {
List<String> filteredList = new ArrayList<>();
for (String item : dataList) {
if (item.contains(keyword)) {
filteredList.add(item);
}
}
dataList = filteredList;
notifyDataSetChanged();
}
在Activity中,可以為搜索框設置一個監聽器,當用戶輸入關鍵詞時,調用適配器的filterData方法進行數據過濾。
searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
@Override
public boolean onQueryTextSubmit(String query) {
return false;
}
@Override
public boolean onQueryTextChange(String newText) {
adapter.filterData(newText);
return true;
}
});
這樣,我們就實現了一個簡單的GridView分頁顯示和數據過濾功能。你可以根據實際需求進行調整和優化。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。