横向方向:更改imageview src(使用Picasso)

时间:2016-05-23 08:43:09

标签: android imageview orientation picasso landscape

我正在使用自定义数组适配器并在每行中显示图像和一些文本(来自电影数据库API的JSON)。当我更改设备的方向时,列表会再次加载,所有内容都会正确显示,即使我没有在代码中的任何位置指定横向方向。到目前为止,太棒了!

我的问题是,在横向中,我想仅更改我正在加载的图片(使用Picasso,因为它是图片网址)。我希望其他一切保持原样。

我怎样才能达到同样的目标?毕加索本身是否有能够解决这个问题的功能?

如果需要一些代码,请告诉我。谢谢!

2 个答案:

答案 0 :(得分:1)

您应该检测设备何时更改方向 然后,您应该根据方向和notifyDataSetChanged

更新列表数据源
@Override
public void onConfigurationChanged(Configuration newConfig) {
    super.onConfigurationChanged(newConfig);

    // Checks the orientation of the screen
    if (newConfig.orientation == Configuration.ORIENTATION_LANDSCAPE) {
        // change all image path in listdata to landscape path

        // for example
        // for (Item item : listData) {
        //     item.setDisplayImagePath(landscapePath);
        // } 
    } else if (newConfig.orientation == Configuration.ORIENTATION_PORTRAIT){
        // change all image path in listdata to portrait path
    }

    youListViewAdapter.notifyDataSetChanged(); 
}

希望这个帮助

答案 1 :(得分:0)

你可以尝试这段代码:

    Display display = ((WindowManager) getApplicationContext().getSystemService(Context.WINDOW_SERVICE)).getDefaultDisplay();
    int rotation = display.getRotation();
    if (rotation != 0) {
        Picasso.with(getApplicationContext()).load(your_url).into(your_imageview);
    }

它检测屏幕是否旋转,display.getRotation返回一个int(如果没有任何旋转则等于0),从那里你可以使用Picasso来改变你的imageview。

干杯,

相关问题