Android列表显示,利用ListView和自定义适配器来构建自定义列表,另也可以设置列表项点击事件监听器

超、凢脫俗 2023-10-09 09:46 66阅读 0赞

原ArrayAdapter适配器列表样式只支持TextView,要自定义列表项,因此自定义一个适配器 :
1.创建自定义类
2.创建这自定义类的ArrayList数组
3.创建自定义适配器, 重写适配器构造函数将该数组作为输入参数 ;重写自定义适配器中的getView() 方法
4.创建自定义XML列表布局
5.ListView设置其适配器

以Miwok项目为例, 具体项目Example见http://github.com/uxk0587/miwok.git

1.创建自定义类

  1. //自定义一个类
  2. public class Word {
  3. private String miwokTranslation;
  4. private String defaultTranslation;
  5. private int imageResourceId;
  6. private int audioResourceId;
  7. public Word(String miwokTranslation, String defaultTranslation){
  8. this.miwokTranslation = miwokTranslation;
  9. this.defaultTranslation = defaultTranslation;
  10. }
  11. public Word(String miwokTranslation, String defaultTranslation, int audioResourceId){
  12. this.miwokTranslation = miwokTranslation;
  13. this.defaultTranslation = defaultTranslation;
  14. this.audioResourceId =audioResourceId;
  15. }
  16. public Word(String miwokTranslation, String defaultTranslation, int imageResourceId, int audioResourceId) {
  17. this.miwokTranslation = miwokTranslation;
  18. this.defaultTranslation = defaultTranslation;
  19. this.imageResourceId = imageResourceId;
  20. this.audioResourceId = audioResourceId;
  21. }
  22. public String getMiwokTranslation(){
  23. return miwokTranslation;
  24. }
  25. public String getDefaultTranslation(){
  26. return defaultTranslation;
  27. }
  28. public int getImageResourceId(){
  29. return imageResourceId;
  30. }
  31. public int getAudioResourceId(){
  32. return audioResourceId;
  33. }
  34. public boolean checkImageResource(){
  35. if (imageResourceId == 0) {
  36. return false;
  37. } else
  38. return true;
  39. }
  40. }

2.创建这个自定义类的ArrayList数组

  1. ArrayList<Word> words = new ArrayList<Word>();
  2. words.add( new Word("one", "lutti"));

为什么用ArrayList?这里对比一下数组和ArrayList的对比
在这里插入图片描述
注意数组列表ArrayList只能存储对象数据类型而不能放原始数据类型(例如整型、布尔型)如果想放整型数据等原始数据类型需要用到对象封装类,是原始数据类型变成对象数据类型。例如是用ArrayList< Integer>而不是用ArrayList< int>

3.创建自定义适配器&修改自定义适配器中的getView()

创建一WordApater类, 继承自ArrayAdapter
自定义constructor 将Word ArrayList数组作为输入参数

这里在WordAdapter中设置了列表项点击事件监听器,这种方法虽然也可以,但是会占用较多内存资源。建议在NumbersAcitivity中直接用listView.setOnItemOnclickListener的方法来设置列表项点击事件监听器

  1. public class WordAdapter extends ArrayAdapter<Word> {
  2. /** * This is our own custom constructor (it doesn't mirror a superclass constructor). * The context is used to inflate the layout file, and the list is the data we want * to populate into the lists. * * @param context The current context. Used to inflate the layout file. * @param words A List of word objects to display in a list */
  3. public WordAdapter(Activity context, ArrayList<Word> words){
  4. super(context, 0, words);
  5. }
  6. /** * Provides a view for an AdapterView (ListView, GridView, etc.) * * @param position The position in the list of data that should be displayed in the list item view. * @param convertView The recycled view to populate. * @param parent The parent ViewGroup that is used for inflation. * @return The View for the position in the AdapterView. */
  7. @NonNull
  8. @Override
  9. public View getView(int position, @Nullable View convertView, @NonNull ViewGroup parent) {
  10. //return super.getView(position, convertView, parent);
  11. // Check if the existing view is being reuse,otherwise inflate the view
  12. // (if convertView is null,there is no view to reuse, In this case we will need to inflate
  13. // one from the list item layout from scratch)
  14. View listItemView = convertView;
  15. if(listItemView == null){
  16. //LayoutInflater is an abstract class,and have no static method :inflate(),so
  17. //we should use LayoutInflater.from(getContext()) method to obtain a LayoutInflater
  18. //from the given context
  19. //
  20. //The third param false, because we don't want to attach the list item view to the parent list view.
  21. //The'getContext()' parameter is defined in ArrayAdapter.
  22. listItemView = LayoutInflater.from(getContext()).inflate(R.layout.list_item, parent, false);
  23. }
  24. //Get the object located at this position in the list. getItem() method from the super class ArrayAdapter
  25. Word currentWord = getItem(position);
  26. //Find the TextView in the list_item.xml layout with the id.
  27. //ArrayAdapter don't have the method of 'findViewById()',
  28. //so must have to use the method of 'View.findViewById()' to find the TextView
  29. TextView defaultTextView = (TextView)listItemView.findViewById(R.id.default_text_view);
  30. defaultTextView.setText(currentWord.getDefaultTranslation());
  31. //Find the TextView in the list_item.xml layout with the id
  32. TextView miwokTextView = (TextView)listItemView.findViewById(R.id.miwok_text_view);
  33. miwokTextView.setText(currentWord.getMiwokTranslation());
  34. if (currentWord.checkImageResource()) {
  35. //Find the ImageView in the list_item.xml layout with the id
  36. ImageView imageView = (ImageView)listItemView.findViewById(R.id.image_view);
  37. imageView.setImageResource(currentWord.getImageResourceId());
  38. }else{
  39. ImageView imageView = (ImageView)listItemView.findViewById(R.id.image_view);
  40. imageView.setVisibility(View.GONE);
  41. }
  42. RelativeLayout textLinearLayout = (RelativeLayout)listItemView.findViewById(R.id.text_relative_layout);
  43. textLinearLayout.setBackgroundResource(R.color.category_numbers);
  44. //由于下面匿名内部类会用到外部类的变量,外部类被引用的变量必须用final
  45. final MediaPlayer mediaPlayer = MediaPlayer.create(getContext(), currentWord.getAudioResourceId());
  46. //设置点击事件监听器
  47. //Instantiate the View.OnclickListener interface
  48. //by using the way of creating anonymous class to set on a Listener
  49. textLinearLayout.setOnClickListener(new View.OnClickListener(){
  50. public void onClick(View view){
  51. Toast.makeText(view.getContext(), "Play", Toast.LENGTH_SHORT).show();
  52. mediaPlayer.start();
  53. }
  54. });
  55. return listItemView;
  56. }
  57. }

创建自定义XML列表布局list_item.xml

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tool="http://schemas.android.com/tools" android:id="@+id/list_item" android:layout_width="match_parent" android:layout_height="wrap_content" android:minHeight="@dimen/list_item_height" android:orientation="horizontal">
  3. <ImageView android:layout_width="@dimen/list_item_height" android:layout_height="@dimen/list_item_height" android:id="@+id/image_view" android:background="@color/tan_background" android:src="@mipmap/ic_launcher"/>
  4. <RelativeLayout android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" android:background="@color/category_numbers" android:id="@+id/text_relative_layout" android:paddingLeft="16dp" android:paddingTop="16dp">
  5. <TextView android:id="@+id/miwok_text_view" android:layout_width="match_parent" android:layout_height="wrap_content" android:textSize="18sp" android:textColor="@android:color/white" android:textStyle="bold" tool:text="lutti" />
  6. <TextView android:id="@+id/default_text_view" android:layout_width="match_parent" android:layout_height="wrap_content" android:textColor="@android:color/white" android:layout_below="@id/miwok_text_view" android:textSize="18sp" tool:text="one" />
  7. </RelativeLayout>
  8. </LinearLayout>

ListView设置其适配器

  1. //实例化自定义适配器
  2. WordAdapter wordAdapter = new WordAdapter(this, words, R.color.category_numbers);
  3. ListView listView = (ListView)findViewById(R.id.numbers_list);
  4. //listview设置适配器
  5. listView.setAdapter(wordAdapter);

发表评论

表情:
评论列表 (有 0 条评论,66人围观)

还没有评论,来说两句吧...

相关阅读