Android Fragment事件使用方法

使用说明:这需要自己单独创建fragment页面的

1、XML文件添加以下

 <LinearLayout
        android:layout_weight="1"
        android:layout_width="match_parent"
        android:layout_height="match_parent">
        <RelativeLayout
            android:layout_weight="1"
            android:id="@+id/main_fragment1"
            android:layout_width="match_parent"
            android:layout_height="match_parent">

        </RelativeLayout>
    </LinearLayout>

    <LinearLayout
        android:layout_weight="8.5"
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <androidx.cardview.widget.CardView
            app:cardBackgroundColor="@color/white"
            android:layout_margin="10dp"
            app:cardCornerRadius="999dp"
            app:cardMaxElevation="0dp"
            android:layout_width="match_parent"
            android:layout_height="match_parent">
            <LinearLayout
                android:orientation="horizontal"
                android:layout_width="match_parent"
                android:layout_height="match_parent">
                <LinearLayout
                    android:id="@+id/shouye"
                    android:gravity="center"
                    android:layout_weight="1"
                    android:orientation="vertical"
                    android:layout_width="match_parent"
                    android:layout_height="match_parent">
                    <ImageView
                        android:id="@+id/shouyeimg"
                        android:src="@mipmap/home"
                        android:layout_width="30dp"
                        android:layout_height="30dp">

                    </ImageView>
                    <TextView
                        android:id="@+id/sycolor"
                        android:text="首页"
                        android:textSize="13sp"
                        android:textColor="#1296db"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content">

                    </TextView>
                </LinearLayout>
                <LinearLayout
                    android:id="@+id/wd"
                    android:orientation="vertical"
                    android:gravity="center"
                    android:layout_weight="1"
                    android:layout_width="match_parent"
                    android:layout_height="match_parent">
                    <ImageView
                        android:id="@+id/wdimg"
                        android:src="@mipmap/wd_1"
                        android:layout_width="30dp"
                        android:layout_height="30dp">

                    </ImageView>
                    <TextView
                        android:id="@+id/wdcolor"
                        android:text="我的"
                        android:textSize="13sp"
                        android:textColor="#E5E5E5"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content">

                    </TextView>

                </LinearLayout>

            </LinearLayout>

      </androidx.cardview.widget.CardView>
    </LinearLayout>
 //底部单击id设置
        LinearLayout linearLayout = findViewById(R.id.shouye);//首页
        LinearLayout linearLayout2 = findViewById(R.id.wd);

        //底部菜单图片设置
        ImageView imageView = findViewById(R.id.shouyeimg);//首页
        ImageView imageView2 = findViewById(R.id.wdimg);//我的

        //底部菜单
        // 文字颜色设置
        TextView textView=findViewById(R.id.sycolor);
        TextView textView2=findViewById(R.id.wdcolor);


        //底部导航fragment部分
        FragmentManager manager = getSupportFragmentManager();
        //将首页添加进去
        FragmentTransaction transaction = manager.beginTransaction();
        transaction.replace(R.id.main_fragment1, new shouyeFragment());//home里的id
        transaction.commit();
        //将首页添加进去结束

        //首页单击事件
        linearLayout.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                imageView2.setImageResource(R.mipmap.wd_1);//图片更改
                imageView.setImageResource(R.mipmap.home);//图片更改
                textView.setTextColor(Color.parseColor("#1296db"));
                textView2.setTextColor(Color.parseColor("#E5E5E5"));
                FragmentTransaction transaction = manager.beginTransaction();
                transaction.replace(R.id.main_fragment1, new shouyeFragment());//home里的id
                transaction.commit();
            }
        });
        //我的单击事件
        linearLayout2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                imageView.setImageResource(R.mipmap.home_1);//图片更改
                imageView2.setImageResource(R.mipmap.wd);//图片更改
                textView2.setTextColor(Color.parseColor("#1296db"));
                textView.setTextColor(Color.parseColor("#E5E5E5"));
                FragmentTransaction transaction = manager.beginTransaction();
                transaction.replace(R.id.main_fragment1, new wdFragment());//home里的id
                transaction.commit();
            }
        });

二哈

文章作者信息...

留下你的评论

*评论支持代码高亮<pre class="prettyprint linenums">代码</pre>

相关推荐