我对Android开发很新,我的问题可能听起来很傻,但它确实让我很难解决问题。关于这个非常特殊的主题有很多问题和解决方案,但它们似乎都不适合我。所以我的问题很简单,我在使用ViewPager的Action Bar选项卡中加载了几周,当我滑动或选择页面/选项卡时,我视图中的数据不会刷新。我试过notifyDataSetChanged(),我无法得到getItemPosition。
这是我的viewPager代码
public class EventAgenda extends AppCompatActivity implements ActionBar.TabListener {
SectionsPagerAdapter mSectionsPagerAdapter;
ViewPager mViewPager;
private ActionBar actionBar;
Integer eventID, dateCount;
String sessionDate, selectedDate;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_event_agenda);
Bundle extras = getIntent().getExtras();
if (extras != null) {
eventID = extras.getInt("EVENT_ID");
EventDetailController eventDetails = new EventDetailController(this);
final List<EventDetails> events = eventDetails.getEventDays(eventID);
dateCount = events.size();
mSectionsPagerAdapter = new SectionsPagerAdapter(getSupportFragmentManager());
actionBar = getSupportActionBar();
// Set up the ViewPager with the sections adapter.
mViewPager = (ViewPager) findViewById(R.id.pager);
mViewPager.setAdapter(mSectionsPagerAdapter);
actionBar.setHomeButtonEnabled(false);
actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
// set background for action bar tab
actionBar.setStackedBackgroundDrawable(new ColorDrawable(Color.parseColor("#FFDE00")));
// Adding Tabs
for (int i = 0; i < dateCount; i++) {
String theDate = String.valueOf(events.get(i).getEventDate());
SimpleDateFormat dateFormat = new SimpleDateFormat("E MMM dd HH:mm:ss Z yyyy");
Date myDate = null;
try {
myDate = dateFormat.parse(theDate);
} catch (ParseException e) {
e.printStackTrace();
}
SimpleDateFormat timeFormat = new SimpleDateFormat("EEE, dd-MM-yyyy");
sessionDate = timeFormat.format(myDate);
actionBar.addTab(actionBar.newTab().setText(sessionDate)
.setTabListener(EventAgenda.this));
}
mViewPager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
@Override
public void onPageSelected(int position) {
// on changing the page
// make respected tab selected
actionBar.setSelectedNavigationItem(position);
}
@Override
public void onPageScrolled(int arg0, float arg1, int arg2) {
}
@Override
public void onPageScrollStateChanged(int arg0) {
}
public int getItemPosition(Object object) { //This never gets called and does not allow me override as well
Log.v("getItem Posistion ", object.toString());
return 0;
}
});
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_event_agenda, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();
if(id==R.id.action_refresh) { //this code on Refresh does refresh the data in the View but i want it to work on swipe and tab selection
int pos=mViewPager.getCurrentItem();//to get the sposition of the current page
mViewPager.setAdapter(mSectionsPagerAdapter);
mSectionsPagerAdapter = new SectionsPagerAdapter(getSupportFragmentManager());
mViewPager.setAdapter(mSectionsPagerAdapter);
mViewPager.setCurrentItem(pos);//to get it to the right position
}
return super.onOptionsItemSelected(item);
}
@Override
public void onTabSelected(ActionBar.Tab tab, FragmentTransaction ft) {
// on tab selected
// show respected fragment view
mViewPager.setCurrentItem(tab.getPosition());
selectedDate = String.valueOf(tab.getText());
CommonConstants.SESSION_DATE = selectedDate;
mSectionsPagerAdapter.notifyDataSetChanged();
Log.v("EM Title : ", String.valueOf(tab.getText()));
}
@Override
public void onTabUnselected(ActionBar.Tab tab, FragmentTransaction ft) {
}
@Override
public void onTabReselected(ActionBar.Tab tab, FragmentTransaction ft) {
}
public class SectionsPagerAdapter extends FragmentStatePagerAdapter {
public SectionsPagerAdapter(FragmentManager fm) {
super(fm);
}
@Override
public Fragment getItem(int position) {
// getItem is called to instantiate the fragment for the given page.
// Return a PlaceholderFragment (defined as a static inner class below).
CommonConstants.SESSION_DATE = selectedDate;
Fragment fragment = null;
fragment = new EventDayFragment(); //This is the only Fragment i have i am passing the selectedDate as Bundle
//refreshContents();
Bundle args = new Bundle();
args.putString("sessionDate", selectedDate);
args.putInt("eventID", eventID);
fragment.setArguments(args);
return fragment;
}
@Override
public int getCount() {
return dateCount;
}
@Override
public CharSequence getPageTitle(int position) {
Locale l = Locale.getDefault();
switch (position) {
case 0:
return getString(R.string.title_section1).toUpperCase(l);
case 1:
return getString(R.string.title_section2).toUpperCase(l);
case 2:
return getString(R.string.title_section3).toUpperCase(l);
}
return null;
}
}
//Here i tried putting the refresh code into following function and tried calling it from getItem but it crashes
private void refreshContents(){
int pos=mViewPager.getCurrentItem();//to get the sposition of the current page
mViewPager.setAdapter(mSectionsPagerAdapter);
mSectionsPagerAdapter = new SectionsPagerAdapter(getSupportFragmentManager());
mViewPager.setAdapter(mSectionsPagerAdapter);
mViewPager.setCurrentItem(pos);//to get it to the right position
}
}
这是我唯一的片段代码
public class EventDayFragment extends Fragment{
ListView lvSession;
String dateString;
Integer eventID;
public static EventDayFragment newInstance() {
EventDayFragment fragment = new EventDayFragment();
return fragment;
}
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.activity_event_agenda_main, container, false);
lvSession = (ListView) rootView.findViewById(R.id.lv_sessions);
dateString= getArguments().getString("sessionDate");
eventID = getArguments().getInt("eventID");
Log.v("EM Date String", dateString);
SimpleDateFormat dateFormat = new SimpleDateFormat("EEE, dd-MM-yyyy");
Date convertedDate = new Date();
try {
convertedDate = dateFormat.parse(dateString);
} catch (ParseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
//ListView lvEvents = (ListView) rootView.findViewById(R.id.lv_events);
EventDetailController eventDetails = new EventDetailController(getContext());
List<EventDetails> sessionList = eventDetails.getEventDetailsDate(convertedDate, eventID);
CommonConstants.ACTIVITY_NAME ="SESSIONS";
final MultiColumnListViewAdapter multiColRowAdapter = new MultiColumnListViewAdapter(getContext()) {
@Override
public View.OnClickListener getListener(final Integer id) {
return new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent intent = new Intent(getActivity(), SessionDetails.class);
intent.putExtra("EVENT_ID", id);
startActivity(intent);
}
};
}
};
multiColRowAdapter.getObjects().addAll(sessionList);
lvSession.setAdapter(multiColRowAdapter);
return rootView;
}
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
return super.onOptionsItemSelected(item);
}
}
请帮我解决一下...... 提前谢谢。
答案 0 :(得分:0)
我通过将会话日期存储在数组中,然后根据getItem中的制表符位置传递相应的索引值来解决问题。