简体   繁体   中英

Showing a customView of android-tv-epg in a fragment (OnDraw not called)

I wonder if it would be possible to use this EPG customview in a fragment? it works perfectly in Activity, but onDraw isn't be called in a fragment, or I don't know how to manage that. I searched a lot about all the questions which tells how to show a customView in a fragment, but it didn't work here.

like this one

Any clue?

@Override
public View onCreateView(@NonNull LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    super.onCreateView(inflater, container, savedInstanceState);
    
    EPG epg = new EPG(container.getContext());
    epg.setEPGClickListener(new EPGClickListener() {
        @Override
        public void onChannelClicked(int channelPosition, EPGChannel epgChannel) {
            Toast.makeText(container.getContext(), epgChannel.getName() + " clicked", Toast.LENGTH_SHORT).show();
        }

        @Override
        public void onEventClicked(int channelPosition, int programPosition, EPGEvent epgEvent) {
            Toast.makeText(container.getContext(), epgEvent.getTitle() + " clicked", Toast.LENGTH_SHORT).show();
        }

        @Override
        public void onResetButtonClicked() {
            epg.recalculateAndRedraw(true);
        }
    });

    EPGData epgData = new EPGDataImpl(MockDataService.getMockData());
    epg.setEPGData(epgData);
    epg.recalculateAndRedraw(false);

    return epg;
}

I solved the problem like this:

fragment_planner.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:id="@+id/planner_container"
    android:orientation="vertical">

</LinearLayout>

and in PlannerFragment.java:

   @Override
public View onCreateView(@NonNull LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    super.onCreateView(inflater, container, savedInstanceState);
    View rootView = inflater.inflate(R.layout.fragment_planner, container, false);

    EPG epg = new EPG(container.getContext());
    epg.setEPGClickListener(new EPGClickListener() {
        @Override
        public void onChannelClicked(int channelPosition, EPGChannel epgChannel) {
            Toast.makeText(container.getContext(), epgChannel.getName() + " clicked", Toast.LENGTH_SHORT).show();
        }

        @Override
        public void onEventClicked(int channelPosition, int programPosition, EPGEvent epgEvent) {
            Toast.makeText(container.getContext(), epgEvent.getTitle() + " clicked", Toast.LENGTH_SHORT).show();
        }

        @Override
        public void onResetButtonClicked() {
            epg.recalculateAndRedraw(true);
        }
    });

    EPGData epgData = new EPGDataImpl(MockDataService.getMockData());
    epg.setEPGData(epgData);
    epg.recalculateAndRedraw(false);

    LinearLayout plannerContainer = rootView.findViewById(R.id.planner_container);
    DisplayMetrics displayMetrics = new DisplayMetrics();
    WindowManager windowManager = (WindowManager) getActivity().getSystemService(Context.WINDOW_SERVICE);
    windowManager.getDefaultDisplay().getMetrics(displayMetrics);
    LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams((int) Math.round(displayMetrics.widthPixels * 5), (int) Math.round(displayMetrics.widthPixels * 5));
    epg.setLayoutParams(layoutParams);
    plannerContainer.addView(epg);

    return rootView;
}

I couldn't manage to add MATCH_PARENT or WRAP_CONTENT yet, but I post it as a different question. Currently, I succeeded to call OnDraw and show the customView in a fragment:D

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM