Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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();
}
}
}
@elincognito

This comment has been minimized.

Copy link

elincognito commented Feb 20, 2014

This gist helped me alot specially because of the getUserVisibleHint

@atul2504

This comment has been minimized.

Copy link

atul2504 commented Jul 22, 2014

Thanks alot mate. Helped me too. :)

@wenLiangcan

This comment has been minimized.

Copy link

wenLiangcan commented Jun 2, 2015

Thanks!

@den3000

This comment has been minimized.

Copy link

den3000 commented Apr 15, 2016

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

@KeitelDOG

This comment has been minimized.

Copy link

KeitelDOG commented Nov 3, 2016

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

This comment has been minimized.

Copy link

nminhanh-git commented Mar 20, 2018

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
You can’t perform that action at this time.