2016-05-16 2 views
1

FrameLayoutのタイプである新しいレイアウトを作成し、画像ビューの4つの要素を追加しました。最初の画像ビューを取りたいプログラムによって最終的に
でframelayoutの要素をプログラムで並べ替えます。私たちはボタンclickListenerを聞いているとします。framelayout要素のために要素の最後に正面画像ビューを送る方法android

<FrameLayout 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:id="@+id/framelayout"> 
    <ImageView 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:background="@mipmap/a20141008_091817" 
     android:id="@+id/i1"/> 
    <ImageView 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:background="@mipmap/a20141008_091819" 
     android:id="@+id/i2"/> 
    <ImageView 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:background="@mipmap/a20141008_091821" 
     android:id="@+id/i3"/> 
    <ImageView 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:background="@mipmap/a20141008_091823" 
     android:id="@+id/i4"/> 

    </FrameLayout> 

私は助けてくれてありがとう。

答えて

1

まずは誰もが私を助けてくれてありがとう、ありがとう。 それから私は良い練習の方法で私の問題を見つけて、誰とでも将来それが必要になるかもしれないとあなたと共有したいと思います。 手順

  1. 私の質問では、XMLのようでframeLayoutを作成
  2. がでframeLayoutにあなたのイメージを追加
  3. はのIDを知って
  4. 私たちがしなければならないのjavaファイルを作成し、それをXMLに接続しますframlayout
  5. framLayout
  6. にすべての画像の数を取得するX秒で画像の順序を変更しますタイマーを作成するにすべての画像
  7. は、私は停止し、我々は、私の場合にしたい場合はタイマーを停止するメソッドを作成し、私は
  8. を述べたように、まだX倍で動作するようにタイマーを呼び出す前に

  9. をもたらすために、画像の隣にあるかを決定しますIntagerのVARを作成しますonPause()メソッドのタイマー

    これは上記のすべてのステップを実行するJavaファイルです。それがいずれかの

パブリッククラスFrameLayout5がAppCompatActivity @ExpensiveBelly {

FrameLayout frameLayout; 
Handler handler;    // this class will work as timer for change the order of images in delay 
List<Integer> views; // this Array list will store all the ids of all the ImageViews into framelayout 
int counter;  // this counter will help us to find the next element to bring it to front 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_frame_layout5); 
    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar); 
    setSupportActionBar(toolbar); 

    frameLayout = (FrameLayout) findViewById(R.id.framelayout); 
    handler = new Handler(); 
    views = new ArrayList<Integer>(); 
    counter=0; 

    // get all the children of framelayout 
    for (int i = 0; i < frameLayout.getChildCount() ; i++) { 
     views.add(frameLayout.getChildAt(i).getId()); 
    } 

    handler.postDelayed(runnable, 1000); // 1000 mean that this method will excute every 1 second 


} 

Runnable runnable = new Runnable() { 
    @Override 
    public void run() { 

     int i=counter%(frameLayout.getChildCount());  
      counter++; 
     ImageView a = (ImageView) findViewById(views.get(i)); 

     frameLayout.bringChildToFront(a); 

     handler.postDelayed(runnable, 1000); 
     Log.d("run", "called "+i); 
    } 
}; 

@Override 
protected void onStop() { 
    super.onStop(); 
    handler.removeCallbacks(runnable); 
} 

}

@ishmaelMakitla

0

ImageViewsの表示を不可視/非表示に変更すると、表示したいものだけを表示できますか?

+0

を拡張し、私はそのことについて考えたが、私はそれは良い習慣はないと思う助けたいですフレームレートで私はその見解を並べ替えることができると読んだ。ちなみにあなたの返信ありがとう –

+0

こんにちは。あなたは私の問題をどのように解決したかを見ることができます。あなたのお手伝いをしてくれてありがとう。 –

2

この場合、FrameLayoutの子ビューを並べ替える必要があるようです。 ViewGroupを使用して、getChildAt(some-index)、addView(viewToAdd、index-to-place-it)、removeViewAt(some-index)を使用して並べ替えを行うことをお勧めします。

ViewGroup viewGroup = (ViewGroup)this.findViewById(android.R.id.content).getRootView(); 

     int first = 0; 
     int second = 1; 
     int third = 2; 
     int fourth = 3; 

     View view1 = viewGroup.getChildAt(first); 
     View view4 = viewGroup.getChildAt(fourth); 
     viewGroup.removeViewAt(first); 
     //move fourth to first 
     viewGroup.addView(view4, first); 
     viewGroup.removeViewAt(fourth); 
     //move first to fourth 
     viewGroup.addView(view1, fourth); 

     View view2 = viewGroup.getChildAt(second); 
     View view3 = viewGroup.getChildAt(third); 

     viewGroup.removeViewAt(second); 
     //move third to second 
     viewGroup.addView(view3, second); 
     //move second to third 
     viewGroup.removeViewAt(third); 
     viewGroup.addView(view2, third); 

私はこれがあなたと他の人に役立ちます願っています: - 私の提案された解決策では、私は、レイアウトは(これを向上させることができる)に変更はありませんと仮定します。選択した回答をthis closely related questionthis oneにチェックアウトすることもできます。the selected answer hereには、ビューの並べ替え方法が示されています。

+0

スワッピングフォームの先頭から最後まではどこですか? –

+0

OK、私はコードサンプルがあなたを始められると思った。アイテムを並べ替える場所にアイテムを追加しましょう。 1秒... – ishmaelMakitla

+0

が答えを更新しました。これがチェックされていれば教えてください。 – ishmaelMakitla

関連する問題