ListView
1.简单列表--ArrayAdapter
XML
<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginRight="10dp"
android:orientation="vertical"
android:paddingLeft="10dp">
<Button
android:id="@+id/btn_array_list"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:text="Array列表"/>
<Button
android:id="@+id/btn_array_simple"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:text="Simple列表"/>
</LinearLayout>
</ScrollView>
XML
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".ArrayListActivity">
<ListView
android:id="@+id/lv"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
</LinearLayout>
java
package com.example.listviewtest;
import androidx.appcompat.app.AppCompatActivity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
public class MainActivity extends AppCompatActivity implements View.OnClickListener {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button btn_array_list = findViewById(R.id.btn_array_list);
btn_array_list.setOnClickListener(this);
Button btn_array_simple = findViewById(R.id.btn_array_simple);
btn_array_simple.setOnClickListener(this);
}
@Override
public void onClick(View view) {
if (view.getId() == R.id.btn_array_list){
Intent intent = new Intent(this, ArrayListActivity.class);
startActivity(intent);
} else if (view.getId() == R.id.btn_array_simple) {
Intent intent = new Intent(this, SimpleListActivity.class);
startActivity(intent);
}
}
}
java
package com.example.listviewtest;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.Toast;
import java.util.ArrayList;
import java.util.List;
public class ArrayListActivity extends AppCompatActivity {
private ListView mListView;
private List<String> mStringList;
private ArrayAdapter<String> mArrayAdapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_array_list);
mListView = findViewById(R.id.lv);
mStringList = new ArrayList<>();
for (int i = 0; i < 50; i++) {
mStringList.add("这是条目"+i);
}
mArrayAdapter = new ArrayAdapter<>(this,android.R.layout.simple_list_item_1,mStringList);
mListView.setAdapter(mArrayAdapter);
mListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
Toast.makeText(ArrayListActivity.this,"你点击了"+i,Toast.LENGTH_LONG).show();
}
});
}
}
2.图文列表--SimpleAdapter
XML
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".SimpleListActivity">
<ListView
android:id="@+id/lv"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
</LinearLayout>
XML
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingLeft="10dp"
android:paddingTop="5dp"
android:paddingRight="10dp"
android:paddingBottom="5dp">
<ImageView
android:id="@+id/iv_img"
android:layout_width="100dp"
android:layout_height="100dp"
android:scaleType="centerCrop"
android:src="@drawable/ic_launcher_background"/>
<TextView
android:id="@+id/tv_title"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:layout_toRightOf="@id/iv_img"
android:ellipsize="end"
android:maxLines="1"
android:textSize="20sp"
android:textStyle="bold"
android:text="雨中漫步"/>
<TextView
android:id="@+id/tv_content"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@id/tv_title"
android:layout_marginLeft="10dp"
android:layout_toRightOf="@id/iv_img"
android:ellipsize="end"
android:maxLines="3"
android:text="人生就像时一场旅行,不必在乎目的地,在乎你妈说的你妈的嘛嘛嘛才是能真的嘛嘛嘛对咯收到反馈大姐夫,啊塞德里克复健科老师的会计法 阿是两地分居"
android:textSize="16sp"/>
</RelativeLayout>
java
package com.example.listviewtest;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
public class SimpleListActivity extends AppCompatActivity {
private ListView mListView;
private SimpleAdapter mSimpleAdapter;
private List<Map<String,Object>> mList;
private int[] imgs = {
R.drawable.test1,
R.drawable.test2,
R.drawable.test3,
R.drawable.test4,
R.drawable.test5,
R.drawable.test6,
R.drawable.test7,
R.drawable.test8,
R.drawable.test9,
R.drawable.test10
};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_simple_list);
mListView = findViewById(R.id.lv);
mList = new ArrayList<>();
for (int i = 0; i < 50; i++) {
Map<String,Object> map = new HashMap<>();
map.put("img",imgs[i%imgs.length]);
map.put("title","这是标题"+i);
map.put("content","这是内容"+i);
mList.add(map);
}
mSimpleAdapter = new SimpleAdapter(this,mList,
R.layout.list_item_layout,
new String[]{"img","title","content"},
new int[]{R.id.iv_img,R.id.tv_title,R.id.tv_content});
mListView.setAdapter(mSimpleAdapter);
}
}
3.图文复杂列表--BaseAdapter
XML
<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginRight="10dp"
android:orientation="vertical"
android:paddingLeft="10dp">
<Button
android:id="@+id/btn_array_list"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:text="Array列表"/>
<Button
android:id="@+id/btn_array_simple"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:text="Simple列表"/>
<Button
android:id="@+id/btn_array_base"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:text="BaseAdapter列表"/>
</LinearLayout>
</ScrollView>
java
package com.example.listviewtest;
import androidx.appcompat.app.AppCompatActivity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
public class MainActivity extends AppCompatActivity implements View.OnClickListener {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button btn_array_list = findViewById(R.id.btn_array_list);
btn_array_list.setOnClickListener(this);
Button btn_array_simple = findViewById(R.id.btn_array_simple);
btn_array_simple.setOnClickListener(this);
Button btn_array_base = findViewById(R.id.btn_array_base);
btn_array_base.setOnClickListener(this);
}
@Override
public void onClick(View view) {
if (view.getId() == R.id.btn_array_list){
Intent intent = new Intent(this, ArrayListActivity.class);
startActivity(intent);
} else if (view.getId() == R.id.btn_array_simple) {
Intent intent = new Intent(this, SimpleListActivity.class);
startActivity(intent);
} else if (view.getId() == R.id.btn_array_base) {
Intent intent = new Intent(this, BaseAdapterActivity.class);
startActivity(intent);
}
}
}
XML
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".BaseAdapterActivity"
android:orientation="vertical">
<ListView
android:id="@+id/lv"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
</LinearLayout>
java
package com.example.listviewtest;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.Toast;
import com.example.listviewtest.adapter.MyAdapter;
import com.example.listviewtest.bean.ItemBean;
import java.util.ArrayList;
import java.util.List;
public class BaseAdapterActivity extends AppCompatActivity {
private ListView mListView;
private List<ItemBean> mBeanList;
private MyAdapter mMyAdapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_base_adapter);
initView();
initData();
initEvent();
}
private void initEvent() {
mMyAdapter = new MyAdapter(this,mBeanList);
mListView.setAdapter(mMyAdapter);
mListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
ItemBean itemBean = mBeanList.get(i);
String title = itemBean.getTitle();
Toast.makeText(BaseAdapterActivity.this,"您点击了"+i+title,Toast.LENGTH_LONG).show();
}
});
mListView.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
@Override
public boolean onItemLongClick(AdapterView<?> adapterView, View view, int i, long l) {
return false;
}
});
}
private void initData() {
mBeanList = new ArrayList<>();
ItemBean itemBean1 = new ItemBean();
itemBean1.setTitle("我的小黑狗");
itemBean1.setContent("我的小黑狗,明天就到了,到底是死是活,没人知道,希望你不会挂");
itemBean1.setImgResId(R.drawable.test1);
ItemBean itemBean2 = new ItemBean();
itemBean2.setTitle("我的小白狗");
itemBean2.setContent("我的小白狗,以前跑丢了,希望你不会被狗贩子抓去杀了,卖狗肉");
itemBean2.setImgResId(R.drawable.test2);
ItemBean itemBean3 = new ItemBean();
itemBean3.setTitle("我的小黑狗");
itemBean3.setContent("我的小黑狗,明天就到了,到底是死是活,没人知道,希望你不会挂");
itemBean3.setImgResId(R.drawable.test3);
ItemBean itemBean4 = new ItemBean();
itemBean4.setTitle("我的小白狗");
itemBean4.setContent("我的小白狗,以前跑丢了,希望你不会被狗贩子抓去杀了,卖狗肉");
itemBean4.setImgResId(R.drawable.test4);
ItemBean itemBean5 = new ItemBean();
itemBean5.setTitle("我的小黑狗");
itemBean5.setContent("我的小黑狗,明天就到了,到底是死是活,没人知道,希望你不会挂");
itemBean5.setImgResId(R.drawable.test5);
ItemBean itemBean6 = new ItemBean();
itemBean6.setTitle("我的小白狗");
itemBean6.setContent("我的小白狗,以前跑丢了,希望你不会被狗贩子抓去杀了,卖狗肉");
itemBean6.setImgResId(R.drawable.test6);
ItemBean itemBean7 = new ItemBean();
itemBean7.setTitle("我的小黑狗");
itemBean7.setContent("我的小黑狗,明天就到了,到底是死是活,没人知道,希望你不会挂");
itemBean7.setImgResId(R.drawable.test7);
ItemBean itemBean8 = new ItemBean();
itemBean8.setTitle("我的小白狗");
itemBean8.setContent("我的小白狗,以前跑丢了,希望你不会被狗贩子抓去杀了,卖狗肉");
itemBean8.setImgResId(R.drawable.test8);
ItemBean itemBean9 = new ItemBean();
itemBean9.setTitle("我的小黑狗");
itemBean9.setContent("我的小黑狗,明天就到了,到底是死是活,没人知道,希望你不会挂");
itemBean9.setImgResId(R.drawable.test9);
ItemBean itemBean10 = new ItemBean();
itemBean10.setTitle("我的小白狗");
itemBean10.setContent("我的小白狗,以前跑丢了,希望你不会被狗贩子抓去杀了,卖狗肉");
itemBean10.setImgResId(R.drawable.test10);
mBeanList.add(itemBean1);
mBeanList.add(itemBean2);
mBeanList.add(itemBean3);
mBeanList.add(itemBean4);
mBeanList.add(itemBean5);
mBeanList.add(itemBean6);
mBeanList.add(itemBean7);
mBeanList.add(itemBean8);
mBeanList.add(itemBean9);
mBeanList.add(itemBean10);
}
private void initView() {
mListView = findViewById(R.id.lv);
}
}
java
package com.example.listviewtest.adapter;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;
import com.example.listviewtest.R;
import com.example.listviewtest.bean.ItemBean;
import java.util.List;
public class MyAdapter extends BaseAdapter {
private List<ItemBean> mBeanList;
private LayoutInflater mLayoutInflater;
private Context mContext;
public MyAdapter(Context context, List<ItemBean> beanList){
this.mContext = context;
this.mBeanList = beanList;
mLayoutInflater = LayoutInflater.from(mContext);
}
@Override
public int getCount() {
return mBeanList.size();
}
@Override
public Object getItem(int i) {
return mBeanList.get(i);
}
@Override
public long getItemId(int i) {
return i;
}
@Override
public View getView(int i, View view, ViewGroup viewGroup) {
view = mLayoutInflater.inflate(R.layout.list_item_layout,viewGroup,false);
ImageView imageView = view.findViewById(R.id.iv_img);
TextView tvTitle= view.findViewById(R.id.tv_title);
TextView tvContent = view.findViewById(R.id.tv_content);
ItemBean itemBean = mBeanList.get(i);
imageView.setImageResource(itemBean.getImgResId());
tvTitle.setText(itemBean.getTitle());
tvContent.setText(itemBean.getContent());
return view;
}
}
java
package com.example.listviewtest.bean;
public class ItemBean {
private String title;
private String content;
private int imgResId;
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public String getContent() {
return content;
}
public void setContent(String content) {
this.content = content;
}
public int getImgResId() {
return imgResId;
}
public void setImgResId(int imgResId) {
this.imgResId = imgResId;
}
@Override
public String toString() {
return "itemBean{" +
"title='" + title + '\'' +
", content='" + content + '\'' +
", imgResId=" + imgResId +
'}';
}
}