public class DownloadFragment extends SimpleFragment{
private static final String TAG = "DownloadFragment";
private TabLayout tabLayout;
private ViewPager mVpTaskList;
private List<String> tableName;
private PagerAdapter pagerAdapter;
private TextView unclaimedTextView;
public static DownloadFragment newInstance() {
Bundle args = new Bundle();
DownloadFragment fragment = new DownloadFragment();
fragment.setArguments(args);
return fragment;
}
@Override
protected int getLayoutId() {
return R.layout.task_fragment_download;
}
@Override
protected void initView() {
EventBusUtils.register(this);
tabLayout = mView.findViewById(R.id.tabLayout);
mVpTaskList = mView.findViewById(R.id.vp_task_list);
unclaimedTextView=mView.findViewById(R.id.unclaimedTextView);
final EditText etSearch = mView.findViewById(R.id.et_search_id);
etSearch.addTextChangedListener(new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
}
@Override
public void afterTextChanged(Editable s) {
EventBusUtils.postSticky(new EventMessage(EventCodeConstant.DOWN_LOAD_SEARCH,etSearch.getText().toString()));
}
});
initPager();
}
private void initPager() {
List<Fragment> fragments = new ArrayList<>();
FragmentManager fm = getChildFragmentManager();
DownloadPagerFragment toBeDownloaded = DownloadPagerFragment.newInstance(DownloadPagerFragment.TO_BE_DOWNLOADED);
DownloadPagerFragment downloaded = DownloadPagerFragment.newInstance(DownloadPagerFragment.DOWNLOADED);
// HaveDownloadedFragment downloaded=HaveDownloadedFragment.newInstance();
fragments.add(toBeDownloaded);
fragments.add(downloaded);
tableName = new ArrayList<>();
tableName.add("待复核");
tableName.add("已复核");
pagerAdapter = new PagerAdapter(fm,fragments,tableName);
mVpTaskList.setAdapter(pagerAdapter);
tabLayout.setupWithViewPager(mVpTaskList);
}
@Override
protected void initData() {
}
@Override
protected void initListener() {
}
/**
* EventBus事件
*
* @param eventMessage
*/
@Subscribe(threadMode = ThreadMode.MAIN)
public void event(EventMessage eventMessage) {
if (eventMessage.getCode()== EventCodeConstant.UPADTE_DOWN_NUMBER){
List<Integer> contList = (List<Integer>) eventMessage.getData();
if(contList != null){
for (int i = 0; i < contList.size(); i++) {
int count = contList.get(i);
String title = tableName.get(i).substring(0, 3);
pagerAdapter.setPageTitle(i, title + "(" + count + ")");
}
pagerAdapter.notifyDataSetChanged();
}
}
}
@Override
public void onDestroy() {
EventBusUtils.unregister(this);
super.onDestroy();
}
}
public class PagerAdapter extends FragmentPagerAdapter {
private List<Fragment> mFragments;
private List<String> mTitles;
public PagerAdapter(FragmentManager fm, List<Fragment> fragments, List<String> mTitles) {
super(fm);
this.mFragments = fragments;
this.mTitles = mTitles;
}
@Override
public Fragment getItem(int i) {
return mFragments.get(i);
}
@Override
public int getCount() {
return mFragments == null ? 0 : mFragments.size();
}
@Nullable
@Override
public CharSequence getPageTitle(int position) {
return mTitles.get(position);
}
/**
* 动态设置标题
* @param position
* @param title
*/
public void setPageTitle(int position, String title)
{
if(position >= 0 && position < mTitles.size())
{
mTitles.set(position, title);
notifyDataSetChanged();
}
}
}
|