Android基础入门教程 2015年最新Android基础入门教程目录 背景相关与系统架构分析 开发环境搭建 使用Eclipse + ADT + SDK开发Android APP 使用Android Studio开发Android APP SDK更新不了问题解决 Genymotion模拟器安装 Git使用教程之本地仓库的基本操作 Git之使用GitHub搭建远程仓库 (九妹)图片怎么玩 界面原型设计 工程相关解析(各种文件,资源访问) Android程序签名打包 反编译APK获取代码&资源 反编译APK获取代码&资源 View与ViewGroup的概念 LinearLayout(线性布局) RelativeLayout(相对布局) TableLayout(表格布局) FrameLayout(帧布局) GridLayout(网格布局) AbsoluteLayout(绝对布局) TextView(文本框)详解 EditText(输入框)详解 Button(按钮)与ImageButton(图像按钮) ImageView(图像视图) RadioButton(单选按钮)&Checkbox(复选框) 开关按钮ToggleButton和开关Switch ProgressBar(进度条) SeekBar(拖动条) RatingBar(星级评分条) ScrollView(滚动条) Date & Time组件(上) Date & Time组件(下) Adapter基础讲解 ListView简单实用 BaseAdapter优化 ListView的焦点问题 ListView之checkbox错位问题解决 ListView的数据更新问题 构建一个可复用的自定义BaseAdapter ListView Item多布局的实现 GridView(网格视图)的基本使用 Spinner(列表选项框)的基本使用 AutoCompleteTextView(自动完成文本框)的基本使用 ExpandableListView(可折叠列表)的基本使用 ViewFlipper(翻转视图)的基本使用 Toast(吐司)的基本使用 Notification(状态栏通知)详解 AlertDialog(对话框)详解 其他几种常用对话框基本使用 PopupWindow(悬浮框)的基本使用 菜单(Menu) ViewPager的简单使用 DrawerLayout(官方侧滑菜单)的简单使用 基于监听的事件处理机制 基于回调的事件处理机制 Handler消息传递机制浅析 TouchListener PK OnTouchEvent + 多点触碰 监听EditText的内容变化 响应系统设置的事件(Configuration类) AnsyncTask异步任务 Gestures(手势) Activity初学乍练 Activity初窥门径 Activity登堂入室 Service初涉 Service进阶 Service精通 BroadcastReceiver牛刀小试 BroadcastReceiver庖丁解牛 ContentProvider初探 ContentProvider再探——Document Provider Intent的基本使用 Intent之复杂数据的传递 Fragment基本概述 Fragment实例精讲——底部导航栏的实现(方法1) Fragment实例精讲——底部导航栏的实现(方法2) Fragment实例精讲——底部导航栏的实现(方法3) Fragment实例精讲——底部导航栏+ViewPager滑动切换页面 Fragment实例精讲——新闻(购物)类App列表Fragment的简单实现 数据存储与访问之——文件存储读写 数据存储与访问之——SharedPreferences保存用户偏好参数 数据存储与访问之——初见SQLite数据库 数据存储与访问之——又见SQLite数据库 Android网络编程要学的东西与Http协议学习 Android Http请求头与响应头的学习 Android HTTP请求方式:HttpURLConnection Android HTTP请求方式:HttpClient Android XML数据解析 Android JSON数据解析 Android 文件上传 Android 文件下载(1) Android 文件下载(2) Android 调用 WebService WebView(网页视图)基本用法 WebView和JavaScrip交互基础 Android 4.4后WebView的一些注意事项 WebView文件下载 WebView缓存问题 WebView处理网页返回的错误码信息 Socket学习网络基础准备 基于TCP协议的Socket通信(1) 分类 Android 基础入门教程 基于TCP协议的Socket通信(2) 基于UDP协议的Socket通信 Android中的13种Drawable小结 Part 1 Android中的13种Drawable小结 Part 2 Android中的13种Drawable小结 Part 3 Bitmap(位图)全解析 Part 1 Bitmap引起的OOM问题 三个绘图工具类详解 绘图类实战示例 Paint API之—— MaskFilter(面具) Paint API之—— Xfermode与PorterDuff详解(一) Paint API之—— Xfermode与PorterDuff详解(二) Paint API之—— Xfermode与PorterDuff详解(三) Paint API之—— Xfermode与PorterDuff详解(四) Paint API之—— Xfermode与PorterDuff详解(五) Paint API之—— ColorFilter(颜色过滤器)(1/3) Paint API之—— ColorFilter(颜色过滤器)(2-3) Paint API之—— ColorFilter(颜色过滤器)(3-3) Paint API之—— PathEffect(路径效果) Paint API之—— Shader(图像渲染) Paint几个枚举/常量值以及ShadowLayer阴影效果 Paint API之——Typeface(字型) Canvas API详解(Part 1) Canvas API详解(Part 2)剪切方法合集 Canvas API详解(Part 3)Matrix和drawBitmapMash Android动画合集之帧动画 Android动画合集之补间动画 Android动画合集之属性动画-初见 Android动画合集之属性动画-又见 使用SoundPool播放音效(Duang~) MediaPlayer播放音频与视频 使用Camera拍照 使用MediaRecord录音 TelephonyManager(电话管理器) SmsManager(短信管理器) AudioManager(音频管理器) Vibrator(振动器) AlarmManager(闹钟服务) PowerManager(电源服务) WindowManager(窗口管理服务) LayoutInflater(布局服务) WallpaperManager(壁纸管理器) 传感器专题(1)——相关介绍 传感器专题(2)——方向传感器 传感器专题(3)——加速度/陀螺仪传感器 传感器专题(4)——其他传感器了解 Android GPS初涉 《2015最新Android基础入门教程》完结散花~

Android 开发手册

浏览86756
更新时间2022-04-15

本节引言:

相信大家对点击列表,然后进入详情这种App并不陌生吧,在购物类App和新闻类App中最为常见: 下面我们简单来讲一下流程逻辑!


逻辑流程讲解:

1.gif

刚好公司测试妹子的测试机上装了楚楚街9块9的APP,呵呵,直接就照这个来研究吧:

嘿嘿,市面上很多APP都是这种样子的,而这个可以用我们学到的Fragment来实现: 可能gif动画看不清,笔者用界面原型工具画个大概吧:

2.png

大概就这样,中间区域是一个布局容器,一般是FrameLayout,然后我们将一个Fragment replace 到这个容器中或者add也行,而这个Fragment中有一个listview,当我们点击这个ListView中的一项, 中间容器中的Fragment就会被replace成对应详细信息的Fragment所替代,如果我们只是replace的话, 就不会保存第一个Fragment的状态,用户又得从头开始浏览,这肯定是很不方便的,这里我们可以 通过Fragment栈的addtobackStack和popbackstack来解决这个问题!当replace的同时,我们将被替换 的Fragment添加到stack中,当用户点击回退按钮时,调用popbackstack弹出栈,具体实现见下述代码 示例!


2.代码示例:简单新闻类APP列表和内容切换的实现

运行效果图:

4.gif

实现代码:

Step 1:先把两个Fragment以及Activity的布局实现了

fg_newlist.xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="../style/images/white"
    android:orientation="horizontal">

    <ListView
        android:id="@+id/list_news"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />

</LinearLayout>

fg_context.xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <TextView
        android:id="@+id/txt_content"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:gravity="center"
        android:textColor="../style/images/blue"
        android:textSize="20sp" />

</LinearLayout>

activity_main.xml:

<RelativeLayout 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"
    tools:context=".MainActivity">

    <TextView
        android:id="@+id/txt_title"
        android:layout_width="match_parent"
        android:layout_height="56dp"
        android:background="../style/images/blue"
        android:textColor="../style/images/white"
        android:text="新闻列表"
        android:textSize="20sp"
        android:textStyle="bold"
        android:gravity="center"/>

    <FrameLayout
        android:id="@+id/fl_content"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_below="@id/txt_title"/>

</RelativeLayout>

Step 2:实现我们的业务Bean类和自定义BaseAdapter类:

Data.java:

/**
 * Created by Jay on 2015/9/6 0006.
 */
public class Data {

    private String new_title;
    private String new_content;

    public Data(){}

    public Data(String new_title, String new_content) {
        this.new_title = new_title;
        this.new_content = new_content;
    }

    public String getNew_title() {
        return new_title;
    }

    public String getNew_content() {
        return new_content;
    }

    public void setNew_title(String new_title) {
        this.new_title = new_title;
    }

    public void setNew_content(String new_content) {
        this.new_content = new_content;
    }
}

MyAdapter.java:

/**
 * Created by Jay on 2015/9/6 0006.
 */
public class MyAdapter extends BaseAdapter{

    private List<Data> mData;
    private Context mContext;

    public MyAdapter(List<Data> mData, Context mContext) {
        this.mData = mData;
        this.mContext = mContext;
    }

    @Override
    public int getCount() {
        return mData.size();
    }

    @Override
    public Object getItem(int position) {
        return null;
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder viewHolder;
        if(convertView == null){
            convertView = LayoutInflater.from(mContext).inflate(R.layout.list_item,parent,false);
            viewHolder = new ViewHolder();
            viewHolder.txt_item_title = (TextView) convertView.findViewById(R.id.txt_item_title);
            convertView.setTag(viewHolder);
        }else{
            viewHolder = (ViewHolder) convertView.getTag();
        }
        viewHolder.txt_item_title.setText(mData.get(position).getNew_title());
        return convertView;
    }

    private class ViewHolder{
        TextView txt_item_title;
    }

}

Step 3:MainActivity的实现

MainActivity.java:

public class MainActivity extends AppCompatActivity {

    private TextView txt_title;
    private FrameLayout fl_content;
    private Context mContext;
    private ArrayList<Data> datas = null;
    private FragmentManager fManager = null;
    private long exitTime = 0;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        mContext = MainActivity.this;
        fManager = getFragmentManager();
        bindViews();

        datas = new ArrayList<Data>();
        for (int i = 1; i <= 20; i++) {
            Data data = new Data("新闻标题" + i, i + "~新闻内容~~~~~~~~");
            datas.add(data);
        }
        NewListFragment nlFragment = new NewListFragment(fManager, datas);
        FragmentTransaction ft = fManager.beginTransaction();
        ft.replace(R.id.fl_content, nlFragment);
        ft.commit();
    }


    private void bindViews() {
        txt_title = (TextView) findViewById(R.id.txt_title);
        fl_content = (FrameLayout) findViewById(R.id.fl_content);
    }


    //点击回退键的处理:判断Fragment栈中是否有Fragment
    //没,双击退出程序,否则像是Toast提示
    //有,popbackstack弹出栈
    @Override
    public void onBackPressed() {
        if (fManager.getBackStackEntryCount() == 0) {
            if ((System.currentTimeMillis() - exitTime) > 2000) {
                Toast.makeText(getApplicationContext(), "再按一次退出程序",
                        Toast.LENGTH_SHORT).show();
                exitTime = System.currentTimeMillis();
            } else {
                super.onBackPressed();
            }
        } else {
            fManager.popBackStack();
            txt_title.setText("新闻列表");
        }
    }
}

Step 4:列表Fragment的实现:

NewListFragment.java:

package com.jay.fragmentdemo4;

import android.app.Fragment;
import android.app.FragmentManager;
import android.app.FragmentTransaction;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.TextView;

import java.util.ArrayList;

/**
 * Created by Jay on 2015/9/6 0006.
 */
public class NewListFragment extends Fragment implements AdapterView.OnItemClickListener {
    private FragmentManager fManager;
    private ArrayList<Data> datas;
    private ListView list_news;

    public NewListFragment(FragmentManager fManager, ArrayList<Data> datas) {
        this.fManager = fManager;
        this.datas = datas;
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fg_newlist, container, false);
        list_news = (ListView) view.findViewById(R.id.list_news);
        MyAdapter myAdapter = new MyAdapter(datas, getActivity());
        list_news.setAdapter(myAdapter);
        list_news.setOnItemClickListener(this);
        return view;
    }


    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        FragmentTransaction fTransaction = fManager.beginTransaction();
        NewContentFragment ncFragment = new NewContentFragment();
        Bundle bd = new Bundle();
        bd.putString("content", datas.get(position).getNew_content());
        ncFragment.setArguments(bd);
        //获取Activity的控件
        TextView txt_title = (TextView) getActivity().findViewById(R.id.txt_title);
        txt_title.setText(datas.get(position).getNew_content());
        //加上Fragment替换动画
        fTransaction.setCustomAnimations(R.anim.fragment_slide_left_enter, R.anim.fragment_slide_left_exit);
        fTransaction.replace(R.id.fl_content, ncFragment);
        //调用addToBackStack将Fragment添加到栈中
        fTransaction.addToBackStack(null);
        fTransaction.commit();
    }
}

Step 5:内容Fragment的实现:

NewContentFragment.java:

/**
 * Created by Jay on 2015/9/6 0006.
 */
public class NewContentFragment extends Fragment {

    NewContentFragment() {
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fg_content, container, false);
        TextView txt_content = (TextView) view.findViewById(R.id.txt_content);
        //getArgument获取传递过来的Bundle对象
        txt_content.setText(getArguments().getString("content"));
        return view;
    }

}

代码很简单,就不慢慢解释了~


3.代码下载

FragmentDemo5.zip:下载 FragmentDemo5.zip


本节小结:

因为时间的关系,并没有详细的去做过多的讲解,示例代码也很简单,方便各位初学者理解! 如果要用到实际项目中还需要对此进行一番修改~!好的,本节就到这里,谢谢~

相关视频

更多

免费

微信小程序--企业微网站
初级微信小程序--企业微网站

5.4万次学习

收藏

免费

微信小程序开发基础教程
初级微信小程序开发基础教程

6.8万次学习

收藏

免费

微信小程序基础API篇
高级微信小程序基础API篇

3.5万次学习

收藏

免费

微信小程序--微商城
高级微信小程序--微商城

3.9万次学习

收藏

精品课程

更多
前端入门_HTML5
前端入门_HTML5

共29课时

65万人学习

CSS视频教程-玉女心经版
CSS视频教程-玉女心经版

共25课时

41.3万人学习

JavaScript极速入门_玉女心经系列
JavaScript极速入门_玉女心经系列

共43课时

76.8万人学习

独孤九贱(1)_HTML5视频教程
独孤九贱(1)_HTML5视频教程

共25课时

64.2万人学习

独孤九贱(2)_CSS视频教程
独孤九贱(2)_CSS视频教程

共22课时

24.4万人学习

独孤九贱(3)_JavaScript视频教程
独孤九贱(3)_JavaScript视频教程

共28课时

36.3万人学习

独孤九贱(4)_PHP视频教程
独孤九贱(4)_PHP视频教程

共89课时

133.5万人学习

热门下载

更多
phpStudy 2018最新版
phpStudy 2018最新版

集成PHP7,16种组合,超全大合集

下载

VC9 32位
VC9 32位

VC9 32位 phpstudy集成安装环境运行库

下载

VC11 32位
VC11 32位

VC11 32位 phpstudy集成安装环境运行库​

下载

php程序员工具箱完整版
php程序员工具箱完整版

程序员工具箱 v1.0 php集成环境

下载

VC14 32位
VC14 32位

VC14 32位 phpstudy安装环境运行库

下载

SublimeText3汉化版
SublimeText3汉化版

中文版,非常好用

下载

Notepad++ Windows版
Notepad++ Windows版

Notepad++ 8.9.7 Windows版官方安装包,适合 Windows 用户进行代码编辑、文本处理、语法高亮和插件扩展配置。

下载