在碎片中显示不同的布局

时间:2012-01-03 12:29:34

标签: android android-fragments

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

我的主要活动:

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);
    }
}

详细信息窗格区域:

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;
    }
}

1 个答案:

答案 0 :(得分:0)

如果您对所呈现的不同数据类型使用单独的Fragments,则可能会更好(更干净的设计)。这将使Fragments更清洁,Activity可以创建适当的Fragment并替换容器视图中的前一个Fragment。但是,如果确实希望按照您描述的方式执行此操作,则可以通过其参数BundleFragment将“模式”传递给{{1}}可以使用该信息来扩充适当的布局文件和逻辑。

有关使用参数和片段的详细信息和示例,请参阅此开发者指南:http://developer.android.com/training/basics/fragments/fragment-ui.html