Android: 如何在列表视图中重复列表项

Android: How to repeat list items in listview

我正在开发一个 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;
}

}

假设你想显示同一个数据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;
}