【问题标题】:Android: How to repeat list items in listviewAndroid:如何在列表视图中重复列表项
【发布时间】:2016-03-11 06:49:23
【问题描述】:

我正在开发一个 android 应用程序,我必须显示一个包含重复项目的列表视图。最初列表视图显示 3 个项目,但我希望它们显示 3 或 4 次。有没有办法做到这一点?

public class CustomAdapter extends ArrayAdapter<HireGroups> {

ArrayList<HireGroups> result;
Context c;
LayoutInflater inflater;

public CustomAdapter(Context context, ArrayList<HireGroups> list) {
    super(context,0, list);
    c=context;
    result=list;
    inflater = ( LayoutInflater )context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}

@Override
public int getCount() {
    return result.size();
}

@Override
public long getItemId(int position) {
    return position;
}

@Override
public boolean isEnabled(int position) {
    final HireGroups h = result.get(position);
    if(h.getSubHireGroup().size() == 0) {
        return false;
    }
    return super.isEnabled(position);
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    View v=convertView;

    final HireGroups i = result.get(position);
    if(i != null) {
        v = inflater.inflate(R.layout.single_hiregroup_item, null);
        final TextView hireGroupName = (TextView) v.findViewById(R.id.hire_group_name);
        final TextView subtitle = (TextView) v.findViewById(R.id.subtitle);
        final TextView desc = (TextView) v.findViewById(R.id.hire_group_desc);
        final ImageView hiregroupImage = (ImageView) v.findViewById(R.id.hire_group_image);
        final TextView vehicleCount = (TextView) v.findViewById(R.id.no_of_vehicles);

        if(hireGroupName != null) {
            hireGroupName.setText(i.getHireGroupName());
        }
        if(subtitle != null) {
            subtitle.setText(i.getSubTitle());
        }
        if(hiregroupImage != null) {
            Picasso.with(c).load(i.getPhotoUrl()).into(hiregroupImage);
        }
        if(desc != null) {
            desc.setText(i.getDescription());
        }
        if(vehicleCount != null)
        {
            int count=i.getSubHireGroup().size();
            Animation anim = new AlphaAnimation(0.0f, 1.0f);
            anim.setDuration(200); //You can manage the blinking time with this parameter
            anim.setStartOffset(20);
            anim.setRepeatMode(Animation.REVERSE);
            anim.setRepeatCount(Animation.INFINITE);
            if(count > 0)
            {
                vehicleCount.startAnimation(anim);
                vehicleCount.setText("  "+count+" Available ");
            }
            else
                vehicleCount.setText("  "+count+" Available ");
        }

    }
    return v;
}

}

【问题讨论】:

  • 您的 Listview 使用什么来源。数组列表还是光标?
  • 它是一个从 json 字符串填充的类对象的数组列表。
  • 创建一个自定义适配器,扩展 BaseAdapter 并“包装”原始适配器并覆盖 getCount,返回的结果是原始适配器的 3 或 4 倍
  • 只需从 json 字符串中填充三次数组列表,每次只需将 json 对象添加到数组列表
  • 我有一个适配器,你可以看到我的编辑。

标签: android listview items


【解决方案1】:

假设你想显示3次相同的数据,那么你可以做的是

像这样修改getCount()

@Override
public int getCount() {
    return result.size() * 3;
}

并修改getView()

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    View v=convertView;

    final HireGroups i = result.get(position % 3);
    if(i != null) {
        v = inflater.inflate(R.layout.single_hiregroup_item, null);
        final TextView hireGroupName = (TextView) v.findViewById(R.id.hire_group_name);
        final TextView subtitle = (TextView) v.findViewById(R.id.subtitle);
        final TextView desc = (TextView) v.findViewById(R.id.hire_group_desc);
        final ImageView hiregroupImage = (ImageView) v.findViewById(R.id.hire_group_image);
        final TextView vehicleCount = (TextView) v.findViewById(R.id.no_of_vehicles);

        if(hireGroupName != null) {
            hireGroupName.setText(i.getHireGroupName());
        }
        if(subtitle != null) {
            subtitle.setText(i.getSubTitle());
        }
        if(hiregroupImage != null) {
            Picasso.with(c).load(i.getPhotoUrl()).into(hiregroupImage);
        }
        if(desc != null) {
            desc.setText(i.getDescription());
        }
        if(vehicleCount != null)
        {
            int count=i.getSubHireGroup().size();
            Animation anim = new AlphaAnimation(0.0f, 1.0f);
            anim.setDuration(200); //You can manage the blinking time with this parameter
            anim.setStartOffset(20);
            anim.setRepeatMode(Animation.REVERSE);
            anim.setRepeatCount(Animation.INFINITE);
            if(count > 0)
            {
                vehicleCount.startAnimation(anim);
                vehicleCount.setText("  "+count+" Available ");
            }
            else
                vehicleCount.setText("  "+count+" Available ");
        }

    }
    return v;
}

【讨论】:

  • 我已经尝试将 result.size() * 3 相乘,但在检索项目时没有应用模数,所以我遇到了异常。最初@pskink 这个答案值得称赞。也谢谢你。
猜你喜欢
  • 1970-01-01
  • 2013-10-01
  • 2016-09-27
  • 1970-01-01
  • 2016-11-10
  • 1970-01-01
  • 2021-12-06
  • 1970-01-01
  • 2015-12-26
相关资源
最近更新 更多