适配器在Android

时间:2018-04-10 05:53:43

标签: android android-fragments android-viewpager android-runonuithread

我有一个在Fragment中管理适配器的错误。我的方案是:在活动中,有TabLayoutViewPager用于创建标签转换。

在每个片段中都有一个RecyclerViewAdapter。实际上,我们不需要使用runOnUiThread,但在这些方法中,我们需要runOnUiThread来管理视图。

   navinAsync.getImageGalleries(paramsMap, new RequestListener() {
        @Override
        public void onResponse(final String response) {
            hideProgressBarScreenShots();


            getActivity().runOnUiThread(new Runnable() {
                @Override
                public void run() {
                    try {
                        String message=parseAdapterMessage(response);
                        productScreenShots = getAllAppProductScreenShots(message);
                        LinearLayoutManager layoutManager
                                = new LinearLayoutManager(getActivity(), LinearLayoutManager.HORIZONTAL, false);
                        app_images.setLayoutManager(layoutManager);
                        app_images.setHasFixedSize(true);
                        ApplicationImagesRecyclerListAdapter listAdapter = new ApplicationImagesRecyclerListAdapter(
                                getActivity(), productScreenShots);
                        app_images.setAdapter(listAdapter);
                        app_images.setFocusable(true);

                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                }
            });




        }

        @Override
        public void onError(String response) {
            hideProgressBarScreenShots();
            Log.e("","");



        }
    });

public List<ProductScreenShot> getAllAppProductScreenShots(String data) {


    List<ProductScreenShot> productScreenShotList = new ArrayList<>();

    try {

        JSONArray array = new JSONArray(data);


        for (int i = 0; i < array.length(); i++) {
            JSONObject jsonObject = array.getJSONObject(i);
            ProductScreenShot productScreenShot = new ProductScreenShot();


            productScreenShot.setProductPackageName(jsonObject.getString("product_packageName"));
            productScreenShot.setScreenShoturl(jsonObject.getString("imageUrl"));
            productScreenShot.setThumbnailUrl(jsonObject.getString("thumbnailURL"));

            productScreenShotList.add(productScreenShot);

        }


    } catch (JSONException e) {
        e.printStackTrace();
    } catch (Exception e) {
        e.printStackTrace();
    }
    return productScreenShotList;
}

和适配器:

public class ApplicationImagesRecyclerListAdapter extends DefaultRecyclerViewAdapter<ApplicationImagesRecyclerListAdapter.ViewHolder> {

    private List<ProductScreenShot> productScreenShots;
    private Context context;
    private ViewPager viewPager;
    public ApplicationImagesRecyclerListAdapter(Context context, List<ProductScreenShot> productScreenShots , ViewPager viewPager) {
        super(context);
        this.context = context;
        this.productScreenShots = productScreenShots;
        this.viewPager = viewPager;
    }

    public ApplicationImagesRecyclerListAdapter(Context context, List<ProductScreenShot> productScreenShots) {
        super(context);
        this.context = context;
        this.productScreenShots = productScreenShots;
    }


    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(context).inflate(R.layout.application_image, null);
        return new ViewHolder(view);
    }

    @Override
    public void onBindViewHolder(ViewHolder holder, final int position) {

        ProductScreenShot productScreenShot=productScreenShots.get(position);

        ImageView appImage = holder.appImage;
        Designer designer = new Designer(context);
        designer.setViewBackground(appImage, designer.getLightGraySelector(), true, BasicDesigner.RECTANGLE, 0);
        if (productScreenShot.getScreenShoturl().equals("null") || productScreenShot.getScreenShoturl() == null) {
            appImage.setImageResource(R.mipmap.ic_launcher);
        } else {
            Picasso.with(context).load(productScreenShot.getScreenShoturl()).error(R.mipmap.ic_launcher).into(appImage);
        }


    }

    @Override
    public int getItemCount() {
        return productScreenShots.size();
    }


    public class ViewHolder extends RecyclerView.ViewHolder {
        ImageView appImage;

        public ViewHolder(View itemView) {
            super(itemView);
            appImage = (ImageView) itemView.findViewById(R.id.app_image);
        }
    }
}

这些代码有时会起作用,有时也不起作用,

使用片段中的适配器运行runOnUiThread有什么问题?

0 个答案:

没有答案