0

我需要在屏幕旋转后保存 id。(安卓 3.2+)。详细信息:在清单中:

android:configChanges="orientation|screenSize"

在活动中:

@Override
public void onConfigurationChanged(Configuration newConfig) {
    // TODO Auto-generated method stub
    super.onConfigurationChanged(newConfig);
    initializeAnimation(true);
    if (newConfig.orientation == Configuration.ORIENTATION_PORTRAIT) {
        refreshView(true);
    } else if (newConfig.orientation == Configuration.ORIENTATION_LANDSCAPE) {
        refreshView(false);
    }
}

refreshView 方法从其他类中获取 ListView 的集合或 GridView 的集合并放入 ViewFlipper。用户可以在 List 或 Grid 视图中触摸元素,然后在 AsyncTask 和所选元素上显示的进度条上开始下载。

模型:

public List<ListView> getListViews(Context c) {
    listViews.clear();
    Iterator<List<Map<String, String>>> iter = arr.iterator();
    while (iter.hasNext()) {
        SimpleAdapter a = new SimpleAdapter(c, iter.next(), R.layout.vertical_list_view_item, new String[] { MONTH, IMAGE, DESCRIPTION}, new int[] { R.id.txtMonthV , R.id.imgV, R.id.txtDecriptionV});
        ListView lv = new ListView(c);
        lv.setDivider(null);
        lv.setDividerHeight(0);
        lv.setAdapter(a);
        listViews.add(lv);
    }
    return listViews;
}

public List<GridView> getGridViews(Context c){
    List<GridView> gridViews = new ArrayList<GridView>();
    Iterator<List<Map<String, String>>> iter = arr.iterator();
    while (iter.hasNext()) {
        SimpleAdapter a = new SimpleAdapter(c, iter.next(), R.layout.horizontal_list_view_item , new String[] { MONTH, IMAGE/*, DESCRIPTION*/}, new int[] { R.id.txtMonthH, R.id.imgH/*, R.id.txtDescriptionH*/ });
        GridView gv = new GridView(c);
        gv.setHorizontalSpacing(15);
        gv.setVerticalSpacing(15);
        gv.setNumColumns(6);
        gv.setAdapter(a);
        gridViews.add(gv);
    }

    return gridViews;       
}

控制器:

public View currentView;

public View getCurrentPressedView(){
    return currentView;
}

public void setCurrentPressedView(View v){
    currentView = v;
}



@Override
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
    if (arg0 instanceof ListView) {
        /*....*/
        setCurrentPressedView(arg1);
        //Start download here
    } else if (arg0 instanceof GridView) {
        /*...*/
        setCurrentPressedView(arg1);
        //Start download here
    }
}

在异步任务中:

View view;
ProgressBar proBar;
@Override
    protected void onPreExecute() {
        super.onPreExecute();
        view = getCurrentPressedView();
        proBar = (ProgressBar) view.findViewById(R.id.progress);
        proBar.setVisibility(View.VISIBLE);
///.....
@Override
    protected void onProgressUpdate(Integer... values) {
        mProDialog.setProgress(values[0]);
        proBar.setVisibility(View.VISIBLE); 
        super.onProgressUpdate(values);
    }

在vertical_list_view_item 和horizo​​ntal_list_view_item 中,进度条具有相同的ID。但是当屏幕旋转时,id 改变了,我无法控制进度条。有人可以帮助我吗?谢谢

4

1 回答 1

0

您是否使用静态变量来存储 id ?

于 2012-11-29T13:22:50.360 回答