android滑动左侧内容?

时间:2014-03-21 17:05:52

标签: android

我有jSON数组内容图片url和标题,列表内容超过100 object

这是示例代码:

[{"url":"http:\/\/web.com\/image\/1.jpg","cap":"image 1"},{"url":"http:\/\/web.com\/image\/2.jpg","cap":"image 2"},{"url":"http:\/\/web.com\/image\/3.jpg","cap":"image 3"}]

我正在寻找的是:

enter image description here

确定其必须内容左右分页滑动图片和标题

有没有asapter能做到的?任何人都可以引导我使用pagerfragment或其他任何东西吗?

请提出任何建议吗?

1 个答案:

答案 0 :(得分:1)

您可以使用此AndroidViewPagerGallery

public class MainActivity extends Activity {
  @Override
  public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    ViewPager viewPager = (ViewPager) findViewById(R.id.view_pager);
    ImageAdapter adapter = new ImageAdapter(this);
    viewPager.setAdapter(adapter);
  }
}

和此适配器,您可以自定义它以创建布局并更改图片和标题

public class ImageAdapter extends PagerAdapter {
    Context context;
    private int[] GalImages = new int[] {
        R.drawable.one,
        R.drawable.two,
        R.drawable.three
    };
    ImageAdapter(Context context){
        this.context=context;
    }
    @Override
    public int getCount() {
      return GalImages.length;
    }

    @Override
    public boolean isViewFromObject(View view, Object object) {
      return view == ((ImageView) object);
    }

    @Override
    public Object instantiateItem(ViewGroup container, int position) {
      ImageView imageView = new ImageView(context);
      int padding = context.getResources().getDimensionPixelSize(R.dimen.padding_medium);
      imageView.setPadding(padding, padding, padding, padding);
      imageView.setScaleType(ImageView.ScaleType.CENTER_INSIDE);
      imageView.setImageResource(GalImages[position]);
      ((ViewPager) container).addView(imageView, 0);
      return imageView;
    }

    @Override
    public void destroyItem(ViewGroup container, int position, Object object) {
      ((ViewPager) container).removeView((ImageView) object);
    }
  }