我在FragmentPagerAdapter中显示了一些文字。 本文包含一些我需要点击的链接。 有一个使用OnTouchEvent和View的解决方案,但是如何获得坐标呢? 或者PagerAdapter有更好的解决方案吗?
编辑:代码是:
private void setupPager() {
if (pagerAdapter == null) {
pagerAdapter = new TextFragmentPagerAdapter(getSupportFragmentManager());
}
pager.setAdapter(pagerAdapter);
findViewById(R.id.progressBar1).setVisibility(View.GONE);
pager.setCurrentItem(current_page);
pager.setVisibility(View.VISIBLE);
}
private class TextFragmentPagerAdapter extends FragmentStatePagerAdapter {
public TextFragmentPagerAdapter(FragmentManager fragmentManager) {
super(fragmentManager);
}
@Override
public Fragment getItem(int position) {
spannable_page = null;
Fragment fragment = new Fragment();
new PageForm(position, line_array, spannable_main).start();
while (true) {
if (spannable_page != null) {
fragment = PageFragment.newInstance(spannable_page.get());
break;
}
}
return fragment;
}
@Override
public int getCount() {
return total_pages;
}
}
内部类PageForm按如下方式构建每个页面:
...
Object[] spans_test = spannable.getSpans(start_range, end_range - 1, Object.class);
CharSequence sequence_test = spannable.subSequence(start_range, end_range);
SpannableStringBuilder builder_test = new SpannableStringBuilder();
builder_test.insert(0, sequence_test);
int sp_start, sp_end, flags, sp_s_fin, sp_e_fin;
for (int i = 0; i < spans_test.length; i++) {
sp_start = spannable.getSpanStart(spans_test[i]);
sp_end = spannable.getSpanEnd(spans_test[i]);
flags = spannable.getSpanFlags(spans_test[i]);
sp_s_fin = sp_start - start_range;
sp_e_fin = sp_end - start_range;
if (sp_s_fin < 0) sp_s_fin = 0;
if (sp_e_fin > builder_test.length() - 1) sp_e_fin = builder_test.length() - 1;
builder_test.setSpan(spans_test[i], sp_s_fin, sp_e_fin, flags);
}
synchronized (this) {
spannable_page = new WeakReference<Spannable>(builder_test);
}
所以,不知怎的,我需要在每个页面内跟踪链接。
答案 0 :(得分:0)
我找到了解决方案,它比我想象的要容易。 使用PagerAdapter可以链接的链接的关键是将监听器放到页面的Fragment类中。
public class PageFragment extends Fragment {
...
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
...
URLSpan[] links = spannable.getSpans(0, spannable.length(), URLSpan.class);
for (URLSpan span : links) {
textLinks(spannable, span);
}
textView.setText(spannable);
textView.setMovementMethod(LinkMovementMethod.getInstance());
return view;
}
private void textLinks(Spannable text, final URLSpan span) {
int start = text.getSpanStart(span);
int end = text.getSpanEnd(span);
int flags = text.getSpanFlags(span);
ClickableSpan clickable = new ClickableSpan() {
@Override
public void onClick(View view) {
System.out.println("LINK CHECK: " + span.getURL());
}
};
text.setSpan(clickable, start, end, flags);
text.removeSpan(span);
}