我创建了一个带有bottomNavigation栏的应用程序,该栏由5个片段组成。第二个片段(搜索片段)由1个带有setOnClickListener的TextView组成,因此一旦选中它,布局活动将在顶部打开,其中包括一个ListView允许用户选择/单击特定项目,因此稍后在此选定项目上的信息应显示在(搜索片段)内的那个TextView上。
我试图使用Bundle从中传递数据,但是它不起作用。
-------------------------- MainActivity -------------------- --------------------
public class MainActivity extends AppCompatActivity {
final Fragment f1 = new HomeFragment();
final Fragment f2 = new SearchFragment();
final Fragment f3 = new CameraFragment();
final Fragment f4 = new ChatFragment();
final Fragment f6 = new LogginFragment();
final FragmentManager fm = getSupportFragmentManager();
Fragment active = f1;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//the fragment
BottomNavigationViewEx bnve = (BottomNavigationViewEx) findViewById(R.id.bottom_navigation);
bnve.enableAnimation(false);
bnve.enableShiftingMode(false);
bnve.enableItemShiftingMode(false);
bnve.setOnNavigationItemSelectedListener(navListener);
fm.beginTransaction().add(R.id.fragment_container, f6, "6").hide(f6).commit();
//fm.beginTransaction().add(R.id.fragment_container, f5, "5").hide(f5).commit();
fm.beginTransaction().add(R.id.fragment_container, f4, "4").hide(f4).commit();
fm.beginTransaction().add(R.id.fragment_container, f3, "3").hide(f3).commit();
fm.beginTransaction().add(R.id.fragment_container, f2, "2").hide(f2).commit();
fm.beginTransaction().add(R.id.fragment_container, f1, "1").commit();
}
public void MovetoSuccess(){
startActivity(new Intent(this,SuccessActivity.class));
}
public void MovetoCategoiry(){
startActivity(new Intent(this,Categories_Activity.class));
}
public BottomNavigationViewEx.OnNavigationItemSelectedListener navListener =
new BottomNavigationViewEx.OnNavigationItemSelectedListener() {
@Override
public boolean onNavigationItemSelected(@NonNull MenuItem item) {
switch (item.getItemId()) {
case R.id.nav_home:
fm.beginTransaction().hide(active).show(f1).commit();
active = f1;
return true;
case R.id.nav_search:
fm.beginTransaction().hide(active).show(f2).commit();
active = f2;
return true;
case R.id.nav_camera:
fm.beginTransaction().hide(active).show(f3).commit();
active = f3;
return true;
case R.id.nav_chat:
fm.beginTransaction().hide(active).show(f4).commit();
active = f4;
return true;
case R.id.nav_account:
fm.beginTransaction().hide(active).show(f6).commit();
active = f6;
return true;
}
return false;
}
};
}
-------------------------- --- SearchFragment ----------------- ----------------
public class SearchFragment extends Fragment {
TextView Categories;
static boolean status = false;
String SelectedItem;
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.fragment_search,container,false);
Categories = (TextView) v.findViewById(R.id.categories);
Bundle bundle = this.getArguments();
if (bundle != null){
String SelectedItem = bundle.getString("ss");
Categories.setText(SelectedItem);
}
Categories.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
((MainActivity)getActivity()).MovetoCategoiry();
}
});
return v;
}
public Boolean getifItemselected(Boolean status){
this.status=status;
return true;
}
}
-------------------------- Categories_Activity -------------------- -------------
public class Categories_Activity extends AppCompatActivity implements View.OnClickListener {
ImageView BacktoMainPage;
ListView subCate;
public String selectedItem;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.tab_subcategory);
subCate = (ListView)findViewById(R.id.listview_subcate);
BacktoMainPage = (ImageView)findViewById(R.id.BacktoMain);
BacktoMainPage.setOnClickListener(this);
final SearchFragment sf = new SearchFragment();
subCate.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
selectedItem = String.valueOf(parent.getItemAtPosition(position));
Bundle bundle = new Bundle();
bundle.putString("ss",selectedItem);
sf.setArguments(bundle);
finish();
}
});
}
@Override
public void onClick(View v) {
if (v == BacktoMainPage)
finish();
}
}
答案 0 :(得分:0)
如果用户单击listview的项目,则可以使用静态列表,可以将其添加到列表中并在该列表中使用任何片段,也可以使用setter getter的公共类来设置和获取值。
答案 1 :(得分:0)
尝试像这样使用Singleton
public class DataHolder {
//design pattern to share arguments between fragments and activities
private static DataHolder dataHolder = null;
private DataHolder() {
}
public static DataHolder getInstance() {
if (dataHolder == null)
{
dataHolder = new DataHolder();
}
return dataHolder;
}
private String item;
public String getItem() {
return item;
}
public void setItem(String item) {
this.item = item;
}
}
要保存您的物品,请使用:
DataHolder.getInstance().setItem(selectedItem);
并使用来访问您的数据:
DataHolder.getInstance().getItem();
这可以帮助您在片段和活动之间传输数据