Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Not able to Update fragments text while swaping cards

I am working on a card swipe and card flip functionality and I am using ViewPager and fragments.

My problem is I am not able to update TextView inside fragments as I swipe the card from left to right or right to left but when I flip the card it update the UI. I tried everything which is available over Internet but none of the soltuion is working for me.

I am following this link https://github.com/jamesmccann/android-view-pager-cards

Here is my code

public class CardContainerFragment extends Fragment {

private boolean cardFlipped = false;
static TextView textview;

public CardContainerFragment() {
    setHasOptionsMenu(true);
}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {

    View rootView = inflater.inflate(R.layout.fragment_card_container, container, false);
    LinearLayout ll = (LinearLayout)rootView.findViewById(R.id.layout);
    rootView.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            flipCard();
        }
    });

    getChildFragmentManager()
            .beginTransaction()
            .add(R.id.container, new CardFrontFragment())
            .commit();

    Message msg = handler.obtainMessage();
    msg.arg1 = 1;
    handler.sendMessage(msg);

    return rootView;
}

final Handler handler = new Handler(){
    @Override
    public void handleMessage(Message msg) {
        int page = CardActivity.mViewPager.getCurrentItem();
        int page_index = page+1;
        String current_page = page_index + " of " + card_activity.deck_map.size();
        CardActivity.tv.setText(current_page);


       super.handleMessage(msg);
    }
};


public void flipCard() {
    Fragment newFragment = null;
    Message msg = handler.obtainMessage();
    msg.arg1 = 1;
    handler.sendMessage(msg);
    if (cardFlipped) {
        newFragment = new CardFrontFragment();
    } else {
        newFragment = new CardBackFragment();
    }

    getChildFragmentManager()
            .beginTransaction()
            .setCustomAnimations(
                    R.animator.card_flip_right_in, R.animator.card_flip_right_out,
                    R.animator.card_flip_left_in, R.animator.card_flip_left_out)
            .replace(R.id.container, newFragment)
            .commit();

    cardFlipped = !cardFlipped;
}

public static class CardFrontFragment extends Fragment {

    public CardFrontFragment() { }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {

        View rootView = inflater.inflate(R.layout.fragment_card, container, false);
        textview = (TextView)rootView.findViewById(R.id.card_front);
        String card_front_string = card_activity.arraylst.get(CardActivity.mViewPager.getCurrentItem());
        Log.e("current Item",CardActivity.mViewPager.getCurrentItem()+"");
        String complete_text = card_front_string +" \n \n + \n Tap now to flip this card.";
        textview.setText(complete_text);
        return rootView;
    }
}

public static class CardBackFragment extends Fragment {

    public CardBackFragment() { }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View rootView = inflater.inflate(R.layout.fragment_card_back, container, false);
        TextView textview = (TextView)rootView.findViewById(R.id.card_back);
        textview.setMovementMethod(new ScrollingMovementMethod());
        String card_front_string = card_activity.arraylst.get(CardActivity.mViewPager.getCurrentItem());
        String deck_data = card_activity.deck_map.get(card_front_string);
        textview.setText(deck_data);
        return rootView;
    }
}

Here is my adapter code

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    try {
        setContentView(R.layout.card_example);
        tv = (TextView)findViewById(R.id.tv_card_number);
        tv1 = (TextView)findViewById(R.id.tv_card_index);
        FragmentManager m = getFragmentManager();
        CardPagerAdapter adapter = new CardPagerAdapter(m);
        index = getIntent().getStringExtra("index");
        card_activity.cardCounter = Integer.parseInt(index);
        int count = card_activity.cardCounter;
        int final_count = count+1;
        String current_page = final_count+" of "+card_activity.deck_map.size();
        //CardActivity.tv.setText(current_page);
        mViewPager = (ViewPager) findViewById(R.id.view_pager);
       // mViewPager.setAllowedSwipeDirection(CustomViewPager.SwipeDirection.all);
        mViewPager.setAdapter(adapter);
        mViewPager.setCurrentItem(card_activity.cardCounter);
        //mViewPager.setOffscreenPageLimit(1);

        //mViewPager.setOnPageChangeListener(new pagechangelistener())


        //Log.e("current Item",CardActivity.mViewPager.getCurrentItem()+"");

    }catch(Exception e){
        e.printStackTrace();
    }
}

public class CardPagerAdapter extends android.support.v13.app.FragmentStatePagerAdapter {

    public CardPagerAdapter(FragmentManager fm) {
        super(fm);
    }

    @Override
    public Fragment getItem(int i) {
        mViewPager.setOffscreenPageLimit(0);
        int page = mViewPager.getCurrentItem();
        int page_index = page+1;
        String current_page = " of " + card_activity.deck_map.size();
        tv.setText(current_page);
        tv1.setText(String.valueOf(page_index));
        CardContainerFragment cardContainerFragment = new CardContainerFragment();
        cardContainerFragment.current_index_front = page;
        cardContainerFragment.current_index_back = page;

        String card_front_string = card_activity.arraylst.get(page);
        CardContainerFragment.complete_text_front = card_front_string +" \n \n + \n Tap now to flip this card.";
        Bundle b = new Bundle();
        b.putInt("index",page);
        CardContainerFragment.complete_text_back = card_activity.deck_map.get(card_front_string);
        cardContainerFragment.setArguments(b);
        return cardContainerFragment;
    }

    @Override
    public int getCount() {
        int len = card_activity.deck_map.size();
        return len;
    }
    @Override
    public int getItemPosition(Object object) {
        return POSITION_NONE;
    }
}

Please let me know what I am doing wrong here.

Thanks in advance

like image 534
Doctor Who Avatar asked Oct 29 '22 14:10

Doctor Who


1 Answers

Finally I am able to do this with some efforts.

After searching, I get to know that getItems (The method in CardPagerAdapter) is called two times because Android creates one extra fragments for smooth transition and I was sending or getting wrong position of fragments from getItems.

By letting the android to create one extra fragment and by sending correct position I am able to solve this issue.

Here is my updated code

public class CardActivity extends android.support.v4.app.FragmentActivity {

public TextView tv;
public static String index=null;
public static ViewPager mViewPager;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    try {
        setContentView(R.layout.card_example);
        tv = (TextView)findViewById(R.id.tv_card_number);
        FragmentManager m = getFragmentManager();
        CardPagerAdapter adapter = new CardPagerAdapter(m);
        index = getIntent().getStringExtra("index");
        card_activity.cardCounter = Integer.parseInt(index);
        int count = card_activity.cardCounter;
        int final_count = count+1;
        String current_page = final_count+" of "+card_activity.deck_map.size();
        mViewPager = (ViewPager) findViewById(R.id.view_pager);
        mViewPager.setOffscreenPageLimit(0);
        mViewPager.setAdapter(adapter);
        mViewPager.setCurrentItem(card_activity.cardCounter);
        String current_page_temp =final_count+" of " + card_activity.deck_map.size();
        tv.setText(current_page_temp);

        mViewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(int position) {
                Log.e("aaa",position+"");
                    String current_page = position+1 + " of " + card_activity.deck_map.size();
                    tv.setText(current_page);
            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });
    }catch(Exception e){
        e.printStackTrace();
    }
}

    public class CardPagerAdapter extends android.support.v13.app.FragmentStatePagerAdapter {

    public CardPagerAdapter(FragmentManager fm) {
        super(fm);
    }

    @Override
    public Fragment getItem(int i) {

        CardContainerFragment cardContainerFragment = new CardContainerFragment();
        Bundle b = new Bundle();
        b.putInt("index",i);
        cardContainerFragment.setArguments(b);
        return cardContainerFragment;
    }

    @Override
    public int getCount() {
        int len = card_activity.deck_map.size();
        return len;
    }
    @Override
    public int getItemPosition(Object object) {
        return POSITION_NONE;
    }
}
  }

==================================================================

public class CardContainerFragment extends Fragment {

private boolean cardFlipped = false;
int current_index=0;
static int pos = 0;
public CardContainerFragment() {
    /*setHasOptionsMenu(true);*/
}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {

    View rootView = inflater.inflate(R.layout.fragment_card_container, container, false);
    LinearLayout ll = (LinearLayout)rootView.findViewById(R.id.layout);
    rootView.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            flipCard();
        }
    });
    pos = getArguments().getInt("index");
    getChildFragmentManager()
            .beginTransaction()
            .add(R.id.container, new CardFrontFragment().newInstance(pos))
            .commit();
    card_activity.tempCounter = card_activity.tempCounter+1;
    return rootView;
}


public void flipCard() {
    Fragment newFragment = null;
    CardFrontFragment cardFrontFragment = null;
    CardBackFragment cardBackFragment = null;

    if (cardFlipped) {
        newFragment = CardFrontFragment.newInstance(CardActivity.mViewPager.getCurrentItem());
    } else {
        newFragment = cardBackFragment.newInstance(CardActivity.mViewPager.getCurrentItem());
    }

    getChildFragmentManager()
            .beginTransaction()
            .setCustomAnimations(
                    R.animator.card_flip_right_in, R.animator.card_flip_right_out,
                    R.animator.card_flip_left_in, R.animator.card_flip_left_out)
            .replace(R.id.container, newFragment)
            .commit();

    cardFlipped = !cardFlipped;
}
public static class CardBackFragment extends Fragment {

    public CardBackFragment() { }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View rootView_back = inflater.inflate(R.layout.fragment_card_back, container, false);
        TextView textview_back = (TextView)rootView_back.findViewById(R.id.card_back);
        textview_back.setMovementMethod(new ScrollingMovementMethod());
        String text = getArguments().getString("back_text");
        textview_back.setText(text);

        return rootView_back;
    }
    static CardBackFragment newInstance(int position) {
        CardBackFragment cardBackFragment = new CardBackFragment();
        Bundle args = new Bundle();
        String card_front_string = card_activity.arraylst.get(position);
        String text = card_activity.deck_map.get(card_front_string);
        args.putString("back_text", text);
        cardBackFragment.setArguments(args);
        return cardBackFragment;
    }
}

public static class CardFrontFragment extends Fragment {

    public CardFrontFragment() { }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View rootView_front = null;
        rootView_front = inflater.inflate(R.layout.fragment_card, container, false);
        TextView textview_front = (TextView)rootView_front.findViewById(R.id.card_front);
        String text = getArguments().getString("front_text");
        textview_front.setText(text);

        return rootView_front;
    }

    static CardFrontFragment newInstance(int position) {
        CardFrontFragment cardFrontFragment = new CardFrontFragment();
        Bundle args = new Bundle();
        String card_front_string = card_activity.arraylst.get(position);
        String text = card_front_string +" \n \n + \n Tap now to flip this card.";
        args.putString("front_text", text);
        cardFrontFragment.setArguments(args);
        return cardFrontFragment;
    }
}


 }
like image 160
Doctor Who Avatar answered Nov 09 '22 15:11

Doctor Who