回收视图滚动导致值改变

Recycle-view scrolling cause value change

当我滚动视图时,回收视图数据丢失焦点,我的 API 和其他东西工作正常,当我单击我的适配器按钮时数据发生变化,但是当我滚动我的回收视图数据时,它丢失了我更改的数据和回到上一个。

我的代码:

Activity:

  public void fetchNotification() {
    String Notification = "https://www.videobeaker.com/secure/assests/video-beaker-api/get-data.php?getPodiums=1";
    /*startAnim();*/
    StringRequest stringRequest = new StringRequest(Request.Method.GET, Notification, new Response.Listener<String>() {
        @Override
        public void onResponse(String response) {
            /*stopAnim();*/
          //  Log.e("podiums data", response);
            try {
                JSONObject jsonObject = new JSONObject(response);

                JSONArray jsonArray = jsonObject.getJSONArray("Podiums_data");
                for (int i = 0; i < jsonArray.length(); i++) {
                    JSONObject jobject = jsonArray.getJSONObject(i);
                    ComonPodium data = new ComonPodium();
                    data.setPodiumname(jobject.getString("podium_name"));
                    data.setPodium_description(jobject.getString("podium_description"));
                    data.setFollowers_data(jobject.getString("followers_data"));
                    data.setPodiumId(jobject.getString("podium_user_id"));
                   // data.setPodiumId(jobject.getString("podium_id"));
                    data.setPodium_cover_image(AppConstants.BASEIMG + jobject.getString("podium_cover_image"));
                    data.setPodium_image(AppConstants.BASEIMG + jobject.getString("podium_image"));
                    comonPodiums.add(data);
                }
                podiumAd.notifyDataSetChanged();
            } catch (JSONException e) {
                e.printStackTrace();
                showToast("No Record Found");
                //Toast.makeText(context,"No Record Found",Toast.LENGTH_SHORT).show();
            }

        }

    }, new Response.ErrorListener() {
        @Override
        public void onErrorResponse(VolleyError error) {
            // Toast.makeText(getContext(), "Network Error,Please wait", Toast.LENGTH_SHORT).show();

        }
    });

    RequestQueue requestQueue = Volley.newRequestQueue(context);
    requestQueue.add(stringRequest);

}

型号Class:

public class ComonPodium {

public String getPodiumname() {
    return podiumname;
}

public void setPodiumname(String podiumname) {
    this.podiumname = podiumname;
}

public String getPodium_description() {
    return podium_description;
}

public void setPodium_description(String podium_description) {
    this.podium_description = podium_description;
}

public String getPodium_image() {
    return podium_image;
}

public void setPodium_image(String podium_image) {
    this.podium_image = podium_image;
}

public String getPodium_cover_image() {
    return podium_cover_image;
}

public void setPodium_cover_image(String podium_cover_image) {
    this.podium_cover_image = podium_cover_image;
}

public String getFollowers_data() {
    return followers_data;
}

public void setFollowers_data(String followers_data) {
    this.followers_data = followers_data;
}

private String podiumname;
private String podium_description;
private String podium_image;
private String podium_cover_image;
private String followers_data;

public String getPodiumId() {
    return podiumId;
}

public void setPodiumId(String podiumId) {
    this.podiumId = podiumId;
}

private String podiumId;

}

适配器:

public class Podium_Ad extends RecyclerView.Adapter<Podium_Ad.ViewHolder> {
public static MyClickListener listener;
private List<ComonPodium> followingbean;
private Context context;

int poss;
String  podiumid1 ;

public Podium_Ad(Context context, List<ComonPodium> fproduct) {
    this.context = context;
    this.followingbean = fproduct;
}

@Override
public Podium_Ad.ViewHolder onCreateViewHolder(ViewGroup viewGroup, int i) {
    View view = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.view_podiumitem, viewGroup, false);
    return new ViewHolder(view);
}

@Override
public void onBindViewHolder(final Podium_Ad.ViewHolder viewHolder, final int i) {
    viewHolder.setIsRecyclable(false);

    viewHolder.podumname.setText(followingbean.get(i).getPodiumname());
    viewHolder.discription_tv.setText(followingbean.get(i).getPodium_description());
    viewHolder.followersCoount_tv.setText(followingbean.get(i).getFollowers_data());
    Picasso.with(context).load(followingbean.get(i).getPodium_cover_image()).placeholder(R.drawable.boy).into(viewHolder.userImage);
    Picasso.with(context).load(followingbean.get(i).getPodium_image()).placeholder(R.drawable.boy).into(viewHolder.coverimage);




}



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

public void setMyClickListener(MyClickListener listener) {
    Podium_Ad.listener = listener;
}

public class ViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
    private TextView podumname, discription_tv,followersCoount_tv, follow_tv;
    private ImageView userImage, coverimage;
    LinearLayout followunfollow;
    String unfollow, follower, followers_count;

    public ViewHolder(View view) {
        super(view);
        podumname = (TextView) view.findViewById(R.id.podiumname_txt);
        discription_tv = (TextView) view.findViewById(R.id.podumdiscripition_txt);
        followersCoount_tv = (TextView) view.findViewById(R.id.followercount_txt);
        userImage = (ImageView) view.findViewById(R.id.profile_image);
        coverimage = (ImageView) view.findViewById(R.id.coverImage);
        followunfollow = (LinearLayout)view.findViewById(R.id.userfoller);
        follow_tv = (TextView) view.findViewById(R.id.follow_btn);

        view.findViewById(R.id.profile_image).setOnClickListener(this);
        view.findViewById(R.id.podiumname_txt).setOnClickListener(this);
        view.findViewById(R.id.podumdiscripition_txt).setOnClickListener(this);
        view.findViewById(R.id.userfoller).setOnClickListener(this);





     followunfollow.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                int s = getAdapterPosition();
                podiumid1 = followingbean.get(s).getPodiumId();
                // poss = getAdapterPosition();
                // podiumid1 = followingbean.get(poss).getPodiumId();
                Log.e("id ===>>>",podiumid1);
                System.out.println("podiumid_podiumid"+podiumid1 +"::"+poss);
                follow();
               // notifyDataSetChanged();
            }
        });

    }

    @Override
    public void onClick(View view) {
        listener.onItemClick(view, followingbean.get(getPosition()));
    }


    public void follow() {
        final String followurl = "https://www.videobeaker.com/secure/assests/video-beaker-api/get-data.php?follow=1";

        System.out.println("podiumid_podiumid"+podiumid1 +"::"+poss);
        Log.e("followurl", followurl);
        StringRequest stringRequest = new StringRequest(Request.Method.POST, followurl, new Response.Listener<String>() {
            @Override
            public void onResponse(String response) {


                try {
                    JSONObject jsonObject = new JSONObject(response);

                    Log.e("follow_response", response);

                    follower = jsonObject.getString("response");
                    unfollow = jsonObject.getString("response");
                    followers_count = String.valueOf(jsonObject.getInt("total_follow_count"));

                    if (follower.equalsIgnoreCase("1")) {
                        follow_tv.setText("Unfollow");
                        followersCoount_tv.setText(followers_count);

                    } else if (unfollow.equalsIgnoreCase("2")) {


                        unfollow();

                    }


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

            }
        }, new Response.ErrorListener() {

            @Override
            public void onErrorResponse(VolleyError error) {
                VolleyLog.d("error", "Error: " + error.getMessage());

            }
        }) {

            @Override
            protected Map<String, String> getParams() {
                Map<String, String> params = new HashMap<>();
                params.put("login_user_id", AppController.getInstance().getKeyString(AppConstants.USERID));
                params.put("podium_id", podiumid1);
                params.put("timezone", "Asia/kolkata");
                Log.e("all_param,", String.valueOf(params));

                return params;
            }

        };


        RequestQueue requestQueue = Volley.newRequestQueue(context);
        requestQueue.add(stringRequest);


    }


    public void unfollow() {
        final String recentupload = "https://www.videobeaker.com/secure/assests/video-beaker-api/get-data.php?unfollow=1";

        System.out.println("podiumid_podiumid"+podiumid1);
        StringRequest stringRequest = new StringRequest(Request.Method.POST, recentupload, new Response.Listener<String>() {
            @Override
            public void onResponse(String response) {


                try {

                    JSONObject jsonObject = new JSONObject(response);
                    Log.e("unfollow", response);
                    unfollow = jsonObject.getString("response");
                    followers_count = String.valueOf(jsonObject.getInt("total_follow_count"));
                    //  followersCoount_tv.setText(followers_count);
                    if (unfollow.equalsIgnoreCase("1")) {
                        follow_tv.setText("Follow");
                        followersCoount_tv.setText(followers_count);


                    } else if (unfollow.equalsIgnoreCase("2")) {
                        follow();
                    }


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

            }
        }, new Response.ErrorListener() {

            @Override
            public void onErrorResponse(VolleyError error) {
                VolleyLog.d("error", "Error: " + error.getMessage());

            }
        }) {

            @Override
            protected Map<String, String> getParams() {
                Map<String, String> params = new HashMap<>();
                params.put("login_user_id", AppController.getInstance().getKeyString(AppConstants.USERID));
                params.put("podium_id",podiumid1);
                params.put("timezone", "Asia/kolkata");

                return params;
            }

        };


        RequestQueue requestQueue = Volley.newRequestQueue(context);
        requestQueue.add(stringRequest);


    }

}

}

我的xml:

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="geek.vediobeaker.fragment.Following_Fragment">

<android.support.v7.widget.RecyclerView
    android:id="@+id/notification_recycler"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_marginBottom="@dimen/_10sdp"
    android:minHeight="@dimen/_120sdp" />

谁能告诉我如何正确获取我的 scoll 数据?

在adapter中添加该方法,调用podiumAd。 setDatalist(comonPodiums) 并删除 podiumAd.notifyDataSetChanged();

 public void setDatalist(List< ComonPodium > list) {

                followingbean = list;

                notifyDataSetChanged();

            }

在onBindViewHolder方法中添加点击监听

 @Override
public void onBindViewHolder(final Podium_Ad.ViewHolder viewHolder, final int i) {
ComonPodium comomPodium = followingbean.get(i);
viewHolder.setIsRecyclable(false);

viewHolder.podumname.setText(comomPodium.getPodiumname());
viewHolder.discription_tv.setText(comomPodium.getPodium_description());
viewHolder.followersCoount_tv.setText(comomPodium.getFollowers_data());
Picasso.with(context).load(comomPodium.getPodium_cover_image()).placeholder(R.drawable.boy).into(viewHolder.userImage);
Picasso.with(context).load(comomPodium.getPodium_image()).placeholder(R.drawable.boy).into(viewHolder.coverimage);

viewHolder.followunfollow.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            int s = getAdapterPosition();
            podiumid1 = followingbean.get(s).getPodiumId();
            // poss = getAdapterPosition();
            // podiumid1 = followingbean.get(poss).getPodiumId();
            Log.e("id ===>>>",podiumid1);
            System.out.println("podiumid_podiumid"+podiumid1 +"::"+poss);
            follow(comomPodium);
        }
    });
 }

在你按照方法更新 ComonPodium 成功时 API 调用结果

public void follow(ComonPodium comonPodium) {
    final String followurl = "https://www.videobeaker.com/secure/assests/video-beaker-api/get-data.php?follow=1";

    System.out.println("podiumid_podiumid"+podiumid1 +"::"+poss);
    Log.e("followurl", followurl);
    StringRequest stringRequest = new StringRequest(Request.Method.POST, followurl, new Response.Listener<String>() {
        @Override
        public void onResponse(String response) {


            try {
                JSONObject jsonObject = new JSONObject(response);

                Log.e("follow_response", response);

                follower = jsonObject.getString("response");
                unfollow = jsonObject.getString("response");
                followers_count = String.valueOf(jsonObject.getInt("total_follow_count"));

                if (follower.equalsIgnoreCase("1")) {
                    follow_tv.setText("Unfollow");
            //Here you need to to update model class///
                     comonPodium.setFollowDate("your response data")
                    followersCoount_tv.setText(followers_count);

                } else if (unfollow.equalsIgnoreCase("2")) {


                    unfollow();

                }


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

        }
    }, new Response.ErrorListener() {

        @Override
        public void onErrorResponse(VolleyError error) {
            VolleyLog.d("error", "Error: " + error.getMessage());

        }
    }) {

        @Override
        protected Map<String, String> getParams() {
            Map<String, String> params = new HashMap<>();
            params.put("login_user_id", AppController.getInstance().getKeyString(AppConstants.USERID));
            params.put("podium_id", podiumid1);
            params.put("timezone", "Asia/kolkata");
            Log.e("all_param,", String.valueOf(params));

            return params;
        }

    };


    RequestQueue requestQueue = Volley.newRequestQueue(context);
    requestQueue.add(stringRequest);


}