首页 > 解决方案 > Viewflipper 图像在使用动态数据时不会改变

问题描述

我正在制作广告横幅,我想在横幅布局中 4 秒后翻转图像,因此我正在使用 viewflipper。我想显示从 Web 服务获取的图像,因此我正在解析来自 Web 服务的图像并创建它的列表,但是当使用 for 循环将该列表项传递给 viewflipper 时,它只显示一个图像并且列表有两个项目。谁能帮我弄清楚我的错误。

查看脚蹼方法..

    public void flipperImages(String image){
    ImageView imageView = new ImageView(this);
    LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
    imageView.setLayoutParams(params);
    Log.e("image_product", image);
    if (image.equals("0")) {
        imageView.setBackgroundResource(R.drawable.placeholder_rec);
    } else {
        Picasso.with(this).load(image).error(R.drawable.placeholder_rec).placeholder(R.drawable.placeholder_rec).into(imageView);
    }


    v_flipper.addView(imageView);
    v_flipper.setFlipInterval(4000);
    v_flipper.setAutoStart(true);

    v_flipper.setInAnimation(this, android.R.anim.slide_in_left);
    v_flipper.setOutAnimation(this, android.R.anim.slide_out_right);

}

Api 调用和 FlipperImages() 调用...

      private void categoryListApi() {

    if (NetworkUtil.isNetworkAvailable(this)) {

        loader.setVisibility(View.VISIBLE);

        Api api = ApiClient.getClient().create( Api.class );
        Call<ResponseBody> call = api.getCatList("1");

        call.enqueue( new Callback<ResponseBody>() {
            @Override
            public void onResponse(Call<ResponseBody> call, Response<ResponseBody> response) {
                try {
                    loader.setVisibility(View.GONE);
                    String JSON_STRING = response.body().string();
                    JSONObject jsonRootObject = new JSONObject( JSON_STRING );

                    String status = jsonRootObject.getString( "status" );
                    String message = jsonRootObject.getString( "message" );

                    if (status.equals( "1" ))
                    {
                        JSONArray jsonArrayBanner = jsonRootObject.getJSONArray( "banner" );
                        for (int i=0; i<jsonArrayBanner.length(); i++)
                        {
                            JSONObject objectData = jsonArrayBanner.getJSONObject(i);
                            String bannerUrl = objectData.getString( "img" );
                            bannerList.add( bannerUrl );
                        }
                        Log.e("banner", bannerList.toString());
                        for (int i=0; i<bannerList.size(); i++)
                        {
                            flipperImages(bannerList.get(i));

                        }

                    } else {
                        loader.setVisibility(View.GONE);
                        Toast.makeText(getApplicationContext(), message, Toast.LENGTH_SHORT).show();
                    }

                } catch (Exception e) {
                    Log.e( "ErrorProfile" , e.getMessage());
                    loader.setVisibility(View.GONE);
                    popUpDialog();
                }
            }

            @Override
            public void onFailure(Call<ResponseBody> call, Throwable t) {
                loader.setVisibility(View.GONE);
                //Toast.makeText( getApplicationContext(), "Failed " + t, Toast.LENGTH_LONG ).show();
            }
        } );
    }
    else {
        popUpDialogInternet();
    }
}

标签: javaandroidimageviewviewflipper

解决方案


推荐阅读