Android 在水平滚动视图或Viewpager中动态插入3个图像
下面是我要找的图片。目前我正在使用查看寻呼机和圆圈指示器。在视图页面中,它仅显示一幅图像 我想在一个viewpager中显示三张图片,如图所示。当我滑动该页面时,再次在FormServer中加载了三个不同的图像,其中包含下面的文本。如何做到这一点?关于这一点有什么想法,或者有什么其他方法可以实现这一点 我的代码如下所示 我在这里添加了一些xml布局:homespizzaview.xmlAndroid 在水平滚动视图或Viewpager中动态插入3个图像,android,android-layout,android-intent,android-widget,android-scrollview,Android,Android Layout,Android Intent,Android Widget,Android Scrollview,下面是我要找的图片。目前我正在使用查看寻呼机和圆圈指示器。在视图页面中,它仅显示一幅图像 我想在一个viewpager中显示三张图片,如图所示。当我滑动该页面时,再次在FormServer中加载了三个不同的图像,其中包含下面的文本。如何做到这一点?关于这一点有什么想法,或者有什么其他方法可以实现这一点 我的代码如下所示 我在这里添加了一些xml布局:homespizzaview.xml <LinearLayout android:id="@+id/linearLayou
<LinearLayout
android:id="@+id/linearLayoutbutton"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dip" >
<android.support.v4.view.ViewPager
android:id="@+id/pagerMenu"
android:layout_width="0dip"
android:layout_height="100dip"
android:layout_weight="1"
android:layout_marginLeft="10dip"
android:layout_marginRight="10dip">
</android.support.v4.view.ViewPager>
</LinearLayout>
<RelativeLayout
android:id="@+id/relativeLayoutDot1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginTop="5dip">
<com.viewpagerindicator.CirclePageIndicator
android:id="@+id/indicatorMenu"
android:layout_width="fill_parent"
android:layout_height="25dip"
android:layout_marginLeft="10dip"
android:layout_marginRight="10dip"
android:padding="10dip" />
</RelativeLayout>
MenuPagerAdapter.java
public class MenuPagerAdapter extends PagerAdapter {
public int getCount() {
return 3;
}
public Object instantiateItem(View collection, int position) {
ImageView image = new ImageView(collection.getContext());
image.setPadding(20, 0, 20, 0);
int resId = 0;
switch (position) {
case 0:
resId = R.drawable.promotion1;
break;
case 1:
resId = R.drawable.promotion2;
break;
case 2:
resId = R.drawable.promotion3;
break;
}
image.setImageResource(resId);
((ViewPager) collection).addView(image, 0);
return image;
}
@Override
public void destroyItem(View arg0, int arg1, Object arg2) {
((ViewPager) arg0).removeView((View) arg2);
}
@Override
public boolean isViewFromObject(View arg0, Object arg1) {
return arg0 == ((View) arg1);
}
@Override
public Parcelable saveState() {
return null;
}
}
下面是更新代码,它用下面的文本视图显示单个图像视图。我正在寻找动态图像和文本。。表示在1视图寻呼机中有3个图像和3个文本。。当前显示1个图像视图,其中包含以下文本。。我在单视图寻呼机中寻找3幅图像。。如果我添加线性布局,它会显示3个图像。但我添加了相对布局,显示单个图像。。知道我做了什么吗
public Object instantiateItem(View collection, int position) {
RelativeLayout lLayout;
RelativeLayout.LayoutParams relativeLayout;
lLayout = new RelativeLayout (collection.getContext());
relativeLayout = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.FILL_PARENT, RelativeLayout.LayoutParams.FILL_PARENT);
for (int i = 0; i < 3; i++) {
image = new ImageView(collection.getContext());
image.setPadding(20, 0, 20, 0);
image.setId(1);
int res = 0;
switch (0) {
case 0:
res = R.drawable.menu_antipasti;
break;
case 1:
res = R.drawable.menu_combos;
break;
case 2:
res = R.drawable.menu_drinks;
break;
}
image.setImageResource(res);
lLayout.addView(image);
textview = new TextView(collection.getContext());
textview.setTextColor(Color.BLACK);
textview.setId(2);
textview.setText("pizza");
textview.setPadding(60, 0, 20, 0);
relativeLayout.addRule(RelativeLayout.BELOW, image.getId());
lLayout.addView(textview, relativeLayout);
}
((ViewPager) collection).addView(lLayout, 0);
return lLayout;
}
public对象实例化项(视图集合,int位置){
相对论;
RelativeLayout.LayoutParams RelativeLayout;
lLayout=新的RelativeLayout(collection.getContext());
relativeLayout=新的relativeLayout.LayoutParams(relativeLayout.LayoutParams.FILL\u父级,relativeLayout.LayoutParams.FILL\u父级);
对于(int i=0;i<3;i++){
image=newImageView(collection.getContext());
设置填充(20,0,20,0);
image.setId(1);
int res=0;
开关(0){
案例0:
res=R.drawable.menu\u antipasti;
打破
案例1:
res=R.drawable.menu\u组合;
打破
案例2:
res=R.可提取菜单\饮料;
打破
}
image.setImageResource(res);
lLayout.addView(图像);
textview=新的textview(collection.getContext());
textview.setTextColor(Color.BLACK);
setId(2);
textview.setText(“比萨饼”);
设置填充(60,0,20,0);
relativeLayout.addRule(relativeLayout.down,image.getId());
添加视图(文本视图,相对视图);
}
((ViewPager)集合).addView(lLayout,0);
返回lLayout;
}
现在您将从实例化Item
方法返回ImageView
。如果您想在一个页面上有3个图像,您需要创建布局,在其上放置3个图像并返回。例如:
public Object instantiateItem(View collection, int position) {
LinearLayour images = new LinearLayout(collection.getContext());
for (int i = 0; i < 3; i++) {
ImageView image = new ImageView(collection.getContext());
image.setPadding(20, 0, 20, 0);
images.addView(image);
int res = someResource depending on i and position.
image.setImageResource(res);
}
((ViewPager) collection).addView(images, 0);
return images;
}
public对象实例化项(视图集合,int位置){
LinearLayour images=newlinearlayout(collection.getContext());
对于(int i=0;i<3;i++){
ImageView image=newImageView(collection.getContext());
设置填充(20,0,20,0);
images.addView(image);
int res=someResource,取决于i和位置。
image.setImageResource(res);
}
((ViewPager)集合).addView(图像,0);
返回图像;
}
更新了下面的代码。。我对安卓系统不太在行&对这个领域还不熟悉。提前感谢如果我添加了LinearLayout它在view pager中显示3个图像,但是如果我添加了相对布局,它只显示单个图像。。知道我做了什么吗。。下面是我的工作代码,一个texview下面有一个图像,如上图所示。
public Object instantiateItem(View collection, int position) {
LinearLayour images = new LinearLayout(collection.getContext());
for (int i = 0; i < 3; i++) {
ImageView image = new ImageView(collection.getContext());
image.setPadding(20, 0, 20, 0);
images.addView(image);
int res = someResource depending on i and position.
image.setImageResource(res);
}
((ViewPager) collection).addView(images, 0);
return images;
}