使用 PagerAdapter 的可点击链接
Clickable links with PagerAdapter
我用 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;
}
}
Inner class 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);
}
所以,我需要在每个页面中跟踪链接。
我找到了解决方案,而且比我想象的要容易。
使用 PagerAdapter 使链接可点击的关键是将侦听器置于页面的 Fragment class.
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);
}
我用 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;
}
}
Inner class 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);
}
所以,我需要在每个页面中跟踪链接。
我找到了解决方案,而且比我想象的要容易。 使用 PagerAdapter 使链接可点击的关键是将侦听器置于页面的 Fragment class.
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);
}