先看一下改动的文件是哪些
然后做出的样子是什么样的:
两个drawable文件的内容很简单,就是定义了控件,代码如下:
main.xml
<LinearLayout android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:android="http://schemas.android.com/apk/res/android">
<ListView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/lv"
/>
LinearLayout>
listItem.xml
<LinearLayout android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
xmlns:android="http://schemas.android.com/apk/res/android">
<TextView
android:id="@+id/tv"
android:textSize="30sp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
LinearLayout>
然后是比较重要的mainActivity文件
package com.sgh.listview;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;
import java.util.ArrayList;
import java.util.List;
public class MainActivity extends AppCompatActivity {
private List<Bean> data = new ArrayList<>();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
for(int i = 0; i < 100; i++){
Bean bean = new Bean();
bean.setName("测试" + i);
data.add(bean);//将创建的Bean对象放入List里面,List数据填充完毕
}
ListView listView = findViewById(R.id.lv);
//数据怎么填充到ListView,要用到BaseAdapter
listView.setAdapter(new MyAdpater(data,this));
//设置点击事件
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
Log.e("test","onItemClick: " + i);
}
});
}
}
MyAdapter代码
package com.sgh.listview;
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 MyAdpater extends BaseAdapter {
private List<Bean> data;
private Context context;
public MyAdpater(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;
//渲染的给view,Item是复用的,不断进行判断
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("test","getView:" + i);
return view;
}
//findViewById是很耗时的,所以我们新建一个类
private final class ViewHolder{
//Listitem里面有几个子控件,这里就几个属性
TextView textView;
}
}
Bean文件是每一个Item内容的属性的定义
package com.sgh.listview;
public class Bean {
String name;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
如果有看不懂的那个Adapter的,可以看看这个文章
BaseAdapter使用说明
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)