将图像从URL加载到ViewPager

时间:2017-03-05 14:32:47

标签: android

我已经实施了PagerAdapter,但它显示了空白活动,这里是我的代码

Adapter.java



public class MarketingViewPagerAdapter extends PagerAdapter {
    private List<MarketingImagesList> marketingImagesLists;
    private LayoutInflater inflater;
    private Context context;


    public MarketingViewPagerAdapter(List<MarketingImagesList> marketingImagesLists,  Context context) {
        this.marketingImagesLists = marketingImagesLists;
        this.inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        this.context = context;
    }

    @Override
    public int getCount() {
        return marketingImagesLists.size();
    }

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

    @Override
    public Object instantiateItem(ViewGroup container, int position) {

        final MarketingImagesList listItem = marketingImagesLists.get(position);

        View itemView = inflater.inflate(R.layout.marketingswiper, container, false);

        ImageView imageView = (ImageView) itemView.findViewById(R.id.swipeImagesMarketing);

        Picasso.with(context).load(listItem.getFilePath())
                .into(imageView);

        container.addView(itemView);

        return itemView;
    }

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

的ImageList

public class MarketingImagesList {

    String filePath;

    public MarketingImagesList(String filePath) {
        this.filePath = filePath;
    }

    public String getFilePath() {
        return filePath;
    }


}

营销活动

public class MarketingActivity extends AppCompatActivity {

    private static final String URL_DATA = "http://cognoamobileapp.com/api/products";

//    private RecyclerView recyclerView;
//
//    private RecyclerView.Adapter adapter;

    private List<MarketingImagesList> listMarketingList;

    ViewPager viewPager;

    //MarketingAdapter adapter;

    MarketingViewPagerAdapter adapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_marketing);



        viewPager = (ViewPager) findViewById(R.id.marketing_viewPager);

        listMarketingList = new ArrayList<>();

        fetchImages();



    }

    private void fetchImages() {
        final ProgressDialog progressDialog = new ProgressDialog(this);
        progressDialog.setMessage("Loading presentations..");
        progressDialog.show();

        StringRequest stringRequest = new StringRequest(Request.Method.GET, URL_DATA,
                new Response.Listener<String>() {
                    @Override
                    public void onResponse(String response) {

                        progressDialog.dismiss();

                        try {
                            JSONObject jsonObject = new JSONObject(response);
                            JSONArray jsonArray = jsonObject.getJSONArray("products");

                            for (int i =0; i<jsonArray.length(); i++) {
                                JSONObject o = jsonArray.getJSONObject(i);

                                MarketingImagesList item = new MarketingImagesList(
                                    o.getString("filepath")
                                );

                                listMarketingList.add(item);
                            }

                            adapter = new MarketingViewPagerAdapter(listMarketingList, getApplicationContext());
                            viewPager.setAdapter(adapter);
                        } catch (JSONException e) {
                            e.printStackTrace();
                        }

                    }
                }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {
                progressDialog.dismiss();
                Toast.makeText(getApplicationContext(), error.getMessage(), Toast.LENGTH_LONG).show();
            }


        });

        RequestQueue requestQueue = Volley.newRequestQueue(this);


        requestQueue.add(stringRequest);

    }
}

1 个答案:

答案 0 :(得分:0)

您需要致电adapter.notifyDataSetChanged();