0

我有Android侧边栏导航抽屉布局的应用程序,我正在实现一个简单的短信应用程序的功能。使用具有活动或碎片的导航抽屉?

我的问题是,如何在活动之间重用导航代码。每个示例都使用在导航抽屉菜单中的项单击后在某个主视图中显示的碎片。如果我推出新的活动并希望拥有与原始活动相同的侧边菜单,该怎么办?

Google有没有官方的建议如何实现?

我的问题是,为了成为Android上的默认SMS应用程序,您必须有一些特殊的活动来处理某些意图。

我应该完全转储活动并使用碎片实现一切吗?

感谢

回答

4

谷歌的Android应用程序有它由以下的架构:

  • 有往往只是一个Activity在整个应用程序,如果需要更多的活动,那么他们都延伸相同BaseActivity
  • 不同的屏幕显示为单个Fragment s。通过片段交易发生的屏幕转换都在相同的BaseActivity之内。

对于这样的例子,没有进一步看比Google I/O app的源代码:

抽屉用不同的屏幕集成的方式在这个项目中说明,并进一步阐述了在下面的文章:

Guide to App Architecture

Android App Structure

2.Planning Screens and Their Relationships

3.Providing Descendant and Lateral Navigation

4.Providing Ancestral and Temporal Navigation

5.Patterns – Navigation

6.Best Practices for User Interface。应用程序的


更多的例子采用这些(和其他)的指导方针:

0

这是一个小问题,所以让我们着重于重用抽屉。 抽屉的内容只不过是一个视图,所以让它可重用的最好方法是通过扩展FrameLayout(或其他适用于您特定设计的其他东西)来创建自定义视图。在非常基本的形式中,您所要做的就是像(以下初始块语法):

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

当然,你可能想要把逻辑的其余部分额外的方法或在init块本身(例如处理点击,初始化适配器等) 然后简单建立这个代码,你准备在你想要的每个地方使用你的新的自定义控件。

0

public class Tab2Fragment extends Fragment {

public int currentimageindex=0; 
// Timer timer; 

// TimerTask task; ImageView slidingimage;

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); 



} 
+1

最好在代码中包含一些上下文/解释,因为这会使答案对于OP和未来的读者更有用。 – EJoshuaS