显示碎片不同的布局 [英] Show different layouts in fragmentation

查看:111
本文介绍了显示碎片不同的布局的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有我想要当从不同的片段点击列表项每次加载不同的布局,在片段的问题。意味着我有一个屏幕上的两个片段,第一个是ListView和另一个我想根据从列表视图中单击项目意味着如果我点击关于我们项目展示不同的布局则详细信息窗格中显示的布局关于我们。如果点击在联系我们的详细信息窗格将改为相应。目前的布局是表示在运行时创建的图像。请建议我正确的结果。

我的主要活动:

 公共类FragmentTestActivity扩展活动实现OnItemClickListener {
    / **当第一次创建活动调用。 * /
    @覆盖
    公共无效的onCreate(捆绑savedInstanceState){
        super.onCreate(savedInstanceState);
        的setContentView(R.layout.main);        ListView的L =(ListView控件)findViewById(R.id.number_list);
        ArrayAdapter<串GT;数=新ArrayAdapter<串GT;(getApplicationContext()
                android.R.layout.simple_list_item_1,
                新的String [] {
            关于我们,联系我们,服务,四有,五,六个一
        });
        l.setAdapter(数字);
        l.setOnItemClickListener(本);
    }    / **
     *添加一个片段给我们的协议栈有n个机器人在它
     * /
    私人无效stackAFragment(INT nAndroids){
        片f =新TestFragment(nAndroids);        FragmentTransaction英尺= getFragmentManager()调用BeginTransaction()。
        ft.replace(R.id.the_frag,F);
        ft.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN);
        ft.addToBackStack(NULL);
        ft.commit();
    }    / **
     *当数被点击调用
     * /
    公共无效onItemClick(适配器视图<>母公司,观景,INT位置,长的id){
        stackAFragment(位置);
    }
}

详细信息窗格区域:

 公共类TestFragment扩展片段{
    私人诠释nAndroids;    公共TestFragment(){    }   / **
    *对于被明确创建构造
    * /
   公共TestFragment(INT nAndroids){
        this.nAndroids = nAndroids;
    }    / **
     *如果我们正在与保存状态创建,恢复我们的国家
     * /
    @覆盖
    公共无效的onCreate(捆绑保存){
        super.onCreate(保存);
        如果(NULL!=保存){
            nAndroids = saved.getInt(nAndroids);
        }
    }    / **
     *减机器人的数量要显示
     * /
    @覆盖
    公共无效的onSaveInstanceState(捆绑toSave){
        toSave.putInt(nAndroids,nAndroids);
    }    / **
     *做一个网格,用正机器人填补它
     * /
    @覆盖
    公共查看onCreateView(LayoutInflater吹气,ViewGroup中的容器中,包保存){
        INT N;        上下文C = getActivity()getApplicationContext()。
        的LinearLayout L =新的LinearLayout(C);        为(n = 0时; N&下; nAndroids; N ++){
            ImageView的I =新ImageView的(C);
            i.setImageResource(R.drawable.android);
            l.addView(ⅰ);
        }
        返回升;
    }
}


解决方案

您可能会更好过(清洁设计),如果使用单独的片段为不同的数据类型你是presenting。这将让你的片段清洁和的活动可以只创建适当的片段并更换previous一个在容器视图。不过,如果你的真正的想这样做,你所描述的方式,你可以通过它的参数传递一个模式的片段 捆绑片段可以使用该信息充气适当布局文件和逻辑。

见细节和例子此开发人员指南使用的参数和片段: http://developer.android.com/training/basics/fragments/fragment-ui.html

I have a problem that I want to load different layouts every time in a fragment when a list Item is clicked from different fragment. Means I have two fragments on a screen, first one is ListView and on another I want to show different layouts according to clicked item from list view means If I clicked on About Us item then the Details pane shows the layout of About Us and If clicked on Contact Us the Details Pane will changed to accordingly. Currently a layout is showing images which is created at run time. Please suggest me for the right result.

My Main Activity:

public class FragmentTestActivity extends Activity implements OnItemClickListener {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        ListView l = (ListView) findViewById(R.id.number_list);
        ArrayAdapter<String> numbers = new ArrayAdapter<String>(getApplicationContext(),
                android.R.layout.simple_list_item_1, 
                new String [] {
            "About US", "Contact Us", "Services", "four", "five", "six"
        });
        l.setAdapter(numbers);
        l.setOnItemClickListener(this);
    }

    /**
     * Add a Fragment to our stack with n Androids in it
     */
    private void stackAFragment(int nAndroids) {
        Fragment f = new TestFragment(nAndroids);

        FragmentTransaction ft = getFragmentManager().beginTransaction();
        ft.replace(R.id.the_frag, f);
        ft.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN);
        ft.addToBackStack(null);
        ft.commit();
    }

    /**
     * Called when a number gets clicked
     */
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        stackAFragment(position);
    }
}

Details Pane Area:

public class TestFragment extends Fragment {
    private int nAndroids;

    public TestFragment() {

    }

   /**
    * Constructor for being created explicitly
    */
   public TestFragment(int nAndroids) {
        this.nAndroids = nAndroids;
    }

    /**
     * If we are being created with saved state, restore our state
     */
    @Override
    public void onCreate(Bundle saved) {
        super.onCreate(saved);
        if (null != saved) {
            nAndroids = saved.getInt("nAndroids");
        }
    }

    /**
     * Save the number of Androids to be displayed
     */
    @Override
    public void onSaveInstanceState(Bundle toSave) {
        toSave.putInt("nAndroids", nAndroids);
    }

    /**
     * Make a grid and fill it with n Androids
     */
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle saved) {
        int n;

        Context c = getActivity().getApplicationContext();
        LinearLayout l = new LinearLayout(c);

        for (n = 0; n < nAndroids; n++) {
            ImageView i = new ImageView(c);
            i.setImageResource(R.drawable.android);
            l.addView(i);
        }
        return l;
    }
}

解决方案

You would probably be better off (cleaner design) if you used separate Fragments for the different data types you are presenting. This will keep your Fragments cleaner and the Activity can just create the appropriate Fragment and replace the previous one in the container view. However, if you really want to do it the way you describe, you can pass a "mode" to the Fragment via its argument Bundle and the Fragment can use that information to inflate the appropriate layout file and logic.

See this developer guide for details and examples with using arguments and fragments: http://developer.android.com/training/basics/fragments/fragment-ui.html

这篇关于显示碎片不同的布局的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆