简体   繁体   English

保存ListView的状态以进行屏幕方向

[英]Saving the state of my ListView for screen orientation

I went through many posts for screen orientation. 我浏览了许多帖子,用于屏幕方向。 But my scenario is little bit different. 但我的情况有点不同。 My ListView have custom row layout. 我的ListView具有自定义行布局。 With ImageView bitmap being loaded from internet using AsyncTask. 使用AsyncTask从Internet加载ImageView位图。 What I want is to, is it possible to save the ArrayAdapter of my ListView or the whole ListView so that its state is preserved upon orientation screen change. 我想要的是,是否可以保存我的ListView的ArrayAdapter或整个Li​​stView,以便在更改方向屏幕时保留其状态。

Please suggests some posts or if some codes are posted it will be very useful. 请建议一些帖子或如果发布了一些代码,它将非常有用。

public View getView(int position, View convertView, ViewGroup parent) {
        final Contents entry = contentList.get(position);
        View row = convertView;
        LayoutInflater infltr = null;
            if (row == null) {
                infltr = (LayoutInflater) context
                        .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
                row = inflater.inflate(R.layout.highlight_contents_layout,
                        parent, false);
        }

            ImageView imgViewImage = (ImageView) row
                    .findViewById(R.id.imgView_Image);
            if (imgViewImage.getTag() == null) {
                new DownloadImage(imgViewImage, entry).execute(); //Async Task to download Image
            }

        }
        return row;
    }

and in onCreate i have a function call to populate the list the function call is : 在onCreate中我有一个函数调用来填充函数调用的列表:

init(){
listContent = (ListView) findViewById(R.id.listview); //vairable declared at top of my class;
contentlist = new ArrayList<Contents>(); //Variable Declared at top of class;

//... Populated the contenlist... in here
//..
//..
adapter = new ListAdapter(getApplicationContext(),
                R.layout._contents_layout, contentList,
                thumb_image_bytes);
        listContent.setAdapter(adapter);
        adapter.notifyDataSetChanged();
}

You can preserve state of your ListView by using onRetainNonConfigurationInstance() method. 您可以使用onRetainNonConfigurationInstance()方法保留ListView的状态。 This method is executed before activity is destroyed during orientation change and it can return any object you want, for example your list adapter. 此方法在方向更改期间销毁活动之前执行,并且可以返回所需的任何对象,例如列表适配器。 Leter, in onCreate() method that object can be restored by calling getLastNonConfigurationInstance( ); Leter,在onCreate()方法中,可以通过调用getLastNonConfigurationInstance( )来恢复对象;

So create field in your activity, for example mAdapter . 因此,在您的活动中创建字段,例如mAdapter That field will hold reference to your list adapter. 该字段将保留对列表适配器的引用。 Override onRetainNonConfigurationInstance() like this: 像这样覆盖onRetainNonConfigurationInstance()

@Override
public Object onRetainNonConfigurationInstance() {
    return mAdapter;
}

And restore list adapter in onCreate() method: 并在onCreate()方法中恢复列表适配器:

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    mList = (ListView) findViewById(android.R.id.list);

    Object obj = getLastNonConfigurationInstance();

    if(null != obj){//if there is saved adapter - restore it
        mAdapter = (ArrayAdapter<String>)obj;
    } else { //if not - create new one
        mAdapter = new ArrayAdapter<String>(getApplicationContext(),
                android.R.layout.simple_list_item_1, listData);
    }
        mList.setAdapter(mAdapter);
}

Edit: 编辑:

This doesn't look good for me: 这对我来说不太好看:

ImageView imgViewImage = (ImageView) row
                    .findViewById(R.id.imgView_Image);
      if (imgViewImage.getTag() == null) {
           new DownloadImage(imgViewImage, entry).execute(); //Async Task to download Image
}

Please, check if imgViewImage.getTag() ever returns false. 请检查imgViewImage.getTag()是否返回false。 I think You shouldn't execute AsyncTask in getView() because it will be executed while you are scrolling list so you could get list items with no imager(I think). 我认为您不应该在getView()执行AsyncTask ,因为它会在您滚动列表时执行,这样您就可以获得没有成像器的列表项(我认为)。

Why dont you store the downloaded data in variables, and upon change of orientation, reload the elements with the stored data. 为什么不将下载的数据存储在变量中,并且在更改方向时,使用存储的数据重新加载元素。 This link will help you catch the screen orientation change event. 此链接将帮助您捕获屏幕方向更改事件。

I am not 100% sure what you are asking. 我不是100%肯定你在问什么。 But you can set the orientation so that it is fixed to a certain value. 但您可以设置方向,使其固定为某个值。 I had a problem where I had a list view change on orientation so I fixed the orientation so that it would only produce the output in portrait mode. 我有一个问题,我有一个列表视图更改方向,所以我修改了方向,以便它只会以纵向模式生成输出。

I added this tag inside my activity tags as an option 我在活动标签中添加了此标签作为选项

<activity android:name="preferences.Preferences" android:screenOrientation="portrait"></activity>. 

I hope this is the answer that you were looking for. 我希望这是你正在寻找的答案。 This will preserve the state. 这将保持国家。 If this does not answer your question. 如果这不能回答你的问题。 I will endeavour to help you further. 我会尽力帮助你。

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

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