0

我正在开发一个家庭替换应用程序,我正在使用 viewpager 来拥有三个带有横向导航的 3 布局。问题是这些布局存储在三个不同的布局资源文件(layout1.xml、layout2.xml 和 layout3.xml)中,而不是它们,我想为每个屏幕动态创建内容。

这是 Adapter 的摘录,其中说明了每个页面的内容:

   public Object instantiateItem(View collection, int position) {
        LayoutInflater inflater = (LayoutInflater) collection.getContext()
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        int resId = 0;
        switch (position) {
        case 0:
            resId = R.layout.layout1;
            break;
        case 1:
            resId = R.layout.layout2;
            break;
        case 2:
            resId = R.layout.layout3;
            break;
        }
        View view = inflater.inflate(resId, null);
        ((ViewPager) collection).addView(view, 0);
        return view;
    }

如何以编程方式创建一个简单的布局并将其放置在我的一个屏幕中?我一直在努力解决这个问题,找不到任何关于如何做到这一点的线索,任何指向我正确方向的信息都会很有用。


编辑:

这就是我现在正在尝试的,我不认为我离得很远,但是所有屏幕都是空白的:

Context context = collection.getContext();
LinearLayout layout = new LinearLayout(context);
TextView view = new TextView(context);
switch (position) {
    case 0:
       view.setText("some text");
       view.setPadding(5, 5, 5, 5);
       view.setLayoutParams(new LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT));
       break;
    case 1:
       view.setText("some text");
       view.setPadding(5, 5, 5, 5);
       view.setLayoutParams(new LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT));
       break;
    case 2:
       view.setText("some text");
       view.setPadding(5, 5, 5, 5);
       view.setLayoutParams(new LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT));
       break;
}
layout.addView(view);
return layout;
4

1 回答 1

1

不要膨胀布局,而是创建布局和视图的对象并将其返回。

位置 1 的示例,

LinearLayout layout = new LinearLayout(context);
 switch (position) {
        case 0:
           TextView tv = new TextView(context);
           tv.setText("some text");
           tv.setPadding(5, 5, 5, 5);
           tv.setLayoutParams(new LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT));
           // And so on customize and create as many views as you want. 

           // And finally add them to your layout
          layout.addView(tv);
          return layout;

        case 1:
          .
          .
          .
        case 2:
          .
          .
          .
}

更新:

在您更新问题后编辑答案。

我已经获取了您的代码并对其进行了修改。这必须有效。

@Override
public Object instantiateItem(View container, int position) {
    Context context = collection.getContext();
    LinearLayout layout = new LinearLayout(context);
    TextView view = new TextView(context);

    switch (position) {

    case 0:
       view.setText("some text");
       view.setPadding(5, 5, 5, 5);
       view.setLayoutParams(new LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT));
       break;
    case 1:
       view.setText("some text");
       view.setPadding(5, 5, 5, 5);
       view.setLayoutParams(new LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT));
       break;
    case 2:
       view.setText("some text");
       view.setPadding(5, 5, 5, 5);
       view.setLayoutParams(new LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT));
       break;
    }
    layout.addView(view);
    ((ViewPager) container).addView(layout, 0); // This is the line I added
    return layout;
}
于 2013-01-08T05:23:16.457 回答