2017-05-30 125 views
0

場景:您有兩個片段,填充ListAdapter的Fragment A,包含具有ListAdapter的SearchView的片段B。從片段B中的ListAdapter更新SearchView列表中的片段A

問:Fragment A如何更新Fragment B的SearchView的ListAdapter?

public class MainActivity extends FragmentActivity { 
@Override 
protected void onCreate(Bundle savedInstanceState) { 
super.onCreate(savedInstanceState); 
setContentView(R.layout.main_activity); 

SectionsPagerAdapter mSectionsPagerAdapter = 
    new SectionsPagerAdapter(getSupportFragmentManager()); 
ViewPager viewPager = 
    (ViewPager)findViewById(R.id.main_activity_container); 
viewPager.setAdapter(mSectionsPagerAdapter); 

TabLayout tabLayout = 
(TabLayout) findViewById(R.id.main_activity_tabs); 
tabLayout.setupWithViewPager(viewPager); 
} //MainActivity class 

private class SectionsPagerAdapter extends FragmentPagerAdapter { 
public SectionsPagerAdapter(FragmentManager fm) { 
    super(fm); 
} 

@Override 
public Fragment getItem(int position) { 
    switch(position){ 
      case 0: 
       Fragment_1 fr1 = new fr1(); 
       return fr1; 
      case 1: 
       Fragment_2 fr2 = new fr2(); 
       return fr2; 
      default: 
       return null; 
     } 
    } 
} //sectionPageAdapter class 

public class Fragment_1 extends Fragment { 
private View rootView; 
@Override 
public View onCreateView(final LayoutInflater inflater, final ViewGroup container, 
         final Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    rootView = inflater.inflate(R.layout.fr1, container, false); 

    /* toggle button will cause data to be read and updated */ 
    /* from fr1 to fr2 */ 
    handleToggleButton(); 

    return rootView; 
} 

private void handleToggleButton(){ 
    ToggleButton tglBroadCaseBtn = (ToggleButton) this.rootView.findViewById(R.id.broadcast_btn); 
    tglBroadCaseBtn.setOnClickListener(new View.OnClickListener() { 
     @Override 
     public void onClick(View view) { 
      /* update content in searchview */ 
      updateSearchView(); 
     } 
    } 
} 

public void updateSearchView(){ 
     new Handler().post(new Runnable() { 
      @Override 
      public void run() { 

      /* get data from database */ 
       final Cursor cursor = dblocate.rawQuery(DB_SEL + DB_TABLE, null); 
      /* populate ListAdapter */ 
       final ListAdapter la = new ListAdapter(getContext(), cursor, false); 

       /* update list adapter in fr_2 */ 
       la.notifyDataSetChanged(); /* <--- THIS IS HARD */ 

       cursor.close(); 
      } 
     }); 
    } 
} 

} //Fragment_1 class 


public class Fragment_2 extends Fragment { 
private View rootView; 
private SearchManager searchManager; 
private android.support.v7.widget.SearchView searchView; 
@Override 
public View onCreateView(final LayoutInflater inflater, final ViewGroup container, 
         final Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    rootView = inflater.inflate(R.layout.fr2, container, false); 

    searchManager = (SearchManager) rootView.getContext().getSystemService(Context.SEARCH_SERVICE); 
    searchView = createSearchView(); 

    new Handler().post(new Runnable() { 
      @Override 
      public void run() { 
       final Cursor cursor = db.rawQuery(DB_SEL + DB_TABLE, null); 
       listAdapter = new ListAdapter(getContext(), cursor, false); 
       searchView.setSuggestionsAdapter(listAdapter); 
       searchView.setOnSuggestionListener(new SearchView.OnSuggestionListener() { 
        @Override 
        public boolean onSuggestionSelect(int position) { 
         return true; 
        } 

        @Override 
        public boolean onSuggestionClick(int position) { 
         Cursor cursor1 = (Cursor) searchView.getSuggestionsAdapter().getItem(position); 
         String suggestion = cursor1.getString(cursor1.getColumnIndex(cursor1.getColumnName(position))); 
         ListSelection selection = new ListSelection(); 
         selection.setSelection(suggestion); 
         return true; 
        } 
       }); 
       cursor.close(); 
      } 
     }); 

    return rootView; 
} 

private SearchView createSearchView(){ 
    SearchView temp; 
    temp = (android.support.v7.widget.SearchView) nestedView.findViewById(R.id.search_view); 
    temp.setSearchableInfo(searchManager.getSearchableInfo(getActivity().getComponentName())); 
    temp.setQueryHint(SEARCH_VIEW_QUERY_HINT); 
    temp.onActionViewExpanded(); 
    temp.setActivated(true); 
    temp.setIconified(false); 

    return temp; 
} 

} //Fragment_2 class 

public class ListAdapter extends CursorAdapter { 
private static final int DB_FIELD = 2; 

public ListAdapter(Context context, Cursor cursor, boolean autoQuery) { 
    super(context, cursor, autoQuery); 
} 

@Override 
public View newView(Context context, Cursor cursor, ViewGroup parent) { 
    LayoutInflater inflater = LayoutInflater.from(context); 
    return inflater.inflate(R.layout.fr2_sv_single_row_item, parent, false); 
} 

@Override 
public void bindView(View view, Context context, Cursor cursor) { 
    TextView tvItem = (TextView) view.findViewById(R.id.tv_some_name); 
    tvItem.setText(cursor.getString(cursor.getColumnIndex(cursor.getColumnName(DB_FIELD)))); 
} 
} //ListAdapter 

enter image description here

+0

使用界面https://developer.android.com/training/basics/fragments/communicating.html –

回答

0

碎片不應該直接通信。您應該使用您的活動來進行子片段之間的通信。您可以創建一個接口來定義片段之間通信的合約。

public interface UpdateSerachViewList { 
    void update(List<> data); 
} 

活動將執行此接口。活動可以同時訪問fragment1和fragment2.

如果要將消息從frag1發送到frag2,則fragment1將在onAttach方法中將活動(Context參數)保存爲UpdateSerachViewList處理程序。

public class Frag1 extends Fragment { 
    private UpdateSerachViewList mCallback; 
    @Override 
    public void onAttach(Context context) { 
     super.onAttach(context); 
     mCallback = (UpdateSerachViewList) context; 
// If exception is throws it measn you want to use frafgment with activity which does not implement communication contract. Design error 
    } 

    @Override 
    public void onStart() { 
     super.onStart(); 
     mCallback.update(...); 
    } 
} 

在Activity中,您可以調用Fragment2上您想要的方法。或者你可以讓frag2同時執行契約,所以你不必直接引用Frag2,而只是通信契約。

+0

感謝您的回覆,Karoslav Klech。我已經給了你一些想法,你說得對,這裏的接口就是解決方案。 – EvOlaNdDaZly

+0

這對我很有幫助:https://developer.android.com/training/basics/fragments/communicating.html – EvOlaNdDaZly

0

您可以訪問Fragment通過ID

Fragment_1 fragment = (Fragment_1) getSupportFragmentManager().findFragmentById(R.layout.fr1); 

fragment.updateSearchView(); 
+0

這是我們當時可用的東西的一個很好的用法。這很簡單。但是,正如Jaroslav Klech指出的那樣,它涉及更多,並且最多隻需要一個通信接口。我的目標是從Fragment 1中傳遞的數據更新Fragment 2.例如,單擊Fragment 1中的按鈕,更新Fragment 2中的SearchView並彈出Fragment 2中的ListView。 – EvOlaNdDaZly

相關問題