Я использую основное действие, чтобы показать разные фрагменты.Мне нужно обновить один из фрагментов в соответствии с результатом трансляции.О чем я упоминаю ниже:
Мой класс фрагментов:
public class ConservationFragment extends Fragment
{
FragmentType fragmentType;
private RecyclerView recyclerView;
private RecyclerView.Adapter mAdapter;
private RecyclerView.LayoutManager layoutManager;
public ConservationFragment()
{
}
@Override
public void onCreate(@Nullable Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
}
@Override
public void onResume()
{
super.onResume();
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
{
View rootView = inflater.inflate(R.layout.fragment_conservation, container, false);
try
{
this.fragmentType = (FragmentType) getArguments().getSerializable("fragmenttype");
ArrayList<Message> messageRecords = getMessagesFromDB(fragmentType);
recyclerView = (RecyclerView) rootView.findViewById(R.id.conservationsRecyclerView);
recyclerView.setHasFixedSize(true);
layoutManager = new LinearLayoutManager(getActivity());
recyclerView.setLayoutManager(layoutManager);
mAdapter = new ConversationAdapter(messageRecords, getActivity());
recyclerView.setAdapter(mAdapter);
recyclerView.setItemAnimator(new DefaultItemAnimator());
return rootView;
} catch (Exception e)
{
Log.d("RUNTIME ERROR", "Please check ConversationFragment's onCreateView method.");
}
return inflater.inflate(R.layout.fragment_conservation, container, false);
}
public ArrayList<Message> getMessagesFromDB(FragmentType fragmentType)
{
..
}
}
Как я начинаю фрагменты в основной деятельности;
public class MainActivity extends AppCompatActivity
{
//This is our tablayout
private TabLayout tabLayout;
//This is our viewPager
private ViewPager viewPager;
ConservationFragment personalFragment;
ConservationFragment blockedFragment;
SmsReceiver receiver;
ViewPagerAdapter adapter;
String[] tabTitle = {"PERSONAL", "BLOCKED"}; //
int[] unreadCount = {1, 2};
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
receiver = new SmsReceiver();
IntentFilter intentFilter = new IntentFilter();
intentFilter.addAction("android.provider.Telephony.SMS_RECEIVED");
registerReceiver(receiver, intentFilter);
}
@Override
protected void onResume()
{
super.onResume();
//Initializing viewPager
viewPager = (ViewPager) findViewById(R.id.viewpager);
setupViewPager(viewPager);
//Initializing the tablayout
tabLayout = (TabLayout) findViewById(R.id.tablayout);
tabLayout.setupWithViewPager(viewPager);
try
{
setupTabIcons();
} catch (Exception e)
{
e.printStackTrace();
}
viewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener()
{
@Override
public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels)
{
}
@Override
public void onPageSelected(int position)
{
viewPager.setCurrentItem(position, false);
}
@Override
public void onPageScrollStateChanged(int state)
{
}
});
}
private void setupViewPager(ViewPager viewPager)
{
Bundle personalCriterial= new Bundle();
personalCriterial.putSerializable("fragmenttype",FragmentType.PERSONAL);
Bundle blockedCriterial= new Bundle();
blockedCriterial.putSerializable("fragmenttype",FragmentType.BLOCKED);
adapter = new ViewPagerAdapter(getSupportFragmentManager());
personalFragment = new ConservationFragment();
personalFragment.setArguments(personalCriterial);
blockedFragment = new ConservationFragment();
blockedFragment.setArguments(blockedCriterial);
adapter.addFragment(personalFragment, tabTitle[0]);
adapter.addFragment(blockedFragment, tabTitle[1]);
viewPager.setAdapter(adapter);
}
private void setupTabIcons()
{
for (int i = 0; i < tabTitle.length; i++)
{
tabLayout.getTabAt(i).setCustomView(prepareTabView(i));
}
}
private View prepareTabView(int pos)
{
View view = getLayoutInflater().inflate(R.layout.custom_tab, null);
TextView tv_title = (TextView) view.findViewById(R.id.tv_title);
TextView tv_count = (TextView) view.findViewById(R.id.tv_count);
tv_title.setText(tabTitle[pos]);
return view;
}
public void receiveMessage(Intent intent)
{
//here I want to refresh one of my fragments. such as personalFragment..
}
}
Критическая точка здесь;вкладка не должна переходить на первую вкладку. (Обновление должно быть в том же фрагменте). Я знаю, что код такой длинный, но я должен показать, как я инициирую объекты, чтобы привести решения.Спасибо.