2

我正在開發一個搜索聯繫功能,在屏幕上,有一個RecyclerViewNestedScrolViewfillViewport =真)。 屏幕設計:這種設計是由顧客接受,我不能改變它
enter image description here
加載當前設備的所有接觸到一個ArrayList之後,搜索結果被從該陣列過濾。
有幾種情況,使應用程序非常laggy
1.當用戶鍵入有沒有結果的輸入,然後用戶清除搜索,我不得不再次顯示所有結果。 NestedScrollView必須爲RecyclerView(例如:300項)的所有項呈現UI。
2.當結果數量有很多變化時(例如,從1到300個項目)。 NestedScrollView有渲染UI進行了大量的項目RecyclerView
糟糕的表現時使用RecyclerView內NestedScrollView

我知道這個設計打破循環RecyclerView的技術,但我不能改變它。
我試了一下:

recyclerView.setNestedScrollingEnabled(false); 

在AndroidManifest:

android:windowSoftInputMode="adjustNothing" 

適配器:

public class RecyclerContactAdapter extends RecyclerView.Adapter<RecyclerContactAdapter.ViewHolder> { 

    private List<MobileContact> contacts; 
    private Context context; 

    public RecyclerContactAdapter() { 
     contacts = new ArrayList<>(); 
    } 

    @Override 
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) { 
     this.context = parent.getContext(); 
     View view = LayoutInflater.from(context) 
       .inflate(R.layout.item_recycler_contact, parent, false); 

     return new ViewHolder(view); 
    } 

    @Override 
    public void onBindViewHolder(ViewHolder holder, int position) { 
     //set data for view 
    } 

    @Override 
    public int getItemCount() { 
     return contacts.size(); 
    } 

    protected class ViewHolder extends RecyclerView.ViewHolder { 
     private TextView tvAlphabetHeader; 
     private CircleImageView civAvatar; 
     private TextView tvContactName; 
     private TextView tvStatus; 
     private CheckBox cbInvited; 
     private RelativeLayout rlAlphabetHeader; 
     private RelativeLayout rlContainer; 

     protected ViewHolder(View itemView) { 
      super(itemView); 
      tvAlphabetHeader = itemView.findViewById(R.id.item_recycler_contact_tv_alphabet_header); 
      civAvatar = itemView.findViewById(R.id.item_recycler_contact_civ_avatar); 
      tvContactName = itemView.findViewById(R.id.item_recycler_contact_tv_name); 
      tvStatus = itemView.findViewById(R.id.item_recycler_contact_tv_status); 
      cbInvited = itemView.findViewById(R.id.item_recycler_contact_cb_contact); 
      rlAlphabetHeader = itemView.findViewById(R.id.item_recycler_contact_rl_alphabet); 
      rlContainer = itemView.findViewById(R.id.item_recycler_contact_rl_contact); 
     } 
    } 

    public void addAll(List<MobileContact> mobileContacts) { 
     this.contacts.clear(); 
     this.contacts.addAll(mobileContacts); 
     notifyDataSetChanged(); 
    } 

    public void add(MobileContact mobileContact) { 
     this.contacts.add(mobileContact); 
    } 

    public List<MobileContact> getContacts() { 
     return this.contacts; 
    } 

} 
+0

發佈您的recyler適配器 –

+0

你能解釋一下你的ui設計嗎?這部分是可滾動的?即你可以滾動nestedscrollview和recyclerview內nestedscrollview或什麼? – Okas

+0

@Okas除了工具欄,用戶可以滾動整個屏幕 –

回答

2

您正確使用RecyclerView。不要將RecyclerView放入NestedScrollView,而是將RecyclerView中的「Header」和「Search box」作爲不同的視圖類型。

answer就是一個很好的例子。

+0

這種方法聽起來合適,但會有4種不同的視圖類型:標題(TextView),按鈕,搜索框,普通項目,處理每個事件時都很複雜。 –

+0

起初看起來很複雜。其實這是標準做法。同樣看着你的UI,看起來你可以把你的標題,按鈕和搜索框放在一個佈局中,所以它只會是兩種視圖類型。 – Okas

+0

哦,我明白了,我希望我能告訴你確切的屏幕設計。我會嘗試你的解決方案 –

相關問題