2017-02-15 7 views
0

ViewPagerに2つ以上のTextViewを追加したいとします。しかし、私はその問題を知らない。それはとても複雑です。ViewPagerで多くのTextViewやその他のビューを追加する方法

私の2番目のTextViewのリソースは、のチップリーダーからです。

は、ここに私の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

XMLビュー&XMLでを膨らまあなたがええのTextView – user3040153

+0

をしたいが、私は配列を使用してprogrammaticaly textViewsを定義する2つまたはできるだけ多くを持っています。ときどき私は10のtextviewが必要です。そして私はそのために10 xmlを定義することはできません。 –

+0

しかしinstantiateItemはbiglierのサイズと同じくらい多くの時間を呼び出します – user3040153

答えて

2

我々は動的ViewPagerTextViewを追加する方法を説明したコードは、以下を参照してくださいです。以下はAdapterクラスです。AdapterViewPagerに設定する必要があります。 instantiateItemで

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

問題に2つのアダプタを定義する必要がありますか?私はbilgiler配列のための1つのアダプタを意味し、tipler配列のための1つのアダプタを意味しますか? –

+0

この2つのデータをどのように表示するかArray 1別のものまたはミックスの後(bilgilerの値とtiplerの次の値のように) –

+0

いいえ、1つのviewPagerに2つのtextviewを設定します。私はスワイプします。私はbilgiler [0]とtipler [0]を参照してください –

関連する問題