简体   繁体   中英

Switching between 2 layouts in android activity

I have 2 different layout files that I want to use for modifying the same data, I was to switch to my layout for the "edit view" that allows the user to modify graph data and then allow them to switch back to a "detailed view" that displays a detailed graph(using androidplot library).

My issue is, when switching back to my "edit view" my graphed lines are gone and only the axises draw(so the layout switches and the onDraw() is being called for my graph View). Everything is stored within the same Activity so I don't understand why this isn't working?

The lines are stored within the Graph View object itself which should be persistent since it is a stored variable in my activity.

I use these two methods for switching layout files on a button click.

public class GraphLibActivity extends Activity {

    private Graph graph;

    private boolean editView;

    private static TextView coordTextView;


    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        editView = true;

        setContentView(R.layout.graphlib);



        graph = (Graph) findViewById(R.id.graph);

        coordTextView = (TextView)findViewById(R.id.currentCoords);


        (lots of calculations)
        graph.addLine(gelHistogramPoints, linePaint);



        graph.invalidate();      
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        MenuInflater inflater = getMenuInflater();
        if(editView == true){
            inflater.inflate(R.menu.activity_menu, menu);
        }else{
            inflater.inflate(R.menu.detailed_view_menu, menu);
        }
        return true;
    }

    public boolean onPrepareOptionsMenu(Menu menu){
        menu.clear();
        MenuInflater inflater = getMenuInflater();
        if(editView == true){
            inflater.inflate(R.menu.activity_menu, menu);
        }else{
            inflater.inflate(R.menu.detailed_view_menu, menu);
        }
        graph.invalidate();
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {

        // Handle item selection
        switch (item.getItemId()) {


        case R.id.detailed_view:
            editView = false;
            setContentView(R.layout.imagegraph);
            return true;


        case R.id.edit_view:
            editView = true;
            setContentView(R.layout.editgraph);             
            return true;

        default:
            return super.onOptionsItemSelected(item);
        }

    }




}

Each time you call setContentView you are inflating the layout so data must be set again. Are you doing that?

Anyway, I would recommend merging the two layouts into one file. Then use ViewFlipper to change from one layout to another. That would look something similar to:

graph.xml:

<ViewFlipper android:id="@+id/viewFlipper">
    <LinearLayout>
        // Here would go the content of R.layout.imagegraph
    </LinearLayout>
    <LinearLayout>
        // Here would go the content of R.layout.editgraph
    </LinearLayout>
</ViewFlipper>

Then you just have to call showNext() to switch layouts in your activity:

ViewFlipper vf = (ViewFlipper) findViewById( R.id.viewFlipper );
vf.showNext();

Hope it helps.

Why are you always inflating the layout? That is really expensive. Try to use the merge layout. This way, you can change the visibilty to "Gone" or "Visible". see more: http://android-developers.blogspot.com/2009/03/android-layout-tricks-3-optimize-by.html

I do something similar for an app I'm working on.

A simple way to solve it is to set the default visibility of the 2nd view to android:visibility="GONE" in XML, then identify each view in code. You can then set the visibility with view1.setVisibility(View.GONE) and view2.setVisibility(View.VISIBLE)

Hope this helps: It worked great for me :)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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