0
  • 最佳答案

    内容显示由谁控制呢?适配器吧。那你是不是要把适配器的代码贴上来呢?

    1153952789488054272  评论     打赏       拉大锯
    • 前期后期  回复 @拉大锯
      好的,麻烦看看!
      前期后期 2019-11-30 20:09   回复 1180111498064793600
  • public class HomeRecAdapter extends RecyclerView.Adapter<HomeRecAdapter.MyRecylerViewHolder> {
    
        private String TAG = this.getClass().getSimpleName();
    
        //② 创建ViewHolder
        public static class MyRecylerViewHolder extends RecyclerView.ViewHolder {
            public final TextView tvName;
            public final TextView tvName1;
            public final ImageView ivSugar;
            public final TextView tvPrice;
            public final TextView tvPrice1;
            public RelativeLayout rlSugar;
            public final TextView tvMini;
            public final LinearLayout llPreice;
            private final View ivCoinFuhao;
    
            public MyRecylerViewHolder(View view) {
                super(view);
                ivSugar = view.findViewById(R.id.iv_sugar);
                tvName = view.findViewById(R.id.tv_name);
                tvName1 = view.findViewById(R.id.tv_name1);
                tvPrice = view.findViewById(R.id.tv_price);
                tvPrice1 = view.findViewById(R.id.tv_price1);
                rlSugar = view.findViewById(R.id.rl_sugar);
                tvMini = view.findViewById(R.id.tv_mini);
                llPreice = view.findViewById(R.id.ll_price);
                ivCoinFuhao = view.findViewById(R.id.iv_coin_fuhao);
            }
        }
    
        private ArrayList<GoodsBean> goodsBeans;
    
        public void setItemListener(RecyclerItemListener itemListener) {
            this.itemListener = itemListener;
        }
    
        private RecyclerItemListener itemListener;
    
        public HomeRecAdapter(ArrayList<GoodsBean> goodsBeans) {
            this.goodsBeans = goodsBeans;
    //        for (GoodsBean goodsBean : goodsBeans) {
    //            Log.d("ttt ",goodsBean.getNameChinese());
    //        }
        }
    
        @NonNull
        @Override
        public HomeRecAdapter.MyRecylerViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
            View inflate;
            int num=13;
    
            if (goodsBeans.size()<num){
                inflate = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.item_user_home_less, viewGroup, false);
            }else {
                inflate = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.item_user_home, viewGroup, false);
            }
    
            return new MyRecylerViewHolder(inflate);
        }
    
        @Override
        public void onBindViewHolder(@NonNull HomeRecAdapter.MyRecylerViewHolder viewHolder, final int i) {
            GoodsBean goodsBean = goodsBeans.get(i);
            Log.d("ttt ",goodsBean.getNameChinese());
            String price = String.valueOf(goodsBean.getPrice());
            int coin = goodsBean.getCoin();
            viewHolder.ivSugar.setImageResource(goodsBean.getImgID());
    //        if (price.length()>=5){
            viewHolder.llPreice.setVisibility(View.GONE);
            viewHolder.tvName1.setVisibility(View.VISIBLE);
            viewHolder.tvPrice1.setVisibility(View.VISIBLE);
            viewHolder.tvName1.setText(goodsBean.getNameId());
            viewHolder.tvName1.setLines(1);
            TextViewCompat.setAutoSizeTextTypeWithDefaults(viewHolder.tvName1, TextViewCompat.AUTO_SIZE_TEXT_TYPE_UNIFORM);
            if (Global.isLocal){
                if (Global.payCCType == 40){
                    viewHolder.tvPrice1.setText("" + price);
                }else{
                    viewHolder.tvPrice1.setText("" + coin);
                }
                viewHolder.ivCoinFuhao.setVisibility(View.VISIBLE);
                Log.d(TAG,""+Global.payCCType);
            } else{
                viewHolder.tvPrice1.setText("¥" + price);
                viewHolder.ivCoinFuhao.setVisibility(View.GONE);
            }
    
    
    
            viewHolder.rlSugar.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (itemListener!=null){
                        itemListener.onItemClickLintener(v, i);
    
                    }
                }
            });
            if (i == 16 || i == 17) {
                viewHolder.tvMini.setVisibility(View.VISIBLE);
            }
        }
    
    
        @Override
        public int getItemCount() {
            return goodsBeans.size();
        }
    }
    


    1180111498064793600  评论     打赏       前期后期
    • 拉大锯  回复 @前期后期
      你这里看起来是没有什么问题的。你说UI有问题,先确保数据是一样的先。 另外,数据不要这样给值,应该在内部有一个List,然后外面把数据设置进来。你这种方法有风险,如果外面一置空,你的程序就崩溃了。多类型请用覆写getType方法,不会的话了解一下RecyclerView的getType方法有什么用的。别这么写,数字要抽取成常量。
      拉大锯 2019-11-30 20:37   回复 1153952789488054272
  • 这个是recyclerview的复用导致的,最简单的就是取消item的复用,还有一种是用map记录和给view加tag。自己也在找一种好的解决方法,同问。

    1200698213460467712  评论     打赏       sofiworker
    • 拉大锯  回复 @sofiworker
      这是ListView的做法。具体问题还是得看看他的适配器代码怎么写的。
      拉大锯 2019-11-30 19:22   回复 1153952789488054272
相关问题
_empty · Android
2019-10-20 23:51 592 2
application · Android
2019-11-05 00:26 743 2
三流废物 · android / 小白
2019-11-22 00:35 1026 5
豪豪好吗 · Android / GPS
2019-11-26 20:15 834 5
Jian · android
2019-11-28 01:51 580 2
2019-12-01 01:01 638 2
小陈学编程 · Android
2019-12-01 19:08 599 2
fkinggod · Android
2019-12-08 09:19 625 5
fkinggod · Android
2019-12-08 20:47 564 2
kissmyass · Android
2019-12-09 07:08 506 2
独一无二的名zi · Android
2019-12-30 06:48 382 2