2017-01-30 50 views
0

我有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共享文本視圖的內容。你能幫我麼?謝謝。

+0

你是什麼意思份額 – yanivtwin

+0

分享文本使用份額的意圖等是指: 意向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); –

+0

嘗試在每個文本視圖上設置一個'LongClickListener',並在偵聽器的'onClick'方法中獲取值。 –

回答

0

根據我的理解,你想獲取textview的內容 - 那麼,你可以使用textview.getText()方法從TextView iteself得到文本。

獲取片段從您的代碼的參考: -

textView2.setText(String.valueOf(str2[position])); 
textView3.setText(String.valueOf(str3[position]));  
textView3.setOnLongClickListener(new View.OnLongClickListener() { 
      @Override 
      public boolean onLongClick(View view) { 
       String str = textView3.getText().toString(); // use str to share 
       return false; 
      } 
     }); 
+0

我知道,但是,我想要的是從ViewPager的文本視圖中獲取內容。在我的情況下,我的ViewPager有3個TextView(3個文本視圖顯示在屏幕上)。如果我點擊第一個TextView,然後從該TextView中獲取文本,而不是從其他viewPager中獲取文本。 –

+0

非常感謝您的先生。它解決了我的問題。這很容易解決,但我並不那麼聰明。 –

+0

如果它解決了您的問題,您可以將其標記爲已接受的答案。 – Wizard