0

I created a swipe tab view inside a fragment. I tested this in a FragmentActivity with no issues. I tested with pages that consisted of different fragment classes and fragments all from the same class... again no issues.
Then, I changed the FragmentActivity to a Fragment. I tested again with different Fragment classes representing each page and had no problem. Finally, i repeated the test with fragments of the same class and ran into issues.
There are 3 pages right now representing three weeks. At first week 1 shows up. I swipe to week 2 and there is nothing. I swipe to week 3 and nothing again. When I swipe back to week 1, the only viewable page ends up being the middle page. Is this an issue with fragment life cycles? I'm not sure how to debug this special circumstance. I'm looking for possible causes so I can start solving this issue.

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);
}
Blo
  • 11,903
  • 5
  • 45
  • 99
  • Could you please edit some relevant parts of your code into your question? Without seeing what you tried so far it will be difficult to pinpoint the problem. – Xaver Kapeller Apr 30 '14 at 23:52
  • I added my code, let me know if you require more information – user1366589 May 01 '14 at 00:58
  • After looking through your code my first intuition would be d that you need to use the `ChildFragmentManager` instead of the other one. You are dealing with `Fragments` inside other `Fragments` after all. – Xaver Kapeller May 01 '14 at 01:07
  • That was one of my first checks. I have the getChildFragmentManager commented out. This didn't make any difference. The pages are swiping but two pages are blank. I was wondering if it was an issue with the xml being from the same source in combination with the inner fragment thing. I'm honestly at a loss. – user1366589 May 01 '14 at 01:23
  • 1
    I solved the issue. I did static array of R.layout.ids and it worked when each listview had its own xml. I created the listview programmatically and it works now...Thanks for your time Xaver Kapeller. Maybe this solution will help others. – user1366589 May 01 '14 at 02:48
  • How You Solved that que Finally ? – Meghal Agrawal Jul 12 '15 at 00:42

0 Answers0