简体   繁体   English

Android图标/图像设置模式可进行多种选择

[英]Android Icon/Image setting pattern for multiple choices

What pattern do I have to use, if I have ListView in which ImageView and like 500 different icons that could be set on that ImageView . 如果我具有ImageView ListView以及可以在该ImageView上设置的500个不同的图标,那么我必须使用哪种模式。 Should I just write If/Switch statement, or there is another way/pattern to do it?. 我应该只编写If / Switch语句,还是有另一种方式/模式来执行此操作? Thanks in advance! 提前致谢!

Where are these icons that you want to set? 您要在哪里设置这些图标? you are getting them from server or they are stored locally in your application file? 您是从服务器获取它们还是将它们本地存储在您的应用程序文件中? or they are from user phone gallery? 还是来自用户电话库?

Here is the code you want for your adapter: 这是您想要的适配器代码:

public class MyAdapter extends BaseAdapter {
    private Context mContext;
    private LayoutInflater mInflater;
    private ArrayList<String> mIconNames;

    public MyAdapter(Context context) {
        mContext = context;
        mIconNames = getIconNames();
        mInflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    }

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

    @Override
    public Object getItem(int position) {
        return mIconNames.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        // Get view for row item
        View rowView = mInflater.inflate(R.layout.your_layout, parent, false);
        ImageView thumbnailImageView =
                (ImageView) rowView.findViewById(R.id.your_image_view_id);
        Picasso.with(mContext).load(mIconNames.get(position)).placeholder(R.mipmap.ic_launcher).into(thumbnailImageView);
        return rowView;
    }

    //this method builds your icon names
    private ArrayList<String> getIconNames() {
        ArrayList<String> iconNames = new ArrayList<>();
        int numberOfIcons = 99;
        String iconBaseName = "icon";
        for (int i = 1; i < numberOfIcons; i++) {
            iconNames.add(iconBaseName + i);
        }

        return iconNames;
    } 

}

Let me assume that you know what icon(I mean the name of icon) to be loaded into the imageView and those icons are available in your drawable resource folder. 让我假设您知道要加载到imageView中的图标(我的意思是图标的名称),并且这些图标在可绘制资源文件夹中可用。 In this case 在这种情况下

@Override
public void onBindViewHolder(final RecyclerAdapter.ViewHolder holder, int position) {
    DataItem dataItem = dataList.get(holder.getAdapterPosistion());
    try {
        int resID = activityContext.getResources().getIdentifier(dataItem.getIconName() , "drawable"/**resource folder name*/, activityContext.getPackageName());
        holder.imageView.setBackgroundResource(resID);
    } catch (Exception e) {
        throw new RuntimeException("Error getting Resource ID.", e)
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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