16. ListView
作者:互联网
Android基础开发——ListView
16. ListView
实现滑动条目
16.1 list_item布局
<?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">
<TextView
android:id="@+id/tv"
android:textSize="30sp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
</LinearLayout>
16.2 Java Bean
package com.dingjiaxiong.mylistview;
public class Bean {
String name;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
16.3 设置数据
16.4 辅助类——Adapter
package com.dingjiaxiong.mylistview;
import android.content.Context;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;
import java.util.List;
public class MyAdapter extends BaseAdapter {
private static final String TAG = "dingjiaxiong";
private List<Bean> data;
private Context context;
//构造方法
public MyAdapter(List<Bean> data, Context context) {
this.data = data;
this.context = context;
}
@Override
public int getCount() {
return data.size();
}
@Override
public Object getItem(int i) {
return null;
}
@Override
public long getItemId(int i) {
return i;
}
@Override
public View getView(int i, View view, ViewGroup viewGroup) {
if(view == null){
view = LayoutInflater.from(context).inflate(R.layout.list_item,viewGroup,false);
}
TextView textView = view.findViewById(R.id.tv);
textView.setText(data.get(i).getName());
Log.e(TAG, "getView: "+ i);
return view;
}
}
16.5 配置数据
//拿到listview
ListView listView = findViewById(R.id.lv);
listView.setAdapter(new MyAdapter(data , this));
运行
16.6 list_view的点击事件
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
Log.e("dingjiaxiong", "onItemClick: " + i);
}
});
16.7 优化 ViewHolder
MyAdapter.java
package com.dingjiaxiong.mylistview;
import android.content.Context;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;
import java.util.List;
public class MyAdapter extends BaseAdapter {
private static final String TAG = "dingjiaxiong";
private List<Bean> data;
private Context context;
//构造方法
public MyAdapter(List<Bean> data, Context context) {
this.data = data;
this.context = context;
}
@Override
public int getCount() {
return data.size();
}
@Override
public Object getItem(int i) {
return null;
}
@Override
public long getItemId(int i) {
return i;
}
@Override
public View getView(int i, View view, ViewGroup viewGroup) {
ViewHolder viewHolder;
if(view == null){
viewHolder = new ViewHolder();
view = LayoutInflater.from(context).inflate(R.layout.list_item,viewGroup,false);
viewHolder.textView = view.findViewById(R.id.tv);
view.setTag(viewHolder);
}
else{
viewHolder = (ViewHolder) view.getTag();
}
viewHolder.textView.setText(data.get(i).getName());
Log.e(TAG, "getView: "+ i);
return view;
}
private final class ViewHolder{
TextView textView;
}
}
标签:16,context,import,android,ListView,data,public,view 来源: https://www.cnblogs.com/55zjc/p/16698962.html