自定义ListView是Android开发中常用的列表视图控件,它可以用于展示大量数据,并且可以根据需求进行自定义样式和功能。添加搜索功能可以让用户快速定位所需的数据。
实现向自定义ListView添加搜索功能可以按以下步骤进行:
以下是一个示例代码:
布局文件(activity_main.xml):
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<EditText
android:id="@+id/editTextSearch"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="输入关键字进行搜索"/>
<ListView
android:id="@+id/listView"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
</LinearLayout>
适配器类(CustomAdapter.java):
public class CustomAdapter extends BaseAdapter {
private List<String> dataList;
private LayoutInflater inflater;
public CustomAdapter(Context context, List<String> data) {
inflater = LayoutInflater.from(context);
dataList = data;
}
@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) {
ViewHolder viewHolder;
if (convertView == null) {
convertView = inflater.inflate(R.layout.list_item, parent, false);
viewHolder = new ViewHolder();
viewHolder.textView = convertView.findViewById(R.id.textView);
convertView.setTag(viewHolder);
} else {
viewHolder = (ViewHolder) convertView.getTag();
}
viewHolder.textView.setText(dataList.get(position));
return convertView;
}
private static class ViewHolder {
TextView textView;
}
}
Activity类(MainActivity.java):
public class MainActivity extends AppCompatActivity {
private EditText editTextSearch;
private ListView listView;
private CustomAdapter adapter;
private List<String> dataList;
private List<String> filteredList;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
editTextSearch = findViewById(R.id.editTextSearch);
listView = findViewById(R.id.listView);
// 初始化数据源
dataList = new ArrayList<>();
dataList.add("Apple");
dataList.add("Banana");
dataList.add("Orange");
dataList.add("Mango");
dataList.add("Grape");
// 初始化适配器
adapter = new CustomAdapter(this, dataList);
listView.setAdapter(adapter);
// 添加搜索功能
filteredList = new ArrayList<>();
editTextSearch.addTextChangedListener(new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
filteredList.clear();
String keyword = s.toString().toLowerCase();
for (String item : dataList) {
if (item.toLowerCase().contains(keyword)) {
filteredList.add(item);
}
}
adapter = new CustomAdapter(MainActivity.this, filteredList);
listView.setAdapter(adapter);
adapter.notifyDataSetChanged();
}
@Override
public void afterTextChanged(Editable s) {
}
});
}
}
上述代码实现了一个简单的搜索功能,用户在EditText中输入关键字时,列表会根据关键字筛选数据并更新显示。可以根据实际需求进行修改和扩展。
腾讯云相关产品和产品介绍链接地址:
领取专属 10元无门槛券
手把手带您无忧上云