ViewPager与简单的寻呼机适配器无法正常工作

时间:2014-04-20 08:54:42

标签: android android-viewpager android-fragmentactivity listadapter android-pageradapter

我有一个里面有ViewPager的片段;这是我的适配器类。

public class QuotesAdapter extends PagerAdapter {
    private LayoutInflater inflater;
    private Context context;
    private Drawable favChecked, favUnChecked;

    public QuotesAdapter(Context context) {
        this.context = context;
        this.inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        favChecked = getResources().getDrawable(R.drawable.fav_checked);
        favUnChecked = getResources().getDrawable(R.drawable.fav_unchecked);
    }

    @Override
    public int getCount() {
        return quoteList.size();
    }

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

    @Override
    public Object instantiateItem(ViewGroup container, final int position) {
        View view = inflater.inflate(R.layout.inflater_main_listview, container);
        final TextView quoteTxt = (TextView) view.findViewById(R.id.textView);
        final ImageView shareButton = (ImageView) view.findViewById(R.id.share_img);
        final ImageView favButton = (ImageView) view.findViewById(R.id.fav_img);
        quoteTxt.setText(quoteList.get(position));
        if (AppConstants.favListDB.get(position).isFavorites()) {
            favButton.setImageDrawable(favChecked);
        } else {
            favButton.setImageDrawable(favUnChecked);
        }
        favButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //My onclick code
            }
        });

        shareButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                 //My onclick code
            }
        });

        ((ViewPager) container).addView(view, ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
        return view;
    }

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

请帮助我哪里出错了。 将此适配器类设置为ViewPager时,不会显示任何内容。

请帮忙!

提前致谢。

0 个答案:

没有答案