1、ListView
ListView是一个ViewGroup(布局控件),可显示一个元素列表,可垂直滚动,在ListView当中显示的原色需要使用一个适配器(adapter)进行包装。
常见的适配器有四种:
- BaseAdapter 基础数据适配器,需要自己重写方法(灵活)
- ArrayAdapter 数组适配器 展示简单文本(简单)
- SimpleAdapter 简单适配器,功能强大(简约而不简单)
- SimpleCursorAdapter 数据库适配器
2、ListView简单应用
package com.example.test_listview2;
import androidx.appcompat.app.AppCompatActivity;
import android.hardware.lights.LightsManager;
import android.os.Bundle;
import android.widget.ArrayAdapter;
import android.widget.ListView;
public class MainActivity extends AppCompatActivity {
private String[] ss =new String[]{
"北京",
"上海",
"广州",
"江西",
"湖南",
"重庆",
"北京",
"上海",
"广州",
"江西",
"湖南",
"重庆",
"北京",
"上海",
"广州",
"江西",
"湖南",
"重庆",
};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ListView listView =(ListView) findViewById(R.id.listView1);
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1,ss);
listView.setAdapter(adapter);
}
}
<ListView
android:id="@+id/listView1"
android:layout_width="409dp"
android:layout_height="729dp"
/>
页面呈现可滑动的ListView
3、ListView间隙调节
解析
android:divider="缝隙颜色"
android:dividerHeight="缝隙大小"
代码
<ListView
android:id="@+id/listView1"
android:layout_width="409dp"
android:layout_height="729dp"
android:divider="#345"
android:dividerHeight="6dp"
/>
可以看到缝隙颜色和粗细均改变了
4、ListView方法一:addHeaderView()
解析
View view =getLayoutInflater().inflate(R.layout.footer,null);
//读取footer
listView.addHeaderView(view);
//放在listview顶部
代码
public class MainActivity extends AppCompatActivity {
private String[] ss =new String[]{
"北京",
"上海",
"广州",
"江西",
"湖南",
"重庆",
"北京",
"上海",
"广州",
"江西",
"湖南",
"重庆",
"北京",
"上海",
"广州",
"江西",
"湖南",
"重庆",
};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ListView listView =(ListView) findViewById(R.id.listView1);
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1,ss);
View view =getLayoutInflater().inflate(R.layout.footer,null);
listView.addHeaderView(view);
listView.setAdapter(adapter);
}
}
//footer.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<EditText
android:id="@+id/editTextTextPersonName"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="3"
android:ems="10"
/>
<Button
android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Search" />
</LinearLayout>
5、ListView方法二:addFooterView()
解析
//和addHeaderView()类似 其余代码不变
View view =getLayoutInflater().inflate(R.layout.footer,null);
//读取footer
listView.addFooterView(view);
//放在listview底部
6、设置查找
代码
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ListView listView =(ListView) findViewById(R.id.listView1);
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1,ss);
ViewGroup group =(ViewGroup) getLayoutInflater().inflate(R.layout.footer,null);
Button button = (Button)group.findViewById(R.id.button);
final EditText editText=(EditText)group.findViewById(R.id.editTextTextPersonName);
//设置button点击事件
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
String indexSring= editText.getText().toString();//获取输入框的内容
listView.setSelection(Integer.valueOf(indexSring));//在listview中选出
}
});
listView.addHeaderView(group);
listView.setAdapter(adapter);
}