gpt4 book ai didi

java - 如何获取 FragmentPagerAdapter 中的当前位置?

转载 作者:塔克拉玛干 更新时间:2023-11-02 08:23:38 26 4
gpt4 key购买 nike

我有一个扩展 FragmentPagerAdapter 并利用 ICS 样式 actionBar 的适配器。此操作栏包含从当前选定页面获取输入的操作。

具体来说,我在操作栏中有一个屏幕截图图标,它获取当前页面的 url 并显示来自该 url 的任何屏幕截图。但是,我不知道如何检索当前选定的页面。

我怎样才能实现类似

的东西
    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));
}

}

这是我所说的当前选定页面的意思。

enter image description here

最佳答案

我相信您正在寻找的是 onPageChangedListener()。此方法属于 TitlePageIndicator。像...

indicator.setOnPageChangedListener(new OnPageChangedListener() {
// Implement unimplemented methods...
});

关于java - 如何获取 FragmentPagerAdapter 中的当前位置?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9731859/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com