我正在使用主要活动来显示不同的片段。我需要根据广播结果刷新片段之一。我在下面提到的是什么。
我的片段类:
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..
}
}
关键点在这里;选项卡视图不应该是第一个选项卡。(更新应该在同一个片段中) 我知道代码是如此之长,但我必须展示如何启动对象以引导解决方案。感谢。
答案 0 :(得分:0)
如果您不需要更新整个片段,但只传递一些数据并刷新文本视图或列表,则可以始终在片段中定义公共方法并从活动中调用它。
片段中的示例方法:
public void updateWithData(String exampleString) {
myTextView.setText(exampleString);
}
活动中的使用示例:
public void receiveMessage(Intent intent) {
myFragment.updateWithDate("Example string message");
}
这样,您不需要重新创建整个片段,而只能更新相关的UI元素。但是,请记住,您需要确保在调用此更新方法时创建并显示片段,否则您将获得未定义的行为。
PS:创建类似Updatable接口的东西并在片段中实现它可能是个好主意,因此您可以在应用程序的其他位置重用此更新逻辑。但这不是必需的,对于小范围,公共方法应该足够了。