用Fragment实现图片简易浏览

原创
小哥 2年前 (2022-12-19) 阅读数 48 #大杂烩

: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;
        }

    }

    /**
     * 根据传入的index用于设置选定对象的参数tab页 每个tab与页面相对应的下标。0意思是微信,1指示地址簿,2指示发现,3表示我
     */

    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) {
                    /*获取登录activity传输的微信号*/
                    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) {
                    /*获取登录activity传输的微信号*/
                    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"));

    }

    /**
     * 将所有的Fragment设置为隐藏状态 用于对Fragment执行操作的事务
     */
    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:游戏分类的界面布局

<?xml version="1.0" encoding="utf-8"?>

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:background="#020202"

<ScrollView
android:background="#330C0C0C"
android:layout_width="match_parent"
android:layout_height="match_parent">



fragment我直接将界面的图片textview是的,您可以使用list view放置,然后重写adapter您可以单击相应的图片跳转到您指定的界面!

ps:我只放置了一个游戏分类界面fragment,其余都一样,如果有任何代码问题错误,请雅正,我会及时纠正,谢谢,让我们一起学习,一起进步!

版权声明

所有资源都来源于爬虫采集,如有侵权请联系我们,我们将立即删除

热门