android - 尝试制作灵活的用户界面

标签 android user-interface android-fragments

我正在尝试制作一个具有灵活 UI 的应用。 我已经为手机设备实现了(我有一个 Activity 和多个 fragment ),我所做的是:主要 fragment 是一个仪表板,当我点击它的一个按钮时,他的仪表板被替换通过一个新的 fragment (被点击的特征)。这是代码:

仪表板 fragment :

public class DashboardFragment extends Fragment {

    GridView gridView;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        return inflater.inflate(R.layout.activity_dashboard, container, false);
    }

    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onActivityCreated(savedInstanceState);
        gridView=(GridView)getView().findViewById(R.id.dashboard_grid);
        gridView.setAdapter(new ImageAdapter(getActivity()));
        gridView.setOnItemClickListener(new android.widget.AdapterView.OnItemClickListener() {

            @Override
            public void onItemClick(AdapterView<?> parent, View view,
                    int position, long id) {

                FragmentTransaction transaction = getFragmentManager().beginTransaction();
                Fragment fragment = null ;

                switch (position) {
                case 0:
                    fragment = new TestFragment();
                    break;
                case 1 :
                    fragment = new TestFragment();
                    break;
                case 2 :
                    fragment = new TestFragment();
                    break;
                case 3 :
                    fragment = new TestFragment();
                    break;
                case 4 :
                    fragment = new TestFragment();
                    break;
                }
                transaction.replace(R.id.container, fragment);
                transaction.addToBackStack(null);
                transaction.commit();
            }
        });

    }
}

和我的主要 Activity :

public class MainActivity extends FragmentActivity{
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        if (findViewById(R.id.container) != null) {
            if (savedInstanceState != null) {
                return;
            }

            // Create an instance of ExampleFragment
            DashboardFragment firstFragment = new DashboardFragment();
            firstFragment.setArguments(getIntent().getExtras());
            getSupportFragmentManager().beginTransaction().add(R.id.container, firstFragment).commit();
        }
    }


}

现在,我想要的是调整此代码并使用平板电脑布局,左侧是仪表板,右侧是所选 fragment ,如下所示:

enter image description here

我能做什么?我已经尝试适应 this例如,但我不能,因为他们只更新 fragment ,而不是替换它。

最佳答案

检查这个great article about multi-pane development . 它还包括一个示例(第 10 节和第 11 节)

基本上您可以检查当前布局中是否有您的“fragment B”的 fragment 元素。如果是,您只需更新其内容,如果不是,则启动一个布局中包含它的 Activity ,或替换当前布局中的一个 Activity 。

DetailFragment fragment = (DetailFragment) getFragmentManager().findFragmentById(R.id.detail_frag);
if (fragment==null || ! fragment.isInLayout()) {
    // start new Activity or replace
}
else {
    fragment.update(...);
} 

关于android - 尝试制作灵活的用户界面,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25315885/

相关文章:

java - Android - 使用标志 Activity 单击推送通知后打开或重新启动应用程序

jquery ui 对话框在按钮前面添加图像

user-interface - 标准 ML 的 GUI?

android - 防止 GoogleApiClient 在 onStop() 上断开连接

android - 我们可以在 fragment 中覆盖 dispatchTouchEvent(MotionEvent me) 吗?

android - SherlockFragmentActivity 与多个 ListFragments 和 SQLite 游标战斗

android - editText 未填充 gridLayout 中的整个列

android - Android 导航组件中处理 onBackPressed

Android:如何构建多级选项卡

user-interface - MySQL Workbench GUI 背后的底层技术是什么?