我在片段中创建了一个滑动选项卡视图。我在FragmentActivity中测试了这个,没有任何问题。我测试了包含不同片段类和片段的页面,这些页面都来自同一个类......再次没有问题 然后,我将FragmentActivity更改为Fragment。我使用代表每个页面的不同Fragment类再次测试并且没有问题。最后,我用相同类的片段重复测试并遇到问题 目前有3页代表三个星期。第一周出现了。我刷到第2周,什么都没有。我刷到第3周,再也没有了。当我滑回到第1周时,唯一可查看的页面最终成为中间页面。这是片段生命周期的问题吗?我不确定如何调试这种特殊情况。我正在寻找可能的原因,所以我可以开始解决这个问题。
public class WeeklyScrollTab extends Fragment {
private ViewPager my_view_pager;
private View my_layout;
private int my_current_page;
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
my_layout = inflater.inflate(R.layout.weekly_scroll_tabs, container, false);
return my_layout;
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
/** Getting the arguments to the Bundle object */
//Bundle data = getArguments();
/** Getting integer data of the key current_page from the bundle */
//my_current_page = data.getInt("week_val", 0);
}
@Override
public void onActivityCreated(Bundle savedInstanceState) {
my_view_pager = (ViewPager)my_layout.findViewById(R.id.week_list_pager);
FragmentManager manager = getActivity().getSupportFragmentManager();
//FragmentManager manager = getChildFragmentManager();
my_view_pager.setAdapter(new WeeklyScrollAdapter(manager));
super.onActivityCreated(savedInstanceState);
}
public int getCurrentPage(){
return my_current_page;
}
public class WeeklyScrollAdapter extends FragmentStatePagerAdapter {
public WeeklyScrollAdapter(FragmentManager the_manager){
super(the_manager);
}
@Override
public Fragment getItem(int the_position) {
Fragment fragment1 = null;
if(the_position == 0){
fragment1 = new MyNflPlayerList();
Bundle args1 = new Bundle();
args1.putInt("week_val"+the_position, the_position);
fragment1.setArguments(args1);
}
if(the_position == 1){
fragment1 = new MyNflPlayerList();
Bundle args2 = new Bundle();
args2.putInt("week_val"+the_position, the_position);
fragment1.setArguments(args2);
}
if(the_position == 2){
fragment1 = new MyNflPlayerList();
Bundle args3 = new Bundle();
args3.putInt("week_val"+the_position, the_position);
fragment1.setArguments(args3);
}
return fragment1;
}
/**
*
* @return number of total pages
*/
@Override
public int getCount() {
return 3;
}
/**
* gets title for tab
*/
@Override
public CharSequence getPageTitle(int position) {
if(position ==0){
return "Week 1";
}
if(position == 1){
return "Week 2";
}
if(position == 2){
return "Week 3";
}
return super.getPageTitle(position);
}
public class NflPlayerAdapter extends BaseAdapter {
private ArrayList<NflPlayerModel> my_list;
private Context my_context;
public NflPlayerAdapter(Context c){
my_context = c;
my_list = new ArrayList<NflPlayerModel>();
my_list.add(new NflPlayerModel("T.","Brady","NE","QB",56));
my_list.add(new NflPlayerModel("A.","Peterson","MN","RB",24));
my_list.add(new NflPlayerModel("D.","Bryant","DAL","WR",18));
my_list.add(new NflPlayerModel("M.","Gattica","TB","K",9));
}
@Override
public int getCount() {
return my_list.size();
}
@Override
public NflPlayerModel getItem(int i) {
return my_list.get(i);
}
@Override
public long getItemId(int i) {
return i;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
//getting the data and injecting it into the each row view
View row = convertView;
NflPlayerHolder holder = null;
if(row == null){
LayoutInflater inflater = (LayoutInflater)my_context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
row = inflater.inflate(R.layout.my_nfl_player_row,parent, false);
holder = new NflPlayerHolder(row);
row.setTag(holder);
}
else{
holder = (NflPlayerHolder) row.getTag();
}
NflPlayerModel temp = my_list.get(position);
holder.getFname().setText(temp.getFname());
holder.getLname().setText(temp.getLname());
holder.getTeam().setText(temp.getTeam());
holder.getPosition().setText(temp.getPosition());
holder.getPoints().setText(Integer.toString(temp.getPoints()));
return row;
}
class NflPlayerHolder {
/**
* TextView object in my_league_row xml.
*/
private TextView my_fname;
private TextView my_lname;
private TextView my_team;
private TextView my_position;
private TextView my_points;
/**
* Constructor. Converts the xml txt_league_name to Java object.
* @param v
*/
public NflPlayerHolder(View v){
my_fname = (TextView)v.findViewById(R.id.player_fname);
my_lname = (TextView)v.findViewById(R.id.player_lname);
my_team = (TextView)v.findViewById(R.id.player_team);
my_position = (TextView)v.findViewById(R.id.player_position);
my_points = (TextView)v.findViewById(R.id.player_points);
}
public TextView getFname(){
return my_fname;
}
public TextView getLname(){
return my_lname;
}
public TextView getTeam(){
return my_team;
}
public TextView getPosition(){
return my_position;
}
public TextView getPoints(){
return my_points;
}
public class MyNflPlayerList extends Fragment implements AdapterView.OnItemClickListener{
private View my_layout;
private ListView my_list_view;
private static HomeActivityCommunicator my_communicator;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
my_layout = inflater.inflate(R.layout.my_nfl_player_list, container, false);
return my_layout;
}
@Override
public void onActivityCreated(Bundle savedInstanceState) {
my_list_view = (ListView)getActivity().findViewById(R.id.nfl_player_list_view);
NflPlayerAdapter adapter = new NflPlayerAdapter(getActivity());
my_list_view.setAdapter(adapter);
my_list_view.setOnItemClickListener(this);
super.onActivityCreated(savedInstanceState);
}
public void setHomeActivityCommunicator(HomeActivityCommunicator the_communicator){
my_communicator = the_communicator;
}
@Override
public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
Log.e("mike", "the player index is " + i);
my_communicator.onMyNflPlayerListSelection(i);
}