我的recyclerview有问题。当我向下滚动一切都没问题但当我滚动到顶部时,我设置为图像的可绘制正在被更改。 这是我的适配器代码:
@Override
public FeedsHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.feed_item, parent, false);
final FeedsHolder holder = new FeedsHolder(view);
//Query
QueryOptions options = new QueryOptions();
options.setRelated(Arrays.asList("usersThatLike"));
BackendlessDataQuery query = new BackendlessDataQuery();
query.setQueryOptions(options);
Backendless.Data.of(Feeds.class).find(query, new AsyncCallback<BackendlessCollection<Feeds>>() {
@Override
public void handleResponse(BackendlessCollection<Feeds> response) {
String userId = Backendless.UserService.CurrentUser().getObjectId();
for (Feeds feed : response.getData()) {
feed = list.get(holder.getAdapterPosition());
List<BackendlessUser> likedUsers = feed.getUsersThatLike();
for (BackendlessUser user : likedUsers)
if (user.getObjectId().equals(userId)) {
//** GOOGLE MATERIAL ICON WITH FILLED HEART
setBackground(holder.like, ContextCompat.getDrawable(context, R.drawable.ic_favorite_black_24dp));
} else if (!user.getObjectId().equals(userId)) {
//** GOOGLE MATERIAL ICON WITH EMPTY HEART
setBackground(holder.like, ContextCompat.getDrawable(context, R.drawable.ic_favorite_border_black_24dp));
}
}
}
@Override
public void handleFault(BackendlessFault backendlessFault) {
}
});
return holder;
}
然后在onBindViewHolder上设置holder.like imageview的默认图像:
@Override
public void onBindViewHolder(final FeedsHolder holder, final int position) {
holder.like.setBackground(ContextCompat.getDrawable(context, R.drawable.ic_favorite_border_black_24dp));
}
我认为这是问题......
但是我不知道如何解决这个问题,因为如果没有这行代码,我的其他行的recyclerview将会有不同的图像:有些会填充心脏,有些会有空的。
所以请帮我找一个解决方案。
编辑:发布我的适配器代码
public class FeedAdapter extends RecyclerView.Adapter<FeedAdapter.FeedsHolder> {
private List<Feeds> list;
private Context context;
private static List<Long> selectedIds;
List<BackendlessUser> backendlessUserList;
private List<Comments> cake = new ArrayList<>();
private static String TAG = "MainActivity";
public FeedAdapter(Context context, List<Feeds> list) {
this.context = context;
this.list = list;
}
@Override
public FeedsHolder onCreateViewHolder(ViewGroup parent, final int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.feed_item, parent, false);
final FeedsHolder holder = new FeedsHolder(view);
//Query
QueryOptions options = new QueryOptions();
options.setRelated(Arrays.asList("usersThatLike"));
BackendlessDataQuery query = new BackendlessDataQuery();
query.setQueryOptions(options);
Backendless.Data.of(Feeds.class).find(query, new AsyncCallback<BackendlessCollection<Feeds>>() {
@Override
public void handleResponse(BackendlessCollection<Feeds> response) {
myFeeds = response;
String userId = Backendless.UserService.CurrentUser().getObjectId();
for (Feeds feed : response.getData()) {
feed = list.get(holder.getAdapterPosition());
List<BackendlessUser> likedUsers = feed.getUsersThatLike();
backendlessUserList = likedUsers;
for (BackendlessUser user : likedUsers)
if (user.getObjectId().equals(userId)) {
setBackground(holder.like, ContextCompat.getDrawable(context, R.drawable.ic_favorite_black_24dp));
} else if (!user.getObjectId().equals(userId)) {
setBackground(holder.like, ContextCompat.getDrawable(context, R.drawable.ic_favorite_border_black_24dp));
}
}
}
@Override
public void handleFault(BackendlessFault backendlessFault) {
}
});
return holder;
}
@Override
public void onBindViewHolder(final FeedsHolder holder, final int position) {
//Starting Feeds
final Feeds feeds = list.get(position);
holder.like.setBackground(ContextCompat.getDrawable(context, R.drawable.ic_favorite_border_black_24dp));
//Name
holder.name.setText(feeds.getOwner());
@Override
public int getItemCount() {
return list.size();
}
public class FeedsHolder extends RecyclerView.ViewHolder {
//Setup Views
CardView card;
TextView name;
public FeedsHolder(View itemView) {
super(itemView);
//Assign views by ID
card = (CardView) itemView.findViewById(R.id.cv);
name = (TextView) itemView.findViewById(R.id.commenter);
}
}
}
答案 0 :(得分:0)
您需要在某处保持单元格的状态(例如需要显示的图像)。一种选择是使一个列表具有相同大小的数据源,并存储一个值,指示该位置的元素是否具有空心,完整的心脏或什么都没有,最后在{{1}中检查该值},并正确更新视图。
编辑:这是一个例子:
onBindViewHolder
关注public class BreweryRecyclerViewAdapter
extends RecyclerView.Adapter<BreweryRecyclerViewAdapter.BreweryRecyclerViewViewHolder>
implements View.OnClickListener {
private static List<Long> selectedIds;
private Context context;
private List<Brewery> breweries;
private View.OnClickListener clickListener;
public BreweryRecyclerViewAdapter(Context context) {
clearBreweries();
this.context = context;
}
public BreweryRecyclerViewAdapter(List<Brewery> breweries) {
setBreweries(breweries);
}
public List<Brewery> getBreweries() {
return breweries;
}
public void setBreweries(List<Brewery> breweries) {
this.breweries = breweries;
selectedIds = new ArrayList<>();
}
public void setClickListener(View.OnClickListener clickListener) {
this.clickListener = clickListener;
}
public void clearBreweries() {
this.breweries = Collections.emptyList();
selectedIds = new ArrayList<>();
}
@Override
public BreweryRecyclerViewViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
LayoutInflater layoutInflater = LayoutInflater.from(context);
View itemView = layoutInflater.inflate(R.layout.item_brewery, parent, false);
itemView.setOnClickListener(this);
return new BreweryRecyclerViewViewHolder(context, itemView);
}
@Override
public void onBindViewHolder(BreweryRecyclerViewViewHolder holder, int position) {
Brewery brewery = breweries.get(position);
if (selectedIds.contains(brewery.getId())) {
holder.openSecondaryLayoutVisibility();
} else {
holder.closeSecondaryLayoutVisibility();
}
holder.update(brewery);
}
@Override
public int getItemCount() {
return breweries.size();
}
@Override
public void onClick(View view) {
if (clickListener != null) {
clickListener.onClick(view);
}
}
public static class BreweryRecyclerViewViewHolder extends RecyclerView.ViewHolder {
@BindView(R.id.brewery_name)
TextView breweryName;
@BindView(R.id.brewery_distance)
TextView breweryDistance;
@BindView(R.id.brewery_duration)
TextView breweryDuration;
@BindView(R.id.brewery_time)
TextView breweryTime;
@BindView(R.id.brewery_is_happy_hour)
ImageView breweryIsHappyHour;
@BindView(R.id.secondary)
RelativeLayout secondaryLayout;
@BindView(R.id.brewery_description)
TextView breweryDescription;
private Context context;
private Brewery brewery;
public BreweryRecyclerViewViewHolder(Context context, View itemView) {
super(itemView);
ButterKnife.bind(this, itemView);
this.context = context;
}
public Brewery getBrewery() {
return brewery;
}
public void toggleSecondaryLayoutVisibility(Long id) {
if (secondaryLayout.getVisibility() != View.VISIBLE) {
openSecondaryLayoutVisibility();
selectedIds.add(id);
} else {
closeSecondaryLayoutVisibility();
selectedIds.remove(id);
}
}
public void openSecondaryLayoutVisibility() {
secondaryLayout.setVisibility(View.VISIBLE);
}
public void closeSecondaryLayoutVisibility() {
secondaryLayout.setVisibility(View.GONE);
}
@OnClick(R.id.view_route_button)
public void viewRouteButtonClicked(Button button) {
String latitude = brewery.getLocation().getLatitude().toString();
String longitude = brewery.getLocation().getLongitude().toString();
Intent intent = new Intent(android.content.Intent.ACTION_VIEW, Uri.parse(
String.format("http://maps.google.com/maps?daddr=%s,%s", latitude, longitude)));
context.startActivity(intent);
}
public void update(Brewery brewery) {
breweryName.setText(brewery.getName());
if (brewery.getDistanceHumanReadable() == null) {
breweryDistance.setVisibility(View.GONE);
} else {
breweryDistance.setText(String.format("%s | ", brewery.getDistanceHumanReadable()));
breweryDistance.setVisibility(View.VISIBLE);
}
if (brewery.getDurationHumanReadable() == null) {
breweryDuration.setVisibility(View.GONE);
} else {
breweryDuration.setText(String.format("%s | ", brewery.getDurationHumanReadable()));
breweryDuration.setVisibility(View.VISIBLE);
}
breweryTime.setText(String.format("%shs a %shs", brewery.getOpen(), brewery.getClose()));
breweryIsHappyHour.setVisibility(brewery.isHappyHour() ? View.VISIBLE : View.GONE);
breweryDescription.setText(brewery.getDescription());
this.brewery = brewery;
}
}
}
和onBindViewHolder
,了解如何使用toggleSecondaryLayoutVisibility
来保持单元状态。