繁体   English   中英

如何动态引用Android中R.drawable文件夹中的资源?

[英]How to dynamically reference resources in the R.drawable folder in Android?

我的适配器有以下代码:

@Override
public void onBindViewHolder(GameViewHolder holder, int position) {
    final Games game = gameList.get(position);
    holder.awayTeamImageView.setBackgroundResource(R.drawable.fortyers);
}

以上工作完美,但我正在硬编码将显示的图像。 我真正需要的是从游戏列表中获取背景图像,我正在寻找这样的事情:

 holder.awayTeamImageView.setBackgroundResource(R.drawable.game.getaBackground());

但这会导致错误

如何动态设置 imageView 的背景资源?

更新:

我附上了所需效果的屏幕截图。

每周日程都会发生变化,因此列表将始终根据所选周而有所不同。

在此处输入图像描述

游戏构造函数:

public Games(DataSnapshot game) {

    this.AwayTeam = game.child("AwayTeam").getValue().toString();
    this.AwayId = Integer.parseInt(game.child("AwayId").getValue().toString());

    this.HomeTeam = game.child("HomeTeam").getValue().toString();
    this.HomeId = Integer.parseInt(game.child("HomeId").getValue().toString());

    this.aBackground = game.child("aBackground").getValue().toString();
    this.hBackground = game.child("hBackground").getValue().toString();

}

创建一个包含可绘制对象 id 的字符串列表,使它们与 position 同步,然后根据列表中的 position 获取 id,以便在设置可绘制对象时通过。

如果它在多个地方重复使用并且顺序一致,您还可以 go 为枚举获取根据您选择的某个键的可绘制 ID。 如果它真的依赖于 position,您可能需要根据 position 使用 when 条件来计算密钥。

希望有效!

假设Games class 应该是模型/数据 class; 因此您可以拥有一个 int 成员字段,该字段可以存储此 class 的每个实例的可绘制 ID,并创建 getter 和 setter:

public class Games {

    private int myDrawable;

    public int getMyDrawable() {
        return myDrawable;
    }

    public void setMyDrawable(int myDrawable) {
        this.myDrawable = myDrawable;
    }
    
}

每当您构造Games对象时,都可以使用setMyDrawable(R.drawabe.foo)设置可绘制图像 id。也许您可以修改Games构造函数以接受可以将myDrawable设置为的 int 参数。

然后在 RecyclerView 中:

@Override
public void onBindViewHolder(GameViewHolder holder, int position) {

    final Games game = gameList.get(position);
    holder.awayTeamImageView.setBackgroundResource(game.getMyDrawable());

}

更新:

public class Games {

    private int homeTeamImage, awayTeamImage;

    public int getHomeTeamImage() {
        return homeTeamImage;
    }

    public void setHomeTeamImage(int homeTeamImage) {
        this.homeTeamImage = homeTeamImage;
    }


    public int getAwayTeamImage() {
        return awayTeamImage;
    }

    public void setAwayTeamImage(int awayTeamImage) {
        this.awayTeamImage = awayTeamImage;
    }

    public Games(DataSnapshot game, int homeTeamImage, int awayTeamImage) {

        //... rest of code

        this.homeTeamImage = homeTeamImage;
        this.awayTeamImage = awayTeamImage;

    }

}

But in terms of OOP, I'd suggest that you can have a Team class that has either team properties, like image, name, .... etc. and the Games class can be the controller of the game, not building the team实例。

不要把事情弄得太复杂。 只需使用一个简单的方法。 使用HashMap应该没问题。

Map<String, Integer> map = new HashMap<String, Integer>();
map.put("Cincinnati", R.drawable.red_striped_helmet);
map.put("Cleveland", R.drawable.red_helmet);
map.put("New York", R.drawable.blue_helmet);
map.put("Chicago", R.drawable.dark_blue_helmet);
map.put(homeTeam, homeId);
...
holder.awayTeamImageView.setBackgroundResource(map.get("New York"));
// holder.awayTeamImageView.setBackgroundResource(map.get(homeTeam));

另一种可能对您有所帮助的方法是使用字符串中的getIdentifier 为此,您将背景字符串名称与资源中的名称相同。 例如,如果你想显示ic_menu_camera那么你的aBackground应该是相同的字符串。

示例游戏 class:

public class Game {

    String aBackground;

    public Game(String aBackground) {
        this.aBackground = aBackground;
    }

    public String getaBackground() {
        return aBackground;
    }

    public void setaBackground(String aBackground) {
        this.aBackground = aBackground;
    }
}

然后像这样使用它。 阅读内联注释以详细了解。

//set the background name that you want same as name in your drawable folder
        // without any extension .png or .xml. Just name should be there
        Game game = new Game("ic_menu_camera");
        ImageView imageView = findViewById(R.id.imageView);
        //get the id by name using this
        /*  @param name The name of the desired resource.
             * @param defType Optional default resource type to find, if "type/" is
             *                not included in the name.  Can be null to require an
             *                explicit type.
             * @param defPackage Optional default package to find, if "package:" is
             *                   not included in the name.  Can be null to require an
             *                   explicit package.
             * 
             * @return int The associated resource identifier.  Returns 0 if no such
             *         resource was found.  (0 is not a valid resource ID.)
             * */
        int resId = getResources().getIdentifier(game.getaBackground(), "drawable",getPackageName());
        // then set that id to your image
        imageView.setBackgroundResource(resId);

暂无
暂无

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

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