-1

I want to copy the text in the current TextView (within a fragment) in a ViewPager, but it copies the previous, or the text in the next TextView. I searched and found that it is because of the ViewPager's normal behaviour to create three pages: The previous, current and the next page. But I could not find how to determine the current one easily?

I use ViewPager with FragmentStatePagerAdapter.

Edit: I have searched for this for two days but after asking my question I found nearly the same problem: This is a better asked and explained question then mine. Atleast I learned how to ask and explain.

Wrong fragment in ViewPager receives onContextItemSelected call

but "getUserVisibleHint()" answer doesnot work for me?

...Fragment.java

    onCreateView
     .........
    tvTextView = (TextView) v.findViewById(R.id.TextView);
    tvTextView.setTextSize( TypedValue.COMPLEX_UNIT_PX, boyut);
    registerForContextMenu(TextView);
    .....

@Override
public void onCreateContextMenu(ContextMenu menu, View view, ContextMenu.ContextMenuInfo menuInfo) {
    super.onCreateContextMenu(menu, view, menuInfo);
    if (view.getId()== R.id.tvTextView) {
        MenuInflater inflater = getActivity().getMenuInflater();
        inflater.inflate(R.menu.menu_context, menu);

        menu.setHeaderTitle("Copy...");
    }
}

@Override
public boolean onContextItemSelected(MenuItem item) {

    if( getUserVisibleHint() == false )
    {
        return false;
    }

    AdapterView.AdapterContextMenuInfo info = (AdapterView.AdapterContextMenuInfo) item.getMenuInfo();
    switch(item.getItemId()) {
        case R.id.copy:
            copy();

            return true;
        default:

            return true;
    }
}

In ..FragmentContainer.java

....

mViewPager = (ViewPager) findViewById(R.id.viewPagerLayout);      

FragmentManager fm = getSupportFragmentManager();
mViewPager.setAdapter(new FragmentStatePagerAdapter(fm) {

    @Override
    public int getCount() {
        return myArray.size();
    }

    @Override
    public Fragment getItem(int pos) {

        return ...Fragment.newInstance(name, pos, checkedItems, fontsize);
    }
});
akrep55tr
  • 1
  • 4

1 Answers1

0

After a lot of searching and I dont know how many trial and error, I found a solution for my problem.

Using "getUserVisibleHint()" or "getGroupId()" methods, I couldnot get the current textview's text. I used "text = tvTextView.getText().toString();" in onContextItemSelected or in onCreateView methods, but it didnot work.

@Override
public boolean onContextItemSelected(MenuItem item) {

    if( ! getUserVisibleHint() )
    {
        return false;
    }

// this is not working
text = tvTextView.getText().toString();

Then I tried making setText and getText methods for fragment and getting the text in onContextItemSelected method using getText method. It works great!

First I added two methods to Fragment:

public String getText() {
    return mText;
}

public void setText(String text) {
    mText = text;
}

In onCreateView I set the text

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup parent,
                         Bundle savedInstanceState) {
    View v = inflater.inflate(R.layout.fragment, parent, false);

    tvTextView = (TextView) v.findViewById(R.id.tvTextView);
    registerForContextMenu(tvTextView);

    String tvText =  tvTextView.getText().toString();

// I set the text
    setText(tvText);

Then in onContextItemSelected I get it using getText

@Override
public boolean onContextItemSelected(MenuItem item) {

    if( ! getUserVisibleHint() )
    {
        return false;
    }

// I get the text
    mCopyText =  getText();

Both "getUserVisibleHint()" or "getGroupId()" methods works great in this way. I hope this help other people.

akrep55tr
  • 1
  • 4