Facebookアプリってたぶん1つのActivityで作ってる気がするんだけど、
・1つのActivityにレイアウト複数個表示する
・フリックでどかしたりする
ってのを実現したくていろいろやった。
んで、Githubのsshの設定してなくて怠いからソース貼る。
読んだほうが早いと思うので説明書かない
main.xml
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/rootLayout" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical" > <TextView android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="@string/hello" /> </LinearLayout>
first.xml
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/firstView" android:layout_width="match_parent" android:layout_height="200px" android:background="#00ff00" android:gravity="center" android:orientation="vertical" > <TextView android:id="@+id/textView1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="111111111111111111111111111111" android:textSize="90px" /> </LinearLayout>
second.xml
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/secondView" android:layout_width="match_parent" android:layout_height="200px" android:background="#ff0000" android:gravity="center" android:orientation="vertical" > <TextView android:id="@+id/textView1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="2222222222222222" android:textSize="90px" /> </LinearLayout>
OneActivityAppActivity.java
package gin.no.OneActivityApp; import java.util.ArrayList; import android.app.Activity; import android.graphics.Rect; import android.os.Bundle; import android.view.LayoutInflater; import android.view.MotionEvent; import android.view.View; import android.widget.LinearLayout; public class OneActivityAppActivity extends Activity { int offsetX; int offsetY; ArrayList<View> views; public OneActivityAppActivity() { this.views = new ArrayList<View>(); this.offsetX = 0; this.offsetY = 0; } @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); LayoutInflater inflater = getLayoutInflater(); LinearLayout rootLayout; rootLayout = (LinearLayout) findViewById(R.id.rootLayout); rootLayout.removeAllViews(); inflater.inflate(R.layout.first, rootLayout, true); inflater.inflate(R.layout.second, rootLayout, true); views.add(findViewById(R.id.firstView)); views.add(findViewById(R.id.secondView)); } @Override public boolean onTouchEvent(MotionEvent event) { int nowX = (int) event.getRawX(); int nowY = (int) event.getRawY(); switch (event.getAction()) { case MotionEvent.ACTION_MOVE: for (View v : views) { Rect rect = new Rect(); v.getGlobalVisibleRect(rect); if (rect.contains(nowX, nowY)) { dragView(v, nowX, nowY); break; } } offsetX = nowX; offsetY = nowY; break; case MotionEvent.ACTION_DOWN: offsetX = nowX; offsetY = nowY; break; } return super.onTouchEvent(event); } public void dragView(View view, int x, int y) { int diffX = offsetX - x; int diffY = offsetY - y; int currentX = view.getLeft() - diffX; int currentY = view.getTop() - diffY; view.layout(currentX, currentY, currentX + view.getWidth(), currentY + view.getHeight()); } }
んで、よくわからないのが、
inflater.inflateしたViewを最初から重ねて表示する方法
そもそもFacebookアプリとは全く違うことやってる可能性も否めない
とりあえずそんな感じ。