简体   繁体   English

Android:带有蓝牙和图形绘制的选项卡式活动,如何?

[英]Android: Tabbed activity with bluetooth and graph plotting, how to?

I am very new to Android, but I would like to develop my own app for a future project. 我是Android的新手,但我想为以后的项目开发自己的应用程序。 The basic idea of this is to have 2 tabs (for now). 这样做的基本思想是有2个选项卡(目前)。

The first tab, Bluetooth, will have a button to establish a connection between Android and a bluetooth module. 第一个选项卡,蓝牙,将具有一个按钮,用于在Android和蓝牙模块之间建立连接。

The second tab, Graph, needs to read the input stream and take this data to plot a graph against time. 第二个选项卡“图形”需要读取输入流并获取此数据以绘制随时间变化的图形。

Now my question is: 现在我的问题是:

Where exactly do I need to do what? 我到底需要在哪里做什么?

  1. MainActivity handles the creation of the tabs MainActivity处理选项卡的创建
  2. BluetoothTab inflates the layout of bluetooth_tab.xml BluetoothTab扩大了bluetooth_tab.xml的布局
  3. Graph inflates the layout of graph_tab.xml 图形使graph_tab.xml的布局膨胀

My problem right now is that I don't know where to put the bluetooth connection stuff. 我现在的问题是我不知道将蓝牙连接的东西放在哪里。 Do I put it in the MainActivity? 是否将其放入MainActivity?

If I do place the bluetooth connection stuff in the MainActivity, how can I make use of it from within the BluetoothTab fragment? 如果确实将蓝牙连接的东西放在MainActivity中,如何从BluetoothTab片段中使用它?

Could someone point me in the right direction with maybe an example of how this is usually done? 有人可以用一个通常如何完成的示例来指出正确的方向吗?

Here some code: 这里有一些代码:

MainActivity 主要活动

package com.bavilo.braumeister;

import android.app.AlertDialog;
import android.content.DialogInterface;
import android.support.v4.view.ViewPager;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.support.v7.widget.Toolbar;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.Button;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Toast;

public class MainActivity extends ActionBarActivity {

    /* Declaring Your View and Variables */

    Toolbar toolbar;
    ViewPager pager;
    ViewPagerAdapter adapter;
    SlidingTabLayout tabs;
    CharSequence Titles[] = {"Bluetooth","Graph"};
    int Numboftabs = 2;

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

        // Creating The Toolbar and setting it as the Toolbar for the activity
        toolbar = (Toolbar) findViewById(R.id.tool_bar);
        setSupportActionBar(toolbar);

        // Creating The ViewPagerAdapter and Passing Fragment Manager, Titles fot the Tabs and Number Of Tabs.
        adapter =  new ViewPagerAdapter(getSupportFragmentManager(),Titles,Numboftabs);

        // Assigning ViewPager View and setting the adapter
        pager = (ViewPager) findViewById(R.id.pager);
        pager.setAdapter(adapter);

        // Assiging the Sliding Tab Layout View
        tabs = (SlidingTabLayout) findViewById(R.id.tabs);
        tabs.setDistributeEvenly(true); // To make the Tabs Fixed set this true, This makes the tabs Space Evenly in Available width

        // Setting Custom Color for the Scroll bar indicator of the Tab View
        tabs.setCustomTabColorizer(new SlidingTabLayout.TabColorizer() {
            @Override
            public int getIndicatorColor(int position) {
                return getResources().getColor(R.color.tabsScrollColor);
            }
        });

        // Setting the ViewPager For the SlidingTabsLayout
        tabs.setViewPager(pager);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        switch(id) {
            case R.id.menu_info:

                new AlertDialog.Builder(this).setTitle("About").setMessage("Hello").show();
                break;
        }

        return super.onOptionsItemSelected(item);
    }
}

BluetoothTab BluetoothTab

package com.bavilo.braumeister;

import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothDevice;
import android.bluetooth.BluetoothSocket;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.os.Handler;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import android.widget.Toast;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.UUID;

public class BluetoothTab extends Fragment {

    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View v = inflater.inflate(R.layout.bluetooth_tab,container,false);
        text = (TextView) v.findViewById(R.id.text);
        return v;
    }
}

Graph 图形

package com.bavilo.braumeister;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

public class Tab2 extends Fragment {

    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View v = inflater.inflate(R.layout.tab_2,container,false);
        return v;
    }
}

All BT connection associated logic should definitely go into MainActivity because both tabs are going to use it. 所有与BT连接相关的逻辑绝对应该进入MainActivity因为两个选项卡都将使用它。 The best way to do it is to define a callback interface that MainActivity will implement, which will allow the Fragments to access the BT related functionality. 最好的方法是定义MainActivity将实现的回调接口,该接口将允许Fragments访问BT相关功能。

Keep the logic in Fragments to minimum. 保持“ Fragments ”中的逻辑最小。 For example: the first Fragment will just register OnClickListener with the button, and call one of the callback methods of MainActivity (this method will initiate BT connection). 例如:第一个Fragment只会使用按钮注册OnClickListener ,并调用MainActivity的回调方法之一(此方法将启动BT连接)。

The steps involved are very similar to ones explained here: http://developer.android.com/training/basics/fragments/communicating.html 涉及的步骤与此处说明的步骤非常相似: http : //developer.android.com/training/basics/fragments/communicating.html

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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