我有一个奖励部分,我从服务器加载数据。
我从服务器中提取了8个奖项并将其加载到我的ListView
中。奖品可以出售,在我的适配器中,我有一个检查器,如果quantity < 1
将有一个条形图,将通知用户奖励已售出。
存在延迟加载,用户可以通过增加索引来滑动以加载更多详细信息,因此服务器可以确定应该返回哪8个奖励。 1分为前8分,2分为第2分等。
问题是,当我滚动到第一个销售奖项时,ListView变得奇怪,所有奖项都会在他们的图像上获得销售额。这是活动代码:
public class AwardListFragment extends Fragment { int offset = 0;
int size = 8;
private int ID;
private boolean _isAwardsLoaded = false;
private SwipyRefreshLayout swipyRefreshLayout;
private ProgressDialog progressBar;
private ArrayList<Awards> listAwards;
ListView awardlist;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup parent, Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.rewards_section, parent, false);
awardlist = (ListView) rootView.findViewById(R.id.awardlist);
awardlist.setItemsCanFocus(true);
listAwards = new ArrayList<>();
swipyRefreshLayout = (SwipyRefreshLayout) rootView.findViewById(R.id.swipe_section_swipe);
awardlist.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Intent intent = new Intent(getActivity(), AwardDetailsScreen.class);
ID = listAwards.get(position).getId();
Bundle bundle = new Bundle();
bundle.putInt("ID", ID);
intent.putExtras(bundle);
startActivity(intent);
}
});
return rootView;
}
@Override
public void onViewCreated(View view, Bundle savedInstanceState) {
}
public void showlist() {
progressBar = new ProgressDialog(getActivity());
progressBar.setMessage("Pls Wait...");
progressBar.show();
final int firstitemposition = 0;
final int currentposition = awardlist.getFirstVisiblePosition();
NetworkSDK.getInstance().getAwards(size, offset, new Callback<List<Awards>>() {
@Override
public void onResponse(Call<List<Awards>> call, Response<List<Awards>> response) {
if (response.code() == 401) {
Intent intent = new Intent(getContext(), MainPreLogin.class);
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intent);
SharedData.getInstance().removeString("token");
} else {
if (response.isSuccess()) {
if (response.body().size() == 0) {
Toast.makeText(getContext(), "All awards loaded", Toast.LENGTH_SHORT).show();
progressBar.setCancelable(false);
progressBar.dismiss();
} else
for (int i = 0; i < response.body().size(); i++)
listAwards.add(response.body().get(i));
AwardsAdapter awardsAdapter = new AwardsAdapter(listAwards);
awardlist.setAdapter(awardsAdapter);
awardsAdapter.notifyDataSetChanged();
awardlist.setSelectionFromTop(currentposition, firstitemposition);
}
progressBar.setCancelable(false);
progressBar.dismiss();
}
}
@Override
public void onFailure(Call<List<Awards>> call, Throwable t) {
Toast.makeText(getContext(), R.string.errorNoconnection, Toast.LENGTH_LONG).show();
progressBar.dismiss();
}
});
}
@Override
public void setUserVisibleHint(boolean isVisibleToUser) {
super.setUserVisibleHint(isVisibleToUser);
if (isVisibleToUser && !_isAwardsLoaded) {
swipyRefreshLayout.setOnRefreshListener(new SwipyRefreshLayout.OnRefreshListener() {
@Override
public void onRefresh(SwipyRefreshLayoutDirection direction) {
if (direction == SwipyRefreshLayoutDirection.BOTTOM) {
offset++;
swipyRefreshLayout.setRefreshing(false);
showlist();
}
}
showlist();
});
}
}
}
这是我的适配器。
public class AwardsAdapter extends BaseAdapter {
ArrayList<Awards> awards;
public AwardsAdapter(ArrayList<Awards> awards) {
this.awards = awards;
}
public void clearData() {
// clear the data
awards.clear();
}
@Override
public int getCount() {
return awards.size();
}
@Override
public Object getItem(int position) {
return awards.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View view = null;
Integer identity;
ViewHolder viewHolder = null;
if (convertView == null) {
view = LayoutInflater.from(parent.getContext()).inflate(R.layout.award_item, parent, false);
viewHolder = new ViewHolder(view);
view.setTag(viewHolder);
} else {
view = convertView;
viewHolder = (ViewHolder) view.getTag();
}
Awards awards = (Awards) getItem(position);
if (awards != null) {
identity = awards.getId();
viewHolder.name.setText(awards.getName().toUpperCase());
viewHolder.price.setText("Money amount: " + awards.getPriceAmount().toString() );
viewHolder.points.setText("Points amount :" + awards.getCreditAmount().toString());
if (awards.getImagePath().isEmpty())
Picasso.with(view.getContext()).load(R.drawable.placeholder).fit().centerCrop().into(viewHolder.picture);
else
Picasso.with(view.getContext()).load(awards.getImagePath()).fit().centerCrop().into(viewHolder.picture);
if (awards.getQuantity()<1)
Picasso.with(view.getContext()).load(R.drawable.sold).into(viewHolder.checker);
else
if (awards.getIsVip())
Picasso.with(view.getContext()).load(R.drawable.vip).into(viewHolder.checker);
}
return view;
}
private class ViewHolder {
TextView name;
TextView price;
TextView points;
ImageView picture;
ImageView checker;
public ViewHolder(View view) {
this.name = (TextView) view.findViewById(R.id.award_name);
this.price = (TextView) view.findViewById(R.id.award_price);
this.picture = (ImageView) view.findViewById(R.id.award_picture);
this.points = (TextView) view.findViewById(R.id.award_points);
this.checker=(ImageView)view.findViewById(R.id.checker);
}
}
}
答案 0 :(得分:1)
在你的适配器getView()
中,你有这段代码:
if (awards.getQuantity()<1)
Picasso.with(view.getContext()).load(R.drawable.sold).into(viewHolder.checker);
else
if (awards.getIsVip())
Picasso.with(view.getContext()).load(R.drawable.vip).into(viewHolder.checker);
将其更改为类似的内容(这取决于您希望如何显示/隐藏您的视图。就目前而言,您不会处理需要隐藏它的情况!):
if (awards.getQuantity()<1) {
Picasso.with(view.getContext()).load(R.drawable.sold).into(viewHolder.checker);
}
else {
// hide viewHolder.checker here
}
if (awards.getIsVip()) {
Picasso.with(view.getContext()).load(R.drawable.vip).into(viewHolder.checker);
}
注意:作为一般建议,请始终使用大括号来避免此类错误和混淆。