简体   繁体   中英

Android - Java how to check which class constructor is called

I have the following class which i use to slide show images pay using the sliderAdapter constructor and passing a List of Strings and its work just fine.

public class sliderAdapter extends PagerAdapter {



    private Context context;

    private List<String> URLs = new ArrayList<>();
    sliderAdapter(Context context, List<String> URLs){
        this.URLs = URLs;
        this.context = context;
    }

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

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

    @Override
    public Object instantiateItem(ViewGroup container, int position) {
        LayoutInflater layoutInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        View view = layoutInflater.inflate(R.layout.slideshow_layout,container,false);
        ImageView img = view.findViewById(R.id.imageview);
        Glide.with(context).load(URLs.get(position)).into(img);



        container.addView(view);
        return view;
    }

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

}

Now i need to improve my slider by adding the ability of load the images from drawable folder so i have to pass an List of Integers , Which i don't know the exact way to do it

As far as i thought i should make new constructor that accept List of Integers but what about all the other functions, how should i change my class to work properly with both constructors ?

Any ideas will be much appreciated

You can do something like this:

public class Item{
   String url;
   int id;
   boolean link = false;
   boolean resource = false;
   public Item(int id){
       this.id = id;
       resource = true;
   }
   public Item(String url){
       this.url = url;
       link = true;
   }
   public boolean isLink(){
       return link;
   }
   public boolean isResource(){
       return resource;
   }
   public int getId(){
       return id;
   }
   public String getUrl(){
       return url;
   }
}



public class sliderAdapter extends PagerAdapter {

    private Context context;

    private List<Item> items = new ArrayList<>();
    sliderAdapter(Context context, List<Item> items){
        this.items = items;
        this.context = context;
    }

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

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

    @Override
    public Object instantiateItem(ViewGroup container, int position) {
        LayoutInflater layoutInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        View view = layoutInflater.inflate(R.layout.slideshow_layout,container,false);
        ImageView img = view.findViewById(R.id.imageview);
        Item item = items.get(position);
        if(item.isLink()){
            Glide.with(context).load(item.getUrl()).into(img);
        }else{
            img.setImageResource(item.getId());
        }

        container.addView(view);
        return view;
    }

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

In this example, instead of passing two different lists to the adapter ( List<String> or List<Integer> ) I've changed it to accept a list of Item objects. As you can see, the Item object can hold an URL or a drawable resource ID. In the adapter I'm checking each item to see what kind of item it is so I can use the appropriate method to set the ImageView 's image.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM