我正在尝试使用fragment
在viewpager
中创建幻灯片图片。
我遇到了一些问题,我无法获得getActivity()
功能,
之前我用过
adapter = new Home_imageadapter (this, imageResource);
因为我的许多错误都将其更改为
adapter = new Home_imageadapter (getActivity (), imageResource);
现在只发生一个错误。
为什么以及如何解决这个问题?
public class OtherFragment extends Fragment {
private ViewPager viewPager;
private Beranda_imageadapter adapter;
private LinearLayout sliderdotspanel;
private int dotscount;
private ImageView[] dots;
private int[] imageResource = {
R.drawable.slide3,
R.drawable.slide1,
R.drawable.slide2
};
public OtherFragment() {
// Required empty public constructor
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View rootView = inflater.inflate(R.layout.fragment_other, container, false);
viewPager = (ViewPager) rootView.findViewById(R.id.viewpager);
sliderdotspanel = (LinearLayout) rootView.findViewById(R.id.viewPagerCountDots);
return rootView;
adapter = new Beranda_imageadapter(getActivity(), imageResource);
viewPager.setAdapter(adapter);
dotscount = adapter.getCount();
dots = new ImageView[dotscount];for (int i = 0; i < dotscount; i++) {
dots[i] = new ImageView(getActivity());
dots[i].setImageDrawable(ContextCompat.getDrawable(getActivity().getApplicationContext(), R.drawable.nonactive_dot));
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
params.setMargins(8, 0, 8, 0);
sliderdotspanel.addView(dots[i], params);
dots[0].setImageDrawable(ContextCompat.getDrawable(getActivity().getApplicationContext(), R.drawable.active_dots));
}
viewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
@Override
public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
}
@Override
public void onPageSelected(int position) {
for (int i = 0; i < dotscount; i++) {
dots[i].setImageDrawable(ContextCompat.getDrawable(getActivity().getApplicationContext(), R.drawable.nonactive_dot));
}
dots[position].setImageDrawable(ContextCompat.getDrawable(getActivity().getApplicationContext(), R.drawable.active_dots));
}
@Override
public void onPageScrollStateChanged(int state) {
}
});
}
}
答案 0 :(得分:3)
您的代码中间有return rootView;
。将其移至onCreateView()
方法的最后一行。
答案 1 :(得分:0)
在设置布局属性后必须返回View [在您的情况下为rootView],否则在无法访问后添加更改:
更改你的onCretae方法:
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View rootView = inflater.inflate(R.layout.fragment_other, container, false);
viewPager = (ViewPager) rootView.findViewById(R.id.viewpager);
sliderdotspanel = (LinearLayout) rootView.findViewById(R.id.viewPagerCountDots);
adapter = new Beranda_imageadapter(getActivity(), imageResource);
viewPager.setAdapter(adapter);
dotscount = adapter.getCount();
dots = new ImageView[dotscount];for (int i = 0; i < dotscount; i++) {
dots[i] = new ImageView(getActivity());
dots[i].setImageDrawable(ContextCompat.getDrawable(getActivity().getApplicationContext(), R.drawable.nonactive_dot));
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
params.setMargins(8, 0, 8, 0);
sliderdotspanel.addView(dots[i], params);
dots[0].setImageDrawable(ContextCompat.getDrawable(getActivity().getApplicationContext(), R.drawable.active_dots));
return rootView;
}
答案 2 :(得分:0)
您可以使用rootView.getContex()
或rootView.getContex().getActivity()
代替getActivity()
,也可以在onCreateView()方法的末尾返回rootView。