[英]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;
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.