我有一个视图寻呼机以滑动水平方式查看图像。我正在使用我的ImageFragmentAdapter类填充视图分页器,该类扩展了FragmentPagerAdapter。在我的ImageFragmentAdapter .java中,我在这一行中遇到错误
int padding = context.getResources().getDimensionPixelSize(R.dimen.padding_medium);
Error is : padding_medium cannot be resolved or is not a field
我只是用Google搜索了这个问题,我才知道我应该将此行添加到导入中。
import your.application.packagename.R;
但我仍然在这条线上得到错误。任何人都能说出问题是什么。?
ImageFragmentAdapter .java
public class ImageFragmentAdapter extends FragmentPagerAdapter implements
IconPagerAdapter
{
private int[] CONTENT = new int[] {
R.drawable.gallery_photo_1,
R.drawable.gallery_photo_2,
R.drawable.gallery_photo_3,
R.drawable.gallery_photo_4
};
protected static final int[] ICONS = new int[] { R.drawable.marker,
R.drawable.marker, R.drawable.marker, R.drawable.marker };
private int mCount = CONTENT.length;
public ImageFragmentAdapter(FragmentManager fm) {
super(fm);
}
@Override
public Fragment getItem(int position) {
return ImageFragment.newInstance(CONTENT[position % CONTENT.length]);
}
@Override
public int getCount() {
return mCount;
}
@Override
public Object instantiateItem(ViewGroup container, int position) {
Context context = MainActivity.appContext;
ImageView imageView = new ImageView(context);
int padding = context.getResources().getDimensionPixelSize(
R.dimen.padding_medium);//error//
imageView.setPadding(padding, padding, padding, padding);
imageView.setScaleType(ImageView.ScaleType.CENTER_INSIDE);
imageView.setImageResource(CONTENT[position]);
((ViewPager) container).addView(imageView, 0);
return imageView;
}
@Override
public boolean isViewFromObject(View view, Object object) {
return view == ((ImageView) object);
}
@Override
public void destroyItem(ViewGroup container, int position, Object object) {
((ViewPager) container).removeView((ImageView) object);
}
/*
@Override
public CharSequence getPageTitle(int position) {
return ImageFragmentAdapter.CONTENT[position % CONTENT.length];
}
*/
@Override
public int getIconResId(int index) {
return ICONS[index % ICONS.length];
}
public void setCount(int count) {
if (count > 0 && count <= 10) {
mCount = count;
notifyDataSetChanged();
}
}
}
我使用DetailsFragment.java将View分页器填充到fragment_details.xml
public class DetailsFragment extends SherlockFragment
{
public static final String TAG = "detailsFragment";
ImageFragmentAdapter mAdapter;
ViewPager mPager;
PageIndicator mIndicator;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState)
{
//container.removeAllViews();
// TODO Auto-generated method stub
View view = inflater.inflate(R.layout.fragment_details, container,
false);
mAdapter = new ImageFragmentAdapter(getActivity().getSupportFragmentManager());
mPager = (ViewPager) view.findViewById(R.id.pager);
mPager.setAdapter(mAdapter);
mIndicator = (CirclePageIndicator) view.findViewById(R.id.indicator);
mIndicator.setViewPager(mPager);
return view;
}
}
fragment_details.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<android.support.v4.view.ViewPager
android:id="@+id/pager"
android:layout_width="fill_parent"
android:layout_height="0dp"
android:layout_weight="1" />
<com.viewpagerindicator.CirclePageIndicator
android:id="@+id/indicator"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:padding="10dip" />
</LinearLayout>