旋转后保持视图ID

时间:2012-11-29 13:19:01

标签: android listview android-asynctask screen-orientation android-progressbar

我需要在屏幕旋转后保存id。 (Android 3.2+)。 Deatils: 在清单中:

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。用户可以触摸列表或网格视图中的元素,并开始在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改变,我无法控制进度条。有人能帮助我吗? THX

1 个答案:

答案 0 :(得分:0)

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