Android: using fragments programmatically

前端 未结 2 933
被撕碎了的回忆
被撕碎了的回忆 2020-12-28 23:04

For some reasons I can\'t use xml layout files.

But I need to create a tablet android app.

I decided to use fragments.

I want to create the same layo

相关标签:
2条回答
  • 2020-12-28 23:50

    What you're missing is the LayoutInflator. See this example from an activity:

    private void createPolicyTable() {
    
        if (customerInfo.getPolicies() == null || customerInfo.getPolicies().isEmpty()) {
            Log.v(TAG, "no policies were found.");
            return;
        }
    
        LinearLayout policyTable = (LinearLayout)findViewById(R.id.manage_my_policy_policy_table);      
        LayoutInflater inflater = getLayoutInflater();
        for(int i = 0; i < customerInfo.getPolicies().size(); i++) {
            Policy policy = customerInfo.getPolicies().get(i);
    
            LinearLayout row = (LinearLayout)inflater.inflate(R.layout.policy_table_row, null);
    
            ImageView type = (ImageView)row.findViewById(R.id.policy_table_row_type);
    
            if (policy instanceof PersonalVehiclePolicy) {
                type.setImageResource(R.drawable.auto_policy);
            }
            else if (policy instanceof HomeownersPolicy) {
                type.setImageResource(R.drawable.home_policy);
            }
    
            ((TextView)row.findViewById(R.id.policy_table_row_policy_number)).setText(policy.getPolicyNumber());
            ((TextView)row.findViewById(R.id.policy_table_row_amount_due)).setText(policy.getAmountDue());
            ((TextView)row.findViewById(R.id.policy_table_row_due_date)).setText(policy.getDueDate());
    
            row.setOnClickListener(createPolicyDetailsOnClickListener(policy));
    
            policyTable.addView(row);
    
        }
    
    }
    

    and the layout that's being inflated:

    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout
      xmlns:android="http://schemas.android.com/apk/res/android" style="@style/ListItem">
    
        <TableLayout android:layout_width="fill_parent" android:layout_height="wrap_content"
          android:stretchColumns="*">
            <TableRow android:layout_width="fill_parent" android:layout_height="wrap_content" 
              android:paddingBottom="5dp">
                <TextView android:text="@string/type_label" style="@style/RowLabel" />
    
                <ImageView android:id="@+id/policy_table_row_type" android:layout_width="wrap_content"
                  android:layout_height="wrap_content" />         
                <TextView style="@style/RowLabel" android:text="@string/policy_num_label" 
                  android:paddingRight="5dp" />
                <TextView android:id="@+id/policy_table_row_policy_number" style="@style/DefaultText" />
            </TableRow>
    
            <TableRow android:layout_width="fill_parent" android:layout_height="wrap_content" 
              android:paddingBottom="5dp">
                <TextView style="@style/RowLabel" android:text="@string/due_label" android:paddingRight="5dp" />
                <TextView android:id="@+id/policy_table_row_amount_due" style="@style/DefaultText" android:paddingRight="5dp" />
                <TextView style="@style/RowLabel" android:text="@string/on_lc" android:paddingRight="5dp" />
                <TextView android:id="@+id/policy_table_row_due_date" style="@style/DefaultText" />
            </TableRow>
        </TableLayout>
    
    
    </LinearLayout>
    
    0 讨论(0)
  • 2020-12-29 00:01

    I've found the solution.

    (the better way was added to the question. Thanks, p-lo)

    If you want to place more than one fragment on activity you should create layouts for each fragment:

        private View createUI() {
            LinearLayout layout = new LinearLayout(this);
    
            layout.setOrientation(LinearLayout.HORIZONTAL);
            layout.setLayoutParams(new LinearLayout.LayoutParams(AbsListView.LayoutParams.FILL_PARENT, AbsListView.LayoutParams.FILL_PARENT));
    
    
            LinearLayout innerLayout1 = new LinearLayout(this);
            innerLayout1.setLayoutParams(new LinearLayout.LayoutParams(300, ViewGroup.LayoutParams.FILL_PARENT));
            innerLayout1.setId(ITEMS_LIST_ID);
            {
                FragmentTransaction fragmentTransaction = getFragmentManager().beginTransaction();
                fragmentTransaction.add(ITEMS_LIST_ID, new ItemsList());
                fragmentTransaction.commit();
            }
            layout.addView(innerLayout1);
    
            LinearLayout innerLayout2 = new LinearLayout(this);
            innerLayout2.setLayoutParams(new LinearLayout.LayoutParams(300, ViewGroup.LayoutParams.FILL_PARENT));
            innerLayout2.setId(ITEM_DETAILS_ID);
            {
                FragmentTransaction fragmentTransaction = getFragmentManager().beginTransaction();
                fragmentTransaction.add(ITEM_DETAILS_ID, new ItemDetails());
                fragmentTransaction.commit();
            }
            layout.addView(innerLayout2);
    
            return layout;
    
    
        }
    
    0 讨论(0)
提交回复
热议问题