在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进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。