我有一个视图寻呼机以水平滑动方式查看图像。我正在使用扩展 FragmentPagerAdapter 的 ImageFragmentAdapter 类填充视图寻呼机。在我的 ImageFragmentAdapter .java 中,我在这一行出现错误
int padding = context.getResources().getDimensionPixelSize(R.dimen.padding_medium);
Error is : padding_medium cannot be resolved or is not a field
我刚刚搜索了这个问题,我知道我应该将此行添加到导入中。
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;
}
}
片段详细信息.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>