插入动态3个图像以水平滚动视图或Viewpager(Insert 3 images dynamica

2019-08-03 16:22发布

有什么我要寻找如下所示的图像。 目前我使用视图寻呼机的圆圈指示符。 鉴于页它是仅示出单个图像。

我想在一个viewpager三幅图像,如图照片。 当我滑到该网页,再而三个的不同的图像加载表格服务器下面的文字。 这该怎么做? 任何有关本或其他任何方式来实现这个想法?

我的代码如下所示。

我在这里补充一些XML布局:homespizzaview.xml

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

HomeView的.java

 public class HomeView extends Activity {

    adaptermenu = new MenuPagerAdapter();
    pagerMenu = (ViewPager)findViewById(R.id.pagerMenu);
    pagerMenu.setAdapter(adaptermenu);
    pagerMenu.setCurrentItem(0);
    pagerMenu.setOffscreenPageLimit(adapter.getCount());
    pagerMenu.getAdapter().notifyDataSetChanged();
    pagerMenu.setPageMargin(15);

    indicator  = (CirclePageIndicator)findViewById(R.id.indicatorMenu);
    indicator.setViewPager(pagerMenu);

    final float density1 = getResources().getDisplayMetrics().density;
    indicator.setRadius(5 * density1);
    indicator.setPageColor(0xFF000000);
    indicator.setFillColor(0xFF888888);
    indicator.setStrokeWidth(2 * density1);

    //We set this on the indicator, NOT the pager
    indicator.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {             
        @Override
        public void onPageSelected(int position) {
         //   Toast.makeText(HomeSpizzaView.this, "Changed to page " + position, Toast.LENGTH_SHORT).show();
            pagerMenu.setCurrentItem(position, false);            
        }
        @Override
        public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
        }
        @Override
        public void onPageScrollStateChanged(int state) {               
        }
    });

    indicator.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View v) {

            pagerMenu.setCurrentItem(0);  
          Log.i("", "getCurrentItem"+  mPager.getCurrentItem());
          pagerMenu.getAdapter().notifyDataSetChanged();
        }
    });

       }
    } 

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

这里是被示出具有下面的TextView单个图像视图更新的代码。 我期待的动态图像和文字..意味着在1个视图寻呼机3张图片包含有3文本..目前其示出1倍的图像视图低于文本..我寻找在单个viewpager 3个图像..如果我添加线性布局它示出了3图像。 但我添加相对布局其示出单个图像..任何知道我MADE MISTIKE。

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

Answer 1:

现在你回来ImageViewinstantiateItem方法。 如果你想拥有1页上的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;
}


文章来源: Insert 3 images dynamically to horizontal scrollview or Viewpager