ActivityGroup实现Tab功能
java文件:
//第一步:初始化各种控件
layout_myorder_content = (LinearLayout) findViewById(R.id.layout_myorder_content);
layout_all = (LinearLayout) findViewById(R.id.layout_all);
layout_goods_to_be_received = (LinearLayout) findViewById(R.id.layout_goods_to_be_received);
layout_to_be_shipped = (LinearLayout) findViewById(R.id.layout_to_be_shipped);
layout_to_be_evaluated = (LinearLayout) findViewById(R.id.layout_to_be_evaluated);
to_be_shipped = (TextView) findViewById(R.id.to_be_shipped);
all = (TextView) findViewById(R.id.all);
goods_to_be_received = (TextView) findViewById(R.id.goods_to_be_received);
to_be_evaluated = (TextView) findViewById(R.id.to_be_evaluated);
layout_all.setOnClickListener(this);
layout_goods_to_be_received.setOnClickListener(this);
layout_to_be_shipped.setOnClickListener(this);
layout_to_be_evaluated.setOnClickListener(this);
//放在开头,
all.setTextColor(Color.RED);
layout_myorder_content.addView(showContentView(0));
//第二步:对layout监控:
@Override
public void onClick(View v) {
layout_myorder_content.removeAllViews();
switch (v.getId()) {
case R.id.layout_all:
all.setTextColor(Color.RED);
to_be_shipped.setTextColor(Color.GRAY);
goods_to_be_received.setTextColor(Color.GRAY);
to_be_evaluated.setTextColor(Color.GRAY);
layout_myorder_content.addView(showContentView(0));
break;
case R.id.layout_goods_to_be_received:
all.setTextColor(Color.GRAY);
to_be_shipped.setTextColor(Color.GRAY);
goods_to_be_received.setTextColor(Color.RED);
to_be_evaluated.setTextColor(Color.GRAY);
layout_myorder_content.addView(showContentView(2));
break;
case R.id.layout_to_be_shipped:
all.setTextColor(Color.GRAY);
to_be_shipped.setTextColor(Color.RED);
goods_to_be_received.setTextColor(Color.GRAY);
to_be_evaluated.setTextColor(Color.GRAY);
layout_myorder_content.addView(showContentView(1));
break;
case R.id.layout_to_be_evaluated:
all.setTextColor(Color.GRAY);
to_be_shipped.setTextColor(Color.GRAY);
goods_to_be_received.setTextColor(Color.GRAY);
to_be_evaluated.setTextColor(Color.RED);
layout_myorder_content.addView(showContentView(3));
break;
default:
break;
//第三步:切换方法showContentView()
private View showContentView(int i) {
if (i == 0) {
return getLocalActivityManager()
.startActivity(i + " ", new Intent(this, AllActivity.class))
.getDecorView();
} else if (i == 1) {
return getLocalActivityManager()
.startActivity(i + " ", new Intent(this, ShinppedActivity.class))
.getDecorView();
} else if (i == 2) {
return getLocalActivityManager()
.startActivity(i + " ", new Intent(this, ReceivedActivity.class))
.getDecorView();
} else if (i == 3) {
return getLocalActivityManager()
.startActivity(i + " ", new Intent(this, EvaluatedActivity.class))
.getDecorView();
}
return null;
}
xml文件:
<include layout="@layout/myorder_header"/>
//可以是LinearLayout ,也可以FrameLayout
<LinearLayout
android:id="@+id/layout_myorder_content"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
/>
myorder_header.xml:
<LinearLayout
android:id="@+id/layout_all"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="center" >
<TextView
android:id="@+id/all"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="全部"
android:textColor="@color/gray" />
</LinearLayout>
<LinearLayout
android:id="@+id/layout_to_be_shipped"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="center" >
<TextView
android:id="@+id/to_be_shipped"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="待发货"
android:textColor="@color/gray" />
</LinearLayout>
<LinearLayout
android:id="@+id/layout_goods_to_be_received"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="center" >
<TextView
android:id="@+id/goods_to_be_received"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="待收货"
android:textColor="@color/gray" />
</LinearLayout>
<LinearLayout
android:id="@+id/layout_to_be_evaluated"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="center" >
<TextView
android:id="@+id/to_be_evaluated"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="待评价"
android:textColor="@color/gray" />
</LinearLayout>