Vijay Vijay - 29 days ago 9
Android Question

In Recyclerview Image get misplaced while scrolling

I used customview contains ImageView and two TextViews, this is my RecyclerView.Adapter I used Picasso library to show image.

public void onBindViewHolder(final ViewHolder holder, final int position) {
Picasso.with(context)
.load(url)
.placeholder(R.drawable.placeholder)
.error(R.drawable.error)
.resize(100, 100)
.into(ivProfile);

holder.tvFirstName.setText(object.getFirstName();
holder.tvSecondName.setText(object.getSecondName);}


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

class ViewHolder extends RecyclerView.ViewHolder {

public TextView tvFirstName ;
public TextView tvSecondName ;
public ImageView ivProfile;

public ViewHolder(View itemView) {
super(itemView);
ivProfile= (ImageView) itemView.findViewById(R.id.ivProfile);
tvFirstName = (TextView) itemView.findViewById(R.id.tvFirstName );
tvSecondName = (TextView) itemView.findViewById(R.id.tvSecondName );

}
}


And my RecyclerView -

<android.support.v7.widget.RecyclerView
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:scrollbarAlwaysDrawVerticalTrack="true"
android:scrollbars="vertical>

</android.support.v7.widget.RecyclerView>


Activity class is:-

public class MainActivity extends AppCompatActivity {

private RecyclerView rv_profileList;
private List<ProfilePOJO> profileList;
private RecyclerView.LayoutManager layoutManager;
private RecyclerView.Adapter recycleAdapter;

protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_profile_list);

rv_profileList = (RecyclerView) findViewById(R.id.rv_profileList );
profileList = new ArrayList<>();

getProfileDetails("");

}


public void getProfileDetails(String ID) {

RestAdapter adapter = new RestAdapter.Builder()
.setEndpoint(Constants.PROFILE_URL)
.build();
API objGetProfile = adapter.create(API.class);


objGetProfile.getProfile(
ID,
new Callback<String>() {
@Override
public void success(String result, Response response) {
try {

Gson gson = new Gson();
Type listType = new TypeToken<List<ProfilePOJO>>() {
}.getType();

profileList = (List<ProfilePOJO>) gson.fromJson(result, listType);

if (profileList.size() != 0) {

recycleAdapter = new ProductListAdapter(productList, MainActivity.this);

rv_profileList.setAdapter(recycleAdapter);

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

}

@Override
public void failure(RetrofitError error) {
error.printStackTrace();
Toast.makeText(MainActivity .this, "Please check Internet connection !", Toast.LENGTH_LONG).show();

}

}
);

}
}


The issue is when I am trying to scroll the recyclerview then image get alter changed,
can any one help me out.
Thanks in Advanced.

Answer

Thanks for respond guys... Finally I have solved this

public void onBindViewHolder(final ViewHolder holder, final int position) {
     Picasso.with(context)
        .load(url)
        .placeholder(R.drawable.placeholder)   
        .error(R.drawable.error)   
        .resize(100, 100)                       
        .into(ivProfile);

I have done change small change in above code... In above code ivProfile declare as globally, so this is unable to link with ViewHolder reference. So I changed it like below...

public void onBindViewHolder(final ViewHolder holder, final int position) {
     Picasso.with(context)
        .load(url)
        .placeholder(R.drawable.placeholder)   
        .error(R.drawable.error)   
        .resize(100, 100)                       
        .into(holder.ivProfile);
Comments