Java 如何在FragmentPagerAdapter中获得当前位置?
我有一个扩展FragmentPagerAdapter的适配器,它利用了ICS风格的actionBar。此actionbar具有从当前选定页面获取输入的操作 具体来说,我在actionbar中有一个截图图标,它获取当前页面的url并显示该url中的任何截图。但是,我不知道如何检索当前选择的页面 我如何实现类似于Java 如何在FragmentPagerAdapter中获得当前位置?,java,android,android-viewpager,Java,Android,Android Viewpager,我有一个扩展FragmentPagerAdapter的适配器,它利用了ICS风格的actionBar。此actionbar具有从当前选定页面获取输入的操作 具体来说,我在actionbar中有一个截图图标,它获取当前页面的url并显示该url中的任何截图。但是,我不知道如何检索当前选择的页面 我如何实现类似于 public int getActivePage() { return position; 我仍在处理viewpager实现,因此我的代码仍然严重依赖于示例,所以请
public int getActivePage() {
return position;
我仍在处理viewpager实现,因此我的代码仍然严重依赖于示例,所以请忽略这些混乱:p
下面标记了问题区域
public class ListFragmentViewPagerActivity extends FragmentActivity {
ArrayList<String> URLS;
ArrayList<String> TITLES;
BroadcastReceiver receiver;
String threadTitle = null;
public static String threadUrl = null;
String type = null;
String threadAuthor = null;
String ident = null;
boolean isFav = false;
String author = null;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.thread_view);
Bundle extras = getIntent().getExtras();
threadTitle = extras.getString("title");
threadUrl = extras.getString("url");
type = extras.getString("type");
ident = extras.getString("ident");
author = extras.getString("author");
try {
URLS = extras.getStringArrayList("urls");
TITLES = extras.getStringArrayList("titles");
} catch (Exception e) {
URLS = null;
}
final FDBAdapter db = new FDBAdapter(this);
db.open();
Cursor c = db.getAllFavs();
if (c.getCount()>0) {
if (c.getString(2).equals(threadTitle)) {
isFav = true;
}
try {
while (c.moveToNext()) {
Log.d("FAVS", c.getString(2));
if (c.getString(2).equals(threadTitle)) {
isFav = true;
}
}
} catch (Exception ep) {
ep.printStackTrace();
}
}
c.close();
db.close();
ViewPager pager = (ViewPager) findViewById(android.R.id.list);
pager.setAdapter(new ExamplePagerAdapter(getSupportFragmentManager()));
TitlePageIndicator indicator = (TitlePageIndicator)findViewById( R.id.indicator );
indicator.setViewPager(pager);
receiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
String action = intent.getAction();
if (DownloadManager.ACTION_DOWNLOAD_COMPLETE.equals(action)) {
long downloadId = intent.getLongExtra(
DownloadManager.EXTRA_DOWNLOAD_ID, 0);
String ns = Context.NOTIFICATION_SERVICE;
NotificationManager mNotificationManager = (NotificationManager) context.getSystemService(ns);
int icon = R.drawable.ic_launcher; // icon from resources
CharSequence tickerText = "Download ready!"; // ticker-text
long when = System.currentTimeMillis(); // notification time
CharSequence contentTitle = "OMG"; // expanded message title
CharSequence contentText = "Your download is finished!"; // expanded message text
Intent notificationIntent = new Intent(context, ExampleListFragment.class);
PendingIntent contentIntent = PendingIntent.getActivity(context, 0, notificationIntent, 0);
Notification notification = new Notification(icon, tickerText, when);
notification.setLatestEventInfo(context, contentTitle, contentText, contentIntent);
notification.defaults |= Notification.DEFAULT_VIBRATE;
notification.flags |= Notification.FLAG_AUTO_CANCEL;
final int HELLO_ID = 1;
mNotificationManager.notify(HELLO_ID, notification);
}
}
};
registerReceiver(receiver, new IntentFilter(
DownloadManager.ACTION_DOWNLOAD_COMPLETE));
}
public class ExamplePagerAdapter extends FragmentPagerAdapter implements TitleProvider{
public ExamplePagerAdapter(FragmentManager fm) {
super(fm);
}
@Override
public int getCount() {
return URLS.size();
}
@Override
public Fragment getItem(int position) {
Fragment fragment = new ExampleListFragment();
// set arguments here, if required
Bundle args = new Bundle();
args.putString("url", URLS.get(position));
fragment.setArguments(args);
return fragment;
}
@Override
public String getTitle(int pos) {
return TITLES.get(pos);
}
}
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater menuinflate = new MenuInflater(this);
menuinflate.inflate(R.menu.thread_menu, menu);
if (type.equals("xda")) {
menu.removeItem(R.id.ss_view);
}
if (isFav) {
menu.getItem(2).setIcon(R.drawable.fav_ab);
}
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case android.R.id.home:
Intent intent = new Intent(this, MainActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(intent);
break;
case R.id.ss_view:
Intent ssi = new Intent(this, SSActivity.class);
ssi.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
Bundle b = new Bundle();
//I need to get the position of the currently active page here so that I can retrieve the //corresponding values for the title and url.
b.putString("title", threadTitle);
b.putString("url", threadUrl);
ssi.putExtras(b);
startActivity(ssi);
break;
case R.id.restart:
break;
case R.id.fav_ab:
threadUrl = new String(threadUrl.replaceAll("http://", ""));
FDBAdapter fdb = new FDBAdapter(this);
fdb.open();
if (isFav) {
Cursor c = fdb.getAllUrls();
while (c.moveToNext()) {
String id = c.getString(c.getColumnIndex("_id"));
int rowId = Integer.parseInt(id);
if(c.getString(c.getColumnIndex("url")).equals(threadUrl)) {
if (fdb.deleteUrl(rowId)) {
Log.d("THREAD", "SUCCESS");
} else {
Log.d("THREAD", "FAILED");
}
}
}
c.close();
item.setIcon(R.drawable.fav_ab_off);
isFav = false;
} else {
fdb.insertFav(threadUrl, threadTitle, ident, type, author, "thread");
item.setIcon(R.drawable.fav_ab);
isFav = true;
}
fdb.close();
default:
return super.onOptionsItemSelected(item);
}
return false;
}
@Override
public void onStop() {
super.onStop();
unregisterReceiver(receiver);
}
@Override
public void onStart() {
super.onStart();
registerReceiver(receiver, new IntentFilter(
DownloadManager.ACTION_DOWNLOAD_COMPLETE));
}
}
公共类ListFragmentViewPageActivity扩展了FragmentActivity{
ArrayList URL;
数组列表标题;
广播接收机;
字符串threadTitle=null;
公共静态字符串threadUrl=null;
字符串类型=null;
字符串threadAuthor=null;
字符串ident=null;
布尔值isFav=false;
字符串author=null;
创建时的公共void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.thread_视图);
Bundle extras=getIntent().getExtras();
threadTitle=extras.getString(“title”);
threadUrl=extras.getString(“url”);
type=extras.getString(“type”);
ident=extras.getString(“ident”);
author=extras.getString(“author”);
试一试{
URL=extras.getStringArrayList(“URL”);
TITLES=额外的getStringArrayList(“TITLES”);
}捕获(例外e){
url=null;
}
最终FDBAdapter db=新FDBAdapter(本);
db.open();
游标c=db.getAllFavs();
如果(c.getCount()>0){
if(c.getString(2).equals(threadTitle)){
isFav=真;
}
试一试{
while(c.moveToNext()){
Log.d(“FAVS”,c.getString(2));
if(c.getString(2).equals(threadTitle)){
isFav=真;
}
}
}捕获(异常ep){
ep.printStackTrace();
}
}
c、 close();
db.close();
ViewPager pager=(ViewPager)findViewById(android.R.id.list);
setAdapter(新示例PageRadapter(getSupportFragmentManager());
TitlePageIndicator=(TitlePageIndicator)findViewById(R.id.indicator);
指示器。设置视图寻呼机(寻呼机);
接收器=新的广播接收器(){
@凌驾
公共void onReceive(上下文、意图){
String action=intent.getAction();
if(DownloadManager.ACTION\u DOWNLOAD\u COMPLETE.equals(ACTION)){
long downloadId=intent.getLongExtra(
DownloadManager.EXTRA_DOWNLOAD_ID,0);
字符串ns=Context.NOTIFICATION\u服务;
NotificationManager mNotificationManager=(NotificationManager)context.getSystemService(ns);
int icon=R.drawable.ic_launcher;//来自资源的图标
CharSequence tickerText=“下载准备就绪!”;//代码文本
long when=System.currentTimeMillis();//通知时间
CharSequence contentTitle=“OMG”//扩展消息标题
CharSequence contentText=“下载完成!”;//扩展消息文本
Intent notificationIntent=新的意图(上下文,ExampleListFragment.class);
PendingEvent contentIntent=PendingEvent.getActivity(上下文,0,notificationIntent,0);
通知通知=新通知(图标,tickerText,何时);
setLateStevenInfo(上下文、contentTitle、contentText、contentIntent);
notification.defaults |=notification.DEFAULT_振动;
notification.flags |=notification.FLAG_AUTO_CANCEL;
最终整数HELLO_ID=1;
mNotificationManager.notify(HELLO\u ID,notification);
}
}
};
registerReceiver(接收器,新意向过滤器(
DownloadManager.ACTION_DOWNLOAD_COMPLETE);
}
公共类ExamplePagerAdapter扩展FragmentPagerAdapter实现TitleProvider{
公共示例页面编辑器(碎片管理器fm){
超级(fm);
}
@凌驾
public int getCount(){
返回url.size();
}
@凌驾
公共片段getItem(int位置){
Fragment Fragment=新的ExampleListFragment();
//如果需要,在此处设置参数
Bundle args=新Bundle();
args.putString(“url”,url.get(position));
fragment.setArguments(args);
返回片段;
}
@凌驾
公共字符串getTitle(int-pos){
返回标题。获取(pos);
}
}
公共布尔onCreateOptions菜单(菜单){
MenuInflater menuinflate=新的MenuInflater(本);
菜单充气(右菜单螺纹菜单,菜单);
if(type.equals(“xda”)){
menu.removietem(R.id.ss_视图);
}
if(isFav){
menu.getItem(2)、setIcon(R.drawable.fav_ab);
}
返回true;
}
@凌驾
公共布尔值onOptionsItemSelected(菜单项项){
开关(item.getItemId()){
案例android.R.id.home:
意向意向=新意向(此,MainActivity.class);
intent.addFlags(intent.FLAG\u ACTIVITY\u CLEAR\u TOP);
星触觉(意向);
打破
案例R.id.ss_视图:
意向ssi=新意向(此为SSActivity.class);
ssi.addFlags(意图、标志、活动、清除、顶部);
Bundle b=新Bundle();
//我需要在这里获取当前活动页面的位置,以便检索//标题和url的相应值。
b、 putString(“标题”,threadTitle);
b、 putString(“url”,threadUrl);
ssi.putExtras(b);
星触觉(ssi);
打破
案例R.id.restart:
打破
案例R.id.fav_ab:
indicator.setOnPageChangedListener(new OnPageChangedListener() {
// Implement unimplemented methods...
});
ViewPager pager = (ViewPager) findViewById(android.R.id.list);
pager.getCurrentItem()
pager.currentItem