您好我是新手并开发Android应用程序。原谅我的英语。
在我的应用程序中,我有MainActivity.java文件,其中我使用json解析来解析Languagelist Array。我将它发送到下一个活动,即抽屉活动。 Drawer Activity包含具有TabLayout的片段。在这个DrawerActivity我有工具栏,我打开微调器并设置我从前一个(MainActivity.java)得到的arraylist。直到这里一切正常,但我想要的是当用户从工具栏微调器中选择或更改语言时,应该刷新第一个选项卡片段的列表。但它没有用,任何人都可以帮我解决这个问题。
DrawerActivity.java
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
drawer.setDrawerListener(toggle);
toggle.syncState();
Bundle bundle=new Bundle();
bundle.putString("frommusic", CAT_ID);
bundle.putString("action", DRAWER_ACTION_LATEST);
bundle.putString("sel_lang", SELECTED_LANGUAGE);
fragmentManager = getSupportFragmentManager();
final FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
fragment = new LibraryFragment();
fragment.setArguments(bundle);
fragmentTransaction.replace(R.id.main_container_wrapper, fragment);
fragmentTransaction.commit();
NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);
View header = navigationView.inflateHeaderView(R.layout.nav_header_music);
TextView profileName = (TextView) header.findViewById(R.id.profile_name);
navigationView.setNavigationItemSelectedListener(new NavigationView.OnNavigationItemSelectedListener() {
@Override
public boolean onNavigationItemSelected(MenuItem item) {
int id = item.getItemId();
if (id == R.id.nav_library) {
Bundle bundle=new Bundle();
bundle.putString("frommusic", CAT_ID);
bundle.putString("action", DRAWER_ACTION_LATEST);
bundle.putString("sel_lang", SELECTED_LANGUAGE);
fragmentManager = getSupportFragmentManager();
final FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
fragment = new LibraryFragment();
fragment.setArguments(bundle);
fragmentTransaction.replace(R.id.main_container_wrapper, fragment);
fragmentTransaction.commit();
} else if (id == R.id.nav_song) {
Bundle bundle=new Bundle();
bundle.putString("frommusic", CAT_ID);
bundle.putString("action", DRAWER_ACTION_POPULAR);
bundle.putString("sel_lang", SELECTED_LANGUAGE);
fragmentManager = getSupportFragmentManager();
final FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
fragment = new LibraryFragment();
fragment.setArguments(bundle);
fragmentTransaction.replace(R.id.main_container_wrapper, fragment);
fragmentTransaction.commit();
} /*else if (id == R.id.nav_playlist) {
} else if (id == R.id.nav_sound_cound) {
} else if (id == R.id.nav_raw_folder) {
}*/
FragmentTransaction transaction = fragmentManager.beginTransaction();
transaction.replace(R.id.main_container_wrapper, fragment);
transaction.commit();
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
assert drawer != null;
drawer.closeDrawer(GravityCompat.START);
return true;
}
});
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.music, menu);
MenuItem item = menu.findItem(R.id.action_settings);
spinner = (Spinner) MenuItemCompat.getActionView(item);
rcAdapter = new CustomBaseAdapter(MusicActivity.this, langList);
spinner.setAdapter(rcAdapter);
spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
LanguageModel ln=new LanguageModel();
ln=langList.get(position);
SELECTED_LANGUAGE=ln.getLangid();
System.out.println("IF SELECT LA"+SELECTED_LANGUAGE);
/*if(SELECTED_LANGUAGE.equals("1"))
{
SELECTED_LANGUAGE=ln.getLangid();
System.out.println("IF SELECT LA"+SELECTED_LANGUAGE);
Bundle bundle=new Bundle();
bundle.putString("frommusic", CAT_ID);
bundle.putString("action", DRAWER_ACTION_LATEST);
bundle.putString("sel_lang", SELECTED_LANGUAGE);
fragmentManager = getSupportFragmentManager();
final FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
fragment = new LibraryFragment();
fragment.setArguments(bundle);
fragmentTransaction.replace(R.id.main_container_wrapper, fragment);
fragmentTransaction.commit();
}
else
{
SELECTED_LANGUAGE=ln.getLangid();
System.out.println("ELSE SELECT LA"+SELECTED_LANGUAGE);
Bundle bundle=new Bundle();
bundle.putString("frommusic", CAT_ID);
bundle.putString("action", DRAWER_ACTION_LATEST);
bundle.putString("sel_lang", SELECTED_LANGUAGE);
fragmentManager = getSupportFragmentManager();
final FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
fragment = new LibraryFragment();
fragment.setArguments(bundle);
fragmentTransaction.replace(R.id.main_container_wrapper, fragment);
fragmentTransaction.commit();
}*/
}
@Override
public void onNothingSelected(AdapterView<?> parent) {
}
});
/*ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(this,
R.array.spinner_list_item_array, android.R.layout.simple_spinner_item);*/
//adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
return true;
}
LibraryFragment.java
catid=getArguments().getString("frommusic");
SELECTED_ACTION=getArguments().getString("action");
SELECTED_LANGUAGE=getArguments().getString("sel_lang");
System.out.println("in library"+SELECTED_LANGUAGE);
tabLayout = (TabLayout)view.findViewById(R.id.tabs);
viewPager = (ViewPager)view.findViewById(R.id.view_pager);
viewPager.setAdapter(new CustomFragmentPageAdapter(getChildFragmentManager()));
tabLayout.setupWithViewPager(viewPager);
return view;
}
public class CustomFragmentPageAdapter extends FragmentPagerAdapter {
private final String TAG = CustomFragmentPageAdapter.class.getSimpleName();
private static final int FRAGMENT_COUNT = 3;
public CustomFragmentPageAdapter(FragmentManager fm) {
super(fm);
}
@Override
public Fragment getItem(int position) {
switch (position){
case 0:
Fragment f1 = new SongFragment();
Bundle args1 = new Bundle();
args1.putString("fromlibfrag", catid);
args1.putString("action", SELECTED_ACTION);
args1.putString("sel_lang", SELECTED_LANGUAGE);
f1.setArguments(args1);
return f1;
case 1:
Fragment f2 = new PhotoFragment();
Bundle args2 = new Bundle();
args2.putString("fromlibfrag", catid);
args2.putString("action", SELECTED_ACTION);
f2.setArguments(args2);
return f2;
case 2:
Fragment f3 = new SmsFragment();
Bundle args3 = new Bundle();
args3.putString("fromlibfrag", catid);
args3.putString("action", SELECTED_ACTION);
f3.setArguments(args3);
return f3;
}
return null;
}
@Override
public int getCount() {
return FRAGMENT_COUNT;
}
@Override
public CharSequence getPageTitle(int position) {
switch (position){
case 0:
return "Videos";
case 1:
return "Photos";
case 2:
return "SMS";
}
return null;
}
}
SongFragment.java
getActivity().setTitle("Your Status");
catid=getArguments().getString("fromlibfrag");
SELECTED_ACTION=getArguments().getString("action");
SELECTED_LANGUAGE=getArguments().getString("sel_lang");
System.out.println("in songfragment"+SELECTED_LANGUAGE);
vidRecyclerView = (RecyclerView)view.findViewById(R.id.song_list);
linearLayoutManager = new LinearLayoutManager(getActivity());
vidRecyclerView.setLayoutManager(linearLayoutManager);
vidRecyclerView.setHasFixedSize(true);
VIDEO_LIST_URL= WebUrls.WEB_URL+"GetVideos&language_id="+SELECTED_LANGUAGE+"&action="+SELECTED_ACTION+"&category_id="+catid;
System.out.println("FINAL URL"+VIDEO_LIST_URL);
pDialog = new ProgressDialog(getActivity());
// Showing progress dialog before making http request
pDialog.setMessage("Loading...");
pDialog.show();
makeJsonArrayRequestvideos();
答案 0 :(得分:0)
在LibraryFragment.java
中创建一个公共方法
将其命名为updateData()
//通过您要使用的参数。
现在在
DrawerActivity.java
制作
LibraryFragment
实例公开
navigationView.setNavigationItemSelectedListener(new NavigationView.OnNavigationItemSelectedListener() {
@Override
public boolean onNavigationItemSelected(MenuItem item) {
int id = item.getItemId();
if (id == R.id.nav_library) {
Bundle bundle=new Bundle();
bundle.putString("frommusic", CAT_ID);
bundle.putString("action", DRAWER_ACTION_LATEST);
bundle.putString("sel_lang", SELECTED_LANGUAGE);
fragmentManager = getSupportFragmentManager();
final FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
mLibraryFragment= new LibraryFragment();
fragment.setArguments(bundle);
fragmentTransaction.replace(R.id.main_container_wrapper, fragment);
fragmentTransaction.commit();
} else if (id == R.id.nav_song) {
Bundle bundle=new Bundle();
bundle.putString("frommusic", CAT_ID);
bundle.putString("action", DRAWER_ACTION_POPULAR);
bundle.putString("sel_lang", SELECTED_LANGUAGE);
fragmentManager = getSupportFragmentManager();
final FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
fragment = new LibraryFragment();
fragment.setArguments(bundle);
fragmentTransaction.replace(R.id.main_container_wrapper, fragment);
fragmentTransaction.commit();
} /*else if (id == R.id.nav_playlist) {
} else if (id == R.id.nav_sound_cound) {
} else if (id == R.id.nav_raw_folder) {
}*/
FragmentTransaction transaction = fragmentManager.beginTransaction();
transaction.replace(R.id.main_container_wrapper, fragment);
transaction.commit();
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
assert drawer != null;
drawer.closeDrawer(GravityCompat.START);
return true;
}
});
spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
LanguageModel ln=new LanguageModel();
ln=langList.get(position);
SELECTED_LANGUAGE=ln.getLangid();
System.out.println("IF SELECT LA"+SELECTED_LANGUAGE);
if(mLibraryFragment!=null){
mLibraryFragment.updateData()//pass the param
you want upate in library fragment.
}
}