如何通过单击按钮将活动替换为同一窗口中的活动

时间:2017-03-01 16:48:27

标签: java android

我创建了2个活动,一个是网格视图,另一个是具有相同信息的列表视图,还有一个默认活动,工具栏包含2个不同的可点击项,一个用于网格活动,另一个用于列表视图全部,我需要,如果我点击el列表图标替换列表活动的网格活动,如果我单击网格图标替换列表活动与网格活动,但此操作应该在同一窗口与相同的工具栏,顺便说一下默认活动是网格一, 这是带有工具栏和网格视图示例的图片 Sample 这是我的menu.xml文件

content <- mutate(content, stdev = sentiment_by(story)[["sd"]])

这是网格活动,是默认的

<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto">

    <item android:id="@+id/searchicon"
        android:icon="@drawable/search"
        android:title="@string/search"
        app:showAsAction="always"/>

    <item android:id="@+id/listicon"
        android:icon="@drawable/list_view"
        android:title="@string/listview"
        app:showAsAction="always"/>

    <item android:id="@+id/gridicon"
        android:icon="@drawable/table_grid"
        android:title="@string/gridview"
        app:showAsAction="always"/>
</menu>

这是列表活动

package abtech.waiteriano.com.waitrer;

import android.os.Build;
import android.os.Bundle;
import android.support.annotation.RequiresApi;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.Spinner;
import android.widget.Toast;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;

import abtech.waiteriano.com.waitrer.adapters.CustomMenuGridViewAdapter;
import abtech.waiteriano.com.waitrer.connection_class.ConnectionClass;
import abtech.waiteriano.com.waitrer.getters_and_setters.MenuItem;

public class MenuActivity extends AppCompatActivity {
    private android.support.v7.widget.Toolbar toolbar;
    private ArrayList<String> category ;
    GridView menuGridView;

    static ArrayList<MenuItem> menuGridArray = new ArrayList<MenuItem>();
    CustomMenuGridViewAdapter customMenuGridViewAdapter;

    ImageView listIcon, gridIcon;

    @RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_menu);

        menuGridView = (GridView) findViewById(R.id.menuGridView);
        customMenuGridViewAdapter = new CustomMenuGridViewAdapter(MenuActivity.this, R.layout.menu_row_grid, menuGridArray);
        menuGridView.setAdapter(customMenuGridViewAdapter);
        String menugridSTR = "Select ID,Code,Name,Name2 From Presets Where Active = 1 And Rest_ID_Active = 1 AND OutLet_ID_Active = 1 ORDER BY Code";
        ResultSet rs = ConnectionClass.Ret_RS(menugridSTR);
        menuGridArray.clear();
        try {
            while (rs.next()) {
                menuGridArray.add(new MenuItem(rs.getString("Name")));
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }

        listIcon = (ImageView) findViewById(R.id.listicon);
        gridIcon = (ImageView) findViewById(R.id.gridicon);
        category = new ArrayList<String>() ;
        String str = "Select ID,Code,Name,Name2 From Presets Where Active = 1 And Rest_ID_Active = 1 AND OutLet_ID_Active = 1 ORDER BY Code";
        rs = ConnectionClass.Ret_RS(str);
        try {
            while (rs.next()) {
                category.add(rs.getString("Name"));
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);
        getSupportActionBar().setDisplayShowTitleEnabled(false);
        Spinner navigationSpinner = new Spinner(getSupportActionBar().getThemedContext());
        ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, category);
        navigationSpinner.setAdapter(adapter);
        toolbar.addView(navigationSpinner, 0);

        navigationSpinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
                Toast.makeText(MenuActivity.this,
                        "you selected: " + category.get(position),
                        Toast.LENGTH_SHORT).show();
            }

            @Override
            public void onNothingSelected(AdapterView<?> parent) {

            }
        });
    }


    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        MenuInflater inflater = getMenuInflater();
        inflater.inflate(R.menu.iconmenu, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(android.view.MenuItem item) {
        switch (item.getItemId()){
            case R.id.searchicon:
                // ...
                return true;
            case R.id.listicon:
                setContentView(R.layout.activity_menu_lv);
                return true;
            case R.id.gridicon:
                // ...
                return true;
        }
        return super.onOptionsItemSelected(item);
    }
}

对不起,如果有任何事情不清楚,对于长代码,如果有任何观察请发表评论

0 个答案:

没有答案