您好,登錄后才能下訂單哦!
在Android開發(fā)中,GridView是一種常用的列表視圖控件,用于展示大量數(shù)據(jù)。為了提高性能和用戶體驗,我們通常需要對GridView進(jìn)行分頁顯示和數(shù)據(jù)過濾。下面是一個簡單的示例,展示了如何實現(xiàn)GridView的分頁顯示和數(shù)據(jù)過濾。
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<>();
// 填充數(shù)據(jù)
for (int i = 1; i <= 20; i++) {
dataList.add("Item " + i);
}
adapter = new MyAdapter(this, dataList);
gridView.setAdapter(adapter);
// 設(shè)置分頁監(jiān)聽器
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() {
// 模擬異步加載數(shù)據(jù)
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中,可以為搜索框設(shè)置一個監(jiān)聽器,當(dāng)用戶輸入關(guān)鍵詞時,調(diào)用適配器的filterData方法進(jìn)行數(shù)據(jù)過濾。
searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
@Override
public boolean onQueryTextSubmit(String query) {
return false;
}
@Override
public boolean onQueryTextChange(String newText) {
adapter.filterData(newText);
return true;
}
});
這樣,我們就實現(xiàn)了一個簡單的GridView分頁顯示和數(shù)據(jù)過濾功能。你可以根據(jù)實際需求進(jìn)行調(diào)整和優(yōu)化。
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。