Swipe时ViewPager卡住了

时间:2016-05-19 12:22:14

标签: android android-fragments android-asynctask android-viewpager

我的视图分页器中有11个片段,10个片段有网络呼叫。应用程序正在运行,数据正在加载。但是在刷片段时,它已经卡住了。我可以让我的滑动更顺畅吗?这是我的片段代码。

public class Entertainment extends Fragment implements URL,Constants {

    Button malayalam_btn, tamil_btn, hindi_btn, english_btn;

    private GridLayoutManager malayalam, tamil, hindi, english;

    RecyclerView malayalam_rv, tamil_rv, hindi_rv, english_rv;
    TextView malayalam_tv, tamil_tv, hindi_tv, english_tv;

    MalayalamAdapter malayalamAdapter;
    TamilAdapter tamilAdapter;
    HindiAdapter hindiAdapter;
    EnglishAdapter englishAdapter;


    public static ArrayList<EntertainmentModel> malayalam_list;
    public static ArrayList<EntertainmentModel> tamil_list;
    public static ArrayList<EntertainmentModel> hindi_list;
    public static ArrayList<EntertainmentModel> english_list;
    public static ArrayList<EntertainmentModel> entertainment_modal_list;

    ProgressBar progressBar;
    FetchData fetchData;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Thread.setDefaultUncaughtExceptionHandler(new ExceptionHandler(getActivity()));
    }


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_entertainment, null);
        Log.i("riyas", "oncreateview called in Entertainment");

        progressBar = (ProgressBar) view.findViewById(R.id.progressBar);
        progressBar.setVisibility(View.VISIBLE);

        String font_path_English = "fonts/DIN Alternate Bold.ttf";
        Typeface fontEnglish = Typeface.createFromAsset(getActivity().getAssets(), font_path_English);
        myClickHandler();
        malayalam_tv = (TextView) view.findViewById(R.id.malayalam_tv);
        tamil_tv = (TextView) view.findViewById(R.id.tamil_tv);
        hindi_tv = (TextView) view.findViewById(R.id.hindi_tv);
        english_tv = (TextView) view.findViewById(R.id.english_tv);



        malayalam_btn=(Button)view.findViewById(R.id.button6);
        malayalam_btn.setVisibility(GONE);
        tamil_btn=(Button)view.findViewById(R.id.button7);
        tamil_btn.setVisibility(GONE);
        hindi_btn=(Button)view.findViewById(R.id.button8);
        hindi_btn.setVisibility(GONE);
        english_btn=(Button)view.findViewById(R.id.button9);
        english_btn.setVisibility(GONE);


        malayalam_tv.setTypeface(fontEnglish);
        tamil_tv.setTypeface(fontEnglish);
        hindi_tv.setTypeface(fontEnglish);
        english_tv.setTypeface(fontEnglish);

        malayalam_rv = (RecyclerView) view.findViewById(R.id.malayalam);
        malayalam = new GridLayoutManager(getActivity(), 2);
        malayalam_rv.setHasFixedSize(true);
        malayalam_rv.setLayoutManager(malayalam);

        tamil_rv = (RecyclerView) view.findViewById(R.id.tamil);
        tamil = new GridLayoutManager(getActivity(), 1);
        tamil_rv.setHasFixedSize(true);
        tamil_rv.setLayoutManager(tamil);

        hindi_rv = (RecyclerView) view.findViewById(R.id.hindi);
        hindi = new GridLayoutManager(getActivity(), 2);
        hindi_rv.setHasFixedSize(true);
        hindi_rv.setLayoutManager(hindi);

        english_rv = (RecyclerView) view.findViewById(R.id.english);
        english = new GridLayoutManager(getActivity(), 1);
        english_rv.setHasFixedSize(true);
        english_rv.setLayoutManager(english);


        return view;
    }


    public void myClickHandler() {

        String Url = ENTERTAINMENT_API;

        ConnectivityManager connMgr = (ConnectivityManager) getActivity().getSystemService(Context.CONNECTIVITY_SERVICE);
        NetworkInfo networkInfo = connMgr.getActiveNetworkInfo();
        if (networkInfo != null && networkInfo.isConnected()) {
            new Network().execute(Url);
        } else {

            Toast.makeText(getActivity(), "No Network connection Available", Toast.LENGTH_SHORT).show();
        }
    }


    private class Network extends AsyncTask<String, Void, Void> implements NetworkOperation {

        @Override
        protected Void doInBackground(String... params) {

            fetchData = new FetchData(this, getActivity(), params[0]);
            fetchData.fromServer();

            return null;
        }


        @Override
        protected void onProgressUpdate(Void... values) {
            super.onProgressUpdate(values);
            progressBar.setVisibility(View.VISIBLE);
        }

        @Override
        protected void onPostExecute(Void aVoid) {
            super.onPostExecute(aVoid);
            progressBar.setVisibility(View.GONE);
        }

        @Override
        protected void onPreExecute() {
            super.onPreExecute();
            progressBar.setVisibility(View.VISIBLE);
        }



        @Override
        public void JsonParse(JSONObject jsonObject) {

            malayalam_list = new ArrayList<EntertainmentModel>();
            tamil_list = new ArrayList<EntertainmentModel>();
            hindi_list = new ArrayList<EntertainmentModel>();
            english_list = new ArrayList<EntertainmentModel>();


            try {
                JSONArray section = jsonObject.getJSONArray("sections");
                for (int i = 0; i < section.length(); i++) {
                    JSONObject subitem = section.getJSONObject(i);
                    String name = subitem.getString(HOME_NAME);
                    String cat_id = subitem.getString(CAT_ID);
                    String style = subitem.getString(STYLE);
                    String seeMore = subitem.getString(HOME_SEEMORE);
                    String lm = subitem.getString("lm");
                    String identifier = subitem.getString("identifier");

                    JSONArray News = subitem.getJSONArray(HOME_NEWS);
                    for (int j = 0; j < News.length(); j++) {
                        entertainment_modal_list = new ArrayList<EntertainmentModel>();
                        EntertainmentModel ent_modalobj = null;
                        JSONObject newsObject = News.getJSONObject(j);
                        String ID = newsObject.getString(HOME_ID);
                        String post_title = newsObject.getString(HOME_POST_TITLE);
                        String post_date = newsObject.getString(HOME_POST_DATE);
                        String post_time = newsObject.getString(HOME_POST_TIME);
                        String is_video = newsObject.getString(ISVIDEO);
                        String medium = newsObject.getString(HOME_MEDIUM);
                        String large = newsObject.getString(HOME_LARGE);


                        ent_modalobj = new EntertainmentModel(name, cat_id, style, seeMore, lm, identifier, ID, post_title, post_date, is_video, post_time, medium, large);
                        entertainment_modal_list.add(ent_modalobj);

                        for (int m = 0; m < entertainment_modal_list.size(); m++) {
                            String news_type = entertainment_modal_list.get(m).getName();

                            switch (news_type) {

                                case MALAYALAM:
                                    malayalam_list.add(ent_modalobj);
                                    malayalam_tv.setText(malayalam_list.get(m).getName().toUpperCase());
                                    malayalam_tv.setBackgroundResource(R.color.violet);
                                    final String id=malayalam_list.get(m).getCat_id().toString();

                                    malayalam_btn.setVisibility(View.VISIBLE);
                                    malayalam_btn.setOnClickListener(new View.OnClickListener() {
                                        @Override
                                        public void onClick(View v) {

                                            Intent intent = new Intent(getActivity(), More.class);
                                            intent.putExtra("cat", id);
                                            intent.putExtra("title", "MALAYALAM");
                                            getContext().startActivity(intent);

                                        }
                                    });
                                    break;

                                case TAMIL:
                                    tamil_list.add(ent_modalobj);
                                    tamil_tv.setText(tamil_list.get(m).getName().toUpperCase());
                                    tamil_tv.setBackgroundResource(R.color.violet);
                                    final String idt=tamil_list.get(m).getCat_id().toString();

                                    tamil_btn.setVisibility(View.VISIBLE);
                                    tamil_btn.setOnClickListener(new View.OnClickListener() {
                                        @Override
                                        public void onClick(View v) {

                                            Intent intent = new Intent(getActivity(), More.class);
                                            intent.putExtra("cat", idt);
                                            intent.putExtra("title", "TAMIL");
                                            getContext().startActivity(intent);

                                        }
                                    });
                                    break;

                                case HINDI:
                                    hindi_list.add(ent_modalobj);
                                    hindi_tv.setText(hindi_list.get(m).getName().toUpperCase());
                                    hindi_tv.setBackgroundResource(R.color.violet);
                                    final String idh=hindi_list.get(m).getCat_id().toString();

                                    hindi_btn.setVisibility(View.VISIBLE);
                                    hindi_btn.setOnClickListener(new View.OnClickListener() {
                                        @Override
                                        public void onClick(View v) {

                                            Intent intent = new Intent(getActivity(), More.class);
                                            intent.putExtra("cat", idh);
                                            intent.putExtra("title", "HINDI");
                                            getContext().startActivity(intent);

                                        }
                                    });
                                    break;

                                case ENGLISH:
                                    english_list.add(ent_modalobj);
                                    english_tv.setText(english_list.get(m).getName().toUpperCase());
                                    english_tv.setBackgroundResource(R.color.violet);
                                    final String ide=english_list.get(m).getCat_id().toString();

                                    english_btn.setVisibility(View.VISIBLE);
                                    english_btn.setOnClickListener(new View.OnClickListener() {
                                        @Override
                                        public void onClick(View v) {

                                            Intent intent = new Intent(getActivity(), More.class);
                                            intent.putExtra("cat", ide);
                                            intent.putExtra("title", "ENGLISH");
                                            getContext().startActivity(intent);

                                        }
                                    });
                                    break;


                            }
                        }
                    }
                }


            } catch (JSONException | OutOfMemoryError e) {
                e.printStackTrace();
            }

            malayalamAdapter = new MalayalamAdapter(getActivity(), malayalam_list);
            malayalam_rv.setAdapter(malayalamAdapter);

            tamilAdapter = new TamilAdapter(getActivity(), tamil_list);
            tamil_rv.setAdapter(tamilAdapter);

            hindiAdapter = new HindiAdapter(getActivity(), hindi_list);
            hindi_rv.setAdapter(hindiAdapter);

            englishAdapter = new EnglishAdapter(getActivity(), english_list);
            english_rv.setAdapter(englishAdapter);
            progressBar.setVisibility(View.GONE);
        }


    }

我尝试了以下内容:

  1. viewPager.setOffscreenPageLimit(4); 2。Log.i("oncreate is called or not")

0 个答案:

没有答案