简体   繁体   English

如何使用customadapter触摸/单击arraylist中位置的行并执行操作

[英]How to touch/click row at position in arraylist with customadapter and perform action

I have ListView with a custom Adapter which includes 2 texts and one button.我有一个带有自定义Adapter ListView ,其中包括 2 个文本和一个按钮。 When I touch any row, it plays an mp3 sound by using convertView.setOnClickListener and highlights the row using the following.当我触摸任何一行时,它会使用convertView.setOnClickListener播放 mp3 声音并使用以下内容突出显示该行。

convertView.setBackgroundResource(android.R.color.darker_gray)

But what I exactly need it, when the sound finishes, It should automatically select and highlight the next row and play another sound using mediaPlayer.setOnCompletionListener .但是我真正需要的是,当声音结束时,它应该自动选择并突出显示下一行并使用mediaPlayer.setOnCompletionListener播放另一个声音。

I have already tried with the following.我已经尝试过以下方法。

- listView.setSelection(position+1);
- listView.performItemClick(listView.getAdapter().getView(position+1, null, null),position+1,listView.getAdapter().getItemId(position+1));
- listView.setItemChecked(position+1,true);

By using this, I expect it should play programmatically clicking the next row, highlight it and play the sound.通过使用它,我希望它应该以编程方式播放,单击下一行,突出显示它并播放声音。 But nothing happens with all these codes except, it scrolls to that particular row.但是所有这些代码都没有发生任何变化,除了它滚动到该特定行。

Here is my code from CustomAdapter Class:这是我来自 CustomAdapter 类的代码:

class CustomAdapter extends BaseAdapter{

    @Override
    public int getCount() {
        return 100;
    }

    @Override
    public Object getItem(int position) {
        return null;
    }

    @Override
    public long getItemId(int position) {
        return 0;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        convertView = getLayoutInflater().inflate(R.layout.custom_cell,null);
        TextView aText = (TextView) convertView.findViewById(R.id.aText);
        TextView bText = (TextView) convertView.findViewById(R.id.bText);
        cButton = (Button) convertView.findViewById(R.id.cButton);



        aText.setText(aTextArray.get(position));
        bText.setText(bTextArray.get(position));

    convertView.setFocusable(false);
        final View finalConvertView1 = convertView;

    convertView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
     readingList.setSelection(position);

                finalConvertView1.setBackgroundResource(android.R.color.darker_gray);
    try {
    mediaPlayer.reset();
                    mediaPlayer = new MediaPlayer();
                    mediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
                    mediaPlayer.setDataSource("mp3link"));
                    mediaPlayer.setOnPreparedListener(ReadingActivity.this);
                    mediaPlayer.prepareAsync();
                    mediaPlayer.setOnCompletionListener(new OnCompletionListener() {
                        @Override
                        public void onCompletion(MediaPlayer mp) {
                            listView.setSelection(position+1);
                            listView.performItemClick(
                                    readingList.getAdapter().getView(position+1, null, null),
                                    position+1,
                                    readingList.getAdapter().getItemId(position+1));
    //listView.setItemChecked(position+1,true);
    //listView.setSelection(position+1);

                        }
                    });

                } catch (Exception e) {
                    e.printStackTrace();
                }

            }
        });

        return convertView;
    }
}
      @Override
public void onPrepared(MediaPlayer mp) {

    mediaPlayer.start();
}

I have a solution: you create a falg isHighlight in arrayListData, using highlishts the row.我有一个解决方案:您在arrayListData 中创建一个标记isHighlight,使用highlishts 行。 When the sound finishs you change arrayListData.get(position+1).isHighlisht = true then you call adapter.notifyDataSetChanged()当声音结束时你改变 arrayListData.get(position+1).isHighlisht = true 然后你调用 adapter.notifyDataSetChanged()

Instead of mocking the click action to the item in your ListView , you might consider keeping a variable to indicate which item is playing now.您可以考虑保留一个变量来指示当前正在播放哪个项目,而不是模拟对ListView项目的点击操作。 When an item is finished playing, set the next element of the array as playing by incrementing the variable and call notifyDataSetChanged on your adapter.当一个项目完成播放时,通过增加变量并在您的适配器上调用notifyDataSetChanged将数组的下一个元素设置为播放。 Based on the value from that variable, change your UI elements accordingly.根据该变量的值,相应地更改您的 UI 元素。 Here is a pseudo-code on how you should proceed.这是关于您应该如何进行的伪代码。

Take an array like the following.取一个如下所示的数组。

private int playingNow = 0; // Initialize this with zero. At first none is being played

Then on clicking an item, set the corresponding array element to 1 which indicates the item is playing now.然后单击一个项目,将相应的数组元素设置为1 ,表示该项目正在播放。 Here's a sample function for your adapter.这是您的适配器的示例函数。

private void playSound(int position) {
    // Start playing the sound here
    // And modify the variable
    playingNow = position;

    notifyDataSetChanged();
}

Now in your getView , modify the views based on the indicator that you have.现在在您的getView ,根据您拥有的指标修改视图。

if(position == playingNow) setBackgroundColorToSelected();
else setBackgroundColorToNormal();

And once you are finished with playing one item, adjust the value of the variable accordingly.一旦你完成了一个项目,相应地调整变量的值。

private void playNext() {
    if(playingNow + 1 == yourItems.size()) playingNow = 0; // I guess you might want to play the first song when it reaches the end of the list
    else playingNow++;

    notifyDataSetChanged();
}

Hope that helps!希望有帮助!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM