Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
203 views
in Technique[技术] by (71.8m points)

java - 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);
        }

    }




}
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...