当前位置:   article > 正文

ListView

ListView

效果图

1、在activity_main.xml中创建ListView,再创建list_item.xml

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3. android:layout_width="match_parent"
  4. android:layout_height="match_parent"
  5. android:orientation="vertical">
  6. <ListView
  7. android:id="@+id/lv"
  8. android:layout_width="match_parent"
  9. android:layout_height="match_parent"/>
  10. </LinearLayout>
  1. <?xml version="1.0" encoding="utf-8"?>
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3. android:orientation="vertical"
  4. android:layout_width="match_parent"
  5. android:layout_height="match_parent">
  6. <TextView
  7. android:id="@+id/tv"
  8. android:layout_width="wrap_content"
  9. android:layout_height="wrap_content"
  10. android:textSize="30sp"/>
  11. </LinearLayout>

2、MainActivity.java

  1. package com.example.myapplication;
  2. import android.os.Bundle;
  3. import android.util.Log;
  4. import android.view.View;
  5. import android.widget.AdapterView;
  6. import android.widget.ListView;
  7. import androidx.appcompat.app.AppCompatActivity;
  8. import java.util.ArrayList;
  9. import java.util.List;
  10. public class MainActivity extends AppCompatActivity {
  11. private List<Bean> data = new ArrayList<>();
  12. @Override
  13. protected void onCreate(Bundle savedInstanceState) {
  14. super.onCreate(savedInstanceState);
  15. setContentView(R.layout.activity_main);
  16. // 每个item显示的内容
  17. for (int i = 0; i < 100; i++) {
  18. Bean bean = new Bean();
  19. bean.setName("局长,我想进步了!" + i);
  20. data.add(bean);
  21. }
  22. ListView lv = findViewById(R.id.lv);
  23. lv.setAdapter(new MyAdapter(data, this));
  24. // 触发item点击事件
  25. lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
  26. @Override
  27. public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
  28. Log.e("leo", "onItemClick" + i);
  29. }
  30. });
  31. }
  32. }

3、创建Bean类

  1. package com.example.myapplication;
  2. public class Bean {
  3. String name;
  4. public String getName() {
  5. return name;
  6. }
  7. public void setName(String name) {
  8. this.name = name;
  9. }
  10. }

4、创建一个适配器MyAdapter

  1. package com.example.myapplication;
  2. import android.content.Context;
  3. import android.util.Log;
  4. import android.view.LayoutInflater;
  5. import android.view.View;
  6. import android.view.ViewGroup;
  7. import android.widget.BaseAdapter;
  8. import android.widget.TextView;
  9. import java.util.List;
  10. public class MyAdapter extends BaseAdapter {
  11. private List<Bean> data;
  12. private Context context;
  13. public MyAdapter(List<Bean> data, Context context) {
  14. this.data = data;
  15. this.context = context;
  16. }
  17. @Override
  18. public int getCount() {
  19. return data.size();
  20. }
  21. @Override
  22. public Object getItem(int i) {
  23. return null;
  24. }
  25. @Override
  26. public long getItemId(int i) {
  27. return i;
  28. }
  29. @Override
  30. public View getView(int i, View view, ViewGroup viewGroup) {
  31. if (view == null) {
  32. view = LayoutInflater.from(context).inflate(R.layout.list_item,viewGroup,false);
  33. }
  34. TextView textView = view.findViewById(R.id.tv);
  35. textView.setText(data.get(i).getName());
  36. Log.e("leo","getView" + i);
  37. return view;
  38. }
  39. }

上述标红处需重点理解

5、使用ViewHolder优化适配器

降低findViewById的使用,提高运行速度

  1. package com.example.myapplication;
  2. import android.content.Context;
  3. import android.util.Log;
  4. import android.view.LayoutInflater;
  5. import android.view.View;
  6. import android.view.ViewGroup;
  7. import android.widget.BaseAdapter;
  8. import android.widget.TextView;
  9. import java.util.List;
  10. public class MyAdapter extends BaseAdapter {
  11. private List<Bean> data;
  12. private Context context;
  13. public MyAdapter(List<Bean> data, Context context) {
  14. this.data = data;
  15. this.context = context;
  16. }
  17. @Override
  18. public int getCount() {
  19. return data.size();
  20. }
  21. @Override
  22. public Object getItem(int i) {
  23. return null;
  24. }
  25. @Override
  26. public long getItemId(int i) {
  27. return i;
  28. }
  29. @Override
  30. public View getView(int i, View view, ViewGroup viewGroup) {
  31. ViewHolder viewHolder;
  32. if (view == null) {
  33. viewHolder = new ViewHolder();
  34. view = LayoutInflater.from(context).inflate(R.layout.list_item,viewGroup,false);
  35. viewHolder.textView = view.findViewById(R.id.tv);
  36. view.setTag(viewHolder);
  37. } else {
  38. viewHolder = (ViewHolder) view.getTag();
  39. }
  40. viewHolder.textView.setText(data.get(i).getName());
  41. Log.e("leo","getView" + i);
  42. return view;
  43. }
  44. private final class ViewHolder {
  45. TextView textView;
  46. }
  47. }

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/我家自动化/article/detail/805920
推荐阅读
相关标签
  

闽ICP备14008679号