当前位置:网站首页>Simple image browsing with fragment

Simple image browsing with fragment

2022-06-11 03:29:00 DY.memory

:activity_main3.xml: Main interface edit the layout of picture classification

<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=" Comic "
                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=" scenery "
                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=" beauty "
                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=" Science fiction "
                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=" game "
                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=" animal "
                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=" Starry sky "
                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=" originality "
                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=" star "
                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: Implement the control of click classification id Call out fragment Interface

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;


    /* Declare component variables */


    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;//  Used to deal with Fragment Conduct management 


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);// The request window does not title
        super.setContentView(R.layout.activity_main3);
        //  Initialize layout elements 
        initViews();
        fragmentManager = getFragmentManager();// Used to deal with Fragment Conduct management 
        //  Set the default display interface 
        setTabSelection(0);
    }

    /**
     *  Get the instance of each control you need , And set up the necessary click events for them 
     */

    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);








        // Handle click events 
        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);//  When you click wechat , Select the first 1 individual tab
                break;
            case R.id.fenjing_layout:
                setTabSelection(1);//  When you click on the address book , Select the first 2 individual tab
                break;
            case R.id.meinu_layout:
                setTabSelection(2);//  When you click discover , Select the first 3 individual tab
                break;
            case R.id.kehuan_layout:
                setTabSelection(3);//  When you click on me , Select the first 4 individual tab
                break;
            case R.id.youxi_layout:
                setTabSelection(4);//  When you click discover , Select the first 5 individual tab
                break;
            case R.id.saiche_layout:
                setTabSelection(5);//  When you click discover , Select the first 6 individual tab
                break;
            case R.id.xingkong_layout:
                setTabSelection(6);//  When you click discover , Select the first 7 individual tab
                break;
            case R.id.meishi_layout:
                setTabSelection(7);//  When you click discover , Select the first 8 individual tab
                break;
            case R.id.mingxing_layout:
                setTabSelection(8);//  When you click discover , Select the first 9 individual tab
                break;
            default:
                break;
        }

    }

    /**
     *  Based on the incoming index Parameter to set the selected tab page   Every tab The subscript corresponding to the page .0 Indicates wechat ,1 Indicates address book ,2 Indicates discovery ,3 Show me 
     */

    private void setTabSelection(int index) {
        clearSelection();//  Clear the last selection before each selection 
        FragmentTransaction transaction = fragmentManager.beginTransaction();//  To start a Fragment Business 
        hideFragments(transaction);//  Hide everything first Fragment, To prevent multiple Fragment The situation displayed on the interface 
        switch (index) {
            case 0:
                //  When you click on my wechat tab Change the picture and text color of the control 
                dongman.setTextColor(Color.parseColor("#0090ff"));// Change the font color 
                if (firstFragment == null) {
                    /* Get login activity The micro signal */
                    Intent intent = getIntent();
                    String number = intent.getStringExtra("weixin_number");
                    //  If FirstFragment It's empty , Create one and add it to the interface 
                    firstFragment = new WeixinFragment(number);
                    transaction.add(R.id.fragment, firstFragment);
                } else {
                    //  If FirstFragment Not empty , Then it will be displayed directly 
                    transaction.show(firstFragment);// Displayed actions 
                }
                break;
            //  The following and firstFragment similar 
            case 1:
               // contactImg.setImageResource(R.drawable.people);
                fenjing.setTextColor(Color.parseColor("#0090ff"));
                if (secondFragment == null) {
                    /* Get login activity The micro signal */
                    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();

    }

    /**
     *  Clear all selected states 
     */
    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"));


    }

    /**
     *  Will all Fragment Are set to hidden   Used to deal with Fragment Transactions that perform operations 
     */
    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);
        }
    }

    // Encapsulates a AlertDialog
    private void exitDialog() {
        Dialog dialog = new AlertDialog.Builder(this)
                .setTitle(" reminder ")
                .setMessage(" Are you sure you want to exit the program ?")
                .setPositiveButton(" Shut down the software ", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface arg0, int arg1) {
                        finish();
                    }
                })
                .setNegativeButton(" Cancel ", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface arg0, int arg1) {
                    }
                }).create();
        dialog.show();// Show dialog 
    }

    /**
     *  Return to the menu key to listen for events 
     */
    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK) {// If it's the back button 
            exitDialog();
        }
        return super.onKeyDown(keyCode, event);
    }

}

fargment_youxi.xml: Interface layout of game classification

<?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: Update blank fragment layout -->
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:orientation="vertical"
            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>

 

 

fragment I put the pictures of the interface directly textview Of , You can use list view To place , And then rewrite adapter You can click the corresponding image to jump to the interface you specify !

 ps: I only put a game category interface fragment, The rest are the same , If there is a problem or error in the code , Yazheng, please , I will correct it in time , thank you , Let's learn together , Common progress !

原网站

版权声明
本文为[DY.memory]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/162/202206110321032515.html