ListView
1.简单列表--ArrayAdapter
<?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"><LinearLayoutandroid:layout_width="match_parent"android:layout_height="wrap_content"android:layout_marginRight="10dp"android:orientation="vertical"android:paddingLeft="10dp"><Buttonandroid:id="@+id/btn_array_list"android:layout_width="match_parent"android:layout_height="wrap_content"android:layout_marginTop="10dp"android:text="Array列表"/><Buttonandroid: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 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"><ListViewandroid:id="@+id/lv"android:layout_width="match_parent"android:layout_height="match_parent"/>
</LinearLayout>
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 {@Overrideprotected 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);}@Overridepublic 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);}}
}
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;@Overrideprotected 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() {@Overridepublic void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {Toast.makeText(ArrayListActivity.this,"你点击了"+i,Toast.LENGTH_LONG).show();}});}
}
2.图文列表--SimpleAdapter
<?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"><ListViewandroid:id="@+id/lv"android:layout_width="match_parent"android:layout_height="match_parent"/>
</LinearLayout>
<?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"><ImageViewandroid:id="@+id/iv_img"android:layout_width="100dp"android:layout_height="100dp"android:scaleType="centerCrop"android:src="@drawable/ic_launcher_background"/><TextViewandroid: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="雨中漫步"/><TextViewandroid: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>
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};@Overrideprotected 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 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"><LinearLayoutandroid:layout_width="match_parent"android:layout_height="wrap_content"android:layout_marginRight="10dp"android:orientation="vertical"android:paddingLeft="10dp"><Buttonandroid:id="@+id/btn_array_list"android:layout_width="match_parent"android:layout_height="wrap_content"android:layout_marginTop="10dp"android:text="Array列表"/><Buttonandroid:id="@+id/btn_array_simple"android:layout_width="match_parent"android:layout_height="wrap_content"android:layout_marginTop="10dp"android:text="Simple列表"/><Buttonandroid:id="@+id/btn_array_base"android:layout_width="match_parent"android:layout_height="wrap_content"android:layout_marginTop="10dp"android:text="BaseAdapter列表"/></LinearLayout></ScrollView>
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 {@Overrideprotected 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);}@Overridepublic 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 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"><ListViewandroid:id="@+id/lv"android:layout_width="match_parent"android:layout_height="match_parent"/>
</LinearLayout>
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;@Overrideprotected 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() {@Overridepublic 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() {@Overridepublic 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);}
}
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);}@Overridepublic int getCount() {return mBeanList.size();}@Overridepublic Object getItem(int i) {return mBeanList.get(i);}@Overridepublic long getItemId(int i) {return i;}@Overridepublic 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;}
}
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;}@Overridepublic String toString() {return "itemBean{" +"title='" + title + '\'' +", content='" + content + '\'' +", imgResId=" + imgResId +'}';}
}