如何从服务器上获取ViewPager上的图像

时间:2017-09-27 05:41:22

标签: android android-viewpager

我想为图像实现一个应用程序,显示图像来自服务器端的位置,最多4个图像,假设服务器上只有一个或两个图像,那么寻呼机将显示额外的页面或滑块,如此图像{ {3}}。

service

用于滑块调用初始化函数。

public class BlankActivity extends FragmentActivity implements View.OnClickListener{ 
public static final String KEY_PIC = "pic";
public static final String KEY_PIC1 = "pic1";
public static final String KEY_PIC2 = "pic2";
public static final String KEY_PIC3 = "pic3";
public static final String JSON_ARRAY = "result";
String url1="https://www.maangal.com/photos/large_";
public String pic0="";
public String pic1="";
public String pic2="";
public String pic3="";
public String p0="";
public String p1="";
public String p2="";
public String p3="";
private ArrayList<String> ImagesArray = new ArrayList<>();
CustomTextView context; 

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_blank);
    mPager = (ViewPager) findViewById(R.id.pager);  
    getData();

getData函数我使用Volley响应从服务器获取图像

  private void init( String p0,String p1,String p2,String p3) {
        this.p0 = p0;
        this.p1 = p1;
        this.p2 = p2;
        this.p3 = p3;

    String[] IMAGES={p0,p1,p2,p3};
    for(int i=0;i<IMAGES.length;i++) {
        ImagesArray.add(IMAGES[i]);
        if (p0.equalsIgnoreCase("https://www.maangal.com/photos/large_"))
        {        Log.e("pic**********", url1 + IMAGES[i]);
            mPager.setVisibility(View.GONE);

    }
        else  Log.e("pic_______________", url1 + IMAGES[i]);

    }
    mPager = (ViewPager) findViewById(R.id.pager);
    mPager.setAdapter(new SlidingImage_Adapter(BlankActivity.this,ImagesArray));
    CirclePageIndicator indicator = (CirclePageIndicator) findViewById(R.id.indicator);
    indicator.setViewPager(mPager);
    final float density = getResources().getDisplayMetrics().density;
    //Set circle indicator radius
    indicator.setRadius(5 * density);
    NUM_PAGES = IMAGES.length;

    indicator.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
        @Override
        public void onPageSelected(int position) {
            currentPage = position;
        }
        @Override
        public void onPageScrolled(int pos, float arg1, int arg2) {
        }
        @Override
        public void onPageScrollStateChanged(int pos) {
        }
    });
}

SlidingImage_Adapter寻呼机适配器

 private void getData() {
    tv21.setText(getIntent().getExtras().getString("id"));
    id = tv21.getText().toString().trim();

    Bundle extras = getIntent().getExtras();
    if (extras != null) {
        id = extras.getString("ID");
        strGender = extras.getString("gender");
    } else {
        // handle case
    }
    String detailsURL ="https://www.maangal.com/maangal_mobile/details2.php?id="+id+"&gender="+strGender+"&curr_user_id="+current_Id;
    StringRequest stringRequest = new StringRequest(Request.Method.POST,detailsURL, new Response.Listener<String>() {
        @Override
        public void onResponse(String response) {
                showJSON(response);
        }
    },
            new Response.ErrorListener() {
                @Override
                public void onErrorResponse(VolleyError error) {
                    Toast.makeText(BlankActivity.this,error.getMessage().toString(),Toast.LENGTH_LONG).show();
                }
            });

    RequestQueue requestQueue = Volley.newRequestQueue(BlankActivity.this);
    requestQueue.add(stringRequest);
}

private void showJSON(String response){
try {
        JSONObject jsonObject = new JSONObject(response);
        JSONArray result = jsonObject.getJSONArray(Config.JSON_ARRAY);
        JSONObject userData = result.getJSONObject(0);

        pic0 = userData.getString(KEY_PIC);
        pic1 = userData.getString(KEY_PIC1);
        pic2 = userData.getString(KEY_PIC2);
        pic3 = userData.getString(KEY_PIC3);

        init(url1+pic0,url1+pic1,url1+pic2,url1+pic3);
 }

2 个答案:

答案 0 :(得分:1)

您需要检查其他网址是否为空,然后您不需要添加到图像阵列中。使用以下代码更改您的代码并检查。

 String[] IMAGES={p0,p1,p2,p3};
 for(int i=0;i<IMAGES.length;i++) {
      if(!IMAGES[i].isEmpty()) {
           ImagesArray.add(IMAGES[i]);
      }
      if (p0.equalsIgnoreCase("https://www.maangal.com/photos/large_"))
      {
            Log.e("pic**********", url1 + IMAGES[i]);
            mPager.setVisibility(View.GONE);
       }
  }

答案 1 :(得分:0)

我已经完成了,如果有图像那么数组将是ImagesArray.add(url1 + IMAGES [i]);如果没有图像,则不会在arrayList上添加任何内容

String[] IMAGES = {p0, p1, p2, p3};
    for (int i = 0; i < IMAGES.length; i++) {
        if (IMAGES[i].equalsIgnoreCase("") || IMAGES[i].equalsIgnoreCase("nophoto.jpg") || IMAGES[i].equalsIgnoreCase("https://www.maangal.com/photos/large_")) {
            Log.e("piccc-----------", IMAGES[i]);
        } else {
            ImagesArray.add(url1 + IMAGES[i]);
            Log.e("else", url1 + IMAGES[i]);
        }
    }