我正在使用一个名为LikeButton的git repo,但我的按钮的状态在我的recyclerview中不停跳跃?

时间:2016-05-04 18:55:42

标签: java android button android-recyclerview

我正在使用名为LikeButton的git repo,但我的按钮状态在我的recyclerview中不断跳跃?这是回购https://github.com/jd-alexander/LikeButton。基本上当我点击recyclerview项目时,它会根据用户是否喜欢该帖子将textview设置为true或false,这样就可以了。然而,我的按钮的状态正在做一些奇怪的东西,它跳转... 这是我的适配器,它们有什么问题吗?

public class ViewpagerAdapter extends RecyclerView.Adapter<ViewpagerAdapter.ViewDashboard>{
    private LayoutInflater mLayoutInflater;

    private ArrayList<QuestionData> data = new ArrayList<>();

    public ViewpagerAdapter(Context context) {
        mLayoutInflater=LayoutInflater.from(context);
    }

    public void setBloglist(ArrayList<QuestionData> listBlogs) {
        this.data = listBlogs;
        notifyItemRangeChanged(0,listBlogs.size());
    }

    @Override
    public ViewDashboard onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = mLayoutInflater.inflate(R.layout.customizejson, parent, false);
        ViewDashboard viewholder = new ViewDashboard(view);
        return viewholder;
    }

    @Override
    public void onBindViewHolder(ViewDashboard holder, int position) {
        QuestionData questionHolder = data.get(position);
        holder.questionText.setText(questionHolder.getMtext());
        //This sets the text, to a true or a false String
        holder.mStudentVoted.setText(questionHolder.getVoters());
        holder.mLikeButton.setTag(holder);
    }

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

    class ViewDashboard extends RecyclerView.ViewHolder {
        private TextView questionText;
        private LikeButton mLikeButton;
        private TextView mStudentVoted;
        public ViewDashboard(View itemView) {
            super(itemView);
            questionText = (TextView)itemView.findViewById(R.id.questionText);
            mStudentVoted = (TextView)itemView.findViewById(R.id.studentVoted);

            mLikeButton = (LikeButton)itemView.findViewById(R.id.like_button_viewpager);

            mLikeButton.setOnLikeListener(new OnLikeListener() {
                @Override
                public void liked(LikeButton likeButton) {
                    Voting voting = new Voting(getAdapterPosition(),ViewpagerAdapter.this, questionId);
                    voting.onUpVote();
                }

                @Override
                public void unLiked(LikeButton likeButton) {
                    Voting voting = new Voting(getAdapterPosition(),ViewpagerAdapter.this, questionId);
                    voting.onDownVote();
                }
            });
        }
    }
}

投票类

public class Voting {

private int adapterPosition;
private RecyclerView.Adapter adapter;
private String stringId;
private TextView studentVoted;
//TODO Trim Constructor
public Voting(int adapterPosition,final RecyclerView.Adapter adapter, TextView questionId, TextView studentVoted) {
    stringId = questionId.getText().toString();
    this.adapter = adapter;
    this.studentVoted=studentVoted;
}

public void onUpVote() {

    final RequestQueue mRequestQueue = VolleySingleton.getInstance().getRequestQueue();

    StringRequest postVoteUp = new StringRequest(Request.Method.PUT, PUT_VOTE_UP, new Response.Listener<String>() {
        @Override
        public void onResponse(String response) {
            System.out.println("Succesful Upvote The Students Value is " + studentVoted);

        }

    }, new Response.ErrorListener() {
        @Override
        public void onErrorResponse(VolleyError error) {
            System.out.println("failed Upvote");


        }
    });
    mRequestQueue.add(postVoteUp);
}
public void onDownVote() {
    final RequestQueue mrequestQueue = VolleySingleton.getInstance().getRequestQueue();
    //TODO Delete Token(inserted for student 3 for testing purposes)
    StringRequest postVoteDown = new StringRequest(Request.Method.PUT, PUT_VOTE_DOWN, new Response.Listener<String>() {
        @Override
        public void onResponse(String response) {
            //TODO OnResponse, must setLiked(False)
            //Succesful downVote The Students Value is true
            //studentVoted.setText("false");

            System.out.println("Succesful downVote The Students Value is "+studentVoted);
        }

    }, new Response.ErrorListener() {
        @Override
        public void onErrorResponse(VolleyError error) {

            System.out.println("failed downVote");
        }
    });
    mrequestQueue.add(postVoteDown);
}
public void realTimeUpVoting(TextView textView){
    String voteString= textView.getText().toString();
    int voteNumber=Integer.parseInt(voteString)+1;
    textView.setText("" + voteNumber);
}
public void realTimeDownVoting(TextView textView){
    String voteString= textView.getText().toString();
    int voteNumber=Integer.parseInt(voteString)-1;
    textView.setText("" + voteNumber);
}
}

Json请求和解析方法

public void JsonRequestMethod() {
    mVolleySingleton = VolleySingleton.getInstance();
    mRequestQueue = mVolleySingleton.getRequestQueue();
    JsonArrayRequest request = new JsonArrayRequest(Request.Method.GET, URL_HOME, (String) null, new Response.Listener<JSONArray>() {
        @Override
        public void onResponse(JSONArray response) {
            try {
                mListblogs.clear();
                mListblogs = new YourTask().execute(response).get();

            } catch (InterruptedException | ExecutionException e) {
                e.printStackTrace();
            }
        }
    }, new Response.ErrorListener() {
        @Override
        public void onErrorResponse(VolleyError error) {
        }
    });
    mRequestQueue.add(request);
}

private ArrayList<QuestionData> parseJSONResponse(JSONArray response) {
    if (!response.equals("")) {
        try {
            StringBuilder data = new StringBuilder();
            for (int x = 0; x < response.length(); x++) {
                JSONObject currentQuestions = response.getJSONObject(x);
                JSONArray arrSubcategory = currentQuestions.optJSONArray("questions");
                for (int y = 0; y < arrSubcategory.length(); y++) {
                    JSONObject objectSubcategory = arrSubcategory.getJSONObject(y);
                    String text = objectSubcategory.optString("text");
                    String studentId = objectSubcategory.optString("studentId");
                    String votes=objectSubcategory.optString("votes");
                    /*JSONArray cycles through the array of voters, when a user votes
                    their ID is added to the array.When they downvote, it is removed
                     */
                    JSONArray voters= objectSubcategory.optJSONArray("voters");

                    QuestionData questionData = new QuestionData();
                    questionData.setMstudentId(studentId);
                    questionData.setMtext(text);
                    questionData.setVotes(votes);
                    questionData.setVoters(checkIfVoted(voters));
                    mQuestionDataArrayList.add(questionData);
                }
            }
        } catch (JSONException e) {
            e.printStackTrace();
        }
    }
    return mQuestionDataArrayList;
}
private static String checkIfVoted(JSONArray jsonArray ) {
    /*pass in a json Array, copy the array into ints, and if
      the students Id is contained in the array return the string true
     */
    int[] voteIds = new int[jsonArray.length()];
    for(int i=0;i<voteIds.length;i++){
        voteIds[i] = jsonArray.optInt(i);
    }
    for(int i=0;i<voteIds.length;i++){
        if(voteIds[i]== Integer.parseInt(Login.getUserId())){
            //TODO String was only used for Testing purposes, Convert to Boolean later
            return "true";
        }
    }
    return "false";
}

1 个答案:

答案 0 :(得分:0)

  

您目前只更新textview,这就是您的recycleview在滚动时更改状态的原因。

应该更改您的投票类并传递问题数据而不是textview

    public Voting(int adapterPosition,final RecyclerView.Adapter adapter, TextView questionId, TextView studentVoted) {

更改为

    public Voting(int adapterPosition,final RecyclerView.Adapter adapter, QuestionData questionData, TextView studentVoted) {
   // make other changes for the data

然后在

public void realTimeUpVoting(QuestionData questionData){
data.votes++ //something like that. idont know your model
// now call back using interface the recyleview data changed method so it updates the count in recycleview automatically.

修改

在点击按钮中传递问题数据

 class ViewDashboard extends RecyclerView.ViewHolder {
    public int position


 public void onBindViewHolder(ViewDashboard holder, int position) {
     holder.position = position
 }

 public void liked(LikeButton likeButton) {
     QuestionData questionHolder = data.get(position);