【问题标题】:RecyclerView item replication using adapter使用适配器的 RecyclerView 项目复制
【发布时间】:2018-06-30 22:20:57
【问题描述】:

我有一个自定义的 ArrayList,其中包含大约 80 到 90 个数据。我正在使用 RecyclerView 复制 Activity 中的视图。但是,该视图中的文本视图会随着向下或向上滚动而变化。此外,我的arraylist 中的所有数据甚至都不会显示在适配器中。但是,我已经对列表进行了排序,并且列表包含所有按升序排列的数据。有时相同的数据会一遍又一遍地重复。但是如果数组列表中的数据很少,那么它会正确显示所有项目。我在这里错过了什么吗?

这是适配器和主要活动的代码以及我的 xml 和我的自定义数组列表。

适配器:

public class Adapter_Usage extends RecyclerView.Adapter<Adapter_Usage.View>{
Context context;
CustomTextView txtsessiontime,txtvolupload,txtvoldownload,txtusageyear,txtusageday,txtusagemon;
ArrayList<Info_Usage> listusage;


public Adapter_Usage(Context context, int resource, ArrayList<Info_Usage> listusage) {
    this.context = context;
    this.listusage=listusage;
}
@Override
public View onCreateViewHolder(ViewGroup parent, int viewType) {
    android.view.View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.adapter_usage, parent, false);
    return new View(view);
}


@RequiresApi(api = Build.VERSION_CODES.JELLY_BEAN)
@Override
public void onBindViewHolder(View holder, int position)
{

    Info_Usage infousage = listusage.get(holder.getAdapterPosition());
    String[] arraybilldate = infousage.strdate.split("\\s+");
    txtusageyear.setText(arraybilldate[2]);
    txtusageday.setText(arraybilldate[1]);
    txtusagemon.setText(arraybilldate[0]+", ");
    txtsessiontime.setText(infousage.strsessiontime);
    txtvolupload.setText("Upload : "+infousage.struploads+" MB");
    txtvoldownload.setText("Download : "+infousage.strdownloads+" MB");
}
@Override
public int getItemCount() {
    return listusage.size();
}
public class View extends RecyclerView.ViewHolder {

    public View(android.view.View view) {
        super(view);
        txtusageyear=view.findViewById(R.id.txtusageyear);
        txtusageday=view.findViewById(R.id.txtusageday);
        txtusagemon=view.findViewById(R.id.txtusagemon);
        txtsessiontime=view.findViewById(R.id.txtsessiontime);
        txtvolupload=view.findViewById(R.id.txtvolupload);
        txtvoldownload=view.findViewById(R.id.txtvoldownload);
    }
}
}

自定义数组列表:

public class Info_Usage {
public String strdate,strdownloads,struploads,strsessiontime,strcurr;

public String getDate() {
    return strdate;
}
public String getcurr() {
    return strcurr;
}
}

我的活动:

Collections.sort(listusage, new Comparator<Info_Usage>()
                    {
                        @Override
                        public int compare(Info_Usage lhs, Info_Usage rhs)
                        {
                            int right=Integer.parseInt(rhs.getcurr());
                            int left=Integer.parseInt(lhs.getcurr());
                            return right<left?1:right>left?-1:0;
                        }
                    });

 if(listusage.size()==0)
            {
                recycler.setVisibility(View.GONE);
                txtnousage.setVisibility(View.VISIBLE);
            }
            else
            {

                txtnousage.setVisibility(View.GONE);
                recycler.setVisibility(View.VISIBLE);
                recycler.setAdapter(new Adapter_Usage(Activity_Usage.this, 0, listusage));
            }

【问题讨论】:

    标签: android arraylist android-recyclerview android-adapter


    【解决方案1】:

    您的Adapter 中有几个问题。

    1. onCreateViewHolder 内正确返回列表行视图:

      @Override
      public View onCreateViewHolder(ViewGroup parent, int viewType) {
          return LayoutInflater.from(parent.getContext()).inflate(R.layout.adapter_usage, parent, false);
      }
      
    2. onBindViewHolder 中正确访问您的列表数据:

      @Override
      public void onBindViewHolder(View holder, int position) {
          Info_Usage infousage = listusage.get(position);
          String[] arraybilldate = infousage.strdate.split("\\s+");
          txtusageyear.setText(arraybilldate[2]);
          txtusageday.setText(arraybilldate[1]);
          txtusagemon.setText(arraybilldate[0]+", ");
          txtsessiontime.setText(infousage.strsessiontime);
          txtvolupload.setText("Upload : "+infousage.struploads+" MB");
          txtvoldownload.setText("Download : "+infousage.strdownloads+" MB");
      }
      

    【讨论】:

    • 先生,第二个解决方案是我已经尝试过的。第一个我不能使用它,因为它是一个自定义适配器。
    猜你喜欢
    • 1970-01-01
    • 2017-09-06
    • 2019-08-07
    • 2017-02-24
    • 2017-06-05
    • 2020-07-27
    • 1970-01-01
    • 2023-03-17
    • 1970-01-01
    相关资源
    最近更新 更多