2016-04-27 11 views
0

私はlistViewを持っています。ユーザーが5つの画像をクリックすると、別の項目をクリックすると5つの異なる画像がViewPagerで表示されます。画像配列を変更する方法はわかりませんlistViewの各項目で変更されますか?それを行うための任意のアイデア?複数の画像配列でViewPagerを使用するにはどうすればよいですか?

マイコード:

public class SwipeActivity extends Activity{ 

    ViewPager vp; 
    Table_customSwipeAdapter adapter; 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     //requestWindowFeature(Window.FEATURE_NO_TITLE); 
     setContentView(R.layout.activity_swipe); 


     vp=(ViewPager)findViewById(R.id.view_pager); 
     adapter=new Table_customSwipeAdapter(this); 
     vp.setAdapter(adapter); 

    } 

    public void imageSwipe(View view){ 
     Toast toast = Toast.makeText(getApplicationContext(), "Open Link!", Toast.LENGTH_SHORT); 
     toast.show(); 
    } 

} 


class Table_customSwipeAdapter extends PagerAdapter{ 

    int[] image_resources={R.drawable.i1,R.drawable.i2,R.drawable.i3,R.drawable.i4,R.drawable.i5}; 

    private Context ctx; 
    private LayoutInflater layoutInflater; 


    public Table_customSwipeAdapter(Context ctx) { 
     this.ctx=ctx; 
    } 


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

    @Override 
    public boolean isViewFromObject(View arg0, Object arg1) { 
     // TODO Auto-generated method stub 
     return (arg0==(LinearLayout)arg1); 
    } 


    public Object instantiateItem(ViewGroup container, int position){ 

     layoutInflater=(LayoutInflater)ctx.getSystemService(Context.LAYOUT_INFLATER_SERVICE); 

     View item_view=layoutInflater.inflate(R.layout.activity_table_custom_swipe_adapter, container,false); 

     ImageView imageView=(ImageView)item_view.findViewById(R.id.image_view); 
     TextView textView=(TextView)item_view.findViewById(R.id.image_count); 
     imageView.setImageResource(image_resources[position]); 
     textView.setText("Image "+(position+1)); 
     container.addView(item_view); 

     return item_view; 

    } 


    public void destroyItem(ViewGroup container, int position, Object object){ 
     container.removeView((LinearLayout)object); 

    } 

} 

ここViewPagerそれは同じ5枚の画像を示す。..

+0

がによってitemClick – Haroon

+0

に設定されたさまざまなデータを渡すことができますホープはそれぞれこの

vp=(ViewPager)findViewById(R.id.view_pager); //HERE YOU WILL PASS WHICHEVER LIST YOU WANT TO DISPLAY e.g image_resources1 or image_resources2 or ANY OTHER adapter=new Table_customSwipeAdapter(this,image_resources); vp.setAdapter(adapter); 

のようなクリックであなたの活動からあなたアダプタに別のリストを渡すが、私はどのように渡すことができます画像? – Doglas

答えて

0
//In your Table_customSwipeAdapter make these chages in contructor get the image array e.g 
class Table_customSwipeAdapter extends PagerAdapter{ 

int[] image_resources; 

private Context ctx; 
private LayoutInflater layoutInflater; 

public Table_customSwipeAdapter(Context ctx, int[] image_resources) { 
    this.ctx=ctx; 
    this.image_resources=image_resources 
} 
// In your SwipeActivity while calling the constructor pass the int array 
adapter=new Table_customSwipeAdapter(this,image_resources); 
+0

ありがとう@Haroon – Doglas

+0

おかげでチャンスが助けになりました! – Haroon

0

あなたはActivityを実装することができViewPagerで定義されたコールバック・インターフェースを持つことができます。 instantiateItemにあるViewPagerは、このコールバックを通じてActivityのメソッドを呼び出して、クリックされたListViewの位置を取得できます。その情報に基づいて画像を表示することができます。あなたは、同じ画像をあなたがそれぞれ異なるに表示する画像の異なるListsを作成するために必要なすべての

まず設定されている

imageView.setImageResource(image_resources[position]); 

ここので、同じ画像を表示

+0

どうすればいいですか? – Doglas

0

そのアクティビティをクリックしてください。

public class SwipeActivity extends Activity{ 

ViewPager vp; 
Table_customSwipeAdapter adapter; 
int[] image_resources1 ={R.drawable.i1,R.drawable.i2,R.drawable.i3}; 
int[] image_resources2 ={R.drawable.i4,R.drawable.i5}; 

その後、

class Table_customSwipeAdapter extends PagerAdapter{ 

int[] image_resources; 
private Context ctx; 
private LayoutInflater layoutInflater; 

public Table_customSwipeAdapter(Context ctx, int[] image_resources) { 
    this.ctx=ctx; 
    this.image_resources = image_resources; 
} 

とアダプタ

はそれが

+0

あなたの素晴らしい説明をありがとう。ありがとう。 :) – Doglas

関連する問題