栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

用Fragment实现图片简易浏览

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

用Fragment实现图片简易浏览

:activity_main3.xml:主界面编辑图片分类的布局




    
        

            

        

        

            
        

        

            
        
        
            
        
        
            

        



        

            

        

        
            

        

        

            

        

        

            

        
    

    

    


Mainl3Activity:实现点击分类的控件id调出fragment界面

package com.example.android;


import android.app.AlertDialog;
import android.app.Dialog;
import android.app.FragmentManager;
import android.app.FragmentTransaction;
import android.content.DialogInterface;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.view.Window;

import android.widget.TextView;


import androidx.fragment.app.FragmentActivity;


public class Main3Activity extends FragmentActivity implements View.OnClickListener {
    private WeixinFragment firstFragment = null;
    private ContactListFragment secondFragment = null;
    private FindFragment thirdFragment = null;
    private SelfFragment fourthFragment = null;
    private youxiFragment fivethFragment = null;
    private saicheFragment sixthFragment = null;
    private xingkongFragment sevenFragment = null;
    private meishiFragment eightFragment = null;
    private mingxingFragment nineFragment = null;


    private View firstLayout = null;
    private View secondLayout = null;
    private View thirdLayout = null;
    private View fourthLayout = null;
    private View fivethLayout = null;
    private View sixthLayout = null;
    private View sevenLayout = null;
    private View eightLayout = null;
    private View nineLayout = null;


    


    private TextView dongman = null;
    private TextView fenjing = null;
    private TextView meinu = null;
    private TextView kehuan = null;
    private TextView youxi = null;
    private TextView saiche = null;
    private TextView xingkong = null;
    private TextView meishi = null;
    private TextView mingxing = null;


    private FragmentManager fragmentManager = null;// 用于对Fragment进行管理


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);//要求窗口没有title
        super.setContentView(R.layout.activity_main3);
        // 初始化布局元素
        initViews();
        fragmentManager = getFragmentManager();//用于对Fragment进行管理
        // 设置默认的显示界面
        setTabSelection(0);
    }

    

    public void initViews() {
        fragmentManager = getFragmentManager();
        firstLayout = findViewById(R.id.dongman_layout);
        secondLayout = findViewById(R.id.fenjing_layout);
        thirdLayout = findViewById(R.id.meinu_layout);
        fourthLayout = findViewById(R.id.kehuan_layout);
        fivethLayout =findViewById(R.id.youxi_layout);
        sixthLayout = findViewById(R.id.saiche_layout);
        sevenLayout = findViewById(R.id.xingkong_layout);
        eightLayout = findViewById(R.id.meishi_layout);
        nineLayout = findViewById(R.id.mingxing_layout);



        dongman = (TextView) findViewById(R.id.dongman);
        fenjing = (TextView) findViewById(R.id.fenjing);
        meinu = (TextView) findViewById(R.id.meinu);
        kehuan = (TextView) findViewById(R.id.kehuan);
        youxi=findViewById(R.id.youxi);
        saiche=findViewById(R.id.saiche);
        xingkong=findViewById(R.id.xingkong);
        meishi=findViewById(R.id.meishi);
        mingxing=findViewById(R.id.mingxing);








        //处理点击事件
        firstLayout.setOnClickListener(this);
        secondLayout.setOnClickListener(this);
        thirdLayout.setOnClickListener(this);
        fourthLayout.setOnClickListener(this);
        fivethLayout.setOnClickListener(this);
        sixthLayout.setOnClickListener(this);
        sevenLayout.setOnClickListener(this);
        eightLayout.setOnClickListener(this);
        nineLayout.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.dongman_layout:
                setTabSelection(0);// 当点击了微信时,选中第1个tab
                break;
            case R.id.fenjing_layout:
                setTabSelection(1);// 当点击了通讯录时,选中第2个tab
                break;
            case R.id.meinu_layout:
                setTabSelection(2);// 当点击了发现时,选中第3个tab
                break;
            case R.id.kehuan_layout:
                setTabSelection(3);// 当点击了我时,选中第4个tab
                break;
            case R.id.youxi_layout:
                setTabSelection(4);// 当点击了发现时,选中第5个tab
                break;
            case R.id.saiche_layout:
                setTabSelection(5);// 当点击了发现时,选中第6个tab
                break;
            case R.id.xingkong_layout:
                setTabSelection(6);// 当点击了发现时,选中第7个tab
                break;
            case R.id.meishi_layout:
                setTabSelection(7);// 当点击了发现时,选中第8个tab
                break;
            case R.id.mingxing_layout:
                setTabSelection(8);// 当点击了发现时,选中第9个tab
                break;
            default:
                break;
        }

    }

    

    private void setTabSelection(int index) {
        clearSelection();// 每次选中之前先清除掉上次的选中状态
        FragmentTransaction transaction = fragmentManager.beginTransaction();// 开启一个Fragment事务
        hideFragments(transaction);// 先隐藏掉所有的Fragment,以防止有多个Fragment显示在界面上的情况
        switch (index) {
            case 0:
                // 当点击了我的微信tab时改变控件的图片和文字颜色
                dongman.setTextColor(Color.parseColor("#0090ff"));//修改字体颜色
                if (firstFragment == null) {
                    
                    Intent intent = getIntent();
                    String number = intent.getStringExtra("weixin_number");
                    // 如果FirstFragment为空,则创建一个并添加到界面上
                    firstFragment = new WeixinFragment(number);
                    transaction.add(R.id.fragment, firstFragment);
                } else {
                    // 如果FirstFragment不为空,则直接将它显示出来
                    transaction.show(firstFragment);//显示的动作
                }
                break;
            // 以下和firstFragment类同
            case 1:
               // contactImg.setImageResource(R.drawable.people);
                fenjing.setTextColor(Color.parseColor("#0090ff"));
                if (secondFragment == null) {
                    
                    Intent intent = getIntent();
                    String number = intent.getStringExtra("fenjing_number");
                    secondFragment = new ContactListFragment(number);
                    transaction.add(R.id.fragment, secondFragment);
                } else {
                    transaction.show(secondFragment);
                }
                break;
            case 2:

                meinu.setTextColor(Color.parseColor("#0090ff"));
                if (thirdFragment == null) {
                    thirdFragment = new FindFragment();
                    transaction.add(R.id.fragment, thirdFragment);
                } else {
                    transaction.show(thirdFragment);
                }
                break;
            case 3:

                kehuan.setTextColor(Color.parseColor("#0090ff"));
                if (fourthFragment == null) {
                    fourthFragment = new SelfFragment();
                    transaction.add(R.id.fragment, fourthFragment);
                } else {
                    transaction.show(fourthFragment);
                }
                break;

           case 4:

                youxi.setTextColor(Color.parseColor("#0090ff"));
                if (fivethFragment == null) {
                    Intent intent = getIntent();
                    String number = intent.getStringExtra("youxi_number");
                    fivethFragment = new youxiFragment(number);
                    transaction.add(R.id.fragment, fivethFragment);
                } else {
                    transaction.show(fivethFragment);
                }
                break;
           case 5:
               saiche.setTextColor(Color.parseColor("#0090ff"));
               if (sixthFragment == null) {
                   Intent intent = getIntent();
                   String number = intent.getStringExtra("saiche_number");
                   sixthFragment = new saicheFragment(number);
                   transaction.add(R.id.fragment, sixthFragment);
               } else {
                   transaction.show(sixthFragment);
               }
                break;
            case 6:

                xingkong.setTextColor(Color.parseColor("#0090ff"));
                if (sevenFragment == null) {
                    Intent intent = getIntent();
                    String number = intent.getStringExtra("number");
                    sevenFragment = new xingkongFragment(number);
                    transaction.add(R.id.fragment, sevenFragment);
                } else {
                    transaction.show(sevenFragment);
                }
                break;
            case 7:

                meishi.setTextColor(Color.parseColor("#0090ff"));
                if (eightFragment == null) {
                    Intent intent = getIntent();
                    String number = intent.getStringExtra("weixin_number");
                    eightFragment = new meishiFragment(number);
                    transaction.add(R.id.fragment, eightFragment);
                } else {
                    transaction.show(eightFragment);
                }
                break;
            case 8:

                mingxing.setTextColor(Color.parseColor("#0090ff"));
                if (nineFragment == null) {
                    Intent intent = getIntent();
                    String number = intent.getStringExtra("weixin_number");
                    nineFragment = new mingxingFragment(number);
                    transaction.add(R.id.fragment, nineFragment);
                } else {
                    transaction.show(nineFragment);
                }
                break;

        }
        transaction.commit();

    }

    
    void clearSelection() {

        dongman.setTextColor(Color.parseColor("#82858b"));

        fenjing.setTextColor(Color.parseColor("#82858b"));

        meinu.setTextColor(Color.parseColor("#82858b"));

        kehuan.setTextColor(Color.parseColor("#82858b"));

        youxi.setTextColor(Color.parseColor("#82858b"));

        saiche.setTextColor(Color.parseColor("#82858b"));

        xingkong.setTextColor(Color.parseColor("#82858b"));

        meishi.setTextColor(Color.parseColor("#82858b"));

        mingxing.setTextColor(Color.parseColor("#82858b"));


    }

    
    private void hideFragments(FragmentTransaction transaction) {
        if (firstFragment != null) {
            transaction.hide(firstFragment);
        }
        if (secondFragment != null) {
            transaction.hide(secondFragment);
        }
        if (thirdFragment != null) {
            transaction.hide(thirdFragment);
        }
        if (fourthFragment != null) {
            transaction.hide(fourthFragment);
        }
        if (fivethFragment != null) {
            transaction.hide(fivethFragment);
        }
        if (sixthFragment != null) {
            transaction.hide(sixthFragment);
        }
        if (sevenFragment != null) {
            transaction.hide(sevenFragment);
        }
        if (eightFragment != null) {
            transaction.hide(eightFragment);
        }
        if (nineFragment != null) {
            transaction.hide(nineFragment);
        }
    }

    //封装一个AlertDialog
    private void exitDialog() {
        Dialog dialog = new AlertDialog.Builder(this)
                .setTitle("温馨提示")
                .setMessage("您确定要退出程序吗?")
                .setPositiveButton("关闭软件", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface arg0, int arg1) {
                        finish();
                    }
                })
                .setNegativeButton("取消", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface arg0, int arg1) {
                    }
                }).create();
        dialog.show();//显示对话框
    }

    
    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK) {//如果是返回按钮
            exitDialog();
        }
        return super.onKeyDown(keyCode, event);
    }

}

fargment_youxi.xml:游戏分类的界面布局

    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:background="#020202"
    >
            android:background="#330C0C0C"
        android:layout_width="match_parent"
        android:layout_height="match_parent">
       
                    android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:orientation="vertical"
            android:background="#330C0C0C"
            >
                            android:layout_marginTop="20dp"
                android:layout_width="match_parent"
                android:layout_height="300dp"
                android:background="@drawable/yx1"
                />
                            android:layout_marginTop="20dp"
                android:layout_width="match_parent"
                android:layout_height="300dp"
                android:background="@drawable/yx2"
                />
                            android:layout_marginTop="20dp"
                android:layout_width="match_parent"
                android:layout_height="300dp"
                android:background="@drawable/yx3"
                />
                            android:layout_marginTop="20dp"
                android:layout_width="match_parent"
                android:layout_height="300dp"
                android:background="@drawable/yx4"
                />
                            android:layout_marginTop="20dp"
                android:layout_width="match_parent"
                android:layout_height="300dp"
                android:background="@drawable/yx5"
                />

       
   

 

 

fragment界面的图片我是直接放textview的,你们可以用list view来放置,然在再重写adapter可以实现点击对应的图片跳转你们指定的界面!

 ps:我只放置了一个游戏分类界面的fragment,其余的同理,如有代码上的问题错误,请雅正,我后面及时改正,谢谢,让我们一起学习,共同进步!

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/938945.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号