一、实验目的
掌握ListView控件和Recyclerview的基本使用方式。
掌握使用ArrayAdapter、SimpleAdapter、BaseAdapter实现填充数据。
二、实验指导
1、写UI界面 xml文件 ListView
2、寻找ListView(初始化)
3、实现ListView的数据适配器adapter
4、给listView设置adapter
5、将模拟器运行结果截图到word文档中,并将核心代码复制到word文档中。
三、实验内容
使用ListView或Recyclerview控件结合适配器的使用实现类似学习强国界面效果。其中自定义的item界面效果及素材等资源可自己发挥。
四、模拟器效果截图
五、实验源代码
BasePerson.java
1. package com.example.myapplication02; 2. 3. public class BasePerson { 4. private int icno; 5. private String title; 6. private String content; 7. 8. public int getIcno() { 9. return icno; 10. } 11. 12. public void setIcno(int icno) { 13. this.icno = icno; 14. } 15. 16. public String getTitle() { 17. return title; 18. } 19. 20. public void setTitle(String title) { 21. this.title = title; 22. } 23. 24. public String getContent() { 25. return content; 26. } 27. 28. public void setContent(String content) { 29. this.content = content; 30. } 31. }
BaseAdapterL.java
1. package com.example.myapplication02; 2. 3. import android.content.Context; 4. import android.view.View; 5. import android.view.ViewGroup; 6. import android.widget.BaseAdapter; 7. import android.widget.ImageView; 8. import android.widget.TextView; 9. import androidx.recyclerview.widget.RecyclerView; 10. import java.util.List; 11. 12. public class BaseAdapterL extends BaseAdapter { 13. private List<BasePerson> PList; 14. private Context context; 15. private int LyId; 16. 17. public BaseAdapterL(List<BasePerson> PList, Context context, int LyId) { 18. this.PList = PList; 19. this.context = context; 20. this.LyId = LyId; 21. } 22. 23. @Override 24. public int getCount() { 25. return PList.size(); 26. } 27. 28. @Override 29. public Object getItem(int i) { 30. return null; 31. } 32. 33. @Override 34. public long getItemId(int i) { 35. return 0; 36. } 37. 38. @Override 39. public View getView(int i, View view, ViewGroup viewGroup) { 40. ViewHolder holder = null; 41. if (view == null){ 42. holder = new ViewHolder(); 43. view = View.inflate(context,LyId,null); 44. holder.imageView = view.findViewById(R.id.imageView); 45. holder.textView2 = view.findViewById(R.id.textView2); 46. holder.textView = view.findViewById(R.id.textView); 47. view.setTag(holder); 48. }else { 49. holder = (ViewHolder) view.getTag(); 50. } 51. holder.imageView.setImageResource(PList.get(i).getIcno()); 52. holder.textView2.setText(PList.get(i).getTitle()); 53. holder.textView.setText(PList.get(i).getContent()); 54. return view; 55. } 56. class ViewHolder{ 57. ImageView imageView; 58. TextView textView; 59. TextView textView2; 60. } 61. }
BaseMain.java
1. package com.example.myapplication02; 2. 3. import android.os.Bundle; 4. import android.widget.ListView; 5. import androidx.appcompat.app.AppCompatActivity; 6. import java.util.ArrayList; 7. import java.util.List; 8. 9. public class BaseMain extends AppCompatActivity { 10. private ListView lv; 11. private List<BasePerson> list=new ArrayList<>(); 12. @Override 13. protected void onCreate(Bundle savedInstanceState) { 14. super.onCreate(savedInstanceState); 15. setContentView(R.layout.larrayadapter); 16. lv=findViewById(R.id.lv); 17. BasePerson BP1 = new BasePerson(); 18. BP1.setIcno(R.drawable.shangqiu01); 19. BP1.setTitle("2023年3月30日"); 20. BP1.setContent("个人爱好:好好学习,天天向上!"); 21. BasePerson BP2 = new BasePerson(); 22. BP2.setIcno(R.drawable.luoyang01); 23. BP2.setTitle("2023年3月30日"); 24. BP2.setContent("洛阳老君山!洛阳老君山!洛阳老君山!洛阳老君山!"); 25. BasePerson BP3 = new BasePerson(); 26. BP3.setIcno(R.drawable.kaifeng01); 27. BP3.setTitle("2023年3月30日"); 28. BP3.setContent("开封清明上河园!开封清明上河园!开封清明上河园!"); 29. BasePerson BP4 = new BasePerson(); 30. BP4.setIcno(R.drawable.luoyang01); 31. BP4.setTitle("2023年3月30日"); 32. BP4.setContent("洛阳老君山!洛阳老君山!洛阳老君山!洛阳老君山!"); 33. BasePerson BP5 = new BasePerson(); 34. BP5.setIcno(R.drawable.kaifeng01); 35. BP5.setTitle("2023年3月30日"); 36. BP5.setContent("开封清明上河园!开封清明上河园!开封清明上河园!"); 37. list.add(BP1); 38. list.add(BP2); 39. list.add(BP3); 40. list.add(BP4); 41. list.add(BP5); 42. BaseAdapterL adapter1=new BaseAdapterL(list,BaseMain.this,R.layout.listadapter); 43. lv.setAdapter(adapter1); 44. } 45. }
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:background="@drawable/p01"> 6. 7. <ListView 8. android:id="@+id/lv" 9. android:layout_width="match_parent" 10. android:layout_height="wrap_content" 11. android:entries="@array/son_name"></ListView> 12. </LinearLayout>
1. <?xml version="1.0" encoding="utf-8"?> 2. <androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android" 3. xmlns:app="http://schemas.android.com/apk/res-auto" 4. xmlns:tools="http://schemas.android.com/tools" 5. android:layout_width="match_parent" 6. android:layout_height="match_parent"> 7. 8. <ImageView 9. android:id="@+id/imageView" 10. android:layout_width="115dp" 11. android:layout_height="92dp" 12. android:layout_margin="10dp" 13. android:adjustViewBounds="true" 14. android:maxWidth="100dp" 15. android:maxHeight="100dp" 16. app:layout_constraintBottom_toBottomOf="parent" 17. app:layout_constraintStart_toStartOf="parent" 18. app:layout_constraintTop_toTopOf="parent" 19. app:srcCompat="@drawable/kaifeng01" /> 20. 21. <TextView 22. android:id="@+id/textView" 23. android:layout_width="262dp" 24. android:layout_height="54dp" 25. android:layout_margin="10dp" 26. android:layout_marginStart="24dp" 27. android:text="TextView" 28. android:textSize="20dp" 29. app:layout_constraintBottom_toBottomOf="@+id/imageView" 30. app:layout_constraintEnd_toEndOf="parent" 31. app:layout_constraintHorizontal_bias="1.0" 32. app:layout_constraintStart_toEndOf="@+id/imageView" 33. app:layout_constraintTop_toTopOf="@+id/imageView" 34. app:layout_constraintVertical_bias="0.0" /> 35. 36. <TextView 37. android:id="@+id/textView2" 38. android:layout_width="126dp" 39. android:layout_height="19dp" 40. android:layout_marginStart="128dp" 41. android:text="TextView" 42. android:textSize="18dp" 43. app:layout_constraintBottom_toBottomOf="@+id/imageView" 44. app:layout_constraintEnd_toEndOf="parent" 45. app:layout_constraintStart_toEndOf="@+id/imageView" 46. app:layout_constraintTop_toBottomOf="@+id/textView" 47. app:layout_constraintVertical_bias="1.0" /> 48. </androidx.constraintlayout.widget.ConstraintLayout>