简体   繁体   English

使用带有活动或片段的导航抽屉?

[英]Use navigation drawer with activities or fragments?

I have Android app with sidebar nav drawer layout and I am implementing functionality of a simple SMS app. 我有带有侧边栏导航抽屉布局的Android应用,正在实现一个简单的SMS应用的功能。

My question is, how to reuse nav drawer code between activities. 我的问题是,如何在活动之间重用导航抽屉代码。 Every example uses Fragments that are displayed in some master view after item in nav drawer menu is clicked. 每个示例都使用单击导航抽屉菜单中的项目后在某些主视图中显示的片段。 What if I launch new activity and want to have the same side menu as the original one? 如果我启动新活动并希望拥有与原始活动相同的侧边菜单怎么办?

Is there an official suggestion by Google how to implement this? Google有官方建议如何实施吗?

The problem for me is that in order to be default SMS app on Android, you must have some special activities handling certain Intents. 我的问题是,要成为Android上的默认SMS应用,您必须进行一些特殊的活动来处理某些Intent。

Should I dump Activities fully and implement everything using Fragments? 我应该完全转储Activity并使用Fragment实现所有功能吗?

Thanks 谢谢

Google's Android apps have a specific architecture which consists of the following: Google的Android应用具有特定的架构,该架构包括以下内容:

  • There is often just a single Activity in the entire app, and if more activities are needed, then they all extend the same BaseActivity . 整个应用程序中通常只有一个Activity ,如果需要更多活动,则它们都将扩展同一BaseActivity
  • Different screens are represented as individual Fragment s. 不同的屏幕表示为单个Fragment Screen transitions take place through fragment transactions all within the same BaseActivity . 屏幕过渡通过片段事务在同一BaseActivity

For an example of this, look no further than the source code of the Google I/O app : 例如,仅使用Google I / O应用程序的源代码即可:

The way to integrate the drawer with different screens is illustrated in this project, and further elaborated in the following articles: 此项目说明了将抽屉与不同屏幕集成在一起的方式,以下文章中进一步阐述了这种方式:

0. Guide to App Architecture 0. App体系结构指南

1. Android App Structure . 1. Android应用程序结构

2. Planning Screens and Their Relationships . 2. 规划屏幕及其关系

3. Providing Descendant and Lateral Navigation . 3. 提供后代和横向导航

4. Providing Ancestral and Temporal Navigation . 4. 提供祖先和时间导航

5. Patterns – Navigation . 5. 模式–导航

6. Best Practices for User Interface . 6. 用户界面最佳做法


More examples of apps that employ these (and other) guidelines: 使用这些(和其他)准则的应用程序的更多示例:

This is little to broad question so let's focus on reusing drawer. 这个问题鲜为人知,因此让我们集中讨论重用抽屉。 Content of the drawer is nothing more than a view, so probably best way to have it reusable is to create custom view by extending FrameLayout (or something else that is suitable for your particular design. In very basic form all you have to do is something like (init block syntax below): 抽屉的内容只不过是视图,因此使它可重用的最佳方法可能是通过扩展FrameLayout(或其他适合您特定设计的东西)来创建自定义视图。以非常基本的形式,您要做的就是像(下面的初始化块语法):

DrawerView extends FrameLayout
{
  {
    LayoutInflater.from(getContext()).inflate(R.layout.my_drawer_layout, this, true);
  }
}

Of course you probably want to put rest of the logic in additional methods or in the init block itself (for example handling clicks, initializing adapters etc.) Then simply build this code and you are ready to use your new custom control in every place you want. 当然,您可能希望将其余逻辑放在其他方法中或init块本身中(例如,处理单击,初始化适配器等)。然后只需构建此代码,即可准备在每个位置使用新的自定义控件想。

public class Tab2Fragment extends Fragment { 公共类Tab2Fragment扩展Fragment {

public int currentimageindex=0;
//  Timer timer;

// TimerTask task; // TimerTask任务; ImageView slidingimage; ImageView滑动图像;

private int[] IMAGE_IDS = {
        R.drawable.splash0, R.drawable.splash1, R.drawable.splash2,
        R.drawable.splash3, R.drawable.splash4, R.drawable.splash5
};


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

    View v = inflater.inflate(R.layout.tab2fragment, container, false);


    final Handler mHandler = new Handler();

    // Create runnable for posting
    final Runnable mUpdateResults = new Runnable() {
        public void run() {

            AnimateandSlideShow();

        }
    };

    int delay = 1000; // delay for 1 sec.

    int period = 5000; // repeat every 4 sec.

    Timer timer = new Timer();

    timer.scheduleAtFixedRate(new TimerTask() {

        public void run() {

            mHandler.post(mUpdateResults);

        }

    }, delay, period);




    return v;
}





/**
 * Helper method to start the animation on the splash screen
 */
private void AnimateandSlideShow() {


    slidingimage = (ImageView) slidingimage.findViewById(R.id.ImageView3_Left);
    slidingimage.setImageResource(IMAGE_IDS[currentimageindex%IMAGE_IDS.length]);

    currentimageindex++;

    Animation rotateimage = AnimationUtils.loadAnimation(getActivity(), R.anim.fade_in);


    slidingimage.startAnimation(rotateimage);



}

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

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