我有一个listview,它使布局中的视图膨胀。我的适配器在首次创建时成功将每个项目添加到其适当的位置。但是当滚动列表视图时,列表视图中的项目有时会在列表视图中丢失其位置,但有时不会丢失,我不确定是什么原因导致行为。我注意到每当我过度滚动列表视图时,行为都会再次发生。为了更好地解释行为,一旦列表视图滚动或过度滚动,或者甚至以非常“快速”的速度滚动,列表视图就会滚过最后一个项目,就好像另一个项目位于其下方一样。一旦我尝试滚动回到最后一项,listview允许我只滚动到列表视图中的最后一项(好像最后一项是列表视图中的唯一项目),那么它不允许我滚动。这是我的适配器
@Override
public View getView(final int position, View convertView, ViewGroup parent) {
viewHolder = new ViewHolder();
positionHolder = position;
Log.i("Position", "" + position);
if(convertView == null) {
try {
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = inflater.inflate(R.layout.post_layout, parent, false);
postLayout = convertView;
viewHolder.addToCalVF = (ViewFlipper)postLayout.findViewById(R.id.addToCalendarVF);
viewHolder.addToCalVF.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
((ViewFlipper)view).showNext();
Log.i("VIEWFLIPPERVF", viewHolder.addToCalVF.toString());
}
});
viewHolder.unameTV = (TextView) postLayout.findViewById(R.id.postUnameTv);
viewHolder.unameTV.setText(viewContent.get(index));
viewHolder.unameTV.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
// Starting new intent
Intent in = new Intent(getActivity(),
Profile.class);
// sending pid to next activity
String username =((TextView)view).getText().toString();
in.putExtra("username", username);
Log.i("Username", "" + username);
// starting new activity and expecting some response back
startActivityForResult(in, 100);
}
});
viewHolder.fillSpace = (TextView)postLayout.findViewById(R.id.posthelpSpace);
viewHolder.fillSpace.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
View relayout = lv.getChildAt(position);
viewHolder.unameTV = (TextView) relayout.findViewById(R.id.postUnameTv);
String username = viewHolder.unameTV.getText().toString();
Intent in = new Intent(getActivity(),
Profile.class);
// sending pid to next activity
in.putExtra("username", username);
Log.i("Username", "" + username);
// starting new activity and expecting some response back
startActivityForResult(in, 100);
}
});
viewHolder.image = (ImageView) postLayout.findViewById(R.id.postProfPic);
DisplayImageOptions options = initiateDisplayImageOptions();
viewHolder.image.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
View relayout = lv.getChildAt(position);
viewHolder.unameTV = (TextView) relayout.findViewById(R.id.postUnameTv);
String username = viewHolder.unameTV.getText().toString();
Intent in = new Intent(getActivity(),
Profile.class);
// sending pid to next activity
in.putExtra("username", username);
Log.i("Username", "" + username);
// starting new activity and expecting some response back
startActivityForResult(in, 100);
}
});
ImageLoader imageloader = ImageLoader.getInstance();
initImageLoader(getActivity());
imageloader.displayImage(viewContent.get(index + 1), viewHolder.image, options);
viewHolder.eventTitle = (TextView) postLayout.findViewById(R.id.postTitleTV);
viewHolder.eventTitle.setText(viewContent.get(index + 3));
testText = viewHolder.eventTitle;
viewHolder.eventImage = (ImageView) postLayout.findViewById(R.id.eventImage);
imageloader.displayImage(viewContent.get(index + 4), viewHolder.eventImage, options);
viewHolder.likesTV = (TextView) postLayout.findViewById(R.id.likesTV);
viewHolder.likesTV.setText("" + viewContent.get(index + 5));
viewHolder.planToAttendTV = (TextView) postLayout.findViewById(R.id.planToAttendTV);
viewHolder.planToAttendTV.setText(viewContent.get(index + 6));
viewHolder.addressTV = (TextView) postLayout.findViewById(R.id.postLocationTV);
viewHolder.addressTV.setText("" + viewContent.get(index + 7));
viewHolder.addressTV.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
}
});
viewHolder.addToCalendarButton = (TextView) postLayout.findViewById(R.id.addToCalendarButton);
viewHolder.addToCalendarButton.setText(viewContent.get(index + 2));
viewHolder.addToCalendarButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Calendar startCal = new GregorianCalendar();
startCal.setTime(new Date());
startCal.set(STARTYEARS.get(position), STARTMONTHS.get(position), STARTDAYS.get(position), STARTHOURS.get(position), STARTMINS.get(position));
Calendar endCal = new GregorianCalendar();
endCal.setTime(new Date());
endCal.set(ENDYEARS.get(position), ENDMONTHS.get(position), ENDDAYS.get(position), ENDHOURS.get(position), ENDMINS.get(position));
String id = Double.toString(EventUtils.assignName());
View relayout = lv.getChildAt(position);
viewHolder.eventTitle = (TextView) relayout.findViewById(R.id.postTitleTV);
title = viewHolder.eventTitle.getText().toString();
addEvent(id, title, startCal.getTimeInMillis(), endCal.getTimeInMillis(), position);
viewHolder.addToCalVF = (ViewFlipper) relayout.findViewById(R.id.addToCalendarVF);
viewHolder.addToCalVF.showNext();
Log.i("VIEWFLIPPER", viewHolder.addToCalVF.toString());
}
});
viewHolder.addToCalConfirm = (TextView) postLayout.findViewById(R.id.addToCalendarConfirmedButton);
viewHolder.addToCalConfirm.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
View relayout = lv.getChildAt(position);
viewHolder.addToCalVF = (ViewFlipper) relayout.findViewById(R.id.addToCalendarVF);
viewHolder.addToCalVF.showNext();
viewHolder.eventTitle = (TextView) relayout.findViewById(R.id.postTitleTV);
title = viewHolder.eventTitle.getText().toString();
DeleteCalendarEntry(EVENTIDS.get(position), title);
}
});
viewHolder.postImageVF = (ViewFlipper) postLayout.findViewById(R.id.postBackgroundHolderVF);
index = index + 8;
}
catch (IndexOutOfBoundsException ie)
{
ie.printStackTrace();
}
}
else
{
viewHolder = (ViewHolder) postLayout.getTag();
}
return postLayout;
}
我在适配器中有一个getCount和getItem方法,它正确地返回正确的数据。我看到在尝试在android中实现listview时会发生很多问题。我如何拥有一个稳定,可靠的列表视图适配器,每次都可以以相同的方式依赖该函数(我必须添加什么才能解决此问题)?