我用customview包含ImageView和两个TextViews,这是我的RecyclerView.Adapter我使用Picasso库来显示图像。
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 );
}
}
我的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>
活动类是: -
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();
}
}
);
}
}
问题是当我尝试滚动recyclerview然后图像变更时, 任何人都可以帮助我。
答案 0 :(得分:0)
感谢您的回复...... 最后我解决了这个问题
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);
我在上面的代码中做了很小的改变...... 在上面的代码中,ivProfile声明为全局,因此无法与ViewHolder引用链接。所以我改变它如下......
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);