范围
我正在为我的大学节申请,其中显示了 4 个特定固定日期的 Activity 。
我有一个 main_activity,它有一个显示每个 fragment 的线性布局。现在我有一个 fragment “ScheduleFragment”。
ScheduleFragment 由一个 ViewPager 和一个 FragmentStatePagerAdapter 组成,可以在 4 个日期之间切换。
ViewPager 有 4 个页面代表 4 个日期,这 4 个页面来自同一 fragment 的 4 个引用,即“ScheduleListSubFragment”
从这里用户选择一个事件并可以查看包含在“EventDescriptionFragment”中的事件描述
问题
流动顺利。用户遇到 ScheduleFragment,然后切换到适当的日期并选择一个事件。
现在,当他从 EventDescriptionFragment 返回到 ScheduleFragment 时(稍后我将说明我是如何执行此操作的),ViewPager 显示空白 View 。当我们滑动到下一个日期时,下一个日期的事件来了,我们滑动回上一个日期,上一个日期的事件又回来了。
我调试了我的应用程序并知道当我们回到 ScheduleFragment 时,除非我滚动 viewpager,否则 viewpager 不会为 ScheduleListSubFragment 创建新的 fragment 实例。
所以,我猜 ViewPager 能够带来用户正在查看的特定引用(ScheduleListSubFragment)。
我如何回到 ScheduleFragment ?
我在 main_activity 中创建了一个堆栈。当用户选择一个事件时,我将 ScheduleFragment 的当前引用插入堆栈。当按下后退时,我弹出该引用并将其放在屏幕上。
代码
* ScheduleFragment*
导入 com.Nit_kkr.updates.app.adapters.ScheduleFragmentAdapter;
导入 com.viewpagerindicator.PageIndicator;
导入 com.viewpagerindicator.TitlePageIndicator;
导入 com.viewpagerindicator.TitlePageIndicator.IndicatorStyle;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import android.widget.Toast;
public class ScheduleFragment extends Fragment{
ScheduleFragmentAdapter mAdapter;
ViewPager mPager;
PageIndicator mIndicator;
TitlePageIndicator indicator;
@Override
public void onViewStateRestored(Bundle savedInstanceState) {
// Toast.makeText(getActivity(), " onViewStateRestored()", Toast.LENGTH_SHORT).show();
super.onViewStateRestored(savedInstanceState);
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mAdapter = new ScheduleFragmentAdapter(getActivity().getSupportFragmentManager());
mAdapter.notifyDataSetChanged();
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.scheduled_list, null);
prepareSlidingPager(v);
return v;
}
private void prepareSlidingPager(View v)
{
mPager = (ViewPager)v.findViewById(R.id.pager);
mPager.setOffscreenPageLimit(4);
mPager.setAdapter(mAdapter);
mIndicator = (TitlePageIndicator)v.findViewById(R.id.indicator);
indicator = (TitlePageIndicator) mIndicator;
mIndicator.setViewPager(mPager);
final float density = getResources().getDisplayMetrics().density;
indicator.setBackgroundColor(0x18AF0000);
indicator.setFooterColor(0xFFAA2222);
//indicator.setFooterColor(0xFFAA33);
indicator.setFooterLineHeight(2 * density); //1dp
indicator.setFooterIndicatorHeight(3 * density); //3dp
indicator.setFooterIndicatorStyle(IndicatorStyle.Underline);
indicator.setTextColor(0xAA000000);
indicator.setSelectedColor(0xFF000000);
indicator.setSelectedBold(true);
}
public static ScheduleFragment newInstance()
{
ScheduleFragment frag = new ScheduleFragment();
return frag;
}
}
ScheduleListSubFragment:
public class ScheduleListSubFragment extends Fragment implements OnItemClickListener{
long startDate;
long endDate;
private ListView eventlist;
private listadapter adapter;
private List events;
private MyDB db;
@Override
public void onAttach(Activity activity) {
Toast.makeText(getActivity(), " onAttatch()------------------sublist schedule", Toast.LENGTH_SHORT).show();
super.onAttach(activity);
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
db=new MyDB(this.getActivity());
Toast.makeText(getActivity(), " onCreate()------------------sublist schedule", Toast.LENGTH_SHORT).show();
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
//Toast.makeText(getActivity(), " onCreateView()------------------sublist schedule", Toast.LENGTH_SHORT).show();
View v = inflater.inflate(R.layout.eventslist_subfragment_layout, null);
eventlist=(ListView)v.findViewById(R.id.Event_list);
eventlist.setOnItemClickListener(this);
fetcheventdata(getArguments().getLong("start_time"), getArguments().getLong("end_time"));
return v;
}
public static ScheduleListSubFragment newInstance(long starttime, long endtime)
{
ScheduleListSubFragment frag = new ScheduleListSubFragment();
Bundle bundle = new Bundle();
bundle.putLong("start_time", starttime);
bundle.putLong("end_time", endtime);
frag.setArguments(bundle);
return frag;
}
private void fetcheventdata(long starttime, long endtime)
{
db.open();
events=db.getEvents_bycategory_bytime(null, starttime, endtime);
db.close();
if(events!=null)
{
adapter=new listadapter(getActivity(), android.R.layout.simple_expandable_list_item_1, events);
eventlist.setAdapter(adapter);
adapter.notifyDataSetChanged();
}
else
Toast.makeText(getActivity(), "No Events Yet", Toast.LENGTH_LONG);
//Toast.makeText(getActivity(), events.size(), Toast.LENGTH_LONG).show();
}
private class listadapter extends ArrayAdapter<Event>
{
private List items;
public listadapter(Context context, int textViewResourceId,
List objects){
super(context, textViewResourceId, objects);
items=new ArrayList();
items=objects;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View v=convertView;
if(v==null)
{
LayoutInflater vi=(LayoutInflater)getActivity().getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
v=vi.inflate(R.layout.upcominglist, null);
}
Event event=(Event)items.get(position);
if(event!=null)
{
TextView name=(TextView)v.findViewById(R.id.Name);
TextView venue=(TextView)v.findViewById(R.id.Venue);
TextView time=(TextView)v.findViewById(R.id.time);
name.setTypeface(Utils.getHeroFace(getActivity().getAssets()));
venue.setTypeface(Utils.getHeroFace(getActivity().getAssets()));
name.setTextColor(getResources().getColor(R.color.black));
venue.setTextColor(getResources().getColor(R.color.darkgrey));
time.setTextColor(getResources().getColor(R.color.black));
//ImageView favouriteicon=(ImageView)v.findViewById(R.id.fav_status_image);
// ImageView remindericon=(ImageView)v.findViewById(R.id.reminder_status_image);
name.setText(event.getName());
time.setText(Html.fromHtml("<b>"+event.getEvent_time()+"</b><br></br>"+event.getEvent_date()));
venue.setText(event.getVenue());
}
return v;
}
}
@Override
public void onItemClick(AdapterView<?> arg0, View arg1, int pos, long arg3) {
eventnotifierapp app=(eventnotifierapp)getActivity().getApplication();
app.crntEvent=(Event) events.get(pos);
//startActivity(new Intent(this.getActivity(), EventDescription.class));
EventDescriptionFragment frag = EventDescriptionFragment.NewInstance();
((MainActivity)getActivity()).OpenNextFragment(frag);
}
}
ScheduleFragmentAdapter:
public class ScheduleFragmentAdapter extends FragmentStatePagerAdapter{
String cat_name;
protected static final String[] CONTENT = new String[] { "Thursday, Feb 20",
"Friday, Feb 21", "Saturday, Feb 22", "Sunday, Feb 23", };
private int mCount = CONTENT.length;
public ScheduleFragmentAdapter(FragmentManager fm) {
super(fm);
}
@Override
public Fragment getItem(int position) {
if(position == 0)
return ScheduleListSubFragment.newInstance(day1start, day1end);
if(position == 1)
return ScheduleListSubFragment.newInstance(day2start, day2end);
if(position == 2)
return ScheduleListSubFragment.newInstance(day3start, day3end);
else
return ScheduleListSubFragment.newInstance(day4start, day4end);
}
@Override
public int getCount() {
return mCount;
}
@Override
public CharSequence getPageTitle(int position) {
return CONTENT[position % CONTENT.length];
}
}
研究
我在stackOverFlow上做了一些挖掘,发现了一些相关的查询,这些查询并不完全相同,但无法得出解决方案
任何类型的帮助,非常感谢。
最佳答案
嗨,让您的代码运行,帮助我解决您的问题的是:
1. 使用 getChildFragmentManager()
初始化您的适配器,例如:fragadapter = new myfragmentAdapter(getChildFragmentManager());
这会给你运行时错误吗??
这是因为在按下按钮的应用程序没有你的 fragment 上下文(根据我的意见)所以一定要在后堆栈中添加你的 fragment ,如:
public void addpagerfragment(View v)
{
pagerFragment frag = pagerFragment.NewInstance();
crrntfrag = frag;
FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
ft.replace(R.id.container, frag).addToBackStack(null);
ft.commit();
}