我有ViewPager,它需要3個字符串數組,並將內容數組設置爲在ViewPager中膨脹的3個文本視圖。我可以將文本設置爲TextView。但是,當我長按它時,我想要獲取並分享特定TextView的內容。如何從ViewPager共享TextView的內容?
這裏的ViewPager適配器
customSwipeAdapter.java
package com.regio.developers.upasana;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.support.v4.view.PagerAdapter;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;
public class customSwipeAdapter extends PagerAdapter {
private String[] str1,str2,str3;
private Context ctx;
private LayoutInflater layoutInflater;
public customSwipeAdapter(Context ctx,String[] str1,String[] str2,String[] str3){
this.ctx = ctx;
this.str1 = str1;
this.str2 = str2;
this.str3 = str3;
}
@Override
public int getCount() {
return str1.length;
}
@Override
public boolean isViewFromObject(View view, Object object) {
return view == ((RelativeLayout)object);
}
@Override
public Object instantiateItem(ViewGroup container, final int position) {
SharedPreferences preferences = ctx.getSharedPreferences("UPASANA",Context.MODE_PRIVATE);
SharedPreferences.Editor editor = preferences.edit();
editor.putString("PAGENUM",String.valueOf(position));
editor.apply();
//Log.d("Shared Pref adapter", "returned: " + position);
layoutInflater = (LayoutInflater) ctx.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View item_view = layoutInflater.inflate(R.layout.swipe_layout,container,false);
TextView textView1 = (TextView) item_view.findViewById(R.id.positiontextview1);
TextView textView2 = (TextView) item_view.findViewById(R.id.positiontextview2);
TextView textView3 = (TextView) item_view.findViewById(R.id.positiontextview3);
TextView pagenum = (TextView) item_view.findViewById(R.id.pagenumtextview);
textView1.setText(String.valueOf(str1[position]));
textView2.setText(String.valueOf(str2[position]));
textView3.setText(String.valueOf(str3[position]));
pagenum.setText("" + (position+ 1) + "/" + str1.length);
container.addView(item_view);
return item_view;
}
@Override
public void destroyItem(ViewGroup container, int position, Object object) {
container.removeView((RelativeLayout)object);
}
}
這裏ViewPager適配器需要三串STR1,STR2,STR3。它總共給了69頁。我沒有使用片段,而是使用滑動佈局來擴大視圖。所以,我想僅從可見ViewPager共享文本視圖的內容。你能幫我麼?謝謝。
你是什麼意思份額 – yanivtwin
分享文本使用份額的意圖等是指: 意向sendIntent =新意圖(); sendIntent.setAction(Intent.ACTION_SEND); sendIntent.putExtra(Intent.EXTRA_TEXT, 「嗨查看我的應用程序:https://play.google.com/store/apps/details?id=com.google.android.apps.plus」); sendIntent.setType(「text/plain」); startActivity(sendIntent); –
嘗試在每個文本視圖上設置一個'LongClickListener',並在偵聽器的'onClick'方法中獲取值。 –