Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
109 views
in Technique[技术] by (71.8m points)

java - Recycler ItemClick doesn't work with Countdown timer

I'm trying to develop Letters and Numbers(Countdown) like game. In this concept, user should able to use the arithmetic operations results of given numbers once for the reach goal number in a given time.

I'm using the Recyclerview to show and list the steps of calculation but while Countdown Timer ticking, I can't click the items of recyclerview. It only works when the Countdown Timer finished up. Please help me to find out the problem.

Here is what I mean as a image: https://hizliresim.com/zWThaa

Here is my adapter

    public interface OnItemListener{
        void onItemClick(int position);
    }


    private OnItemListener mOnItemListener;    
    private ArrayList<String> n1;
    private ArrayList<String> n2;
    private ArrayList<String> res;
    private ArrayList<String> op;

    public NotesRecyclerAdapter(ArrayList<String> n1, ArrayList<String> n2, ArrayList<String> res, ArrayList<String> op, OnItemListener onItemListener) {
        this.n1 = n1;
        this.n2 = n2;
        this.res = res;
        this.op = op;
        this.mOnItemListener = onItemListener;
    }

    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.listview_item, parent, false);
        return new ViewHolder(view, mOnItemListener);
    }

    @Override
    public void onBindViewHolder(@NonNull ViewHolder holder, int position) {

        if(n2.size() <= position ){
            holder.second.setText(" ");
        }
        else{
            holder.second.setText(n2.get(position));

        }
        if(op.size() <= position){
            holder.operation.setText(" ");
        }
        else{
            holder.operation.setText(op.get(position));
        }
        if (res.size() <= position){
            holder.result.setText(" ");
        }
        else {
            holder.result.setText(res.get(position));
        }


        holder.first.setText(n1.get(position));

    }

    @Override
    public int getItemCount() {
        return n1.size();
    }

    public class ViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {

        TextView first;
        TextView operation;
        TextView second;
        TextView result;
        OnItemListener mOnItemListener;

        public ViewHolder(View itemView, OnItemListener onItemListener) {
            super(itemView);

            first = itemView.findViewById(R.id.first);
            operation = itemView.findViewById(R.id.op);
            second = itemView.findViewById(R.id.secondNum);
            result = itemView.findViewById(R.id.res);
            this.mOnItemListener = onItemListener;

            itemView.setOnClickListener(this);

    }

        @Override
        public void onClick(View v) {
            mOnItemListener.onItemClick(getAdapterPosition());
        }
    }
}

Here is Activity,

public class CalculateActivity extends AppCompatActivity implements Adapter.OnItemListener{
RecyclerView rView;
Adapter adapter;
.//Other definitions etc.
.
protected void onCreate(Bundle savedInstanceState) {
. . . // Other 
adapter = new Adapter(firstNumbers,secondNumbers,operationResults,operators,this);
        rView.setLayoutManager(new LinearLayoutManager(getApplicationContext()));
        rView.setAdapter(adapter);

 new CountDownTimer(45000, 1) {

            @Override
            public void onTick(long millisUntilFinished) {
 
            // Calculation step
           }
                
        
            @Override
            public void onFinish() {

            }
        }.start();

...//Button Listeneres etc...
} // end of onCreate
    
@Override
public void onItemClick(int position) {
     System.out.println(position + " Clicked!");
     View view = rView.getChildAt(position);
     TextView res = view.findViewById(R.id.res);
     moveCount++;
     if(moveCount % 2 == 0){
        firstNumbers.add((String)res.getText()); //res is textview of the operation results.
     }
     else{
        secondNumbers.add((String)res.getText());
     }
     view.setClickable(false)
     dataChange(); //just notifyDataSetChange
            
}

PS: I also used this method. I can able to click with this method but it returns wrong value of item's position while Countdown Timer ticking. Just like I explained above, it works perfectly when the timer runs out.

question from:https://stackoverflow.com/questions/65871567/recycler-itemclick-doesnt-work-with-countdown-timer

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Probably you have a problem with adapter...

I don't know what you do in the onTick method, whether you block something or...

I'm not sure what do you want but consider whether it is possible to do so

You can create Step class like

public class Step {

    private String first;
    private String operation;
    private String second;
    private String result;
    private boolean disabled;  //instead view clickable
    
    //TODO generate getters and setters

}

Try this adapter

public class NotesRecyclerAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
    
        private final OnItemListener mOnItemListener;
        private final List<Step> items;
    
        public NotesRecyclerAdapter(List<Step> items, OnItemListener onItemListener) {
            this.items = items;
            this.mOnItemListener = onItemListener;
        }
    
        @NonNull
        @Override
        public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
            View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.listview_item, parent, false);
            return new NotesViewHolder(view);
        }
    
        @Override
        public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, int position) {
            NotesViewHolder mHolder = (NotesViewHolder) holder;
            Step item = items.get(position);
    
            mHolder.first.setText(item.getFirst());
    
            if(items.size() <= position){ // Im not sure why this
                mHolder.second.setText(" ");
                mHolder.operation.setText(" ");
                mHolder.result.setText(" ");
            }else{
                mHolder.second.setText(item.getSecond());
                mHolder.operation.setText(item.getOperation());
                mHolder.result.setText(item.getResult());
            }
    
            if(mOnItemListener != null){
                mHolder.itemView.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        mOnItemListener.onItemClick(mHolder.getAdapterPosition());
                    }
                });
            }
        }
    
        @Override
        public int getItemCount() {
            return items.size();
        }
    
        public Step getItem(int position) {
            if(this.items == null || position < 0 || position >= this.items.size()) return null;
            return this.items.get(position);
        }
    
    
        public interface OnItemListener{
            void onItemClick(int position);
        }
    
        public static class NotesViewHolder extends RecyclerView.ViewHolder {
    
            public TextView first;
            public TextView operation;
            public TextView second;
            public TextView result;
    
            public NotesViewHolder(View itemView) {
                super(itemView);
    
                first = itemView.findViewById(R.id.first);
                operation = itemView.findViewById(R.id.op);
                second = itemView.findViewById(R.id.secondNum);
                result = itemView.findViewById(R.id.res);
    
            }
        }
    }

and onClick method

@Override
    public void onItemClick(int position) {
        Toast.makeText(this, "Clicked: "+ position, Toast.LENGTH_SHORT).show();
        Step step = adapter.getItem(position);

        if(step != null){
            if(step.isDisabled()){
                Toast.makeText(this, "Disabled"+ position, Toast.LENGTH_SHORT).show();
                return;
            }
            
            moveCount++;
            if(moveCount % 2 == 0){
                step.setFirst(random number or what); //how do you get number, randomly or... 
            } else{
                step.setSecond(random number or what);
            }
            step.setDisabled(false); //instead view.setClickable(false)
            dataChange(); //just notifyDataSetChange
        }
 

    }

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...