2016-07-10 18 views
0

我有這個如何使片段爲每個ImageView的

enter image description here

widnowManger這個4圖像,而在它們的下方佈局 我只是想知道如何添加片段,每imageView,當我點擊它 和它將在service不活動我是一個初學者,所以任何人都可以告訴我的方式來添加片段時,點擊一些圖像,並通過圖像keyid當點擊它的片段,當點擊另一個imageView我不想摧毀最後一個片段,並在最後我做nt想讓每個圖像視圖4片段,因爲我不知道我有多少imageView

回答

1

試試這個!使用隱藏()顯示()

imageview.setOnClickListener(this); 
//…… 

@Override 
public void onClick(View v) { 

    FragmentTransaction transaction = getSupportFragmentManager().beginTransaction(); 
    transaction.add(fragment1,"f1"); 
    transaction.add(fragment2,"f2"); 
    transaction.add(fragment3,"f3"); 
    transaction.add(fragment4,"f4"); 

    switch (v.getId()){ 
      case R.id.imageView1: 
       transaction.show(fragment1); 
       if(lastFragment!=null) 
        transaction.hide(lastFragment); 
       lastFragment=fragment1; 
       break; 
      case R.id.imageView2: 
       transaction.show(fragment2); 
       if(lastFragment!=null) 
        transaction.hide(lastFragment); 
       lastFragment=fragment2; 

       break; 
      case R.id.imageView3: 
       transaction.show(fragment3); 
       if(lastFragment!=null) 
        transaction.hide(lastFragment); 
       lastFragment=fragment3; 
       break; 
      case R.id.imageView4: 
       transaction.show(fragment4); 
       if(lastFragment!=null) 
        transaction.hide(lastFragment); 
       lastFragment=fragment4; 
       break; 
     } 
    transaction.commit(); 

} 
+0

它的好,但我不想寫的情況下爲每個圖像視圖......我的意思是,如果我有mabye 10'imageView'我必須做出10案件和問題我不知道有多少imageView我mabye超過10 我投票給你,但嘗試解決這個問題,因爲我不知道有多少ImageView我有 – medo

+0

嘗試使用viewpager與片段? – Cgx

相關問題