目录:android.widget.ListView
xml布局
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/activity_main"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ListView
android:id="@+id/lv_list"
android:dividerHeight="1px"
android:divider="#f0f0f0"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
</RelativeLayout>
调用代码:
public class MainActivity extends AppCompatActivity {
private ListView lv_list;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
init();
}
private void init() {
//数据源
List<String> list = new ArrayList<>();
list.add("");
list.add("");
list.add("");
list.add("");
list.add("");
lv_list = (ListView) findViewById(R.id.lv_list);
lv_list.setAdapter(new ListAdapter(list));
}
private class ListAdapter extends BaseAdapter{
private List<String> list;
public ListAdapter(List<String> list) {
this.list = list;
}
@Override
public int getCount() {
return list.size();
}
@Override
public Object getItem(int position) {
return null;
}
@Override
public long getItemId(int position) {
return 0;
}
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder;
if (convertView == null) {
convertView = LayoutInflater.from(MainActivity.this).inflate(R.layout.list_item_icon_text, parent, false);
holder = new ViewHolder();
holder.tv_title = (TextView) convertView.findViewById(R.id.tv_title);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
//处理逻辑
holder.processData(position);
return convertView;
}
}
private static class ViewHolder {
private TextView tv_title;
private void processData(int position) {
tv_title.setText("我是item"+position);
}
}
}
网友评论