【问题标题】:While changing the orientation list item are repeating更改方向列表项时重复
【发布时间】:2015-03-17 18:46:44
【问题描述】:

当我更改应用程序的方向时,应用程序中的列表项会重复,并且许多项目在横向模式下不可见。

源代码:https://github.com/amar0891/Aroma-Holiday.git

由于我的声誉较低,很抱歉没有发布预期输出的图像。

结果输出

【问题讨论】:

  • 您需要横向模式吗?您可以阻止它以横向模式显示。

标签: android android-layout android-fragments


【解决方案1】:

我认为您得到意外结果的原因是您在 if(convertView == null) 块内执行视图修改。如果您按以下方式提取代码,我认为它会为您解决问题:

if(convertView == null) {
    convertView = activity.getLayoutInflater().inflate(R.layout.item_adapter_view, null);
}

final TextView itemDetailsViews = (TextView) convertView.findViewById(R.id.item_details);
final TextView itemsDescriptionsViews = (TextView) convertView.findViewById(R.id.item_description);
final ImageView itemImagesViews = (ImageView) convertView.findViewById(R.id.item_image);
itemDetailsView.setText(itemDetails[position]);
itemsDescriptionsView.setText(itemDescriptions[position]);
switch (position) {
    case 0:
        itemImagesViews.setImageResource(R.drawable.plane);
        break;
    case 1:
        itemImagesViews.setImageResource(R.drawable.train);
        break;
    case 2:
        itemImagesViews.setImageResource(R.drawable.bus);
        break;
    case 3:
        itemImagesViews.setImageResource(R.drawable.hotel);
        break;
    case 4:
        itemImagesViews.setImageResource(R.drawable.holiday);
        break;
    case 5:
        itemImagesViews.setImageResource(R.drawable.booking);
        break;
    case 6:
        itemImagesViews.setImageResource(R.drawable.discount);
        break;
    default:
        itemImagesViews.setImageResource(R.drawable.train);
        break;
}

【讨论】:

  • 我看到你使用了我上面留下的代码,这解决了你的问题吗?如果是这样,请您接受答案吗?
【解决方案2】:

如果我理解正确,您还没有准备好以横向模式显示项目。在这种情况下,您只需在 AndroidManifest.xml 中执行此操作:

<activity android:name=".MainActivity"
          android:screenOrientation="portrait">

这里的主要属性是screenOrientation。这声明您的视图处于某种模式。相关谷歌网页@activity-element,搜索screenOrientation。

但是,与 getView() 相关的先前答案将是更好的解决方案。同时,您可以使用此属性,直到您准备好处理横向问题。

【讨论】:

    猜你喜欢
    • 2012-08-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-05-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多