简体   繁体   English

在活动中创建片段

[英]Create a fragment inside an activity

In my my main activity layout(first layout) I have dynamically set up text views. 在我的主要活动布局(第一个布局)中,我动态设置了文本视图。 Now, when these text views are clicked, I want to display another layout(second layout) again having text views(dynamically set). 现在,当单击这些文本视图时,我想再次显示具有文本视图(动态设置)的另一个布局(第二个布局)。 When these text views are clicked I want the text of this text view of this second layout to be set as the text of the text view of the first layout which was clicked. 当单击这些文本视图时,我希望将此第二布局的文本视图的文本设置为被单击的第一个布局的文本视图的文本。

For eg. 例如。

first layout -> [click] [click] ->be the two text views in the layout. 第一个布局-> [单击] [单击]->是布局中的两个文本视图。 Suppose I click the first, then I want to display 假设我单击第一个,然后要显示

[english] [maths] [science]. [英语] [数学] [科学]。 And suppose I click on english then I want to set the screen to display the first layout again but replacing the text of the view clicked 并假设我单击英语,然后我想要设置屏幕以再次显示第一个布局,但是替换所单击视图的文本

first layout -> [english] [click]. 第一个布局-> [english] [click]。

Now The Problem I have set up the first layout as main_layout. 现在,问题我已经将第一个布局设置为main_layout。 Now I need to know how can a display the second layout in my fragment_layout and then return back to the main_activity. 现在,我需要知道如何在fragment_layout中显示第二个布局,然后返回到main_activity。 I want to save the instance of the main_layout as it goes from main to fragment and back to main. 我想保存main_layout的实例,因为它从main到fragment再回到main。

Function called on clicking first layout's text view 单击第一个布局的文本视图时调用的函数

         View.OnClickListener timetable_click_listen = new View.OnClickListener() {

    @Override
    public void onClick(View timetable_viewtext) {

        setContentView(R.layout.fragment_time_table);


        int no_subjects;

        /*opens the database and finds out the number of subjects user has entered
        Opening database and getting the number of subjects*/
        SQLiteDatabase db = openOrCreateDatabase("DATABASE",MODE_PRIVATE,null);     

        if(db==null)
        {
            Log.d("Error in TimeTable","There was a error in Opening Database");
        }

        else{
            Log.d("Inside TimeTable Onclick","Opening Database");
        }

        Cursor c= db.rawQuery("SELECT Num_subjects FROM DETAILS;",null);

        c.moveToFirst();

        no_subjects = c.getInt(c.getColumnIndex("Num_subjects"));

        c.close();

        /*Getting the names of the subjects so as to put that name
         * as the button's text
         */

        String[] name = new String[no_subjects];

        c = db.rawQuery("SELECT * FROM SUBJECTS;",null);

        c.moveToFirst();

        for(int i=0;i<no_subjects;i++)
        {   
            name[i] = c.getString(c.getColumnIndex("Name"));
            c.moveToNext();
        }

        c.close();
        db.close();

        LinearLayout linearlayout = (LinearLayout) findViewById(R.id.timetable_fragment);
        TextView ed;
        List<TextView> allEds = new ArrayList<TextView>();


        for (int i = 0; i < no_subjects; i++) {   

            ed = new TextView(TimeTable.this);

            allEds.add(ed);

            ed.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT,
                                                            LinearLayout.LayoutParams.WRAP_CONTENT));

            ed.setId(1000+i);

            ed.setText(name[i]);

            tempId = timetable_viewtext.getId();

            ed.setClickable(true);
            ed.setOnClickListener(subname_click_listen);

            ((LinearLayout)linearlayout).addView(ed);

        }

    }
};

Now I am not understanding what to do in the function of the second layout's view.onclick() 现在我不明白在第二个布局的view.onclick()函数中该怎么做。

Thanks in advance 提前致谢

You should probably use a FragmentManager and a FragmentTransaction . 您可能应该使用FragmentManagerFragmentTransaction To do this you will need to extend the Fragment class. 为此,您将需要扩展Fragment类。 For example, you might want to make a Fragment to display each layout. 例如,您可能需要制作一个片段以显示每个布局。 These fragments will have their own layout file so you can customize how they look. 这些片段将具有自己的布局文件,因此您可以自定义它们的外观。 You will need to provide a container to hold the fragments. 您将需要提供一个容纳碎片的容器。 I used a FrameLayout called fragment_container . 我使用了一个称为fragment_containerFrameLayout To initialize the fragment, use the FragmentTransaction.add() method and use the FragmentTransaction.replace() method to switch between them. 要初始化片段,请使用FragmentTransaction.add()方法,并使用FragmentTransaction.replace()方法在它们之间进行切换。

FragmentManager fm = getFragmentManager();
FragmentTransaction ft = fm.beginTransaction();
MyFragment fragment = new MyFragment();
ft.add(R.id.fragment_container, fragment);
ft.commit();

Check out the API for more info: FragmentTransaction 查看API以获取更多信息: FragmentTransaction

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM