Use ActivityGroup to make homepage framework in Android

Last time I summarized the production of the bottom tab bar, and also summarized the use of TabActivity to make the homepage frame. This time, let’s talk about using ActivityGroup to make a homepage framework. In fact, the two homepage frameworks are pretty much the same, because the core content page uses Activity, not Fragment. So I put these two together and talk about it. We are divided into two parts, one is to compare with TabActivity, after all, it is the same, we must be good at summarizing. One is to post the code.

1. Comparison of ActivityGroup and TabActivity

  • The root layout node no longer uses TabHost, and uses the common linear layout LinearLayout instead.
  • Removed TabWidget, a routine option widget
  • The content page changed from a fixed numbered frame layout to a custom numbered linear layout

2. Code example

It can be seen that ActivityGroup is more concise than TabActivity, so how do we look at the code

Layout file

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <FrameLayout
        android:id="@+id/fr_container"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1" />

    <!-- 标签-->
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal">

        <TextView
            android:id="@+id/tv1"
            style="@style/TabButton"
            android:drawableTop="@drawable/text_pic"
            android:text="首页" />

        <TextView
            android:id="@+id/tv2"
            style="@style/TabButton"
            android:drawableTop="@drawable/text_pic"
            android:text="商品" />

        <TextView
            android:id="@+id/tv3"
            style="@style/TabButton"
            android:drawableTop="@drawable/text_pic"
            android:text="发现" />

        <TextView
            android:id="@+id/tv4"
            style="@style/TabButton"
            android:drawableTop="@drawable/text_pic"
            android:text="我的" />
    </LinearLayout>
</LinearLayout>

The core of the layout is the FrameLayout, and the others are ordinary label buttons, as I talked about before.

java source file

public class MainActivity extends ActivityGroup implements View.OnClickListener {

    private TextView tv1;
    private TextView tv2;
    private TextView tv3;
    private TextView tv4;
    private FrameLayout fr_container;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        tv1 = findViewById(R.id.tv1);
        tv2 = findViewById(R.id.tv2);
        tv3 = findViewById(R.id.tv3);
        tv4 = findViewById(R.id.tv4);
        fr_container = findViewById(R.id.fr_container);
        tv1.setOnClickListener(this);
        tv2.setOnClickListener(this);
        tv3.setOnClickListener(this);
        tv4.setOnClickListener(this);
        tv1.setSelected(true);
        toActivity("one",OneActivity.class);
    }

    /**
     * 把内容视图切换到对应的Activity活动页面
     * @param label
     * @param cls
     */
    private void toActivity(String label,Class<?> cls){
        //创建一个意图,并存入指定包裹
        Intent intent = new Intent(this,cls);
        //移除内容框架下面的所有下级视图
        fr_container.removeAllViews();
        //启动意图指向的活动,并获取该活动页面的顶层视图
        View v = getLocalActivityManager().startActivity(label,intent).getDecorView();
        //设置内容视图的布局参数
        v.setLayoutParams(new FrameLayout.LayoutParams(FrameLayout.LayoutParams.MATCH_PARENT, FrameLayout.LayoutParams.MATCH_PARENT));
        //把活动页面的顶层视图(即内容视图)添加到内容框架上
        fr_container.addView(v);
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()){
            case R.id.tv1:
                if (!tv1.isSelected()){
                    tv1.setSelected(true);
                    tv2.setSelected(false);
                    tv3.setSelected(false);
                    tv4.setSelected(false);
                    toActivity("one",OneActivity.class);
                }
                break;
            case R.id.tv2:
                if (!tv2.isSelected()) {
                    tv1.setSelected(false);
                    tv2.setSelected(true);
                    tv3.setSelected(false);
                    tv4.setSelected(false);
                    toActivity("two",TwoActivity.class);
                }
                break;
            case R.id.tv3:
                if (!tv3.isSelected()) {
                    tv1.setSelected(false);
                    tv2.setSelected(false);
                    tv3.setSelected(true);
                    tv4.setSelected(false);
                    toActivity("three",ThreeActivity.class);
                }
                break;
            case R.id.tv4:
                if (!tv4.isSelected()) {
                    tv1.setSelected(false);
                    tv2.setSelected(false);
                    tv3.setSelected(false);
                    tv4.setSelected(true);
                    toActivity("four",FourActivity.class);
                }
                break;
        }
    }
}

In Activity, we first inherited ActivityGroup, and secondly, the focus is on the toActivity method. We can pass in a labeled label and a Class object corresponding to the Activity. The toActivity method is called when jumping.

Guess you like

Origin blog.csdn.net/weixin_38322371/article/details/114068467