如何将数据从 mainAcitivy 中的搜索视图传递到片段?
How to pass data from a searchview that is in mainAcitivy, to a fragment?
如何将数据从 mainActivity 中的搜索视图传递到片段?我正在为 BottonNavigationActivity 出汗。
已经尝试过:
@Override
public boolean onCreateOptionsMenu(Menu menu) {
super.onCreateOptionsMenu(menu);
getMenuInflater().inflate(R.menu.menu_search, menu);
MenuItem searchItem = menu.findItem(R.id.action_search);
searchView = (SearchView) searchItem.getActionView();
searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
@Override
public boolean onQueryTextSubmit(String query) {
Bundle bundle = new Bundle();
bundle.putString("teste", "123");
fragment2.setArguments(bundle);
return false;
}
@Override
public boolean onQueryTextChange(String newText) {
return false;
}
});
return true;
}
在片段中:
Bundle bundle = this.getArguments();
if (bundle != null) {
String foo = getArguments().getString("teste");
Toast.makeText(getActivity().getApplicationContext(), foo, Toast.LENGTH_SHORT).show();
}
还有:
Bundle bundle = this.getArguments();
if (bundle != null) {
String foo = bundle.getString("teste");
Toast.makeText(getActivity().getApplicationContext(), foo,
Toast.LENGTH_SHORT).show();
}
应用程序正常运行。有人可以帮我吗?
添加片段:
Fragment currentFragment;
private home fragment1 = new home();
private clientes fragment2 = new clientes();
private contas_pagar fragment3 = new contas_pagar();
private BottomViewPagerAdapter bottomViewPagerAdapter;
private ArrayList<AHBottomNavigationItem> bottomNavigationItems = new ArrayList<>();
// UI
private AHBottomNavigationViewPager viewPagerBottom;
private AHBottomNavigation bottomNavigation;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initUI();
}
private void initUI() {
bottomNavigation = (AHBottomNavigation) findViewById(R.id.bottom_navigation);
viewPagerBottom = (AHBottomNavigationViewPager) findViewById(R.id.view_pager_bottom);
//Aqui onde é adicionado os fragments no bottom
viewPagerBottom.setOffscreenPageLimit(2);
bottomViewPagerAdapter = new BottomViewPagerAdapter(getSupportFragmentManager());
bottomViewPagerAdapter.add(fragment1);
bottomViewPagerAdapter.add(fragment2);
bottomViewPagerAdapter.add(fragment3);
viewPagerBottom.setAdapter(bottomViewPagerAdapter);
currentFragment = bottomViewPagerAdapter.getCurrentFragment();
AHBottomNavigationItem item1 = new AHBottomNavigationItem("Home", R.drawable.ic_home_black_24dp);
AHBottomNavigationItem item2 = new AHBottomNavigationItem("Clientes", R.drawable.clientes);
AHBottomNavigationItem item3 = new AHBottomNavigationItem("Contas", R.drawable.pagar);;
bottomNavigationItems.add(item1);
bottomNavigationItems.add(item2);
bottomNavigationItems.add(item3);
bottomNavigation.addItems(bottomNavigationItems);
bottomNavigation.setAccentColor(Color.parseColor("#F63D2B"));
bottomNavigation.setInactiveColor(Color.parseColor("#747474"));
bottomNavigation.setCurrentItem(0);
//bottomNavigation.setNotification("Vencida", 2);
bottomNavigation.findViewById(R.id.textView18);
bottomNavigation.setOnTabSelectedListener(new AHBottomNavigation.OnTabSelectedListener() {
@Override
public boolean onTabSelected(int position, boolean wasSelected) {
if (currentFragment == null) {
currentFragment = bottomViewPagerAdapter.getCurrentFragment();
}
if (currentFragment != null) {
if (currentFragment instanceof home) {
fragment1.willBeHidden();
} else if (currentFragment instanceof clientes) {
fragment2.willBeHidden();
} else if (currentFragment instanceof contas_pagar) {
fragment3.willBeHidden();
}
}
//Aqui é onde é setado qual o fragment atual
//Em seguida é pego o fragment atual e feito o fade dependendo de qual instancia for
viewPagerBottom.setCurrentItem(position, false);
currentFragment = bottomViewPagerAdapter.getCurrentFragment();
if (currentFragment instanceof home) {
fragment1.willBeDisplayed();
} else if (currentFragment instanceof clientes) {
fragment2.willBeDisplayed();
} else if (currentFragment instanceof contas_pagar) {
fragment3.willBeDisplayed();
}
if (position == 0) {
}
return true;
}
});
bottomNavigation.setOnNavigationPositionListener(new AHBottomNavigation.OnNavigationPositionListener() {
@Override
public void onPositionChange(int y) {
Log.d("DemoActivity", "BottomNavigation Position: " + y);
}
});
}
适配器:
public class BottomViewPagerAdapter extends FragmentPagerAdapter {
private ArrayList<Fragment> fragments = new ArrayList<>();
private Fragment currentFragment;
public BottomViewPagerAdapter(FragmentManager fm) {
super(fm);
}
public void add(Fragment frag) {
this.fragments.add(frag);
}
@Override
public Fragment getItem(int position) {
return fragments.get(position);
}
@Override
public int getCount() {
return fragments.size();
}
@Override
public void setPrimaryItem(ViewGroup container, int position, Object object)
{
if (getCurrentFragment() != object) {
currentFragment = ((Fragment) object);
}
super.setPrimaryItem(container, position, object);
}
/**
* Get the current fragment
*/
public Fragment getCurrentFragment() {
return currentFragment;
}
}
Logcat:
02-14 13:50:07.435 12250-12250/insidetechnology.studio.ostdor.forbusiness E/AndroidRuntime: FATAL EXCEPTION: main
Process: insidetechnology.studio.ostdor.forbusiness, PID: 12250
java.lang.IllegalStateException: Fragment already active
at android.support.v4.app.Fragment.setArguments(Fragment.java:562)
at insidetechnology.studio.ostdor.forbusiness.MainActivity.onQueryTextSubmit(MainActivity.java:196)
at android.support.v7.widget.SearchView.onSubmitQuery(SearchView.java:1242)
at android.support.v7.widget.SearchView.onEditorAction(SearchView.java:1219)
at android.widget.TextView.onEditorAction(TextView.java:4983)
at com.android.internal.widget.EditableInputConnection.performEditorAction(EditableInputConnection.java:145)
at com.android.internal.view.IInputConnectionWrapper.executeMessage(IInputConnectionWrapper.java:364)
at com.android.internal.view.IInputConnectionWrapper$MyHandler.handleMessage(IInputConnectionWrapper.java:91)
at android.os.Handler.dispatchMessage(Handler.java:110)
at android.os.Looper.loop(Looper.java:208)
at android.app.ActivityThread.main(ActivityThread.java:6267)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1063)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:924)
setArguments()
函数用于传递创建Fragments的数据。要与现有片段通信,您只需调用自定义片段 class 本身的方法:
public boolean onQueryTextSubmit(String query) {
if (fragment2 instanceof clientes) {
((clientes)fragment2).setTestString("123");
}
}
您需要向 fragment2
添加一个方法 setTestString
,如下所示:
public void setTestString(String text) {
Toast.makeText(getActivity(), foo, Toast.LENGTH_SHORT).show();
}
实际上 Google's Android Training 中有一篇很好的文章也描述了如何将数据传回 Activity。
您可以通过创建接口回调在 activity 和 fragment 之间进行通信。在 onQueryTextSubmit 中调用该回调并在片段中接收该值。
您可以使用 getCurrentFragment()
使用您的适配器,将其类型转换为您的 fragment2 并调用要传递字符串的 public 方法。
如何将数据从 mainActivity 中的搜索视图传递到片段?我正在为 BottonNavigationActivity 出汗。
已经尝试过:
@Override
public boolean onCreateOptionsMenu(Menu menu) {
super.onCreateOptionsMenu(menu);
getMenuInflater().inflate(R.menu.menu_search, menu);
MenuItem searchItem = menu.findItem(R.id.action_search);
searchView = (SearchView) searchItem.getActionView();
searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
@Override
public boolean onQueryTextSubmit(String query) {
Bundle bundle = new Bundle();
bundle.putString("teste", "123");
fragment2.setArguments(bundle);
return false;
}
@Override
public boolean onQueryTextChange(String newText) {
return false;
}
});
return true;
}
在片段中:
Bundle bundle = this.getArguments();
if (bundle != null) {
String foo = getArguments().getString("teste");
Toast.makeText(getActivity().getApplicationContext(), foo, Toast.LENGTH_SHORT).show();
}
还有:
Bundle bundle = this.getArguments();
if (bundle != null) {
String foo = bundle.getString("teste");
Toast.makeText(getActivity().getApplicationContext(), foo,
Toast.LENGTH_SHORT).show();
}
应用程序正常运行。有人可以帮我吗?
添加片段:
Fragment currentFragment;
private home fragment1 = new home();
private clientes fragment2 = new clientes();
private contas_pagar fragment3 = new contas_pagar();
private BottomViewPagerAdapter bottomViewPagerAdapter;
private ArrayList<AHBottomNavigationItem> bottomNavigationItems = new ArrayList<>();
// UI
private AHBottomNavigationViewPager viewPagerBottom;
private AHBottomNavigation bottomNavigation;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initUI();
}
private void initUI() {
bottomNavigation = (AHBottomNavigation) findViewById(R.id.bottom_navigation);
viewPagerBottom = (AHBottomNavigationViewPager) findViewById(R.id.view_pager_bottom);
//Aqui onde é adicionado os fragments no bottom
viewPagerBottom.setOffscreenPageLimit(2);
bottomViewPagerAdapter = new BottomViewPagerAdapter(getSupportFragmentManager());
bottomViewPagerAdapter.add(fragment1);
bottomViewPagerAdapter.add(fragment2);
bottomViewPagerAdapter.add(fragment3);
viewPagerBottom.setAdapter(bottomViewPagerAdapter);
currentFragment = bottomViewPagerAdapter.getCurrentFragment();
AHBottomNavigationItem item1 = new AHBottomNavigationItem("Home", R.drawable.ic_home_black_24dp);
AHBottomNavigationItem item2 = new AHBottomNavigationItem("Clientes", R.drawable.clientes);
AHBottomNavigationItem item3 = new AHBottomNavigationItem("Contas", R.drawable.pagar);;
bottomNavigationItems.add(item1);
bottomNavigationItems.add(item2);
bottomNavigationItems.add(item3);
bottomNavigation.addItems(bottomNavigationItems);
bottomNavigation.setAccentColor(Color.parseColor("#F63D2B"));
bottomNavigation.setInactiveColor(Color.parseColor("#747474"));
bottomNavigation.setCurrentItem(0);
//bottomNavigation.setNotification("Vencida", 2);
bottomNavigation.findViewById(R.id.textView18);
bottomNavigation.setOnTabSelectedListener(new AHBottomNavigation.OnTabSelectedListener() {
@Override
public boolean onTabSelected(int position, boolean wasSelected) {
if (currentFragment == null) {
currentFragment = bottomViewPagerAdapter.getCurrentFragment();
}
if (currentFragment != null) {
if (currentFragment instanceof home) {
fragment1.willBeHidden();
} else if (currentFragment instanceof clientes) {
fragment2.willBeHidden();
} else if (currentFragment instanceof contas_pagar) {
fragment3.willBeHidden();
}
}
//Aqui é onde é setado qual o fragment atual
//Em seguida é pego o fragment atual e feito o fade dependendo de qual instancia for
viewPagerBottom.setCurrentItem(position, false);
currentFragment = bottomViewPagerAdapter.getCurrentFragment();
if (currentFragment instanceof home) {
fragment1.willBeDisplayed();
} else if (currentFragment instanceof clientes) {
fragment2.willBeDisplayed();
} else if (currentFragment instanceof contas_pagar) {
fragment3.willBeDisplayed();
}
if (position == 0) {
}
return true;
}
});
bottomNavigation.setOnNavigationPositionListener(new AHBottomNavigation.OnNavigationPositionListener() {
@Override
public void onPositionChange(int y) {
Log.d("DemoActivity", "BottomNavigation Position: " + y);
}
});
}
适配器:
public class BottomViewPagerAdapter extends FragmentPagerAdapter {
private ArrayList<Fragment> fragments = new ArrayList<>();
private Fragment currentFragment;
public BottomViewPagerAdapter(FragmentManager fm) {
super(fm);
}
public void add(Fragment frag) {
this.fragments.add(frag);
}
@Override
public Fragment getItem(int position) {
return fragments.get(position);
}
@Override
public int getCount() {
return fragments.size();
}
@Override
public void setPrimaryItem(ViewGroup container, int position, Object object)
{
if (getCurrentFragment() != object) {
currentFragment = ((Fragment) object);
}
super.setPrimaryItem(container, position, object);
}
/**
* Get the current fragment
*/
public Fragment getCurrentFragment() {
return currentFragment;
}
}
Logcat:
02-14 13:50:07.435 12250-12250/insidetechnology.studio.ostdor.forbusiness E/AndroidRuntime: FATAL EXCEPTION: main Process: insidetechnology.studio.ostdor.forbusiness, PID: 12250 java.lang.IllegalStateException: Fragment already active at android.support.v4.app.Fragment.setArguments(Fragment.java:562) at insidetechnology.studio.ostdor.forbusiness.MainActivity.onQueryTextSubmit(MainActivity.java:196) at android.support.v7.widget.SearchView.onSubmitQuery(SearchView.java:1242) at android.support.v7.widget.SearchView.onEditorAction(SearchView.java:1219) at android.widget.TextView.onEditorAction(TextView.java:4983) at com.android.internal.widget.EditableInputConnection.performEditorAction(EditableInputConnection.java:145) at com.android.internal.view.IInputConnectionWrapper.executeMessage(IInputConnectionWrapper.java:364) at com.android.internal.view.IInputConnectionWrapper$MyHandler.handleMessage(IInputConnectionWrapper.java:91) at android.os.Handler.dispatchMessage(Handler.java:110) at android.os.Looper.loop(Looper.java:208) at android.app.ActivityThread.main(ActivityThread.java:6267) at java.lang.reflect.Method.invoke(Native Method) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1063) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:924)
setArguments()
函数用于传递创建Fragments的数据。要与现有片段通信,您只需调用自定义片段 class 本身的方法:
public boolean onQueryTextSubmit(String query) {
if (fragment2 instanceof clientes) {
((clientes)fragment2).setTestString("123");
}
}
您需要向 fragment2
添加一个方法 setTestString
,如下所示:
public void setTestString(String text) {
Toast.makeText(getActivity(), foo, Toast.LENGTH_SHORT).show();
}
实际上 Google's Android Training 中有一篇很好的文章也描述了如何将数据传回 Activity。
您可以通过创建接口回调在 activity 和 fragment 之间进行通信。在 onQueryTextSubmit 中调用该回调并在片段中接收该值。
您可以使用 getCurrentFragment()
使用您的适配器,将其类型转换为您的 fragment2 并调用要传递字符串的 public 方法。