[英]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 原则
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.