ListView中利用另一方法AdapterView.setOnItemClickListener来设置列表项的点击事件监听器

Dear 丶 2023-08-17 17:12 110阅读 0赞

对比之间在自定义适配器中设置列表项点击事件监听器的方法, 这里说明第二种方法, 这种办法相对更好,更省内存资源

同是Miwok项目, 举个例子, 在NumbersActivity中可以用一种方法设置列表项的点击事件监听器, 之间在NumbersActivity中利用listView.setOnItemClickListener()设置;

另外注意: 在播放完后, 一旦该MediaPlayer对象再被使用,应该调用release()方法将这些资源释放.

  1. public class NumbersActivity extends AppCompatActivity {
  2. MediaPlayer mediaPlayer;
  3. //This listener gets triggered when the {@link MediaPlayer} has completed
  4. //playing the audio files.
  5. private MediaPlayer.OnCompletionListener completionListener = new MediaPlayer.OnCompletionListener(){
  6. public void onCompletion(MediaPlayer mediaPlayer){
  7. //Toast.makeText(NumbersActivity.this, "I'm done" , Toast.LENGTH_SHORT).show();
  8. //Now that the sound file has finished playing, release the media player resources.
  9. releaseMediaPlayer();
  10. }
  11. };
  12. @Override
  13. protected void onCreate(Bundle savedInstanceState) {
  14. super.onCreate(savedInstanceState);
  15. setContentView(R.layout.activity_numbers);
  16. //create a list of Word
  17. final ArrayList<Word> words = new ArrayList<Word>();
  18. words.add(new Word("lutti", "one", R.drawable.number_one, R.raw.number_one));
  19. words.add(new Word("otiiko", "two", R.drawable.number_two,R.raw.number_two));
  20. words.add(new Word("tolookosu", "three", R.drawable.number_three, R.raw.number_three));
  21. words.add(new Word("oyyisa", "four", R.drawable.number_four, R.raw.number_four));
  22. words.add(new Word("massokka", "five", R.drawable.number_five, R.raw.number_five));
  23. words.add(new Word("temmokka", "six", R.drawable.number_six, R.raw.number_six));
  24. words.add(new Word("kenekaku", "seven", R.drawable.number_seven, R.raw.number_seven));
  25. words.add(new Word("kawinta", "eight", R.drawable.number_eight, R.raw.number_eight));
  26. words.add(new Word("wo'e", "nine", R.drawable.number_nine, R.raw.number_nine));
  27. words.add(new Word("na'aacha", "ten", R.drawable.number_ten, R.raw.number_ten));
  28. WordAdapter wordAdapter = new WordAdapter(this, words);
  29. ListView listView = (ListView)findViewById(R.id.numbers_list);
  30. listView.setAdapter(wordAdapter);
  31. listView.setOnItemClickListener(new AdapterView.OnItemClickListener(){
  32. @Override
  33. //AdapterView: the AdapterView where the Click happened.
  34. //view: the view within the AdapterView that was clicked(this will be view provided by the adapter).
  35. //position: the position of the view int the adapter.
  36. //id: the row id of the item that was clicked.
  37. public void onItemClick(AdapterView<?> AdapterView, View view, int position, long id) {
  38. //Release the media player if it currently exists because we are about to
  39. //play a different sound file.
  40. releaseMediaPlayer();
  41. Word currentWord = words.get(position);
  42. mediaPlayer = MediaPlayer.create(view.getContext(), currentWord.getAudioResourceId());
  43. mediaPlayer.start();
  44. //Setup a listener on the media player, so that we can stop and release the
  45. //media player once the sounds has finished playing.
  46. mediaPlayer.setOnCompletionListener(completionListener);
  47. }
  48. });
  49. }
  50. /** * Clean up the media player by releasing its resources. */
  51. private void releaseMediaPlayer(){
  52. //If the media player is not null, the it may be currently playing a sound.
  53. if(mediaPlayer != null){
  54. //Regardless of the current state of the media, release its resources.
  55. //because wo no longer need it.
  56. mediaPlayer.release();
  57. //Set the mediaPlayer back to null.For our code, we've decided that
  58. //setting the media player to null is an easy way to tell that the media player
  59. //is not configured to play an audio file at the moment.
  60. mediaPlayer = null;
  61. }
  62. }
  63. }

注意:ListView 和GridView 同是AdapterView的子类
删除第一种设置列表项点击事件监听器的方法, 即在适配器中设置点击事件监听器

  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. final 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. // textLinearLayout.setOnClickListener(new View.OnClickListener(){
  45. // public void onClick(final View view){
  46. // MediaPlayer mediaPlayer = MediaPlayer.create(getContext(), currentWord.getAudioResourceId());
  47. // Toast.makeText(view.getContext(), "Play", Toast.LENGTH_SHORT).show();
  48. // mediaPlayer.start();
  49. // mediaPlayer.setOnCompletionListener(new MediaPlayer.OnCompletionListener(){
  50. //
  51. // public void onCompletion(MediaPlayer mediaPlayer){
  52. // //Toast.makeText(view.getContext(),"I'm done!", Toast.LENGTH_SHORT).show();
  53. // mediaPlayer.release();
  54. // }
  55. // });
  56. // }
  57. // });
  58. return listItemView;
  59. }
  60. }

发表评论

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

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

相关阅读