使用AsycTask查看寻呼机仅显示最后一页

时间:2018-05-23 22:08:46

标签: android arrays android-asynctask android-viewpager android-inflate

我的viewpager似乎加载了所有5个页面,但只有最后一个页面附加了图片和文字。在某些地方我似乎只能抓住我阵列中的最后一个位置。 我认为我可能在实例化方法中做错了,但我不太确定如何进一步提升这个位置。

public class CustomPagerAdapter extends PagerAdapter {

private Context context;
private LayoutInflater layoutInflater;
private ViewGroup container;
private int position;
private String[] businessNames;
private String[] businessSlogans;
private TypedArray[] businessAds;
private TypedArray[] businessLogos;

View item_view;

public CustomPagerAdapter (Context context){
    this.context = context;

}

@Override
public int getCount() {
    return 5;
}

@Override
public boolean isViewFromObject(@NonNull View view, @NonNull Object object) {
    return view == (object);
}

@NonNull
@Override
public Object instantiateItem(@NonNull ViewGroup container, int position) {
    this.container = container;
    this.position = position;

    VPTask vpTask = new VPTask();
    vpTask.execute(businessNames, businessSlogans, businessAds, businessLogos);

    View result = null;

    try {
        result = vpTask.get();
    } catch (InterruptedException e) {
    } catch (ExecutionException e) {
    }

    return result;
}

class VPTask extends AsyncTask<Object,View,View> {
    ImageView businessLogo;
    TextView businessName;
    TextView businessSlogan;
    ImageView adContent;
    String[] businessNames;
    String[] businessSlogans;
    TypedArray businessAds;
    TypedArray businessLogos;

    @Override
    protected void onPreExecute() {
        layoutInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        item_view = layoutInflater.inflate(R.layout.ad_view,container,false);
    }

    @Override
    protected View doInBackground(Object... params) {
        businessNames = (String[]) params[0];
        businessSlogans = (String[]) params[1];
        businessAds = (TypedArray) params[2];
        businessLogos = (TypedArray)params[3];

        businessNames = context.getResources().getStringArray(R.array.store_names);
        businessSlogans = context.getResources().getStringArray(R.array.store_slogans);
        businessAds = context.getResources().obtainTypedArray(R.array.ads);
        businessLogos = context.getResources().obtainTypedArray(R.array.ad_logo);

        return item_view;
    }

    @Override
    protected void onProgressUpdate(View... values) {

    }

    @Override
    protected void onPostExecute(View result) {
        businessLogo = item_view.findViewById(R.id.business_logo);
        businessName = item_view.findViewById(R.id.business_name);
        businessSlogan = item_view.findViewById(R.id.business_slogan);
        adContent = item_view.findViewById(R.id.ad_content);


        adContent.setImageDrawable(businessAds.getDrawable(position));
        businessName.setText(businessNames[position]);
        businessSlogan.setText(businessSlogans[position]);
        businessLogo.setImageDrawable(businessLogos.getDrawable(position));
        container.addView(result);
    }
}

@Override
public void destroyItem(@NonNull ViewGroup container, int position, @NonNull Object object) {
    container.removeView((ConstraintLayout)object);
}

}

我使用这个人代码作为一个想法,但代码没有弄清楚如何进行必要的修改。 How to use AsyncTask inside instantiateItem method of PagerAdapter

0 个答案:

没有答案