简体   繁体   English

我可以在多个地方使用相同的片段吗?

[英]Can i use the same Fragments more than one place?

I create a lot of the same fragments.我创建了很多相同的片段。 They have the same shape.它们具有相同的形状。 Can I delete all of them and use just one in more than place if I relied on position like this?如果我像这样依赖 position,我可以删除所有这些并仅在多个位置使用一个吗?

 ViewPager pager = getView().findViewById( R.id.pager_saraya );

        for(int i=0 ; i<data.size() ; i++)
        {
            if(data.get( i ).getImage() == R.drawable.food_1)
            {
                ArrayList<Integer> det=new ArrayList<>(  );

                for( int j=1 ;j<=7;j++)
                {

                    int photoId= getResources().getIdentifier( "makdoos_"+i,"drawable" ,getContext().getPackageName() );

                    det.add(photoId);
                }

                det.add( R.drawable.seha );
                ImageAdapter_details adapter_makdoos = new ImageAdapter_details( getContext(), det );
                pager.setAdapter( adapter_makdoos );
            }

            else if(data.get( i ).getImage() == R.drawable.food_2)
            {

                ArrayList<Integer> det=new ArrayList<>(  );

                for( int j=1 ;j<=10;j++)
                {

                    int photoId= getResources().getIdentifier( "fish"+i,"drawable" ,getContext().getPackageName() );

                    det.add(photoId);
                }

                det.add( R.drawable.seha );
                ImageAdapter_details adapter_makdoos = new ImageAdapter_details( getContext(), det );
                pager.setAdapter( adapter_makdoos );
            }
        }

Answer to your question if you can move common code to one place and use it where it is needed is Yes you can do it and is a good idea.如果您可以将通用代码移动到一个地方并在需要的地方使用它,那么回答您的问题是,您可以做到,这是一个好主意。 Follow the principle of DRY遵循 DRY 原则

  • D Do not D 不要
  • R repeat R 重复
  • Y yourself你自己

You can move the common code to a helper or a util class name it that fits your standards.您可以将通用代码移至帮助程序或实用程序 class 名称,以符合您的标准。 Make it static or non static and it is ready to be used.将其设为 static 或非 static 即可使用。

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

相关问题 如何同时滚动多个对象? - How can I scroll more than one object at the same time? 如何在确认JOptionPane的同一“行”上放置多个JComponent? - How do I place more than one JComponent on the same “line” on a confirm JOptionPane? 如何使用多个@RequestParam 注解? - How can I use more than one @RequestParam annotations? 如何在javafx 8中多次使用控件? - How can i use a control more than one time in javafx 8? 如何在活动中使用多个广播? - How can I use more than one Broadcast in activity? 如何与.equals函数进行多个比较,或者可以使用另一个函数进行检查? - How can I use more than one comparison with the .equals function or is there another function that I can use to check? 如何避免在同一java项目上同时运行多个实例? - how can i avoid running of more than one instance on same java project at the same time? jberet,一个wildfly实例,多个数据源:我可以使用多个作业存储库吗? - jberet, one wildfly instance, multiple datasources: can I use more than one job-repository? 如何在jar中导出多个同名文件? - How can I export more than one file with the same name in a jar? 如何使用Spring + Hibernate开发可同时在多个数据库上运行的应用程序? - How can I develop an application which will run on more than one database at a same time using Spring + Hibernate?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM