gpt4 book ai didi

android - RecyclerView 的行正在洗牌并更改图像 : android

转载 作者:塔克拉玛干 更新时间:2023-11-02 08:36:51 27 4
gpt4 key购买 nike

  • 随着其他问题的解决,问题发生了变化。

我的问题是 Recycler View 上下滚动

  • 行项目在快速滚动时改变它的位置并返回到它的原始位置。
  • 行项目使其某些部分固定为背景

这里是我如何在 Fragment 中设置 My Recycler View 。

private void setRecyclerView() {
recyclerView.setHasFixedSize(true);
StaggeredGridLayoutManager layoutManager =
new StaggeredGridLayoutManager(2, 1);
recyclerView.setLayoutManager(layoutManager);
adapter = new TwitterTweetAdapter(getActivity());

// setting every thing false in item animator
recyclerView.setItemAnimator(new RecyclerView.ItemAnimator() {

@Override
public void runPendingAnimations() {

}

@Override
public boolean animateRemove(RecyclerView.ViewHolder viewHolder) {
return false;
}

@Override
public boolean animateAdd(RecyclerView.ViewHolder viewHolder) {
return false;
}

@Override
public boolean animateMove(RecyclerView.ViewHolder viewHolder, int i, int i2, int i3, int i4) {
return false;
}

@Override
public boolean animateChange(RecyclerView.ViewHolder viewHolder, RecyclerView.ViewHolder viewHolder2, int i, int i2, int i3, int i4) {
return false;
}

@Override
public void endAnimation(RecyclerView.ViewHolder viewHolder) {

}

@Override
public void endAnimations() {

}

@Override
public boolean isRunning() {
return false;
}
});
recyclerView.setAdapter(adapter);
}

这是我的适配器类

在我的适配器中,我正在设置一个标题

//我的ViewHolders

class Header extends RecyclerView.ViewHolder {

@Bind(R.id.feed_head)
CardView feedHeader;
@Bind(R.id.feed_header_count)
TextView userCount;
@Bind(R.id.feed_header_text)
TextView userText;

public TwitterHeader(View itemView) {
super(itemView);
ButterKnife.bind(this, itemView);
}
}

public static class RowViewHolder extends RecyclerView.ViewHolder {

@Bind(R.id.feed_row_view)
CardView feedRow;
@Bind(R.id.feed_image)
ImageView feedImage;
@Bind(R.id.video_play_icon)
ImageButton playButton;
@Bind(R.id.feed_description)
TextView feedDescription;
@Bind(R.id.feed_user_image)
ImageView userImage;
@Bind(R.id.feed_user_name)
TextView userName;
@Bind(R.id.feed_time)
TextView feedDate;
@Bind(R.id.feed_progress_bar)
ProgressBar progressBar;

public RowViewHolder(View itemView) {
super(itemView);
ButterKnife.bind(this, itemView);
}
}

//变量声明

private static final int TYPE_HEADER = 0;
private static final int TYPE_FEED = 1;

Context context;
private Activity activity;
private List<MockData> dataLists;
public static List<MockData> dataListsupdated;

int userSearchCount;

//构造函数

public FeedAdapter(Activity activity) {
this.activity = activity;
}

//设置数据列表

public void setDataList(List<MockData> dataLists, int userSearchCount, float density) {
this.dataLists = dataLists;
this.density = density;
this.userSearchCount = userSearchCount;
}

//onCreateViewHolder

@Override
public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup viewGroup, int viewType) {
if (viewType == TYPE_HEADER) {
View view = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.feed_header, viewGroup, false);
return new Header(view);
} else {
View v = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.feed_row, viewGroup, false);
return new RowViewHolder(v);
}
}

//onBindViewHolder 我将我的 View 与数据绑定(bind)。

if (holder instanceof Header) {
Header header = (Header) holder;
if (userSearchCount == 20) {
header.userCount.setText(R.string.twitter_default_count);
} else {
header.userCount.setText(userSearchCount);
}
header.userText.setText(R.string.user);
header.feedHeader.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(activity, TwitterSearchActivity.class);
activity.startActivity(intent);
}
});

} else {
if (holder instanceof RowViewHolder) {
final RowViewHolder rowViewHolder = (RowViewHolder) holder;
final MockData responseList = dataLists.get(position);

rowWidth = rowViewHolder.feedImage.getWidth();
rowViewHolder.playButton.setVisibility(View.GONE);

if (responseList.text.startsWith("RT")) {
rowViewHolder.feedRow.setVisibility(View.GONE);
FrameLayout.LayoutParams params = new FrameLayout.LayoutParams(FrameLayout.LayoutParams.MATCH_PARENT, FrameLayout.LayoutParams.WRAP_CONTENT);
params.setMargins(0, 0, 0, 0);
rowViewHolder.feed.setLayoutParams(params);
} else {
setRowImage(rowViewHolder, responseList);
rowViewHolder.feedDescription.setText(responseList.text);

rowViewHolder.userImage.setVisibility(View.VISIBLE);
Glide.with(activity)
.load(responseList.user.profileImageUrlHttps)
.into(rowViewHolder.userImage);

rowViewHolder.userName.setText(responseList.user.screenName);

Date date = new Date(responseList.createdAt);
DateFormat dateFormat = android.text.format.DateFormat.getDateFormat(activity.getApplicationContext());
rowViewHolder.feedDate.setText(dateFormat.format(date));

rowViewHolder.feedRow.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
startActivity(position);
}
});
}
}
}

}

//设置行图像。

private void setRowImage(RowViewHolder rowViewHolder, Tweet responseList) {
if (responseList.entities.media != null) {
MediaEntity mediaEntity = responseList.entities.media.get(0);
if (mediaEntity.mediaUrlHttps != null) {
int height = mediaEntity.sizes.medium.h;
int width = mediaEntity.sizes.medium.w;

int ratio = width / 143;
int newHeight = (int) ((height / ratio) * density);

Glide.with(activity)
.load(mediaEntity.mediaUrlHttps)
.override(width, newHeight)
.placeholder(R.color.colorAccent)
.into(rowViewHolder.feedImage);

} else {
rowViewHolder.feedImage.setImageDrawable(null);
}
} else {
rowViewHolder.feedImage.setImageDrawable(null);
}
}

//查看条件以设置标题和行。

@Override
public int getItemViewType(int position) {
if (isPositionHeader(position))
return TYPE_HEADER;
return TYPE_FEED;
}

private boolean isPositionHeader(int position) {
return position == 0;
}

@Override
public int getItemCount() {
return dataLists == null ? 0 : dataLists.size();
}

最佳答案

我的应用程序中有一个非常相似的适配器,我在其中显示来自 Facebook 而不是 Twitter 的提要,首先,切换到 Glide 而不是 Picasso,因为它使用 RecyclerView 提供了更好的缓存和图像加载,检查 THIS .所以现在来解决你的问题,第一步在你的适配器上调用 setHasStableIds(true) ,覆盖 getItemId 方法以从你的适配器返回一个合适的长,因为你使用的是一个提要,你可以计算一个散列说 postId 或一些唯一标识提要中每个帖子的字段。我的提要也有一个可选图像,这就是为什么我有这两种方法

public void setProfilePicture(String uri) {

//As per the solution discussed here http://stackoverflow.com/questions/32706246/recyclerview-adapter-and-glide-same-image-every-4-5-rows
if (uri != null) {
Glide.with(mContext)
.load(uri)
.asBitmap()
.transform(new CropCircleTransform(mContext))
.into(mProfilePicture);
} else {
Glide.clear(mProfilePicture);
mProfilePicture.setImageResource(R.drawable.com_facebook_profile_picture_blank_square);
}
}

public void setPostPicture(String uri) {

//As per the solution discussed here http://stackoverflow.com/questions/32706246/recyclerview-adapter-and-glide-same-image-every-4-5-rows
if (uri != null) {
Glide.with(mContext)
.load(uri)
.asBitmap()
.transform(new CropTransformation(mContext, mPostImageWidth, mPostImageHeight))
.into(mPostPicture);
} else {
Glide.clear(mPostPicture);
mPostPicture.setImageDrawable(null);
}
}

然后您要做的就是从您的 onBindViewHolder 调用这些方法来设置图像。帖子是我的对象,它包含单个帖子的详细信息,例如姓名、用户 ID、人物图片的图像 url 和帖子图片的图像 url,这是可选的。如果您进一步遇到任何问题,请告诉我。

@Override
public void onBindViewHolder(ItemHolder holder, int position) {
Post post = mResults.get(position);
holder.setUserName(post.getUserName());
holder.setUpdatedTime(post.getUpdatedTime());
holder.setMessage(post.getMessage(), mState, position);
holder.setPostPicture(post.getPicture());
holder.setProfilePicture(post.getUserPicture());
// Check for an expanded view, collapse if you find one

}

关于android - RecyclerView 的行正在洗牌并更改图像 : android,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33579542/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com