当前位置:网站首页>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 !
边栏推荐
- What has TCL done right to break through the technological strength of Chinese brand innovation?
- Troubleshooting of single chip microcomputer communication data delay
- B_ QuRT_ User_ Guide(18)
- Discussion on the concurrency security of a common set on players
- Artalk | how to build a domestic hyperfusion evolutionary base with minimum investment?
- canvas旋转绘图h5动画js特效
- /10个值得推荐的学习编程的网站 世界已经进入了互联网的时代。据最近发布的一篇《2016年互联网趋势》报告显示,中国已成为互联网市场的领导者,中国互联网用户的数量达到了6.68亿。可以预见,有
- 科技PRO实力测评:高端按摩椅市场综合PK,究竟谁才配得上机皇?
- 蓄力618 ,苏宁如何打下这场硬仗?
- Instructor add function_ Enable auto fill_ Instructor modification function
猜你喜欢

Computer vision (AI) interview

突破中国品牌创新技术实力,TCL做对了什么?

OPPO K9试水“捆绑销售”,消费者“赚了”还是“亏了”?

MySQL learning notes: JSON nested array query

PostgreSQL source code learning (17) -- mvcc ② - Introduction to snapshot and isolation level

TweenMax五彩小球弹跳动画

three. JS cool technology background H5 animation

【安全科普】挖矿技术,从一个理工男的爱情故事讲起

JS top icon menu click to switch background color JS special effect

Promise使用
随机推荐
Promise使用
ARM开发板方案与厂商分析
ArTalk | 如何用最小投入,构建国产超融合进化底座?
net::ERR_ FILE_ NOT_ Found error
Disk quota exceeded
three. JS cool technology background H5 animation
618将至!全渠道开售,高价低配的OPPO Reno6能赢吗?
RequestContextHolder
Difference between idea open and import project
B_ QuRT_ User_ Guide(16)
名不副实的雅迪高端品牌VFLY,为何“不高端”?
OpenGL第七章 基础光照
618 coming! Can oppo reno6, which is sold through all channels with high price and low configuration, win?
PostgreSQL source code learning (XX) -- fault recovery ① - transaction log format
【ELT.ZIP】OpenHarmony啃论文俱乐部——快速随机访问字符串压缩
Gd32 can sends no mailbox fault
SQL | some indicators of the game industry
Mazhiqiang: research progress and application of speech recognition technology -- RTC dev Meetup
【安全科普】挖矿技术,从一个理工男的爱情故事讲起
js点击太阳月亮切换白天黑夜js特效