2

我有片段,它有一個searchview和viewpager。我的viewpager有3個片段和每個片段調用適配器。現在要實現searchview,我需要在視圖尋呼機中調用可見片段的適配器。從ViewPager中的片段調用適配器

SlidingTab(片段與搜索查看和ViewPager):

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:app="http://schemas.android.com/apk/res-auto" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:id="@+id/frame_main" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:orientation="vertical" 
    tools:context="com.RareMediaCompany.BDTrial.SlidingTab"> 

    <include 
     android:id="@+id/toolbar1" 
     layout="@layout/toolbar_job" /> 

    <com.RareMediaCompany.BDTrial.Utils.CustomTabLayout 
     android:id="@+id/sliding_tabs" 
     style="@style/CustomTabLayout" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:background="#eeeeee" 
     app:tabIndicatorColor="#f39220" 
     app:tabIndicatorHeight="3dp" 
     app:tabMaxWidth="0dp" 
     app:tabMode="fixed" 
     app:tabPaddingEnd="0dp" 
     app:tabPaddingStart="0dp" 
     app:tabTextAppearance="@android:style/TextAppearance.Widget.TabWidget" 
     app:tabSelectedTextColor="#808080" /> 

    <LinearLayout 
     android:id="@+id/linear1" 
     android:background="@android:color/white" 
     android:layout_width="match_parent" 
     android:layout_height="70dp" 
     android:orientation="horizontal" 
     android:weightSum="1"> 

     <android.support.v7.widget.SearchView 
     android:layout_width="300dp" 
     android:layout_height="45dp" 
     android:id="@+id/searchView" 
     android:layout_weight="0.9" 
     android:layout_marginRight="5dp" 
     android:layout_gravity="center" 
     style="@style/CitySearchView" 
     android:background="@drawable/searchview" 
     android:layout_marginLeft="10dp"/> 

     <LinearLayout 
      android:id="@+id/list_linearlayout" 
      android:layout_width="45dp" 
      android:layout_height="45dp" 
      android:layout_weight="0" 
      android:layout_gravity="center" 
      android:background="#f39220"> 
      <ImageView 
       android:layout_width="match_parent" 
       android:layout_height="wrap_content" 
       android:layout_marginTop="10dp" 
       android:layout_weight="0.3" 
       android:src="@drawable/list_icon_white"/> 

     </LinearLayout> 

     <LinearLayout 
      android:layout_width="45dp" 
      android:layout_height="45dp" 
      android:layout_gravity="center" 
      android:layout_weight="0" 
      android:layout_marginRight="10dp" 
      android:background="#75aadb"> 
      <ImageView 
       android:layout_width="20dp" 
       android:layout_height="wrap_content" 
       android:layout_marginTop="10dp" 
       android:layout_marginRight="2dp" 
       android:layout_weight="0.3" 
       android:src="@drawable/mapmarker"/> 

     </LinearLayout> 


    </LinearLayout> 
     <!--android:layout_width="320dp"--> 
     <!--android:layout_height="wrap_content"--> 
     <!--android:layout_marginLeft="10dp"--> 
     <!--android:layout_marginTop="10dp"--> 
     <!--android:id="@+id/searchview"/>--> 

    <android.support.v4.view.ViewPager 
     android:id="@+id/view_pager" 
     android:layout_width="match_parent" 
     android:layout_height="0dp" 
     android:layout_weight="1" 
     android:background="@android:color/white" /> 

</LinearLayout> 

SlidingTab.class(實施搜索查看)

public class SlidingTab extends Fragment { 
    private static final String TAG = "TabLayout"; 
    private static String POSITION = "POSITION"; 
    private Context context; 
    private SearchView searchview; 
    private Toolbar toolbar ; 


    /*searching/filteration query*/ 
    private SearchView.OnQueryTextListener OnQuerySearchView = new SearchView.OnQueryTextListener() { 

     @Override 
     public boolean onQueryTextSubmit(String arg0) { 
      // TODO Auto-generated method stub 
      return false; 
     } 

     @Override 
     public boolean onQueryTextChange(String arg0) { 
      // TODO Auto-generated method stub 
//   if (searchCheck) { 
      Log.i("Recycler query", arg0); 

//   Fragment frag = new PageAdapter(getChildFragmentManager(),context).getItem(viewPager.getCurrentItem()); 
      PageAdapter adapter = ((PageAdapter) viewPager.getAdapter()); 
      Fragment fragment = adapter.getFragment(viewPager.getCurrentItem()); 
      return false; 
     } 
    }; 

    public SlidingTab() { 
    } 
    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
    } 
    @Override 
    public View onCreateView(LayoutInflater inflater, ViewGroup container, 
          Bundle savedInstanceState) { 
     View v = inflater.inflate(R.layout.sliding_tab, container, false); 
     viewPager = (ViewPager) v.findViewById(R.id.view_pager); 
     viewPager.setOffscreenPageLimit(2); 
     viewPager.setAdapter(new PageAdapter(getChildFragmentManager(), context)); 
     slidingTab = (TabLayout) v.findViewById(R.id.sliding_tabs); 
     searchview = (SearchView)v.findViewById(R.id.searchView); 
     searchview.setOnQueryTextListener(OnQuerySearchView); 
     return v; 
    } 

PageAdapter.class

public class PageAdapter extends FragmentStatePagerAdapter { 
    private static final String TAG = "PageAdapter"; 
    final int PAGE_COUNT = 3; 
    private String tabTitles[] = new String[]{"New", "In Progress", "Completed"}; 
    private Context context; 

    public PageAdapter(FragmentManager fm, Context context) { 
     super(fm); 
     this.context = context; 
    } 


    @Override 
    public Fragment getItem(int position) { 
     Log.d(TAG, "New Instance Created :" + position); 
     switch (position) { 
      case 0: 
       return new JobFragment(); 

      case 1: 
       return new InProgressJobFragment(); 
      case 2: 
       return new CompletedJobFragment(); 
     } 
     return null; 
    } 


    @Override 
    public CharSequence getPageTitle(int position) { 
     return tabTitles[position]; 
    } 
    @Override 
    public int getCount() { 
     return PAGE_COUNT; 
    } 


} 

片段RecyclerView適配器(在圖尋呼機每個片段呼叫和具有搜索查看濾波器法)

public class JobAdapter extends RecyclerView.Adapter<JobAdapter.ViewHolder> { 
    private static final String TAG = "EMPLOYEE ADAPTER"; 
    private static final int VIEW_NORMAL = 0; 
    private ArrayList<Info> mDataset; 
    private ArrayList<Info> filterList; 
    private Context mAct; 

    public class ViewHolder extends RecyclerView.ViewHolder { 

     } 
    } 

    public void addApplications(ArrayList<Info> candidates) { 
     } 

    public void clearApplications() { 
    } 

    public JobAdapter(ArrayList<Info> myDataset, Context context) { 
     this.mDataset = myDataset; 
     this.mAct = context; 
    } 



    @Override 
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) { 
     View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.job_card_new, parent, false); 
     ViewHolder fh = new ViewHolder(v); 
     return fh; 
    } 

    @Override 
    public void onBindViewHolder(final ViewHolder holder, final int position) { 

//  } 



**// *searching/filtration on List*/** 
    public void filter(String charText) { 
     filterList.clear(); 
     if (charText.length() == 0) { 
      if(mDataset != null) { 
       filterList.addAll(mDataset); 
      } 
     } else { 
      if (mDataset != null){ 
       for (Info wp : mDataset) { 
        if(wp.jobNumber !=null){ 
         if(Pattern.compile(Pattern.quote(charText), Pattern.CASE_INSENSITIVE).matcher(wp.jobNumber).find()){ 
//     if (wp.heading1.toLowerCase().contains(charText.toLowerCase())) { 
          filterList.add(wp); 
         }}else{ 

        } 

        this.notifyItemRangeRemoved(0, filterList.size()); 

       } 
      } 
     } 
     notifyDataSetChanged(); 
    } 


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

請幫幫我在viewpager執行搜索查看。

回答

3

隨着上傳代碼 的幫助,我可以告訴大家,在你的片段

化妝方法命名爲搜索和 呼叫轉接器過濾方法

public void search(String text) 
{ 
adapter.filter(text) 
} 

你可以檢查片段的實例,並調用搜索方法此線後的片段

Fragment fragment = adapter.getFragment(viewPager.getCurrentItem()); 
+0

我正是那樣做的。 public boolean onQueryTextChange(final String arg0){ Log.i(「Recycler query」,arg0); int id = viewPager.getCurrentItem(); PageAdapter adapter =((PageAdapter)viewPager.getAdapter()); Fragment fragment = adapter.getFragment(viewPager.getCurrentItem()); ((JobFragment)片段).recycleAdapter.filter(arg0);如果(JobFragment的片段實例){ ((JobFragment)片段)。 }但我收到錯誤:在NULL對象上調用過濾器方法。 –

+1

我編輯了我的答案。請嘗試一個。 –

+0

還沒有工作。我每次打電話都會收到我的適配器。 –

0

I認爲最好的設計是使用Activity作爲需要去掉碎片的事件的中心「樞紐」。

所以這是我會怎麼做:用監聽器接口

  • 第一次啓動。這可以被聲明爲Activity的內接口:

    static interface QueryListener { 
    
        void onQueryChange(String query); 
    } 
    
  • 活動獲取屬性用於保持用於添加和刪除聽衆的聽衆和方法(聲明同步,防止那些討厭ConcurrentModificationException S:

    private List<QueryListener> mQueryListeners = new ArrayList<>(); 
    
    public synchronized void addQueryListener(@NonNull QueryListener listener) { 
    
        // check if the listener was already added 
        // you could also use a Set instead of a List for this 
        if (mQueryListeners.contains(listener)) return; 
    
        mQueryListeners.add(listener); 
    } 
    
    public synchronized void removeQueryListener(@NonNull QueryListener listener) { 
        mQueryListeners.remove(listener); 
    } 
    
  • 活動也得到一個方法,可以調用的片段與SearchView

    public synchronized void changeQuery(String query) { 
    
         for (QueryListener listener : mQueryListeners) { 
          listener.onQueryChange(query); 
         } 
    } 
    
  • 現在你與適配器片段需要實現QueryListener和註冊/註銷自己:

    onCreate()某處添加此行:

     ((YourActivity) getActivity()).addQueryListener(this); 
    

    註銷在onDestroy()

    @Override 
        public void onDestroy() { 
         ((YourActivity) getActivity()).removeQueryListener(this); 
         super.onDestroy(); 
        } 
    

    添加QueryListener方法實現:

    @Override 
    public void onQueryChange(String query) { 
    
        // if (isVisible()) {  if desired 
        mAdapter.filter(query); 
    } 
    

    注意:您不必具有片段實施QueryListener;你可以使用匿名類。但是你必須持有這個參考資料,以便以後可以撥打removeQueryListener()

  • 現在只要掛鉤的SearchView

    @Override 
    public boolean onQueryTextChange(String query) { 
    
        ((YourActivity) getActivity()).changeQuery(query); 
    } 
    

我用另一種方式是創建一個專用類註冊/通知聽衆。我創建這個類的一個實例,並有方法上的活動進行訪問:

 QueryHandler handler = ((YourActivity) getActivity()).getQueryHandler(); 
     // QueryHandler has all the listener properties and methods shown for the Activity 

     ... 

     handler.changeQuery(query); 

另外,您還可以使用很多開源事件總線包的一個像奧托傳遞事件之間的片段。

相關問題