0

私は、それぞれのフラグメントがrecyclerviewを含むビューページャーを持っています。ただし、ビューページのフラグメントが作成され、さらにrecyclerviewのスクロールラグが発生すると、クリックに反応するまでに時間がかかります。 getItemPositionメソッドでPOSITION_NONEを返して、ユーザーがテキストのサイズ/スタイルを変更したときにフラグメントが再作成されるようにします。二つの問題は、低RAM 装置とより深刻である上に、高いRAM/CPUとしかしながら、RecyclerViewスクロールも遅れ Android ViewPagerにrecyclerviewフラグメントがある(パフォーマンスが低い)

  • 問題3.:

    1. 問題1:Viewpagerが作成およびスワイプの断片は
    2. 問題2に遅れますコアデバイスであれば、それは受け入れ可能ですが、マークまでは対応していません。

    ここでは、私のコードは次のとおりです。 ViewPagerAdapter:

    private class ViewPagerAdapter extends FragmentStatePagerAdapter { 
    
         public ViewPagerAdapter(FragmentManager fm) { 
          super(fm); 
         } 
    
         @Override 
         public Fragment getItem(int position) { 
    
          subTitlesFragment = new SubTitlesFragment(); 
          Bundle bundle = new Bundle(); 
          if (isRtl) { 
           int positionRev; 
           positionRev = position - (subTitlesList.size() - 1); 
           bundle.putInt("view_pager_pos", Math.abs(positionRev)); 
          } else { 
           bundle.putInt("view_pager_pos", position); 
          } 
          bundle.putString("type", type); 
          bundle.putString("query", query); 
          subTitlesFragment.setArguments(bundle); 
          return subTitlesFragment; 
         } 
    
         @Override 
         public int getItemPosition(Object object) { 
          return POSITION_NONE; 
         } 
    
         @Override 
         public int getCount() { 
          return subTitlesList.size(); 
         } 
    
        } 
    

    MyRecyclerViewAdapter:

    public RecyclerAdapterDetails(Context context, ArrayList<DataView> data, String query, String fav, String language, 
                Typeface typefaceAR, Typeface typefaceTran, 
                String textFontSize, String textFontSizeTran, 
                boolean diacriticsON) { 
    
         sharedPref = context.getSharedPreferences("sPreferencesFile", Context.MODE_PRIVATE); 
         inflater = LayoutInflater.from(context); 
         this.dataViews = data; 
         this.context = context; 
         this.query = query; 
         this.fav = fav; 
         this.language = language; 
         this.typefaceAR = typefaceAR; 
         this.typefaceTran = typefaceTran; 
         this.textFontSize = textFontSize; 
         this.textFontSizeTran = textFontSizeTran; 
         this.diacriticsON = diacriticsON; 
         currentNightMode = context.getResources().getConfiguration().uiMode 
           & Configuration.UI_MODE_NIGHT_MASK; 
        } 
    
        @Override 
        public int getItemViewType(int position) { 
         int viewType = 0; 
         if (position == 0 && fav == null) { 
          viewType = 1; 
         } 
         if (position == dataViews.size()) { 
          viewType = 2; 
         } 
         return viewType; 
        } 
    
        @Override 
        public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) { 
         View view; 
    
         if (viewType == 0) { 
          view = inflater.inflate(R.layout.single_item_recycler_details, parent, false); 
          return new MyViewHolder(view); 
         } else if (viewType == 1) { 
          view = inflater.inflate(R.layout.single_item_title_details, parent, false); 
          return new TitleViewHolder(view); 
         } else { 
          view = inflater.inflate(R.layout.blank_item, parent, false); 
          return new BlankItemViewHolder(view); 
         } 
        } 
    
        @Override 
        public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) { 
    
         if (holder.getItemViewType() == 0) { 
          processMyViewHolder(holder, position); 
         } else if (holder.getItemViewType() == 1) { 
          TitleViewHolder titleViewHolder = (TitleViewHolder) holder; 
    
          if (language.equals("en")) { 
           titleViewHolder.duaTitle.setText(dataViews.get(1).getSubTitleEN()); 
            titleViewHolder.duaTitle.setText(dataViews.get(1).getTranslation_Sub()); 
          } else { 
           titleViewHolder.duaTitle.setText(dataViews.get(1).getSubTitleAR()); 
    titleViewHolder.duaTitle.setText(dataViews.get(1).getSub_title()); 
          } 
    
          if (language.equals("ar")) { 
            titleViewHolder.duaTitle.setTypeface(typefaceAR); 
          } else { 
    
            titleViewHolder.duaTitle.setTypeface(typefaceTran); 
          } 
    
          if (language.equals("ar")) { 
           titleViewHolder.duaTitle.setTextSize(TypedValue.COMPLEX_UNIT_SP, Float.parseFloat(textFontSize)); 
          } else { 
           titleViewHolder.duaTitle.setTextSize(TypedValue.COMPLEX_UNIT_SP, Float.parseFloat(textFontSizeTran)); 
          } 
          if (query != null) { 
           String title = titleViewHolder.duaTitle.getText().toString().toLowerCase(); 
           int ofe3 = title.indexOf(query.toLowerCase(), 0); 
           Spannable spannable3 = new SpannableString(titleViewHolder.duaTitle.getText()); 
    
           for (int ofs = 0; ofs < title.length() && ofe3 != -1; ofs = ofe3 + 1) { 
            ofe3 = title.indexOf(query, ofs); 
            if (ofe3 == -1) 
             break; 
            else { 
             if (currentNightMode == Configuration.UI_MODE_NIGHT_NO) { 
              spannable3.setSpan(new BackgroundColorSpan(0xFFFFFF00), ofe3, ofe3 + query.length(), 
                Spannable.SPAN_EXCLUSIVE_EXCLUSIVE); 
             } else { 
              spannable3.setSpan(new BackgroundColorSpan(0xffcccccc), ofe3, ofe3 + query.length(), 
                Spannable.SPAN_EXCLUSIVE_EXCLUSIVE); 
             } 
             titleViewHolder.duaTitle.setText(spannable3, TextView.BufferType.SPANNABLE); 
            } 
           } 
          } 
    
         } else { 
          Log.w(TAG, "BlankItemViewHolder called..."); 
          BlankItemViewHolder blankItemViewHolder = (BlankItemViewHolder) holder; 
          if (language.equals("ar")) { 
           try { 
            blankItemViewHolder.footnote.setText(dataViews.get(1).getFootnoteAR()); 
           } catch (Exception e) { 
    
           } 
          } else { 
           try { 
            blankItemViewHolder.footnote.setText(dataViews.get(1).getFootnoteEN()); 
           } catch (Exception e) { 
    
           } 
          } 
         } 
        } 
    
        public void processMyViewHolder(RecyclerView.ViewHolder viewHolder, int position) { 
    
         MyViewHolder holder = (MyViewHolder) viewHolder; 
         if (currentNightMode == Configuration.UI_MODE_NIGHT_NO) { 
          holder.cardView.setCardBackgroundColor(Color.WHITE); 
         } 
         DataView current = dataViews.get(position); 
          holder.contentsAr.setTypeface(typefaceAR); 
    
         if (diacriticsON) { 
          holder.contentsAr.setText(current.getContentsAR().trim()); 
         } else { 
          Matcher matcher = pattern.matcher(current.getContentsAR().trim()); 
          matcher.reset(); 
          String contentsDiaRemoved = matcher.replaceAll(""); 
          holder.contentsAr.setText(contentsDiaRemoved); 
         } 
    
         if (language.equals("ar")) { 
          holder.contentsTran.setVisibility(View.GONE); 
          if (current.getDaleelAR().trim().length() > 0) { 
           holder.divider.setVisibility(View.VISIBLE); 
           holder.daleel.setVisibility(View.VISIBLE); 
           if (diacriticsON) { 
            holder.daleel.setText(current.getDaleelAR().trim()); 
           } else { 
            if (language.equals("ar")) { 
             holder.contentsTran.setVisibility(View.GONE); 
            } else { 
             if (current.getContentsEN() != null && !current.getContentsEN().trim().isEmpty()) { 
              holder.contentsTran.setText(current.getContentsEN().trim()); 
             } else { 
              holder.contentsTran.setVisibility(View.GONE); 
             } 
            } 
    
            Matcher matcher = pattern.matcher(current.getDaleelAR().trim()); 
            matcher.reset(); 
            String daleelText = matcher.replaceAll(""); 
            holder.daleel.setText(daleelText); 
           } 
          } else { 
           holder.divider.setVisibility(View.GONE); 
           holder.daleel.setVisibility(View.GONE); 
          } 
         } else { 
          if (current.getContentsEN() != null && !current.getContentsEN().trim().isEmpty()) { 
           holder.contentsTran.setText(current.getContentsEN().trim()); 
          } else { 
           holder.contentsTran.setVisibility(View.GONE); 
          } 
          if (current.getDaleelEN() != null && !current.getDaleelEN().trim().isEmpty()) { 
           holder.daleel.setText(current.getDaleelEN().trim()); 
          } else { 
           if (current.getDaleelAR().trim().length() > 0) { 
            holder.divider.setVisibility(View.VISIBLE); 
            holder.daleel.setVisibility(View.VISIBLE); 
            holder.daleel.setText(current.getDaleelAR().trim()); 
           } else { 
            holder.divider.setVisibility(View.GONE); 
            holder.daleel.setVisibility(View.GONE); 
           } 
          } 
         } 
    
         if (language.equals("ar")) { 
          holder.contentsTran.setVisibility(View.GONE); 
          holder.daleel.setTypeface(typefaceAR); 
         } else { 
           holder.contentsTran.setTypeface(typefaceTran); 
           holder.daleel.setTypeface(typefaceTran); 
         } 
    
         holder.contentsAr.setTextSize(TypedValue.COMPLEX_UNIT_SP, Float.parseFloat(textFontSize)); 
    
         if (language.equals("ar")) { 
          float newSize = Float.parseFloat(textFontSize) - 2; 
          holder.daleel.setTextSize(TypedValue.COMPLEX_UNIT_SP, newSize); 
         } else { 
          holder.contentsTran.setTextSize(TypedValue.COMPLEX_UNIT_SP, Float.parseFloat(textFontSizeTran)); 
          holder.daleel.setTextSize(TypedValue.COMPLEX_UNIT_SP, Float.parseFloat(textFontSizeTran)); 
         } 
    
         if (query != null) { 
          String daleel = holder.daleel.getText().toString().toLowerCase(); 
          int ofe = daleel.indexOf(query.toLowerCase(), 0); 
          Spannable spannable = new SpannableString(holder.daleel.getText()); 
    
          for (int ofs = 0; ofs < daleel.length() && ofe != -1; ofs = ofe + 1) { 
           ofe = daleel.indexOf(query, ofs); 
           if (ofe == -1) 
            break; 
           else { 
            if (currentNightMode == Configuration.UI_MODE_NIGHT_NO) { 
             spannable.setSpan(new BackgroundColorSpan(0xFFFFFF00), ofe, ofe + query.length(), 
               Spannable.SPAN_EXCLUSIVE_EXCLUSIVE); 
            } else { 
             spannable.setSpan(new BackgroundColorSpan(0xffcccccc), ofe, ofe + query.length(), 
               Spannable.SPAN_EXCLUSIVE_EXCLUSIVE); 
            } 
            holder.daleel.setText(spannable, TextView.BufferType.SPANNABLE); 
           } 
          } 
    
          String contents = holder.contentsAr.getText().toString().toLowerCase(); 
          int ofe2 = contents.indexOf(query.toLowerCase(), 0); 
          Spannable spannable2 = new SpannableString(holder.contentsAr.getText()); 
    
          for (int ofs = 0; ofs < contents.length() && ofe2 != -1; ofs = ofe2 + 1) { 
           ofe2 = contents.indexOf(query, ofs); 
           if (ofe2 == -1) 
            break; 
           else { 
            if (currentNightMode == Configuration.UI_MODE_NIGHT_NO) { 
             spannable2.setSpan(new BackgroundColorSpan(0xFFFFFF00), ofe2, ofe2 + query.length(), 
               Spannable.SPAN_EXCLUSIVE_EXCLUSIVE); 
            } else { 
             spannable2.setSpan(new BackgroundColorSpan(0xffcccccc), ofe2, ofe2 + query.length(), 
               Spannable.SPAN_EXCLUSIVE_EXCLUSIVE); 
            } 
            holder.contentsAr.setText(spannable2, TextView.BufferType.SPANNABLE); 
           } 
          } 
         } 
    
         if (sharedPref.getBoolean("bookmark" + dataViews.get(position).getHadithNo(), false)) { 
          holder.bookmark.setImageResource(R.drawable.ic_bookmark_grey_600_24dp); 
         } else { 
          holder.bookmark.setImageResource(R.drawable.ic_bookmark_border_grey_600_24dp); 
         } 
    
        }  
    
        @Override 
        public int getItemCount() { 
         return dataViews.size() + 1; 
        } 
    
        public void setClickListener(ClickListener clickListener) { 
         this.clickListener = clickListener; 
        } 
    
        public interface ClickListener { 
         void itemClicked(int position); 
        } 
    
        private class MyViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener, View.OnLongClickListener { 
    
         TextView contentsAr, contentsTran; 
         TextView daleel; 
         ImageView bookmark, share; 
         View divider; 
         CardView cardView; 
    
         public MyViewHolder(View itemView) { 
          super(itemView); 
          contentsAr = (TextView) itemView.findViewById(R.id.contents_ar); 
          contentsTran = (TextView) itemView.findViewById(R.id.contents_tran); 
          daleel = (TextView) itemView.findViewById(R.id.daleel); 
          bookmark = (ImageView) itemView.findViewById(R.id.bookmark); 
          share = (ImageView) itemView.findViewById(R.id.share); 
          divider = itemView.findViewById(R.id.divider); 
          cardView = (CardView) itemView.findViewById(R.id.card_view1); 
          bookmark.setOnClickListener(this); 
          bookmark.setOnLongClickListener(this); 
          share.setOnClickListener(this); 
         } 
    
         @Override 
         public void onClick(View v) { 
          if (v.getId() == bookmark.getId()) { 
           if (fav == null) { 
            modifySharedPref(); 
           } 
           if (fav != null) { 
            Toast.makeText(context, R.string.long_press, Toast.LENGTH_SHORT).show(); 
           } 
          } 
          if (v.getId() == share.getId()) { 
           clickListener.itemClicked(getLayoutPosition()); 
          } 
         } 
    
         @Override 
         public boolean onLongClick(View v) { 
          if (fav != null) { 
           bookmark.performHapticFeedback(HapticFeedbackConstants.LONG_PRESS); 
           modifySharedPref(); 
           removeAt(getLayoutPosition()); 
          } 
          return true; 
         } 
    
         void modifySharedPref() { 
          SharedPreferences.Editor editor = sharedPref.edit(); 
          if (!sharedPref.getBoolean("bookmark" + dataViews.get(getLayoutPosition()).getHadithNo(), false)) { 
           bookmark.setImageResource(R.drawable.ic_bookmark_grey_600_24dp); 
           editor.putBoolean("bookmark" + dataViews.get(getLayoutPosition()).getHadithNo(), true); 
          } else { 
           bookmark.setImageResource(R.drawable.ic_bookmark_border_grey_600_24dp); 
           editor.putBoolean("bookmark" + dataViews.get(getLayoutPosition()).getHadithNo(), false); 
          } 
          editor.apply(); 
         } 
        } 
    
        private class TitleViewHolder extends RecyclerView.ViewHolder { 
         TextView duaTitle; 
    
         public TitleViewHolder(View itemView) { 
          super(itemView); 
          duaTitle = (TextView) itemView.findViewById(R.id.dua_title); 
         } 
        } 
    
        private class BlankItemViewHolder extends RecyclerView.ViewHolder { 
         LinearLayout blankItem; 
         TextView footnote; 
    
         public BlankItemViewHolder(View itemView) { 
          super(itemView); 
          blankItem = (LinearLayout) itemView.findViewById(R.id.blank_item); 
          footnote = (TextView) itemView.findViewById(R.id.foot_note); 
         } 
        } 
    
        void removeAt(int position) { 
         dataViews.remove(position); 
         notifyItemRemoved(position); 
         notifyItemRangeChanged(position, dataViews.size()); 
        } 
    

    SubTitlesFragment:

    私が研究し何

    は:1. ViewPager PagerAdapter not updating the View 2. Update ViewPager dynamically?

    は、しかし、私は、パフォーマンスを向上させることに成功することができませんでした。 viewpagerとrecyclerviewのパフォーマンスを改善する方法を提案してください。おかげさまで

  • 答えて

    1

    大きなonBindViewHolderメソッドとproccessMyViewHolderメソッドでこの問題が発生しているようです。共有プリファレンスや作成フォントからそのメソッド内のアセットからデータを読み込むなどの処理を行わないでください。そのデータを一度ロードし、必要に応じて使用する方がよいでしょう。

    +0

    ありがとうございます。リサイクラー・アダプターでは、共有プリファレンスへの不要なアクセスとアセットからのフォントの作成を削除しましたが、パフォーマンスは改善されていません。遅れは同じです。私の更新されたRecyclerAdapterDetails.javaコードを見てください – Abdullah

    関連する問題