RecyclerView水平滚动数据随机变化

时间:2017-03-25 07:18:55

标签: android android-recyclerview

我在水平Recyclerview适配器中遇到问题。我的适配器包含位置0的镜像视图,剩余位置为空。 当我从左向右滚动时,我的图像从位置1到4和7重新出现。如果我一直从左向右滚动,我的imageview出现在所有位置这里是我的适配器类。我已将此水平Recyclerview适配器放入Vertical Recyclerview适配器。

HomeBannerAdapter.Java

公共类HomeBannerAdapter扩展了RecyclerView.Adapter {

class Receipt(models.Model):
    # Bunch of products

class Product(models.Model):
    name = models.CharField(max_length=100)
    price = models.IntegerField()

}

这是我的Vertical Recyclerview适配器

HomeDataAdapter.Java

public class HomeDataAdapter扩展了RecyclerView.Adapter {

private List<HomePageList> mHomepageList;
private Context mContext;


public HomeBannerAdapter(Context context, List<HomePageList> mHomepageList) {
    this.mHomepageList = mHomepageList;
    this.mContext = context;
}

@Override
public SingleItemRowHolder onCreateViewHolder(ViewGroup viewGroup, int position) {
    View v = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.list_single_card, viewGroup, false);
    SingleItemRowHolder mh = new SingleItemRowHolder(v);
    return mh;
}

@Override
public void onBindViewHolder(final SingleItemRowHolder holder, final int position) {
    final HomePageList singleItem = mHomepageList.get(position);
    holder.mTitle.setText(singleItem.getTitle());
    if (!singleItem.getBannerUrl().isEmpty()) {
        Glide.with(mContext).load(singleItem.getBannerUrl().get(0)).placeholder(R.drawable.placeholder).dontAnimate().into(holder.itemImage);
        Log.e("Homebanner not empty", "url" + singleItem.getBannerUrl().toString() + "position" + position);

    } else {
        Log.e("Homebanner url empty", "url" + singleItem.getBannerUrl().toString() + "position" + position);
    }

}

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

public class SingleItemRowHolder extends RecyclerView.ViewHolder {

    public ImageView itemImage;
    //int position = getAdapterPosition();
    public TextView mTitle;
    public HomePageList mHomePageList;

    public SingleItemRowHolder(View view) {
        super(view);
        this.itemImage = (ImageView) view.findViewById(R.id.itemImage);

        this.mTitle = (TextView) view.findViewById(R.id.tvTitle);
        // mHomePageList = albumList.get(getPosition());
        view.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                Toast.makeText(v.getContext(), mTitle.getText().toString() + " position " + getPosition() + "getAdapterPosition" + getAdapterPosition(), Toast.LENGTH_SHORT).show();

            }
        });
    }
}

}

1 个答案:

答案 0 :(得分:0)

我认为你的一些项目bannerUrls是空的。因此,如果它们为空,则不要使用Glide或手动设置默认空图像。在这里,我稍微修改了你的代码。我认为这会有所帮助。

 editText1 = (EditText) findViewById(R.id.editText1);

 editText1.setOnFocusChangeListener(new OnFocusChangeListener() {
@Override
public void onFocusChange(View v, boolean hasFocus) {
     String value = editText1.getText().toString();
        if(value.length() == 0) {  
            //editext is empty
            if(! hasFocus){
                editText1.requestFocus();
                getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE);
                Toast.makeText(MainActivity.this, "Enter value correctly", Toast.LENGTH_SHORT).show();
            }
        }
   }
});

注意:此外,使用@Override public void onBindViewHolder(final SingleItemRowHolder holder, final int position) { final HomePageList singleItem = mHomepageList.get(position); holder.mTitle.setText(singleItem.getTitle()); if (!TextUtils.isEmpty(singleItem.getBannerUrl())) { Glide.with(mContext).load(singleItem.getBannerUrl().get(0)).placeholder(R.drawable.placeholder).dontAnimate().into(holder.itemImage); Log.e("Homebanner not empty", "url" + singleItem.getBannerUrl().toString() + "position" + position); } else { holder.itemImage.setImageResource(R.drawable.your_place_holder_for_empty_url); } } 代替TextUtils.isEmpty(string)以避免string.isEmpty()是更好的选择。

修改:

此外,您可以Null Pointer Exception删除Glide声明,让ImageView将占位符添加到if

if (!TextUtils.isEmpty(singleItem.getBannerUrl()))

我认为这个解决方案也可以帮到你。如果您选择这种方式不要忘记为您的空网址或空网址设置空图像Glide