屏幕上显示进度条

时间:2019-06-13 06:53:12

标签: android android-tv

我遇到了一个问题,屏幕上仅显示了Loader。我遵循以下链接:

this link

MainFragment.java

package com.jagran.jagrantv.fragment;


import android.app.Fragment;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v17.leanback.app.BackgroundManager;
import android.support.v17.leanback.app.BrowseFragment;
import android.support.v17.leanback.widget.ArrayObjectAdapter;
import android.support.v17.leanback.widget.ListRow;
import android.support.v17.leanback.widget.ListRowPresenter;
import android.support.v17.leanback.widget.PageRow;
import android.support.v17.leanback.widget.Presenter;
import android.support.v17.leanback.widget.PresenterSelector;
import android.support.v17.leanback.widget.Row;
import android.support.v4.content.ContextCompat;
import android.util.DisplayMetrics;
import android.util.Log;
import android.view.View;
import android.widget.Toast;

import com.google.gson.Gson;
import com.jagran.jagrantv.R;
import com.jagran.jagrantv.model.homejson.VideosResponse;
import com.jagran.jagrantv.network.DocsApi;
import com.jagran.jagrantv.network.NetworkCallHandler;
import com.jagran.jagrantv.network.NetworkCallInterface;
import com.jagran.jagrantv.network.RestHttpApiClient;
import com.jagran.jagrantv.presenter.IconHeaderItemPresenter;
import com.jagran.jagrantv.utils.IconHeaderItem;
import com.jagran.jagrantv.utils.Utils;
import com.jagran.jagrantv.view.CustomBrowseFragment;

public class MainFragment extends CustomBrowseFragment {

    private BackgroundManager mBackgroundManager;

    private ArrayObjectAdapter mRowsAdapter;
    private Drawable mDefaultBackground;
    private DisplayMetrics mMetrics;
private  static VideosResponse keyResponse;


    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        prepareBackgroundManager();
        setupUi();


    }


    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);

        loadRows();
    }

    @Override
    public void onDestroy() {
        super.onDestroy();

    }

    private void prepareBackgroundManager() {


        mBackgroundManager = BackgroundManager.getInstance(getActivity());
        mBackgroundManager.attach(getActivity().getWindow());
        mDefaultBackground = ContextCompat.getDrawable(getActivity(), R.drawable.default_background);
        mBackgroundManager.setDrawable(mDefaultBackground);
        mMetrics = new DisplayMetrics();
        getActivity().getWindowManager().getDefaultDisplay().getMetrics(mMetrics);

        getMainFragmentRegistry().registerFragment(PageRow.class,
                new PageRowFragmentFactory(mBackgroundManager));
    }



    private void setupUi() {
        setHeadersState(HEADERS_ENABLED);
        setHeadersTransitionOnBackEnabled(true);
        setBadgeDrawable(getActivity().getResources().getDrawable(
                R.mipmap.logo));

        setBrandColor(ContextCompat.getColor(getActivity(), R.color.black));

        setHeaderPresenterSelector(new PresenterSelector() {
            @Override
            public Presenter getPresenter(Object o) {
                return new IconHeaderItemPresenter();
            }
        });

        setOnSearchClickedListener(new View.OnClickListener() {

            @Override
            public void onClick(View view) {
                Toast.makeText(
                        getActivity(), "search", Toast.LENGTH_SHORT)
                        .show();
            }
        });

        prepareEntranceTransition();
    }


    private void loadRows() {
        //new SpinnerTask().execute();

        mRowsAdapter = new ArrayObjectAdapter(new ListRowPresenter());
        if(Utils.isNetworkConnected(getActivity()))
            getVideoSubCategories();
        else
            Toast.makeText(getActivity(), "No network Available!",Toast.LENGTH_LONG).show();

        setAdapter(mRowsAdapter);
    }


    private static class PageRowFragmentFactory extends BrowseFragment.FragmentFactory {
        private final BackgroundManager mBackgroundManager;

        PageRowFragmentFactory(BackgroundManager backgroundManager) {
            this.mBackgroundManager = backgroundManager;
        }

        @Override
        public Fragment createFragment(Object rowObj) {
            Row row = (Row)rowObj;
            mBackgroundManager.setDrawable(null);
            if (row.getHeaderItem().getId() == 0) {
                String gson =  new Gson().toJson(keyResponse);
                return CustomRowsFragment.newInstance(row.getHeaderItem().getId(),gson);
            } else if (row.getHeaderItem().getId() == 1) {
                String gson =  new Gson().toJson(keyResponse);
                return CustomRowsFragment.newInstance(row.getHeaderItem().getId(),gson);
            } else if (row.getHeaderItem().getId() == 2) {
                String gson =  new Gson().toJson(keyResponse);
                return CustomRowsFragment.newInstance(row.getHeaderItem().getId(),gson);
            }

            throw new IllegalArgumentException(String.format("Invalid row %s", rowObj));
        }
    }



    public void getVideoSubCategories(){


       /* final SpinnerFragment mSpinnerFragment = new SpinnerFragment();
        try {
            getFragmentManager().beginTransaction().add(R.id.main_browse_fragment, mSpinnerFragment).commit();
        }catch (Exception ex){
            ex.printStackTrace();
        }*/
        DocsApi apiService = RestHttpApiClient.getClient("https://pastebin.com/raw/").create(DocsApi.class);
        retrofit2.Call<VideosResponse> call = apiService.getVideoSubCategories
                ("1i9J3LGF" );

        //System.out.println("urlstring = " + "https://rssm-jag.jagranjosh.com/JagranApsFeeds/feed/apps/ver4.0/videos/" + "jagranJsonVideoFeedWOTBody.jsp?key=videos.common-man-issues&cp=" + cp + "&rpp=" + rpp);
        NetworkCallHandler networkCallHandler = new NetworkCallHandler(getActivity(), new NetworkCallInterface() {
            @Override
            public void onResponse(Object response, int responseCode, Bundle data) {
                if (responseCode == 1004) {
                    try {
                         keyResponse = (VideosResponse) response;
                      //  PostAdapter listRowAdapter;
                        //CardPresenter cardPresenter = new CardPresenter();
                        PageRow pageRow[] = new PageRow[keyResponse.getVideos().getAppCategorys().size()];
                        for (int i = 0; i < /*keyResponse.getVideos().getAppCategorys().size()*/1; i++) {

                          /*  listRowAdapter = new PostAdapter(getActivity(), keyResponse.docList.get(i).subKey);
                            listRowAdapter.setIsLoadMore(keyResponse.docList.get(i).readMore);
                            listRowAdapter.setItemCount(keyResponse.docList.get(i).itemCount);
                            listRowAdapter.setKeyType(keyResponse.docList.get(i).key_type);
                            addPostLoadSubscription(listRowAdapter);*/

                            IconHeaderItem header = new IconHeaderItem(i,keyResponse. getVideos().getAppCategorys().get(i).getLabel(),keyResponse. getVideos().getAppCategorys().get(i).getIconUrl());
                            pageRow[i] = new PageRow(header);

                            mRowsAdapter.add(pageRow[i]);
                        }
                       // getFragmentManager().beginTransaction().remove(mSpinnerFragment).commit();
                    }catch (Exception ex){
                        ex.printStackTrace();
                    }

                }


            }

            @Override
            public void onFailure(Object response, int responseCode, Bundle data) {

                try {
                    //getFragmentManager().beginTransaction().remove(mSpinnerFragment).commit();

                    Toast.makeText(
                            getActivity(),
                            getString(R.string.error_message_loading_more_posts),
                            Toast.LENGTH_SHORT
                    ).show();
                }catch (Exception ex){
                    ex.printStackTrace();
                }



            }
        }, 1004);
        if(Utils.isNetworkConnected(getActivity()))
            networkCallHandler.callToServerForData(call, null);
    }


}

CustomRowsFragment.java

package com.jagran.jagrantv.fragment;

import android.app.Fragment;
import android.os.Bundle;
import android.support.v17.leanback.app.RowsFragment;
import android.support.v17.leanback.widget.ArrayObjectAdapter;
import android.support.v17.leanback.widget.HeaderItem;
import android.support.v17.leanback.widget.ListRowPresenter;
import android.support.v17.leanback.widget.OnItemViewClickedListener;
import android.support.v17.leanback.widget.Presenter;
import android.support.v17.leanback.widget.PresenterSelector;
import android.support.v17.leanback.widget.Row;
import android.support.v17.leanback.widget.RowPresenter;
import android.widget.Toast;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.jagran.jagrantv.R;
import com.jagran.jagrantv.model.homejson.AppCategory;
import com.jagran.jagrantv.model.homejson.VideosResponse;
import com.jagran.jagrantv.model.videolistresponse.VideosListResponse;
import com.jagran.jagrantv.network.DocsApi;
import com.jagran.jagrantv.network.NetworkCallHandler;
import com.jagran.jagrantv.network.NetworkCallInterface;
import com.jagran.jagrantv.network.RestHttpApiClient;
import com.jagran.jagrantv.temp.Card;
import com.jagran.jagrantv.temp.CardListRow;
import com.jagran.jagrantv.temp.CardPresenterSelector;
import com.jagran.jagrantv.temp.CardRow;
import com.jagran.jagrantv.temp.ShadowRowPresenterSelector;
import com.jagran.jagrantv.utils.Utils;
import com.jagran.jagrantv.view.CustomBrowseFragment;

import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

public class CustomRowsFragment extends RowsFragment {
    private  ArrayObjectAdapter mRowsAdapter;

    int position;
    VideosResponse mVideosResponse;
    private static String Base_url ="";
    private  static String Sub_url = "";

    public static Fragment newInstance(long position ,String json){
        CustomRowsFragment fragment = new CustomRowsFragment();
        Bundle args = new Bundle();
        //args.putParcelableArrayList(ARG_LIST, dataList);
        args.putLong("position", position);
        args.putString("category",json);
        fragment.setArguments(args);
        return fragment;
    }

    public CustomRowsFragment() {

    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);


        if (getArguments() != null)
        {
            Type typeMyType = new TypeToken<VideosResponse>(){}.getType();
            String strGSON = getArguments().getString("category");
            mVideosResponse  = new Gson().fromJson(strGSON, typeMyType);
            position = (int)getArguments().getLong("position");

            Base_url = mVideosResponse.getVideos().getBaseUrl();
            Sub_url = mVideosResponse.getVideos().getListingUrl();

        }


        mRowsAdapter = new ArrayObjectAdapter(new ShadowRowPresenterSelector());

        setAdapter(mRowsAdapter);
        setOnItemViewClickedListener(new OnItemViewClickedListener() {
            @Override
            public void onItemClicked(
                    Presenter.ViewHolder itemViewHolder,
                    Object item,
                    RowPresenter.ViewHolder rowViewHolder,
                    Row row) {
                Toast.makeText(getActivity(), "Implement click handler", Toast.LENGTH_SHORT)
                        .show();
            }
        });


       // createRows();
        //getMainFragmentAdapter().getFragmentHost().notifyDataReady(getMainFragmentAdapter());
    }

    private void createRows() {
        String json = Utils.inputStreamToString(getResources().openRawResource(
                R.raw.page_row_example));
        CardRow[] rows = new Gson().fromJson(json, CardRow[].class);
        for (CardRow row : rows) {
            if (row.getType() == CardRow.TYPE_DEFAULT) {
                mRowsAdapter.add(createCardRow(row));
            }
        }
    }

    private Row createCardRow(CardRow cardRow) {
        PresenterSelector presenterSelector = new CardPresenterSelector(getActivity());
        ArrayObjectAdapter adapter = new ArrayObjectAdapter(presenterSelector);
        for (Card card : cardRow.getCards()) {
            adapter.add(card);
        }

        HeaderItem headerItem = new HeaderItem(cardRow.getTitle());
        return new CardListRow(headerItem, adapter, cardRow);
    }


/*
    private void addPostLoadSubscription(AppCategory category) {

        final int cp = 1;
        final int rpp = 10;


//        String url = "http://rssm-jag.jagranjosh.com/JagranApsFeeds/feed/apps/ver4.0/videos/jagranJsonVideoFeedWOTBody.jsp?" + tag + "&cp=" +
//                cp + "&rpp=" + rpp;
        for (int i = 0; i < category.getSub().size(); i++) {


            if (category != null && !category.getSub().get(i).getKeyType().equalsIgnoreCase("feed")){
              //  url = tag;
                DocsApi apiService = RestHttpApiClient.getClient(Base_url).create(DocsApi.class);
                retrofit2.Call<VideosListResponse> call = apiService.getVideosList(Sub_url+category.getSub().get(i).getSubKey()+"cp=1&rpp=10");

            }else{
                DocsApi apiService = RestHttpApiClient.getClient(category.getSub().get(i).getSubKey()).create(DocsApi.class);
                retrofit2.Call<VideosListResponse> call = apiService.getVideosList("");
            }


            NetworkCallHandler networkCallHandler = new NetworkCallHandler(getActivity(), new NetworkCallInterface() {
                @Override
                public void onResponse(Object response, int responseCode, Bundle data) {
                    if (responseCode == 1004) {
                        VideosListResponse keyResponse = (VideosListResponse) response;



                       */
/* adapter.removeLoadingIndicator();
                        if (adapter.size() == 0 && keyResponse.responseData.docList.isEmpty()) {
                            adapter.showReloadCard();
                        } else {


                            int noOfItem = 10;
                            try {
                                noOfItem = Integer.parseInt(itemCount);
                            } catch (Exception ex) {
                                noOfItem = keyResponse.responseData.docList.size();
                            }
                            if (noOfItem > keyResponse.responseData.docList.size())
                                noOfItem = keyResponse.responseData.docList.size();
                            List subCategory = keyResponse.responseData.docList.subList(0, noOfItem);
                            adapter.addAllItems(subCategory);
                            adapter.setNextPage(cp + 1);
                            if (isLoadmore != null) {
                                if (isLoadmore.equalsIgnoreCase("1"))
                                    adapter.showLoadMoreCard();
                            }*//*

                            //addPostLoadSubscription( adapter);
                       // }

                    }


                }

                @Override
                public void onFailure(Object response, int responseCode, Bundle data) {

                    try {
                       */
/* adapter.removeLoadingIndicator();
                        if (adapter.size() == 0) {
                            adapter.showTryAgainCard();
                        } else {
                            Toast.makeText(
                                    getActivity(),
                                    getString(R.string.error_message_loading_more_posts),
                                    Toast.LENGTH_SHORT
                            ).show();
                        }*//*

                    } catch (Exception ex) {
                        ex.printStackTrace();
                    }


                }
            }, 1004);
            networkCallHandler.callToServerForData(call, null);
        }
    }
*/

    }

任何帮助将不胜感激。谢谢。

0 个答案:

没有答案