TL; DR - 我有一个数组适配器,由User对象的ArrayList填充, 我想打印他们的订单号 - 第1,第2,第3 ......等 - 快速来回滚动..这些数字混合在一起......
在快速来回滚动之后而不是1,2,3,4我得到1,2,9,4例如..
我不明白为什么会发生这种情况,我已经尝试了几种方法来解决它但没有成功,我甚至从视图持有者那里获取了该文本视图,看看它是否有帮助但是没有成功太
这是我的适配器:
public class HomeHallOfFameAdapter extends ArrayAdapter<User> {
private HomeFragment fragment;
private ArrayList<User> data;
int page;
public HomeHallOfFameAdapter(Context context, ArrayList<User> data, HomeFragment fragment, int page) {
super(context, 0);
this.data = data;
this.fragment = fragment;
this.page = page;
}
@Override
public int getCount() {
return data != null ? data.size() : 0;
}
@Override
public User getItem(final int position) {
return data.get(position);
}
@Override
public long getItemId(final int position) {
//only used for adapters connected to a cursor.
return position;
}
@Override
public View getView(final int position, View convertView, ViewGroup parent) {
MyHolder holder = null;
final User user = data.get(position);
//initiallize the views
if (convertView == null) {
convertView = LayoutInflater.from(getContext()).inflate(R.layout.home_frag_hall_of_fame_item, null);
holder = new MyHolder();
holder.profileImage1 = (ImageView) convertView.findViewById(R.id.home_hallofame_image);
convertView.setTag(holder);
} else {
holder = (MyHolder) convertView.getTag();
}
final TextView titleRank = (TextView) convertView.findViewById(R.id.home_hallofame_title);
final String numberEnding = MainScreenActivity.getInstance().NumberWithSuffix(position+1);
titleRank.setText(numberEnding);
loadImage(user.getImages().get(0), holder.profileImage1);
return convertView;
//return super.getView(position, convertView, parent);
}
private class MyHolder {
//TextView titleRank;
ImageView profileImage1; //fameBlueBtn;
}
}
问题是titleRank textView ..
NumberWithSuffix只是一个实现结尾数字的方法:
//method to get the currect number endings.
public String NumberWithSuffix(int number) {
String outputNumber = "";
String numberString = String.valueOf(number);
if (String.valueOf(number).endsWith("11")) {
outputNumber = numberString + "th";
} else if (String.valueOf(number).endsWith("12")) {
outputNumber = numberString + "th";
} else if (String.valueOf(number).endsWith("13")) {
outputNumber = numberString + "th";
} else if (String.valueOf(number).endsWith("1")) {
outputNumber = numberString + "st";
} else if (String.valueOf(number).endsWith("2")) {
outputNumber = numberString + "nd";
} else if (String.valueOf(number).endsWith("3")) {
outputNumber = numberString + "rd";
} else {
outputNumber = numberString + "th";
}
return outputNumber.trim();
}
知道我的问题是什么?
感谢您的帮助。
答案 0 :(得分:1)
你有这个
MainScreenActivity.getInstance()
我假设你在这里实例化一个Activity类。我建议你将NumberWithSuffix(int number)
移动到适当的Utility类,使方法静态,你可以在其他地方使用相同的。您的所有实用程序都不应该在Activity中。还要注意保持对活动的引用并避免内存泄漏
你的物品位置似乎很好。我测试了你的样品,它工作正常。
您可以进行一些更改,以遵循以下
的视图模式@Override
public View getView(final int position, View convertView, ViewGroup parent) {
MyHolder holder = null;
final User user = data.get(position);
//initiallize the views
if (convertView == null) {
convertView = LayoutInflater.from(getContext()).inflate(R.layout.home_frag_hall_of_fame_item, null);
holder = new MyHolder();
holder.profileImage1 = (ImageView) convertView.findViewById(R.id.home_hallofame_image);
holder.titleRank = (TextView) convertView.findViewById(R.id.home_hallofame_title);
convertView.setTag(holder);
} else {
holder = (MyHolder) convertView.getTag();
}
final String numberEnding = Utility.NumberWithSuffix(position+1);
holder.titleRank.setText(numberEnding);
loadImage(user.getImages().get(0), holder.profileImage1);
return convertView;
}
ViewHolder
private static class MyHolder {
TextView titleRank;
ImageView profileImage1; //fameBlueBtn;
}
创建一个实用程序类
public class Utility {
public static String NumberWithSuffix(int number) {
String outputNumber = "";
String numberString = String.valueOf(number);
if (String.valueOf(number).endsWith("11")) {
outputNumber = numberString + "th";
} else if (String.valueOf(number).endsWith("12")) {
outputNumber = numberString + "th";
} else if (String.valueOf(number).endsWith("13")) {
outputNumber = numberString + "th";
} else if (String.valueOf(number).endsWith("1")) {
outputNumber = numberString + "st";
} else if (String.valueOf(number).endsWith("2")) {
outputNumber = numberString + "nd";
} else if (String.valueOf(number).endsWith("3")) {
outputNumber = numberString + "rd";
} else {
outputNumber = numberString + "th";
}
return outputNumber.trim();
}
}
也代替了这个
final User user = data.get(position);
您可以使用
final User user = getItem(position);
因为你已经
了@Override
public User getItem(final int position) {
return data.get(position);
}
答案 1 :(得分:0)
我会选择带有2个数据集的BaseAdapter(或者你需要更多数据集),然后在getView()的数据集中按类型混合......
答案 2 :(得分:0)
所以..我已经解决了这个问题.. 我的代码没问题,正如我所提到的,它在垂直列表视图中工作正常,但这个问题发生在水平列表视图中。 我删除了我正在使用的列表视图github库并切换到Horizontal Recycler View ..它现在很好用!