0

我正在考虑一个解决方案,如何实现两个不同的活动,以便我可以像浏览器一样在它们之间滑动。

我在 Eclipse 中创建了两个项目。在其中一个中,我实现了一个简单的音乐播放器,它显示了 mp3.file 的封面。它还会在播放歌曲时显示搜索栏的移动。在另一个项目中,我实现了一个 openstreetmap 活动,它在地图上显示了我的位置。我想要的是结合这两个活动,这样我就可以在它们之间滑动。那可能吗?我必须如何解决它?提供有关使用片段的其他解决方案?我将非常感谢您提前提供建议并原谅我的英语:)

编辑:为了更好地理解:

我有一个项目,它播放一首歌曲并显示封面、歌曲名称、解释器和一个搜索栏,它在歌曲播放时移动。它仍然工作正常。我想要的是把一个布局分成两个这样的 布局。在图片中,您可以看到我实现的播放器(在左侧)。我想拆分相同的布局组件,以便在最后(右侧)有 2 个布局。我用片段尝试过。这是我当前的代码:

public class MainActivity extends FragmentActivity {

SectionsPagerAdapter mSectionsPagerAdapter;
ViewPager mViewPager;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(com.example.swipeview3.R.layout.main);

    // Create the adapter that will return a fragment for each of the three
    // primary sections of the app.
    mSectionsPagerAdapter = new SectionsPagerAdapter(
            getSupportFragmentManager());

    // Set up the ViewPager with the sections adapter.
    mViewPager = (ViewPager) findViewById(com.example.swipeview3.R.id.pager);
    mViewPager.setAdapter(mSectionsPagerAdapter);

}

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

//  ------------------------------#########################------------------------------
//  ------------------------------#########################------------------------------

public class SectionsPagerAdapter extends FragmentPagerAdapter {

    public SectionsPagerAdapter(FragmentManager fm) {
        super(fm);
    }

    @Override
    public Fragment getItem(int position) {

        if(position == 0) {
            PlayerFragment f = new PlayerFragment(position);
            return f;
        }
        if(position == 1) {
            SongParameterFragment f = new SongParameterFragment(position);
            return f;
        }
        else {
            Fragment fragment = new DummySectionFragment();
            Bundle args = new Bundle();
            args.putInt(DummySectionFragment.ARG_SECTION_NUMBER, position + 1);
            fragment.setArguments(args);
            return fragment;
        }
    }

    @Override
    public int getCount() {
        // Show 3 total pages.
        return 3;
    }

    @Override
    public CharSequence getPageTitle(int position) {
        switch (position) {
        case 0:
            return getString(com.example.swipeview3.R.string.title_section1).toUpperCase();
        case 1:
            return getString(com.example.swipeview3.R.string.title_section2).toUpperCase();
        case 2:
            return getString(com.example.swipeview3.R.string.title_section3).toUpperCase();
        }
        return null;
    }
}

//  ------------------------------#########################------------------------------
//  ------------------------------#########################------------------------------

public static class DummySectionFragment extends Fragment {

    public static final String ARG_SECTION_NUMBER = "section_number";

    public DummySectionFragment() {
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        // Create a new TextView and set its text to the fragment's section
        // number argument value.
        TextView textView = new TextView(getActivity());
        textView.setGravity(Gravity.CENTER);
        textView.setText(Integer.toString(getArguments().getInt(
                ARG_SECTION_NUMBER)));
        return textView;
    }
}

//  ------------------------------#########################------------------------------
//  ------------------------------#########################------------------------------

public static class PlayerFragment extends Fragment {

    int fragmentNR;
    Button btnPlay, btnStop, btnExit;
    SeekBar seekBar;

    public PlayerFragment(int position) {
        this.fragmentNR = position;
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

        btnPlay = (Button)getView().findViewById(com.example.swipeview3.R.id.button_play);
        btnStop = (Button)getView().findViewById(com.example.swipeview3.R.id.button_stop);
        btnExit = (Button)getView().findViewById(com.example.swipeview3.R.id.button_exit);

        return inflater.inflate(com.example.swipeview3.R.layout.player, container, false);
    }



}

//  ------------------------------#########################------------------------------
//  ------------------------------#########################------------------------------

public static class SongParameterFragment extends Fragment {

    int fragmentNR;
    ImageView cover;
    TextView songParameter, songDuration;

    public SongParameterFragment(int position) {
        this.fragmentNR = position;
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { 
        return inflater.inflate(com.example.swipeview3.R.layout.song_parameters, container, false);
    }
}
}

当我在 PlayerFragment 中分配按钮 ID 时,我变成了 nullpointerException

01-23 13:33:09.553: D/AndroidRuntime(15881): Shutting down VM
01-23 13:33:09.553: W/dalvikvm(15881): threadid=1: thread exiting with uncaught exception (group=0x40c571f8)
01-23 13:33:09.553: E/AndroidRuntime(15881): FATAL EXCEPTION: main
01-23 13:33:09.553: E/AndroidRuntime(15881): java.lang.NullPointerException
01-23 13:33:09.553: E/AndroidRuntime(15881):    at com.example.swipeview3.MainActivity$PlayerFragment.onCreateView(MainActivity.java:142)
01-23 13:33:09.553: E/AndroidRuntime(15881):    at android.support.v4.app.Fragment.performCreateView(Fragment.java:1460)
01-23 13:33:09.553: E/AndroidRuntime(15881):    at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:911)
01-23 13:33:09.553: E/AndroidRuntime(15881):    at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1088)
01-23 13:33:09.553: E/AndroidRuntime(15881):    at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:682)
01-23 13:33:09.553: E/AndroidRuntime(15881):    at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1444)
01-23 13:33:09.553: E/AndroidRuntime(15881):    at android.support.v4.app.FragmentManagerImpl.executePendingTransactions(FragmentManager.java:461)
01-23 13:33:09.553: E/AndroidRuntime(15881):    at android.support.v4.app.FragmentPagerAdapter.finishUpdate(FragmentPagerAdapter.java:141)
01-23 13:33:09.553: E/AndroidRuntime(15881):    at android.support.v4.view.ViewPager.populate(ViewPager.java:1012)
01-23 13:33:09.553: E/AndroidRuntime(15881):    at android.support.v4.view.ViewPager.populate(ViewPager.java:881)
01-23 13:33:09.553: E/AndroidRuntime(15881):    at android.support.v4.view.ViewPager.onMeasure(ViewPager.java:1366)
01-23 13:33:09.553: E/AndroidRuntime(15881):    at android.view.View.measure(View.java:12929)
01-23 13:33:09.553: E/AndroidRuntime(15881):    at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4703)
01-23 13:33:09.553: E/AndroidRuntime(15881):    at android.widget.FrameLayout.onMeasure(FrameLayout.java:293)
01-23 13:33:09.553: E/AndroidRuntime(15881):    at android.view.View.measure(View.java:12929)
01-23 13:33:09.553: E/AndroidRuntime(15881):    at android.widget.LinearLayout.measureVertical(LinearLayout.java:822)
01-23 13:33:09.553: E/AndroidRuntime(15881):    at android.widget.LinearLayout.onMeasure(LinearLayout.java:563)
01-23 13:33:09.553: E/AndroidRuntime(15881):    at android.view.View.measure(View.java:12929)
01-23 13:33:09.553: E/AndroidRuntime(15881):    at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4703)
01-23 13:33:09.553: E/AndroidRuntime(15881):    at android.widget.FrameLayout.onMeasure(FrameLayout.java:293)
01-23 13:33:09.553: E/AndroidRuntime(15881):    at com.android.internal.policy.impl.PhoneWindow$DecorView.onMeasure(PhoneWindow.java:2257)
01-23 13:33:09.553: E/AndroidRuntime(15881):    at android.view.View.measure(View.java:12929)
01-23 13:33:09.553: E/AndroidRuntime(15881):    at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1240)
01-23 13:33:09.553: E/AndroidRuntime(15881):    at android.view.ViewRootImpl.handleMessage(ViewRootImpl.java:2628)
01-23 13:33:09.553: E/AndroidRuntime(15881):    at android.os.Handler.dispatchMessage(Handler.java:99)
01-23 13:33:09.553: E/AndroidRuntime(15881):    at android.os.Looper.loop(Looper.java:137)
01-23 13:33:09.553: E/AndroidRuntime(15881):    at android.app.ActivityThread.main(ActivityThread.java:4511)
01-23 13:33:09.553: E/AndroidRuntime(15881):    at java.lang.reflect.Method.invokeNative(Native Method)
01-23 13:33:09.553: E/AndroidRuntime(15881):    at java.lang.reflect.Method.invoke(Method.java:511)
01-23 13:33:09.553: E/AndroidRuntime(15881):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:980)
01-23 13:33:09.553: E/AndroidRuntime(15881):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:747)
01-23 13:33:09.553: E/AndroidRuntime(15881):    at dalvik.system.NativeStart.main(Native Method)

这正是我的问题。我不知道在哪里分配它们或在哪里编写函数。sry 伙计们,但我真的不明白它是如何与片段一起工作的。希望你能理解并帮助我:/非常感谢

4

3 回答 3

0

您可以将gestureOverlay放在活动中以检测手势和滚动,动画关闭/移出第一个活动和打开/移入第二个活动。

于 2013-01-22T11:37:53.420 回答
0

framelayout在其布局文件中使用仅带有 a 的 Fragment 活动。在两个单独的片段中实现两个活动的 UI(回想一下,片段实现了一个活动的所有生命周期方法)。

A Fragment represents a behavior or a portion of user interface in an Activity.

更多关于片段

首先FragmentManager在接收到来自用户的访客输入后,使用该类向框架布局添加一个片段,用您的第二个框架替换该片段。

使用片段,您可以非常轻松地重用 UI 元素(假设在扩展应用程序以支持平板电脑的情况下,您希望将两个活动的 UI 放置在单个屏幕上)。

于 2013-01-23T09:25:06.737 回答
0

看这个例子

public class MainActivity extends FragmentActivity 
{
    private ViewPager mPager;
    private FragAdapter mAdapter;

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


        mPager = (ViewPager) findViewById(R.id.pager);
        mAdapter = new FragAdapter(getSupportFragmentManager());        
        mPager.setAdapter(mAdapter);     

        mPager.setOnPageChangeListener(new OnPageChangeListener() 
        {
            public void onPageSelected(int position) 
            {
            }

            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) 
            {

            }

            public void onPageScrollStateChanged(int state) 
            {
                if (state == ViewPager.SCROLL_STATE_DRAGGING)
                {

                }
                if (state == ViewPager.SCROLL_STATE_IDLE)
                {

                }
            }
        });
    }


    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.activity_main, menu);
        return true;
    }

    public class FragAdapter extends FragmentPagerAdapter 
    {
        private Map<Integer, Object> mPageReferenceMap = new HashMap<Integer, Object>();
        public FragAdapter(FragmentManager fm) 
        {
            super(fm);
        }

        @Override
        public int getCount() 
        {
            return 3;
        }

        @Override
        public Fragment getItem(int position) 
        {
          Fragment frag;
          if (position == 0)
          {
              frag = new FirstActivity();  
          }   
          else if (position == 3)
          {
              frag =  new SecondActivity();
          }
          else if (position == 6)
          {
              frag =  new ThirdActivity();
          }

          mPageReferenceMap.put(Integer.valueOf(position),frag);
          return frag;
        }


        @SuppressWarnings("deprecation")
        @Override
        public void destroyItem(View container, int position, Object object) {

            super.destroyItem(container, position, object);

            mPageReferenceMap.remove(Integer.valueOf(position));
        }

        public Object getFragment(int key) {

            return mPageReferenceMap.get(key);
        }
    }

}

现在创建一个新的 FirstActivity.java

public class FirstActivity extends Fragment 
{
    public RadioGroup rythm;

    @Override
    public void onCreate(Bundle savedInstanceState) 
    {
       super.onCreate(savedInstanceState);
       Log.e("Test", "hello");
       //position = getArguments().getInt("position");

    }

 /**
  * Handle the results from the voice recognition activity.
  */     
    @Override
    public void onActivityCreated(Bundle savedInstanceState) 
    {
        super.onActivityCreated(savedInstanceState);
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) 
    {
       View view = inflater.inflate(R.layout.rythm, container, false);
       TextView text = (TextView) view.findViewById(R.id.textView);
       text.setText("Select Rythm");

       rythm = (RadioGroup) view.findViewById(R.id.rythm);
       RadioButton first = (RadioButton) rythm.getChildAt(0); 
       first.setText("Sinus");

       RadioButton second = (RadioButton) rythm.getChildAt(1); 
       second.setText("Afib");
       RadioButton third = (RadioButton) rythm.getChildAt(1); 
       third.setText("Other");

       return view;
    }
}
于 2013-01-22T18:42:04.583 回答