Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/375.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java Android RecyclerView适配器ImageButton问题_Java_Android_Android Recyclerview_Onclicklistener_Android Imagebutton - Fatal编程技术网

Java Android RecyclerView适配器ImageButton问题

Java Android RecyclerView适配器ImageButton问题,java,android,android-recyclerview,onclicklistener,android-imagebutton,Java,Android,Android Recyclerview,Onclicklistener,Android Imagebutton,我有我的适配器,等等。当按下第六个按钮时,它会按照预期的方式进行操作。这是最后一次正确更新 但它也在做同样的事情。现在这个 只调用一次。这是正确的。但这是从UI更新调用每秒调用一次的 private void sendPlayTimeButtons() { cardButtons.get(TimeCardButtonId.PLAY_BUTTON.getId()).setVisibility(View.INVISIBLE); cardButtons.get(TimeCardBu

我有我的适配器,等等。当按下第六个按钮时,它会按照预期的方式进行操作。这是最后一次正确更新

但它也在做同样的事情。现在这个

只调用一次。这是正确的。但这是从UI更新调用每秒调用一次的

private void sendPlayTimeButtons() {
    cardButtons.get(TimeCardButtonId.PLAY_BUTTON.getId()).setVisibility(View.INVISIBLE);
    cardButtons.get(TimeCardButtonId.EDIT_BUTTON.getId()).setVisibility(View.INVISIBLE);
    cardButtons.get(TimeCardButtonId.PAUSE_BUTTON.getId()).setVisibility(View.VISIBLE);
    cardButtons.get(TimeCardButtonId.STOP_BUTTON.getId()).setVisibility(View.VISIBLE);
   // logger.debug("Sending Play Buttons");
}
以下是适配器上my
BindViewHolder
的代码

@Override
public void onBindViewHolder(MyViewHolder holder, int position) {
    //TODO: add everything back
    holder.playButton.setOnClickListener(new TimeCardButton(context, holder.getAdapterPosition(), holder.buttons).checkStatus());
    holder.editButton.setOnClickListener(new TimeCardButton(context, holder.getAdapterPosition(), holder.buttons).checkStatus());
    holder.pauseButton.setOnClickListener(new TimeCardButton(context, holder.getAdapterPosition(), holder.buttons).checkStatus());
    holder.stopButton.setOnClickListener(new TimeCardButton(context, holder.getAdapterPosition(), holder.buttons).checkStatus());
}
最后一个也是最后一个是适配器内my
ViewHolder
的代码

public class MyViewHolder extends RecyclerView.ViewHolder {

    TextView timerTitle;
    public TextView timeRemaining;

    ImageButton playButton;
    ImageButton editButton;
    ImageButton pauseButton;
    ImageButton stopButton;

    LinkedList<ImageButton> buttons = new LinkedList<>();

    public MyViewHolder(final View view) {
        super(view);

        timerTitle = view.findViewById(R.id.titleCardName);
        timeRemaining = view.findViewById(R.id.timeLeftTextCard);

        playButton = view.findViewById(R.id.playButton);
        editButton = view.findViewById(R.id.editButton);
        pauseButton = view.findViewById(R.id.pauseButton);
        stopButton = view.findViewById(R.id.stopButton);

        buttons.add(playButton);
        buttons.add(editButton);
        buttons.add(pauseButton);
        buttons.add(stopButton);
    }
}
通过我的任务系统一次完成一个任务。我的任务系统仅从处理UI调用的活动向回收者发送更新


同样,在这些图像上,按钮会无缘无故地每秒向上移动一次列表。我试着用标签替换按钮ID。但是仍然失败。

您需要在适配器中有一个单独的数组,其中包含播放/暂停/停止列表的曲目。让我们考虑以下问题:
0 -> Stopped
1 -> Playing
2 -> Paused
// This initializes the array with the number of elements of your list
// and all initialized by 0 to indicate primarily all tracks were not playing. 
int[] trackPlayer = new int[cards.size]; 
现在,在适配器中设置一个数组,如下所示

0 -> Stopped
1 -> Playing
2 -> Paused
// This initializes the array with the number of elements of your list
// and all initialized by 0 to indicate primarily all tracks were not playing. 
int[] trackPlayer = new int[cards.size]; 
现在,当您单击按钮执行某些操作时,还需要使用该操作更新数组

@Override
public void onClick(View view) {
    TimeCard card = MyAdapter.cards.get(position);
    switch (view.getId()) {
        case R.id.playButton:
            startTimer(card);
            trackPlayer[position] = 1;  // Playing
            break;
        case R.id.editButton:
            Intent intent = new Intent(context, TimeCardAdd.class);
            intent.putExtra("cardPosition", position);
            context.startActivity(intent);
            //TODO: Finish the editing so we can modify the timer card
            Toast.makeText(context, "Edit button has been pressed.", Toast.LENGTH_SHORT).show();
            break;

        case R.id.stopButton:
            stopTimer(card);
            trackPlayer[position] = 0;  // Stopped
            break;

        case R.id.pauseButton:
            pauseTimer(card);
            trackPlayer[position] = 2;  // Paused
            break;
    }
}
private void startTimer(TimeCard card) {
    new Logger(TimeCardButton.class).debug("Play button was pressed");
    if (!card.isTimeStarted()) {
        card.setTimeStarted(true);
        notifyDataSetChanged(); // Call notifyDataSetChanged instead here.
    } else if(card.isTimerPaused() && card.isTimeStarted()) {
        TimerTask.notifyUpdate();
        card.setTimerPaused(false);
        notifyDataSetChanged(); // Call notifyDataSetChanged instead here.
    }
}
现在,在您的
onBindViewHolder
中,您需要根据
trackPlayer
值更改按钮的可见性

@Override
public void onBindViewHolder(MyViewHolder holder, int position) {
    holder.playButton.setOnClickListener(new TimeCardButton(context, holder.getAdapterPosition(), holder.buttons).checkStatus());
    holder.editButton.setOnClickListener(new TimeCardButton(context, holder.getAdapterPosition(), holder.buttons).checkStatus());
    holder.pauseButton.setOnClickListener(new TimeCardButton(context, holder.getAdapterPosition(), holder.buttons).checkStatus());
    holder.stopButton.setOnClickListener(new TimeCardButton(context, holder.getAdapterPosition(), holder.buttons).checkStatus());

    // Set the buttons visibility changes here. 
    if(playTrack[position] == 1) { 
        // Item in this position is being played
        playButton.setVisibility(View.INVISIBLE);
        editButton.setVisibility(View.INVISIBLE);
        pauseButton.setVisibility(View.VISIBLE);
        stopButton.setVisibility(View.VISIBLE);

    } else if(playTrack[position] == 0) { 
        // Item in this position is not being played/stopped
        playButton.setVisibility(View.VISIBLE);
        editButton.setVisibility(View.VISIBLE);
        pauseButton.setVisibility(View.INVISIBLE);
        stopButton.setVisibility(View.INVISIBLE);
    } else if(playTrack[position] == 2) { 
        // Item in this position is paused
        playButton.setVisibility(View.VISIBLE);
        editButton.setVisibility(View.INVISIBLE);
        pauseButton.setVisibility(View.INVISIBLE);
        stopButton.setVisibility(View.VISIBLE);
    }
}
并从
startTimer
函数中删除
sendPlayTimeButtons
函数调用。我想你也可以考虑删除<代码> sEngPyTimeButtudis函数。
@Override
public void onClick(View view) {
    TimeCard card = MyAdapter.cards.get(position);
    switch (view.getId()) {
        case R.id.playButton:
            startTimer(card);
            trackPlayer[position] = 1;  // Playing
            break;
        case R.id.editButton:
            Intent intent = new Intent(context, TimeCardAdd.class);
            intent.putExtra("cardPosition", position);
            context.startActivity(intent);
            //TODO: Finish the editing so we can modify the timer card
            Toast.makeText(context, "Edit button has been pressed.", Toast.LENGTH_SHORT).show();
            break;

        case R.id.stopButton:
            stopTimer(card);
            trackPlayer[position] = 0;  // Stopped
            break;

        case R.id.pauseButton:
            pauseTimer(card);
            trackPlayer[position] = 2;  // Paused
            break;
    }
}
private void startTimer(TimeCard card) {
    new Logger(TimeCardButton.class).debug("Play button was pressed");
    if (!card.isTimeStarted()) {
        card.setTimeStarted(true);
        notifyDataSetChanged(); // Call notifyDataSetChanged instead here.
    } else if(card.isTimerPaused() && card.isTimeStarted()) {
        TimerTask.notifyUpdate();
        card.setTimerPaused(false);
        notifyDataSetChanged(); // Call notifyDataSetChanged instead here.
    }
}

希望你能明白

您的适配器中需要有一个单独的数组,其中包含播放/暂停/停止列表的曲目。让我们考虑以下问题:
0 -> Stopped
1 -> Playing
2 -> Paused
// This initializes the array with the number of elements of your list
// and all initialized by 0 to indicate primarily all tracks were not playing. 
int[] trackPlayer = new int[cards.size]; 
现在,在适配器中设置一个数组,如下所示

0 -> Stopped
1 -> Playing
2 -> Paused
// This initializes the array with the number of elements of your list
// and all initialized by 0 to indicate primarily all tracks were not playing. 
int[] trackPlayer = new int[cards.size]; 
现在,当您单击按钮执行某些操作时,还需要使用该操作更新数组

@Override
public void onClick(View view) {
    TimeCard card = MyAdapter.cards.get(position);
    switch (view.getId()) {
        case R.id.playButton:
            startTimer(card);
            trackPlayer[position] = 1;  // Playing
            break;
        case R.id.editButton:
            Intent intent = new Intent(context, TimeCardAdd.class);
            intent.putExtra("cardPosition", position);
            context.startActivity(intent);
            //TODO: Finish the editing so we can modify the timer card
            Toast.makeText(context, "Edit button has been pressed.", Toast.LENGTH_SHORT).show();
            break;

        case R.id.stopButton:
            stopTimer(card);
            trackPlayer[position] = 0;  // Stopped
            break;

        case R.id.pauseButton:
            pauseTimer(card);
            trackPlayer[position] = 2;  // Paused
            break;
    }
}
private void startTimer(TimeCard card) {
    new Logger(TimeCardButton.class).debug("Play button was pressed");
    if (!card.isTimeStarted()) {
        card.setTimeStarted(true);
        notifyDataSetChanged(); // Call notifyDataSetChanged instead here.
    } else if(card.isTimerPaused() && card.isTimeStarted()) {
        TimerTask.notifyUpdate();
        card.setTimerPaused(false);
        notifyDataSetChanged(); // Call notifyDataSetChanged instead here.
    }
}
现在,在您的
onBindViewHolder
中,您需要根据
trackPlayer
值更改按钮的可见性

@Override
public void onBindViewHolder(MyViewHolder holder, int position) {
    holder.playButton.setOnClickListener(new TimeCardButton(context, holder.getAdapterPosition(), holder.buttons).checkStatus());
    holder.editButton.setOnClickListener(new TimeCardButton(context, holder.getAdapterPosition(), holder.buttons).checkStatus());
    holder.pauseButton.setOnClickListener(new TimeCardButton(context, holder.getAdapterPosition(), holder.buttons).checkStatus());
    holder.stopButton.setOnClickListener(new TimeCardButton(context, holder.getAdapterPosition(), holder.buttons).checkStatus());

    // Set the buttons visibility changes here. 
    if(playTrack[position] == 1) { 
        // Item in this position is being played
        playButton.setVisibility(View.INVISIBLE);
        editButton.setVisibility(View.INVISIBLE);
        pauseButton.setVisibility(View.VISIBLE);
        stopButton.setVisibility(View.VISIBLE);

    } else if(playTrack[position] == 0) { 
        // Item in this position is not being played/stopped
        playButton.setVisibility(View.VISIBLE);
        editButton.setVisibility(View.VISIBLE);
        pauseButton.setVisibility(View.INVISIBLE);
        stopButton.setVisibility(View.INVISIBLE);
    } else if(playTrack[position] == 2) { 
        // Item in this position is paused
        playButton.setVisibility(View.VISIBLE);
        editButton.setVisibility(View.INVISIBLE);
        pauseButton.setVisibility(View.INVISIBLE);
        stopButton.setVisibility(View.VISIBLE);
    }
}
并从
startTimer
函数中删除
sendPlayTimeButtons
函数调用。我想你也可以考虑删除<代码> sEngPyTimeButtudis函数。
@Override
public void onClick(View view) {
    TimeCard card = MyAdapter.cards.get(position);
    switch (view.getId()) {
        case R.id.playButton:
            startTimer(card);
            trackPlayer[position] = 1;  // Playing
            break;
        case R.id.editButton:
            Intent intent = new Intent(context, TimeCardAdd.class);
            intent.putExtra("cardPosition", position);
            context.startActivity(intent);
            //TODO: Finish the editing so we can modify the timer card
            Toast.makeText(context, "Edit button has been pressed.", Toast.LENGTH_SHORT).show();
            break;

        case R.id.stopButton:
            stopTimer(card);
            trackPlayer[position] = 0;  // Stopped
            break;

        case R.id.pauseButton:
            pauseTimer(card);
            trackPlayer[position] = 2;  // Paused
            break;
    }
}
private void startTimer(TimeCard card) {
    new Logger(TimeCardButton.class).debug("Play button was pressed");
    if (!card.isTimeStarted()) {
        card.setTimeStarted(true);
        notifyDataSetChanged(); // Call notifyDataSetChanged instead here.
    } else if(card.isTimerPaused() && card.isTimeStarted()) {
        TimerTask.notifyUpdate();
        card.setTimerPaused(false);
        notifyDataSetChanged(); // Call notifyDataSetChanged instead here.
    }
}

希望你能明白

嗨,特里斯坦。这不是你应该问问题的方式。提供示例代码,然后描述您的问题,以便有人可以帮助您。没有人能读懂另一边的思想,如果你把问题抽象成现在的样子,你会得到负分。你是对的,我没有意识到现在就更新它。如果能看到
startTimer
函数的内容,那就太好了。@KrystianG我在Tristan中添加了这个方法。这不是你应该问问题的方式。提供示例代码,然后描述您的问题,以便有人可以帮助您。没有人能读懂另一边的思想,如果你把问题抽象成现在的样子,你会得到负分。你是对的,我没有意识到现在就更新它。看到
startTimer
函数的内容会很好。@KrystianG我添加了这个方法现在我觉得自己很笨,我是这么简单的解决方案忽略了感谢现在我觉得自己很傻这么简单的解决方案我忽略了感谢