使用幻灯片菜单激活片段转换

时间:2015-12-05 13:13:54

标签: android android-fragments menu slide

我正在尝试在Android上制作应用程序。我在菜单栏中制作了幻灯片,如下图所示。蓝色吧。这是我的主要活动。现在我想要的是我的每个屏幕应该显示相同的菜单选项。不是那些有后退按钮的人。我怎么做? 现在我有一个主要活动,初始化幻灯片菜单和菜单有选项。它显示主屏幕是一个片段。从主屏幕使用按钮我移动到我的下一个屏幕,这是一个条形码扫描仪,是一个新的活动,没有幻灯片菜单。在这个屏幕上,我移动到一个新的屏幕,显示扫描仪屏幕的结果,这是一个新的活动,通过额外的意图放置和传递。

现在我希望结果屏幕也显示与主页上相同的幻灯片菜单。

我的所有其他屏幕也都是作为活动制作的。

我该怎么做?

主幻灯片菜单活动代码:

public class MainActivity extends ActionBarActivity {

DrawerLayout drawerLayout;
RelativeLayout drawerPane;
ListView lvNav;

List<NavItem> listNavItems;
List<Fragment> listFragments;

ActionBarDrawerToggle actionBarDrawerToggle;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    getSupportActionBar().setDisplayHomeAsUpEnabled(true);

    drawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
    drawerPane = (RelativeLayout) findViewById(R.id.drawer_pane);
    lvNav = (ListView) findViewById(R.id.nav_list);

    listNavItems = new ArrayList<NavItem>();
    listNavItems.add(new NavItem("Home", "MyHome page",
            R.drawable.ic_action_home));
    listNavItems.add(new NavItem("Settings", "Change something",
            R.drawable.ic_action_settings));

主屏代码:

 public class MainActivityFrag extends Fragment implements View.OnClickListener {

ImageButton scan;
@Override
public View onCreateView(LayoutInflater inflater,
                         @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    View v = inflater.inflate(R.layout.home_screen, container, false);
    // setContentView(R.layout.home_screen);
    scan=(ImageButton)v.findViewById(R.id.scanBtn);
    scan.setOnClickListener(this);

    return v;
}


public void ScanBarcode(View view) {
    Intent newScreen = new Intent("com.aaa.fyp.SimpleScannerActivity");
    // finish();

    startActivity(newScreen);
    //  finish();
}

条码扫描屏幕代码:

   @Override
    public void handleResult(Result rawResult) {
        // Do something with the result here
        AccountManager manager = (AccountManager) getSystemService(ACCOUNT_SERVICE);
        Account[] list = manager.getAccounts();
        String gmail = null;

        for(Account account: list)
        {
            if(account.type.equalsIgnoreCase("com.google"))
            {
                gmail = account.name;
                break;
            }
        }

        Toast.makeText(SimpleScannerActivity.this,gmail,Toast.LENGTH_LONG).show();

        Log.v(TAG, rawResult.getText()); // Prints scan results
       // Toast.makeText(SimpleScannerActivity.this, rawResult.toString() + "  WOW scanned", Toast.LENGTH_LONG).show();
       // Toast.makeText(SimpleScannerActivity.this, rawResult.getBarcodeFormat().toString(), Toast.LENGTH_LONG).show();
        Log.v(TAG, rawResult.getBarcodeFormat().toString()); // Prints the scan format (qrcode, pdf417 etc.)
        //Intent scanScreenResult= new Intent("com.aaa.fyp.ScanResultScreen");

        Intent nextScreen = new Intent("com.aaa.fyp.ScanResultScreen");
        nextScreen.putExtra("barcode",rawResult.toString());
        nextScreen.putExtra("format", rawResult.getBarcodeFormat().toString());
       finish();
        startActivity(nextScreen);

扫描结果屏幕,显示新活动中扫描屏幕的结果。这是我想要相同幻灯片菜单的页面:

public class ScanResultScreen extends SimpleScannerActivity {

ImageView scanned;
TextView bc;
TextView f;
String Barcode;
String format;

TextView d;
@Override
public void onCreate(Bundle state) {
    super.onCreate(state);
    setContentView(R.layout.scan_screen_with_button);
    // setContentView(R.layout.scan_screen_with_button);

    ViewGroup layout = (ViewGroup) findViewById(R.id.scanScreenWithButton);
    layout.setLayoutParams(new LinearLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT));
    //layout.setOrientation(LinearLayout.VERTICAL);

    setContentView(layout);
    Intent prevScreen = getIntent(); // gets the previously created intent

    Barcode=prevScreen.getStringExtra("barcode");
    bc= (TextView)findViewById(R.id.barcode_label);
    bc.setText(Barcode);

    format=prevScreen.getStringExtra("format");
    f=(TextView)findViewById(R.id.format_label);
    f.setText(prevScreen.getStringExtra("format").toString());

enter image description here

1 个答案:

答案 0 :(得分:0)

您是否尝试创建由其他活动扩展的BaseActivity。在BaseActivity创建侧边菜单中,以便扩展此活动的其他活动可以使用它。

仅提及在#34;细节视图&#34;内有抽屉菜单(侧面菜单)活动不是很好的做法(用户体验)。阅读有关提供祖先和时间导航herehere的信息。

您的活动之间的远程数据可以使用:

活动1设置数据

//Get the intent parameters and pass it to aragment
Class data=new Class();
//Set data attributes
Intent detailsIntent = new Intent(getActivity(), Activity2.class);
detailsIntent.putExtra("data", data);
getActivity().startActivity(detailsIntent);

Activity2在onCreate()方法中获取数据:

Class data = ((Class) getIntent().getParcelableExtra("data"));
//Use data in new activity

在你的情况下:

Intent nextScreen = new Intent(ScanResultScreen.class);
nextScreen.putExtra("barcode",rawResult.toString());
nextScreen.putExtra("format", rawResult.getBarcodeFormat().toString());
getActivity().startActivity(nextScreen);

在您的ScanResultScreen活动中执行以下操作:

String barcode = getIntent().getStringExtra("barcode");
String format = getIntent().getStringExtra("format");