RecyclerView行中的两个按钮与

时间:2017-07-22 02:06:46

标签: android android-recyclerview onclicklistener recycler-adapter android-viewholder

我有一个Recycler View,它使用TextView和两个按钮来构建行。按钮用作向上/向下投票,一次只能点击一次。如果单击其中一个按钮,则如果之前未单击“未单击”,则无法单击另一个按钮。 TextView代表得分。

我遇到的问题是TextView中的文本正在被正确处理,只有在可能的情况下才会增加,但是,行的背景颜色总是错误的并且不遵循规则。 我使用SQLite作为表来检查帖子是否上/下投票,并在没有单击任何按钮时设置为“null”。

以下代码编写在MainActivity.java中。

   scoreAdapter.setOnButtonDownClickListener(new ScoreAdapter.onButtonClickListener() {
        @Override
        public void onButtonClick(View parent, Button btn, int position) {
            int postId = (int) parent.getTag();
            String vote = databaseHandler.getVote(postId);
            if(vote.equals("down")){
                scoreAdapter.setScore(position,Integer.parseInt(scoreAdapter.getScore(position)) + 1);
                databaseHandler.writeVote((int) parent.getTag(),"null");
                parent.setBackgroundColor(getResources().getColor(R.color.notVoted,getApplication().getTheme()));
                scoreAdapter.notifyItemChanged(position);
            }else if(! vote.equals("up")){
                scoreAdapter.setScore(position,Integer.parseInt(scoreAdapter.getScore(position)) - 1);
                databaseHandler.writeVote((int) parent.getTag(),"down");
                parent.setBackgroundColor(getResources().getColor(R.color.downVoted,getApplication().getTheme()));
                scoreAdapter.notifyItemChanged(position);
            }
            Log.i(parent.getTag().toString(),databaseHandler.getVote((int) parent.getTag()));
        }
    });

    scoreAdapter.setOnButtonUpClickListener(new ScoreAdapter.onButtonClickListener() {
        @Override
        public void onButtonClick(View parent, Button btn, int position) {
            int postId = (int) parent.getTag();
            String vote = databaseHandler.getVote(postId);
            if(vote.equals("up")){
                scoreAdapter.setScore(position,Integer.parseInt(scoreAdapter.getScore(position)) - 1);
                databaseHandler.writeVote((int) parent.getTag(),"null");
                parent.setBackgroundColor(getResources().getColor(R.color.notVoted,getApplication().getTheme()));
                scoreAdapter.notifyItemChanged(position);
            }else if(! vote.equals("down")){
                scoreAdapter.setScore(position,Integer.parseInt(scoreAdapter.getScore(position)) + 1);
                databaseHandler.writeVote((int) parent.getTag(),"up");
                parent.setBackgroundColor(getResources().getColor(R.color.upVoted,getApplication().getTheme()));
                scoreAdapter.notifyItemChanged(position);
            }
            Log.i(parent.getTag().toString(),databaseHandler.getVote((int) parent.getTag()));
        }
    });

ScoreAdapter是RecyclerView适配器。我知道用于onClick事件的接口应该是正确的,因为TextView修改是正确的。以下代码是ViewHolder类中的代码。

public interface onButtonClickListener{
    void onButtonClick(View parent, Button btn, int position);
}

public class ViewHolder extends RecyclerView.ViewHolder{
    public TextView score;
    public Button buttonUp;
    public Button buttonDown;

    public ViewHolder(View v){
        super(v);
        score = (TextView) v.findViewById(R.id.textScore);
        buttonUp = (Button) v.findViewById(R.id.btnUp);
        buttonDown = (Button) v.findViewById(R.id.btnDown);

        buttonDown.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Log.i("Down","clicked");
                if(onButtonDownClickListener != null){
                    int position = getAdapterPosition();
                    if(position != RecyclerView.NO_POSITION){
                        onButtonDownClickListener.onButtonClick(itemView, buttonDown, position);
                    }
                }
            }
        });

        buttonUp.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Log.i("Up","clicked");
                if(onButtonUpClickListener != null){
                    int position = getAdapterPosition();
                    if(position != RecyclerView.NO_POSITION){
                        onButtonUpClickListener.onButtonClick(itemView, buttonUp, position);
                    }
                }
            }
        });

如何修复背景颜色变化?是否有可能通过单击其中一个按钮,另一个按钮也会收到点击?

编辑:

MainAcitivty.java

public class MainActivity extends AppCompatActivity {


@Override
protected void onCreate(Bundle savedInstanceState){
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    final RecyclerView recyclerView = (RecyclerView) findViewById(R.id.recyclerView);
    ArrayList<Score> scores = new ArrayList<>();
    scores.add(new Score(2,0));
    scores.add(new Score(0,1));
    scores.add(new Score(-1,2));
    scores.add(new Score(3,3));
    scores.add(new Score(10,4));
    scores.add(new Score(50,5));
    scores.add(new Score(62,6));
    scores.add(new Score(8,7));
    scores.add(new Score(-5,8));
    scores.add(new Score(12,10));
    scores.add(new Score(26,11));
    scores.add(new Score(32,12));
    scores.add(new Score(9,13));
    scores.add(new Score(95,14));
    final LocalDatabaseHandler databaseHandler = new LocalDatabaseHandler(this);

    final ScoreAdapter scoreAdapter = new ScoreAdapter(this,scores);
    recyclerView.setAdapter(scoreAdapter);
    recyclerView.setLayoutManager(new LinearLayoutManager(this));

    scoreAdapter.setOnButtonDownClickListener(new ScoreAdapter.onButtonClickListener() {
        @Override
        public void onButtonClick(View parent, Button btn, int position) {
            int postId = (int) parent.getTag();
            String vote = databaseHandler.getVote(postId);
            if(vote.equals("down")){

                scoreAdapter.setScore(position,Integer.parseInt(scoreAdapter.getScore(position)) + 1);
                databaseHandler.writeVote((int) parent.getTag(),"null");
                btn.setBackgroundColor(getResources().getColor(R.color.notVoted));

            }else if(! vote.equals("up")){

                scoreAdapter.setScore(position,Integer.parseInt(scoreAdapter.getScore(position)) - 1);
                databaseHandler.writeVote((int) parent.getTag(),"down");
                btn.setBackgroundColor(getResources().getColor(R.color.downVoted));

            }
        }
    });

    scoreAdapter.setOnButtonUpClickListener(new ScoreAdapter.onButtonClickListener() {
        @Override
        public void onButtonClick(View parent, Button btn, int position) {
            int postId = (int) parent.getTag();
            String vote = databaseHandler.getVote(postId);
            if(vote.equals("up")){

                scoreAdapter.setScore(position,Integer.parseInt(scoreAdapter.getScore(position)) - 1);
                databaseHandler.writeVote((int) parent.getTag(),"null");
                btn.setBackgroundColor(getResources().getColor(R.color.notVoted));

            }else if(! vote.equals("down")){

                scoreAdapter.setScore(position,Integer.parseInt(scoreAdapter.getScore(position)) + 1);
                databaseHandler.writeVote((int) parent.getTag(),"up");
                btn.setBackgroundColor(getResources().getColor(R.color.upVoted));

            }
        }
    });

    scoreAdapter.setOnItemClickListener(new ScoreAdapter.onItemClickListener() {
        @Override
        public void onItemClick(View view, int position) {
            Toast.makeText(getApplicationContext(),Integer.toString((int) view.getTag()),Toast.LENGTH_SHORT).show();
        }
    });

    recyclerView.addItemDecoration(new DividerItemDecoration(this,DividerItemDecoration.VERTICAL));
}

}

ScoreAdapter.java

public class ScoreAdapter extends RecyclerView.Adapter<ScoreAdapter.ViewHolder> {

public interface onItemClickListener{
    void onItemClick(View view, int position);
}

public interface onButtonClickListener{
    void onButtonClick(View parent, Button btn, int position);
}

public class ViewHolder extends RecyclerView.ViewHolder{
    TextView score;
    Button buttonUp;
    Button buttonDown;
    View parent;

    public ViewHolder(View v){
        super(v);
        score = (TextView) v.findViewById(R.id.textScore);
        buttonUp = (Button) v.findViewById(R.id.btnUp);
        buttonDown = (Button) v.findViewById(R.id.btnDown);
        parent = v;

        buttonDown.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Log.i("Down","clicked");
                if(onButtonDownClickListener != null){
                    int position = getAdapterPosition();
                    if(position != RecyclerView.NO_POSITION){
                        onButtonDownClickListener.onButtonClick(parent, buttonDown, position);
                    }
                }
            }
        });

        buttonUp.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Log.i("Up","clicked");
                if(onButtonUpClickListener != null){
                    int position = getAdapterPosition();
                    if(position != RecyclerView.NO_POSITION){
                        onButtonUpClickListener.onButtonClick(parent, buttonUp, position);
                    }
                }
            }
        });

        v.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Log.i("View","clicked");
                if(onItemClickListener != null){
                    int position = getAdapterPosition();
                    if(position != RecyclerView.NO_POSITION){
                        onItemClickListener.onItemClick(parent,position);
                    }
                }
            }
        });
    }

}

private List<Score> scoreList;
private Context context;
private onItemClickListener onItemClickListener;
public onButtonClickListener onButtonUpClickListener;
public onButtonClickListener onButtonDownClickListener;

public ScoreAdapter(Context context,List<Score> scores){
    this.scoreList = scores;
    this.context = context;
}

public void setOnItemClickListener(ScoreAdapter.onItemClickListener onItemClickListener) {
    this.onItemClickListener = onItemClickListener;
}

public void setOnButtonUpClickListener(onButtonClickListener onButtonUpClickListener) {
    this.onButtonUpClickListener = onButtonUpClickListener;
}

public void setOnButtonDownClickListener(onButtonClickListener onButtonDownClickListener) {
    this.onButtonDownClickListener = onButtonDownClickListener;
}

public List<Score> getScoreList() {
    return scoreList;
}

public Context getContext() {
    return context;
}

public ScoreAdapter.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType){
    Context context = parent.getContext();
    View scoreView = LayoutInflater.from(context).inflate(R.layout.data_layout,parent,false);
    return new ScoreAdapter.ViewHolder(scoreView);
}

public void onBindViewHolder(ScoreAdapter.ViewHolder viewHolder, int position){
    Score s = scoreList.get(position);
    viewHolder.parent.setTag(s.getId());
    viewHolder.score.setText(s.getScore());
}

public int getItemCount(){
    return scoreList.size();
}

public String getScore(int position){
    return getScoreList().get(position).getScore();
}

public void setScore(int position,int score){
    getScoreList().get(position).setScore(Integer.toString(score));
    notifyItemChanged(position);
}

}

2 个答案:

答案 0 :(得分:0)

你的病情有误。据我所知,downup

的反义词
// Your vote is down
    if(vote.equals("down")){
        scoreAdapter.setScore(position,Integer.parseInt(scoreAdapter.getScore(position)) + 1);
        databaseHandler.writeVote((int) parent.getTag(),"null");
        parent.setBackgroundColor(getResources().getColor(R.color.notVoted,getApplication().getTheme()));
        scoreAdapter.notifyItemChanged(position);
// Your vote is still down (Or some thing that just not equal to "up")
    }else if(! vote.equals("up")){
        scoreAdapter.setScore(position,Integer.parseInt(scoreAdapter.getScore(position)) - 1);
        databaseHandler.writeVote((int) parent.getTag(),"down");
        parent.setBackgroundColor(getResources().getColor(R.color.downVoted,getApplication().getTheme()));
        scoreAdapter.notifyItemChanged(position);
    }

只需使用简单的if/else条件

即可

答案 1 :(得分:0)

/* your first ClickListener for the Button */  
  scoreAdapter.setOnButtonDownClickListener(new ScoreAdapter.onButtonClickListener() {
            @Override
            public void onButtonClick(View parent, Button btn, int position) {
                int postId = (int) parent.getTag();
                String vote = databaseHandler.getVote(postId);
                if(vote.equals("down")){

                    scoreAdapter.setScore(position,Integer.parseInt(scoreAdapter.getScore(position)) + 1);
                    databaseHandler.writeVote((int) parent.getTag(),"null");
                    btn.setBackgroundColor(getResources().getColor(R.color.notVoted));

                }else if(! vote.equals("up")){

                    scoreAdapter.setScore(position,Integer.parseInt(scoreAdapter.getScore(position)) - 1);
                    databaseHandler.writeVote((int) parent.getTag(),"down");
                    btn.setBackgroundColor(getResources().getColor(R.color.downVoted));

                }
            }
        });

/ *再次为Button设置第二个ClickListener。* /

/ *这是您设置两个时间按钮单击侦听器的问题,因此您必须单击两次才能使其正常工作* /

你不需要setOnClickListener() - 下面的代码 - 里面的ScoreAdapter类

buttonDown.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Log.i("Down","clicked");
                if(onButtonDownClickListener != null){
                    int position = getAdapterPosition();
                    if(position != RecyclerView.NO_POSITION){
                        onButtonDownClickListener.onButtonClick(itemView, buttonDown, position);
                    }
                }
            }
        });