2017-02-15 46 views
0

我想在ViewPager中添加2個或多個TextView。但我不知道這個問題。這太複雜了。如何在ViewPager中添加多個TextView或其他視圖

我的第二個TextView的資源來自tipler數組。

這裏是我的Fragment.java

public class YaklasanlarFragment extends Fragment { 

CircularProgressBar c3; 

ViewPager viewPager = null; 
String bilgiler[] = {"34 YNS 54","54 E 2554"}; 
String tipler[] = {"Muayene Tarihi","Sigorta Tarihi"}; 

public YaklasanlarFragment(){} 

@Override 
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { 
    View view= inflater.inflate(R.layout.activity_yaklasanlar_fragment, container, false); 
    viewPager = (ViewPager) view.findViewById(R.id.viewPager); 
    viewPager.setAdapter(new MyAdapter()); 
    return view; 
} 

class MyAdapter extends PagerAdapter{ 

    @Override 
    public int getCount() 
    { 
     return bilgiler.length; 
    } 

    @Override 
    public Object instantiateItem(View container, int position) 
    { 
     TextView plaka = new TextView(getActivity()); 
     plaka.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, 
       LayoutParams.WRAP_CONTENT)); 
     plaka.setText(bilgiler[position]); 
     ((ViewPager) container).addView(plaka, 0); 
     return plaka; 


    } 
} 
+0

在instantiateItem膨脹XML視圖和XML中有兩個或多達你想要的TextView – user3040153

+0

是的,但我用數組和programmaticaly定義textViews。有時我需要10個textview。我不能定義10 xml。 –

+0

但你的instantiateItem會調用更多的時間作爲更大的尺寸 – user3040153

回答

2

請參閱下方說明我們如何才能增加TextView動態到ViewPager代碼。以下是Adapter類,您需要將Adapter設置爲ViewPager

import android.content.Context; 
import android.support.v4.view.PagerAdapter; 
import android.view.LayoutInflater; 
import android.view.View; 
import android.view.ViewGroup; 
import android.widget.TextView; 

public class CustomPagerAdapter extends PagerAdapter { 

    String bilgiler[] = {"34 YNS 54","54 E 2554"}; 
    String tipler[] = {"Muayene Tarihi","Sigorta Tarihi"}; 
    private Context mContext; 

    public CustomPagerAdapter(Context context) { 
     mContext = context; 
    } 

    @Override 
    public Object instantiateItem(ViewGroup collection, int position) { 
     //if Length of both array's (bilgiler and tipler) Always remains same then we can do code like below. 
    String modelObject = bilgiler[position] + tipler[position]; 
    LayoutInflater inflater = LayoutInflater.from(mContext); 
    LinearLayout linearLayout = new LinearLayout(mContext); 
    linearLayout.setOrientation(LinearLayout.HORIZONTAL); 
    TextView textView = new TextView(linearLayout.getContext()); 
    textView.setText(modelObject); 
    linearLayout.addView(textView); 
    Button button = new Button(linearLayout.getContext()); 
    linearLayout.addView(button); 
    collection.addView(linearLayout); 
    return linearLayout; 
    } 

    @Override 
    public void destroyItem(ViewGroup collection, int position, Object view) { 
     collection.removeView((View) view); 
    } 

    @Override 
    public int getCount() { 
     return bilgiler.length; 
    } 

    @Override 
    public boolean isViewFromObject(View view, Object object) { 
     return view == object; 
    } 
} 
+0

我應該爲我的問題定義兩個適配器嗎?我的意思是一個用於bilgiler陣列的適配器,一個用於tipler陣列的適配器? –

+0

你想如何顯示這兩個數組一個接一個或混合(像bilgiler中的一個vlaue和來自tipler的下一個值) –

+0

不,我想在1個viewPager中設置2個textview。當我滑動。我想看bilgiler [0]和tipler [0] –

相關問題