gpt4 book ai didi

java - 以编程方式创建布局以添加到 ViewPager?

转载 作者:行者123 更新时间:2023-12-01 15:05:25 24 4
gpt4 key购买 nike

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

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

   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;

最佳答案

不要膨胀布局,而是创建布局和 View 对象并返回它。

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

关于java - 以编程方式创建布局以添加到 ViewPager?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13021195/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com