我正在为平板电脑和智能手机开发应用程序。
我的问题是我在使用手机或平板电脑时是否需要其他课程和重复代码。这是我的代码..
大地/main.xml
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="match_parent">
<fragment class="com.example.example.FragmentMenu"
android:id="@+id/FragmentMenu"
android:layout_weight="30"
android:layout_width="0px"
android:layout_height="match_parent" />
<FrameLayout
android:id="@+id/content_frame"
android:layout_weight="70"
android:layout_width="0px"
android:layout_height="match_parent" />
布局/main.xml
<?xml version="1.0" encoding="utf-8"?>
<fragment xmlns:android="http://schemas.android.com/apk/res/android"
class="com.example.example.FragmentMenu"
android:id="@+id/FragmentMenu"
android:layout_width="match_parent"
android:layout_height="match_parent" />
main.java
public class main extends SherlockFragmentActivity {
private boolean isTablet;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
isTablet = findViewById(R.id.content_frame) != null;
if(isTablet) {
FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
ft.add(R.id.content_frame, new FragmentInicio());
ft.commit();
}
final FragmentMenu frgMenu =(FragmentMenu)getSupportFragmentManager().findFragmentById(R.id.FragmentMenu);
frgMenu.setListMenuListener(new MenuListListener() {
@Override
public void onListMenuSeleccionado(Menu_List c) {
// TODO Auto-generated method stub
Toast.makeText(MainActivity.this, String.valueOf(c.getID()), Toast.LENGTH_SHORT).show();
// HERE IS MY PROBLEM.. WHEN THE DEVICE IS A TABLET THE APP
// OPEN 'FRAGMENTINICIO' OR 'FRAGMENTBLOG',
// BUT WHEN THE DEVICE IS A MOBILE. DO I NEED TO CREATE ANOTHER CLASS AND DUPLICATE CODE?
if(isTablet) {
FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
switch (c.getID()) {
case 0:
ft.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_CLOSE);
ft.replace(R.id.content_frame, new FragmentInicio());
ft.commit();
break;
case 1:
ft.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_FADE);
ft.replace(R.id.content_frame, new FragmentBlog());
ft.commit();
break;
}
} else {
Intent i;
switch (c.getID()) {
case 0:
i = new Intent(main.this, InicioActivity.class);
startActivity(i);
break;
case 1:
i = new Intent(main.this, BlogActivity.class);
startActivity(i);
break;
}
}
}
});
}
}
对不起我的英语不好