Skip to content

Instantly share code, notes, and snippets.

@slightfoot
Last active March 7, 2019 11:40
Show Gist options
  • Save slightfoot/5514457 to your computer and use it in GitHub Desktop.
Save slightfoot/5514457 to your computer and use it in GitHub Desktop.
ContextMenu's in Fragments within a ViewPager
import java.util.ArrayList;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.app.ListFragment;
import android.support.v4.view.ViewPager;
import android.view.ContextMenu;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.view.ContextMenu.ContextMenuInfo;
import android.widget.AdapterView.AdapterContextMenuInfo;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;
public class MainActivity extends FragmentActivity
{
private ViewPager mViewPager;
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
mViewPager = new ViewPager(this);
mViewPager.setId(R.id.view_pager);
mViewPager.setAdapter(new TestAdapter(getSupportFragmentManager()));
setContentView(mViewPager);
}
@Override
public boolean onCreateOptionsMenu(Menu menu)
{
getMenuInflater().inflate(R.menu.main, menu);
return super.onCreateOptionsMenu(menu) | true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item)
{
if(item.getItemId() == R.id.action_search){
startActivity(SearchActivity.createIntent(this));
}
return super.onOptionsItemSelected(item);
}
public static class TestAdapter extends FragmentPagerAdapter
{
private static final String[] mTitles = { "Alpha", "Beta", "Gamma" };
public TestAdapter(FragmentManager fm)
{
super(fm);
}
@Override
public Fragment getItem(int position)
{
return TestListFragment.newInstance(mTitles[position]);
}
@Override
public CharSequence getPageTitle(int position)
{
return mTitles[position];
}
@Override
public int getCount()
{
return mTitles.length;
}
}
public static class TestListFragment extends ListFragment
{
private static final String ARG_TITLE = "argTitle";
private String mTitle;
private ListView mListView;
public static TestListFragment newInstance(String title)
{
TestListFragment frag = new TestListFragment();
Bundle args = new Bundle();
args.putString(ARG_TITLE, title);
frag.setArguments(args);
return frag;
}
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
mTitle = getArguments().getString(ARG_TITLE);
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
{
ArrayList<String> testData = new ArrayList<String>(100);
for(int i = 0; i < 100; i++){
testData.add(mTitle + " Item " + (i + 1));
}
mListView = new ListView(getActivity());
mListView.setId(android.R.id.list);
mListView.setAdapter(new ArrayAdapter<String>(getActivity(), android.R.layout.simple_list_item_1, testData));
return mListView;
}
@Override
public void onResume()
{
super.onResume();
registerForContextMenu(mListView);
}
@Override
public void onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo menuInfo)
{
super.onCreateContextMenu(menu, v, menuInfo);
menu.setHeaderTitle(((TextView)((AdapterContextMenuInfo)menuInfo).targetView).getText());
menu.add("Item 1");
menu.add("Item 2");
menu.add("Item 3");
}
@Override
public boolean onContextItemSelected(MenuItem item)
{
if(getUserVisibleHint()){
Toast.makeText(getActivity(),
mTitle + " {" + item.getTitle() + ", " +
((TextView)((AdapterContextMenuInfo)item.getMenuInfo()).targetView).getText() +
'}', Toast.LENGTH_SHORT).show();
return true;
}
return super.onContextItemSelected(item);
}
@Override
public void onPause()
{
unregisterForContextMenu(mListView);
super.onPause();
}
}
}
@den3000
Copy link

den3000 commented Apr 15, 2016

Thanks! You've saved me a lot of time due to your getUserVisibleHint usage !

@KeitelDOG
Copy link

A THOUSAND THANKS!!! Before I found this approach, my contextual menu was being kept on the listview of the previous page (fragment). I tried to unregister onPause and register on setUserVisibleHint method, but I didn't think about onResume and getUserVisibleHint in onContextItemSelected. I even think about giving up for PopMenu approach.

You must be a genius, you saved my week!

@nminhanh-git
Copy link

thank you so much, the getUserVisibleHint() save a tons of my times!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment