我正在尝试在Android中创建一个ViewPager,但Android Studio告诉我'myAdapter无法解析'。这是我的代码:
public class MainActivity extends FragmentActivity {
GridView gridview_id;
FloatingActionButton fab_main;
ViewPager pager;
objectAdapter oa;
MainActivity activity;
Fragment fragment;
@Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.menu, menu);
final MenuItem searchItem = menu.findItem(R.id.action_search);
SearchView searchView = (SearchView)
MenuItemCompat.getActionView(searchItem);
searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
@Override
public boolean onQueryTextSubmit(String query) {
oa.filter(query);
return false;
}
@Override
public boolean onQueryTextChange(String newText) {
return false;
}
});
return true;
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
fab_main = (FloatingActionButton) findViewById(R.id.fab_main);
gridview_id = (GridView) findViewById(R.id.gridview_id);
pager = (ViewPager) findViewById(R.id.pager);
fab_main.show();
AdView mAdView = (AdView) findViewById(R.id.adView);
//AdRequest adRequest = new AdRequest.Builder()
//.addTestDevice("YOUR_DEVICE_HASH")
//.build();
//do not make visible while testing!!
mAdView.loadAd(adRequest);
android.support.v4.app.FragmentManager fragmentManager = getSupportFragmentManager();
gridview_id.setAdapter(new objectAdapter(this));
pager.setAdapter(new myAdapter(fragmentManager));
class myAdapter extends FragmentPagerAdapter {
public myAdapter(android.support.v4.app.FragmentManager fm) {
super(fm);
}
@Override
public android.support.v4.app.Fragment getItem(int position) {
if (position == 0) {
activity = new MainActivity();
}
if (position == 1) {
fragment = new DailyIntakeActivity();
fab_main.hide();
}
return null;
}
@Override
public int getCount() {
return 2;
}
}
setDefaultKeyMode(DEFAULT_KEYS_SEARCH_LOCAL);
fab_main.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
fab_main.hide();
onSearchRequested();
}
});
gridview_id.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
fab_main.hide();
Intent intent = new Intent(view.getContext(), SecondActivity.class);
objectAdapter.ViewHolder holder = (objectAdapter.ViewHolder) view.getTag();
SingleItem temp = (SingleItem) holder.myItem.getTag();
intent.putExtra("image", temp.image);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
ActivityOptionsCompat options = ActivityOptions.makeSceneTransitionAnimation(MainActivity.this, temp.image, getString(R.string.image_transition));
startActivity(intent, options.toBundle());
} else {
startActivity(intent);
}
}
});
}
}