今天学习了ListView
main.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"
android:orientation="vertical">
<ListView
android:id="@+id/lv"
android:layout_width="match_parent"
android:layout_height="match_parent"
>
</ListView>
</LinearLayout>
item.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"
android:orientation="vertical">
<TextView
android:id="@+id/tv"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="30sp"
>
</TextView>
</LinearLayout>
Bean.java
package com.example.listview;
public class Bean
{
String name;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
MainActivity.java
package com.example.listview;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
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);
}
ListView listView = findViewById(R.id.lv);
listView.setAdapter(new MyAadapter(data,this));
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
}
});
}
}
MyAdapter.java
package com.example.listview;标签:总结,layout,parent,3.10,学习,import,android,data,public From: https://www.cnblogs.com/jy-all-bug/p/17201615.html
import android.content.Context;
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 MyAadapter extends BaseAdapter {
private List<Bean> data;
private Context context;
public MyAadapter(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 converVIew, ViewGroup parent) {
ViewHoulder viewHoulder;
if(converVIew==null) {
viewHoulder=new ViewHoulder();
converVIew = LayoutInflater.from(context).inflate(R.layout.list_item, parent, false);
viewHoulder.textView=converVIew.findViewById(R.id.tv);
converVIew.setTag(viewHoulder);
}
else {
viewHoulder=(ViewHoulder)converVIew.getTag();
}
viewHoulder.textView.setText(data.get(i).getName());
return converVIew;
}
private final class ViewHoulder{
TextView textView;
}
}