Fragmentを使って簡単に画像閲覧を実現

:activity_main3.xml: メイン インターフェイスは、画像カテゴリのレイアウトを編集します

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"

    android:orientation="vertical">
<HorizontalScrollView
    android:layout_width="match_parent"
    android:layout_height="wrap_content"

    >

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="#f7f7f7"
        android:gravity="center"
        android:orientation="horizontal"
        >
        <LinearLayout
            android:id="@+id/dongman_layout"
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:layout_centerVertical="true"
            android:orientation="vertical"
            android:padding="3dp">

            <TextView
                android:layout_marginLeft="5dp"
                android:id="@+id/dongman"
                android:layout_width="50dp"
                android:layout_height="30dp"
                android:layout_gravity="center_horizontal"
                android:gravity="top"
                android:text="动漫"
                android:textColor="#82858b"
                android:textSize="20dp" />

        </LinearLayout>

        <LinearLayout
            android:id="@+id/fenjing_layout"
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:layout_centerVertical="true"
            android:orientation="vertical"
            android:padding="3dp">

            <TextView
                android:layout_marginLeft="5dp"
                android:id="@+id/fenjing"
                android:layout_width="50dp"
                android:layout_height="30dp"
                android:layout_gravity="center_horizontal"
                android:gravity="top"
                android:text="风景"
                android:textColor="#82858b"
                android:textSize="20dp" />
        </LinearLayout>

        <LinearLayout
            android:id="@+id/meinu_layout"
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:layout_centerVertical="true"
            android:orientation="vertical"
            android:padding="3dp">

            <TextView
                android:layout_marginLeft="5dp"
                android:id="@+id/meinu"
                android:layout_width="50dp"
                android:layout_height="30dp"
                android:layout_gravity="center_horizontal"
                android:gravity="top"
                android:text="美女"
                android:textColor="#82858b"
                android:textSize="20dp" />
        </LinearLayout>
        <LinearLayout
            android:id="@+id/kehuan_layout"
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:layout_centerVertical="true"
            android:orientation="vertical"
            android:padding="3dp">
            <TextView
                android:layout_marginLeft="5dp"
                android:id="@+id/kehuan"
                android:layout_width="50dp"
                android:layout_height="30dp"
                android:layout_gravity="center_horizontal"
                android:gravity="top"
                android:text="科幻"
                android:textColor="#82858b"
                android:textSize="20dp" />
        </LinearLayout>
        <LinearLayout
            android:id="@+id/youxi_layout"
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:orientation="vertical"
            android:padding="3dp"
          >
            <TextView
                android:layout_marginLeft="5dp"
                android:id="@+id/youxi"
                android:layout_width="50dp"
                android:layout_height="30dp"
                android:layout_gravity="center_horizontal"
                android:gravity="top"
                android:text="游戏"
                android:textColor="#82858b"
                android:textSize="20dp" />

        </LinearLayout>



        <LinearLayout
            android:id="@+id/saiche_layout"
            android:layout_width="wrap_content"
            android:layout_height="match_parent"

            android:orientation="vertical"
            android:padding="3dp"

            >

            <TextView
                android:layout_marginLeft="5dp"
                android:id="@+id/saiche"
                android:layout_width="50dp"
                android:layout_height="30dp"
                android:layout_gravity="center_horizontal"
                android:gravity="top"
                android:text="动物"
                android:textColor="#82858b"
                android:textSize="20dp" />

        </LinearLayout>

        <LinearLayout
            android:id="@+id/xingkong_layout"
            android:layout_width="wrap_content"
            android:layout_height="match_parent"

            android:orientation="vertical"
            android:padding="3dp"
            >
            <TextView
                android:layout_marginLeft="5dp"
                android:id="@+id/xingkong"
                android:layout_width="50dp"
                android:layout_height="30dp"
                android:layout_gravity="center_horizontal"
                android:gravity="top"
                android:text="星空"
                android:textColor="#82858b"
                android:textSize="20dp" />

        </LinearLayout>

        <LinearLayout
            android:id="@+id/meishi_layout"
            android:layout_width="wrap_content"
            android:layout_height="match_parent"

            android:orientation="vertical"
            android:padding="3dp"
            >

            <TextView
                android:layout_marginLeft="5dp"
                android:id="@+id/meishi"
                android:layout_width="50dp"
                android:layout_height="30dp"
                android:layout_gravity="center_horizontal"
                android:gravity="top"
                android:text="创意"
                android:textColor="#82858b"
                android:textSize="20dp" />

        </LinearLayout>

        <LinearLayout
            android:id="@+id/mingxing_layout"
            android:layout_width="wrap_content"
            android:layout_height="match_parent"

            android:orientation="vertical"
            android:padding="3dp"
            >

            <TextView
                android:layout_marginLeft="5dp"
                android:id="@+id/mingxing"
                android:layout_width="50dp"
                android:layout_height="30dp"
                android:layout_gravity="center_horizontal"
                android:gravity="top"
                android:text="明星"
                android:textColor="#82858b"
                android:textSize="20dp" />

        </LinearLayout>
    </LinearLayout>
</HorizontalScrollView>
    <FrameLayout
        android:id="@+id/fragment"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
      >

    </FrameLayout>

</LinearLayout>

Mainl3Activity: 分類のコントロール ID をクリックして、フラグメント インターフェイスを呼び出します。

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">
        <!-- TODO: 空白のフラグメント レイアウトを更新 -->
        <LinearLayout
            android:layout_width="match_parent"
            Android:layout_height="match_parent"
            Android:向き="垂直"
            android:background="#330C0C0C"
            >
            <TextView
                android:layout_marginTop="20dp"
                android:layout_width="match_parent"
                android:layout_height="300dp"
                android:background="@drawable/yx1"
                />
            <TextView
                android:layout_marginTop="20dp"
                android:layout_width=" match_parent"
                android:layout_height="300dp"
                android:background="@drawable/yx2"
                />
            <TextView
                android:layout_marginTop="20dp"
                android:layout_width="match_parent"
                android:layout_height="300dp"
                android:background="@drawable/yx3"
                />
            <TextView
                android:layout_marginTop="20dp"
                android:layout_width="match_parent"
                android:layout_height="300dp"
                android:background="@drawable/ yx4"
                />
            <TextView
                android:layout_marginTop="20dp"
                android:layout_width="match_parent"
                android:layout_height="300dp"
                android:background="@drawable/yx5"
                />

        </LinearLayout>
    </ScrollView>
</FrameLayout>

 

 

フラグメント インターフェイスの画像を textview に直接配置します。リスト ビューを使用して配置し、アダプタを書き換えて、対応する画像をクリックして指定したインターフェイスにジャンプすることを実現できます。

 ps: ゲーム分類インターフェースの断片を配置しただけです。残りは同じです。コードにエラーがある場合は、修正してください。時間内に修正します。ありがとうございます。一緒に学び、一緒に進歩しましょう。 !

Supongo que te gusta

Origin blog.csdn.net/Abtxr/article/details/125074372
Recomendado
Clasificación