Skip to content

Instantly share code, notes, and snippets.

@tieubao
Forked from slightfoot/MainActivity.java
Created May 5, 2014 05:54
Show Gist options
  • Save tieubao/3a1956896f49d730d230 to your computer and use it in GitHub Desktop.
Save tieubao/3a1956896f49d730d230 to your computer and use it in GitHub Desktop.
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();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment