Android ListView在正常工作之前会回到顶部几秒钟

时间:2018-05-17 01:07:55

标签: android image list listview imageview

我正在尝试使用包含国家标志和名称的ListView创建应用程序。这样,用户可以点击它们并显示他们之前拍摄的国家的图像。然而,如果我尝试滚动列表视图加载时大约3秒钟将会出现故障并将我发送回顶部。这是代码..

public class CountriesListAdapter extends ArrayAdapter {
    private int resource;
    private LayoutInflater inflater;
    private List<CountryModel> countryModels;
    private WeakReference<TextView> selectedCountryIdtxt;
    private boolean useFilter;
    private WeakReference<ProgressBar> progressBarWeakReference;

    public int getSelectedCountryId() {
        return selectedCountryId;
    }

    public void setSelectedCountryId(int selectedCountryId) {
        this.selectedCountryId = selectedCountryId;
    }

    private int selectedCountryId;

    public CountriesListAdapter(@NonNull WeakReference<Context> context, int resourceId, WeakReference<TextView> textView, @NonNull List<CountryModel> countryModelList, boolean useFilter, WeakReference<ProgressBar> progressBarWeakReference){
        super(context.get(), resourceId, countryModelList);
        selectedCountryIdtxt = textView;
        resource = resourceId; //the id of the template file
        inflater = LayoutInflater.from(context.get());
        this.countryModels = countryModelList;
        selectedCountryId = -1;
        this.useFilter = useFilter;
        this.progressBarWeakReference = progressBarWeakReference;
    }

    public int getCount() {
        if (countryModels != null)
            return countryModels.size();
        return 0;
    }

    public CountryModel getItem(int position) {
        if (countryModels != null)
            return countryModels.get(position);
        return null;
    }

    public long getItemId(int position) {
        if (countryModels != null)
            return countryModels.get(position).hashCode();
        return 0;
    }

    @NonNull
    @Override
    public View getView(int position, @Nullable View convertView, @NonNull ViewGroup parent) {
    // this method is automatically called for every object in our list
    //basically it's called for every single row before it is generated
    // this method is called per row
        convertView = (ConstraintLayout) inflater.inflate(resource, null);

    //the variable countryModel is fiiled with current object that is being processed
        final CountryModel countryModel = countryModels.get(position);
        TextView countryName = convertView.findViewById(R.id.countryNamelbl);
        final ImageView countryFlag = convertView.findViewById(R.id.countryFlagimg);
        final ImageView checked = convertView.findViewById(R.id.countryCheckedimg);

    //this is done for every object in the list
        assert countryModel != null;
        countryName.setText(countryModel.getName());
        Picasso.get().load(countryModel.getImage()).fit().into(countryFlag);
        if(!useFilter) {
            if (selectedCountryId == countryModel.getId()) {
                checked.setVisibility(View.VISIBLE);
            } else {
            checked.setVisibility(View.INVISIBLE);
            }
        }

        convertView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if(!useFilter) {
                    if (checked.getVisibility() == View.VISIBLE) {
                        checked.setVisibility(View.INVISIBLE);
                        selectedCountryId = -1;
                            selectedCountryIdtxt.get().setText(String.valueOf(selectedCountryId));
                    } else {
                        if (selectedCountryId == -1) {
                            checked.setVisibility(View.VISIBLE);
                            selectedCountryId = countryModel.getId();
                        } else {
                            selectedCountryId = countryModel.getId();
                            notifyDataSetChanged();
                        }
                    selectedCountryIdtxt.get().setText(String.valueOf(selectedCountryId));
                    }
                } else {
                    Intent i = new Intent(getContext(), PicturesActivity.class);
                    i.putExtra("countryId",countryModel.getId());
                    i.putExtra("countryName", countryModel.getName());
                    getContext().startActivity(i);
               }
           }
       });
       progressBarWeakReference.get().setVisibility(View.INVISIBLE);
       return convertView;
    }

    public List<CountryModel> getCountryModels() {
        return countryModels;
    }

    public void setCountryModels(List<CountryModel> countryModels) {
        this.countryModels = countryModels;
    }
}

1 个答案:

答案 0 :(得分:0)

问题实际上是在另一个类中,我为每个列表项调用适配器而不只是一次... oops。 谢谢你的回复!