0

我',试图在我的 Android 应用程序中使用带有水平滚动的简单 PagerAdapter。我尝试在页面中有一个带有图像的网格。实现这一目标的最简单方法是什么?

我尝试这样的事情(它不起作用,:这是我的 MainActivity.onCreate

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    createHeroList();

    LayoutInflater inflater = LayoutInflater.from(this);
    List<View> pages = new ArrayList<View>();

    View page = inflater.inflate(R.layout.page, null);
    pages.add(page);

    page = inflater.inflate(R.layout.page, null);
    pages.add(page);

    page = inflater.inflate(R.layout.page, null);
    pages.add(page);

    SamplePagerAdapter pagerAdapter = new SamplePagerAdapter(pages);
    ViewPager viewPager = (ViewPager) findViewById(R.id.pager);
    viewPager.setAdapter(pagerAdapter);
    viewPager.setCurrentItem(1);
    setContentView(viewPager);
    initImageLoader(getApplicationContext());
}

这是我的图像适配器

public class ImageAdapter extends BaseAdapter {

ImageLoader imageLoader = ImageLoader.getInstance();
private Context mContext;
ArrayList<String> HeroList;
String[] imageUrls;

// create a new ImageView for each item referenced by the Adapter
public View getView(int position, View convertView, ViewGroup parent) {
    // TODO Auto-generated method stub

    View grid;
    DisplayImageOptions options = new DisplayImageOptions.Builder()
            .showStubImage(R.drawable.ic_stub)
            .showImageForEmptyUri(R.drawable.ic_empty)
            .showImageOnFail(R.drawable.ic_error).cacheInMemory()
            .cacheOnDisc().bitmapConfig(Bitmap.Config.RGB_565).build();
    if (convertView == null) {
        grid = new View(mContext);
        // LayoutInflater inflater = getLayoutInflater();
        LayoutInflater inflater = (LayoutInflater) mContext
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        grid = inflater.inflate(R.layout.cellgrid, parent, false);
    } else {
        grid = (View) convertView;
    }
    ImageView imageView = (ImageView) grid.findViewById(R.id.imagepart);
    TextView textView = (TextView) grid.findViewById(R.id.textpart);
    imageLoader.displayImage(imageUrls[position], imageView, options);
    textView.setText(HeroList.get(position));
    return grid;
}
}

和我的寻呼机适配器。

    public class SamplePagerAdapter extends PagerAdapter{

    List<View> pages = null;
    ArrayAdapter<String> adapter;
    ArrayList<String> NamesList = new ArrayList<String>();
    int HeroCount = 98;
    ImageLoader imageLoader = ImageLoader.getInstance();
    String[] imageUrls;

    public SamplePagerAdapter(List<View> pages){
        this.pages = pages;
    }

    @Override
    public Object instantiateItem(View collection, int position){
        View v = pages.get(position);
        GridView gridview = (GridView) v.findViewById(R.id.gridview);
        Resources myResources = v.getResources();
        imageUrls = myResources.getStringArray(R.array.Urls);
        gridview.setAdapter(new ImageAdapter(v.getContext()));
        gridview.setOnItemClickListener(gridviewOnItemClickListener);       
        ((ViewPager) collection).addView(v, 0);
        return v;
    }

    private GridView.OnItemClickListener gridviewOnItemClickListener = new GridView.OnItemClickListener() {

        @Override
        public void onItemClick(AdapterView<?> parent, View v, int position,
                long id) {
            // TODO Auto-generated method stub
            // Sending image id to FullScreenActivity
            Intent i = new Intent(v.getContext(),
                    HeroInformation.class);
            i.putExtra("HeroName", NamesList.get(position));
            i.putExtra("HeroUrl", imageUrls[position]);
            i.putExtra("HeroNum", position);
            startActivity(i);
        }
    };
}
4

0 回答 0