在片段中添加活动

时间:2018-09-01 05:52:04

标签: java android android-layout android-studio android-fragments

我正在一个项目中,我想在底部通知栏的一个片段中添加Pageviewer。我指的是https://youtu.be/R_AIUy7tFVA-YouTube视频。

但是我遇到错误,请帮忙。

滑块适配器代码:

public class SliderAdapter extends PagerAdapter {

    public SliderAdapter(Context context){
        this.context = context;
    }

    Context context;
    LayoutInflater layoutInflater;

    public int[] events_img = {
        R.drawable.delhievnt,
        R.drawable.img2,
        R.drawable.img1
    };

    public String[] event_titles = {
        "Event 1", "Event 2", "Event 3"
    };

    public  String[] event_details = {
        "This is event 1 description",
        "This is event 2 description",
        "This is event 3 description"
    };

    public int[] events_bgcolor = {
        Color.rgb(55,55,55),
        Color.rgb(239,85,85),
        Color.rgb(1,188,212)
    };

    @Override
    public int getCount() {
        return event_titles.length;
    }

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

    @NonNull
    @Override
    public Object instantiateItem(@NonNull ViewGroup container, int position) {
        layoutInflater = (LayoutInflater) context.getSystemService(context.LAYOUT_INFLATER_SERVICE);
        View view = layoutInflater.inflate(R.layout.slides_viewpager, container, false);
        LinearLayout layout = (LinearLayout) view.findViewById(R.id.slider_layout);
        ImageView imageView = view.findViewById(R.id.slider_img);
        TextView tvtitle = view.findViewById(R.id.title_slider);
        TextView tvdetails = view.findViewById(R.id.detail_slider);
        layout.setBackgroundColor(events_bgcolor[position]);
        imageView.setImageResource(events_img[position]);
        tvtitle.setText(event_titles[position]);
        tvdetails.setText(event_details[position]);
        container.addView(view);
        return view;
    }

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

事件片段代码

public class EventFragment extends Fragment {


    private ViewPager viewPager;
    private SliderAdapter sliderAdapter;


    public EventFragment() {
    // Required empty public constructor
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {

        viewPager = (ViewPager) getView().findViewById(R.id.slider_layout);
        sliderAdapter = new SliderAdapter(this);
        viewPager.setAdapter(sliderAdapter);

        // Inflate the layout for this fragment
        return inflater.inflate(R.layout.fragment_event, container, false);
    }

此行sliderAdapter = new SliderAdapter(this);中出现错误。 当我使用 getActivity ()代替应用崩溃时

0 个答案:

没有答案