第二十八篇-Fragment静态用法
效果图:

首先,先大致布局成这个形状
看动画中,横看分为两个区域,所以整体是一个水平排列
设置外层LinearLayout的参数
android:orientation="horizontal"
在看左边,上面是一个文本,下面是一个list,成线性排列,右边是一个Fragement
所以布局方式为:

然后,可以看出左边和右边空间比例为1:3
左边设置android:layout_weight="3"
右边设置android:layout_weight="1"
此时基本形状已经出来了。

接下来设置Fragement,新建一个MyFragement.java的类。右键new--->java class
通过view添加一个布局文件进去
View view=inflater.inflate(R.layout.layout2,container,false);
layout2就是Fragement里面显示的页面,很简单,就一个textview加上一个button

textview就是content里面的内容,button是设置左边textview中要显示的值。
将数据存到数组中
String[] name={"人工智能","大数据","区块链","物联网","云计算","AR"};
String[] content={"人工智能babababababb....","大数据blablabla....",
"区块链。。。。","物联网....","云计算...","AR...."};
通过Adapter添加到listView中
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.layout);
listView=findViewById(R.id.listview1);
textView=findViewById(R.id.textView);
adapter=new ArrayAdapter<String>(MainActivity.this,R.layout.layout3,name);
listView.setAdapter(adapter);
manager=getSupportFragmentManager();//初始化
MyFragment myFragment= (MyFragment) manager.findFragmentById(R.id.fragement1);
final TextView textView1=myFragment.getView().findViewById(R.id.textView2);
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
textView1.setText(content[position]);
}
});
}
apapter使数据绑定到控件变得更加简单和灵活...用途为容器提供子视图,利用视图的数据和元数据来构建每个子视图。
有 arrayAdapter ,simpleCursorAdapter, cursorAdapter resourceCursorAdapter 如果需要自定义适配器 可以扩展抽象类BaseAdapter
然后通过listView.setAdapter(adapter);
将内容放入listview中显示,当点击name里面的内容是,position是记录点击的索引位置,这时候将textview1里面的内容设置为content对应position的数据,所以name和content要对应好。
最后将代码附上。
layout.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"
xmlns:tools="http://schemas.android.com/tools"
android:orientation="horizontal"
tools:context=".MainActivity"> <LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="3"
android:orientation="vertical"> <TextView
android:id="@+id/textView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="TextView" /> <ListView
android:id="@+id/listview1"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</LinearLayout> <fragment
android:id="@+id/fragement1"
android:name="com.example.aimee.fragementtest.MyFragment"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"> </fragment>
</LinearLayout>
layout2.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:gravity="center"
android:background="#ccffcc"
android:layout_height="match_parent"
android:orientation="vertical"> <TextView
android:id="@+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="TextView" /> <Button
android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button" />
</LinearLayout>
layout3.xml
<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:padding="28dp"
android:layout_height="match_parent"> </TextView>
MainActivity.java
package com.example.aimee.fragementtest; import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.TextView; public class MainActivity extends FragmentActivity {
ListView listView;
TextView textView;
FragmentManager manager;
ArrayAdapter<String>adapter;
String[] name={"人工智能","大数据","区块链","物联网","云计算","AR"};
String[] content={"人工智能babababababb....","大数据blablabla....",
"区块链。。。。","物联网....","云计算...","AR...."}; @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.layout);
listView=findViewById(R.id.listview1);
textView=findViewById(R.id.textView);
adapter=new ArrayAdapter<String>(MainActivity.this,R.layout.layout3,name);
listView.setAdapter(adapter);
manager=getSupportFragmentManager();//初始化
MyFragment myFragment= (MyFragment) manager.findFragmentById(R.id.fragement1);
final TextView textView1=myFragment.getView().findViewById(R.id.textView2);
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
textView1.setText(content[position]);
}
});
}
}
MyFragement.java
package com.example.aimee.fragementtest; import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast; public class MyFragment extends Fragment {
@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View view=inflater.inflate(R.layout.layout2,container,false); Button button=view.findViewById(R.id.button);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
TextView textView=getActivity().findViewById(R.id.textView);
textView.setText("来自");
Toast.makeText(getActivity(),"值已传完",Toast.LENGTH_LONG).show();
}
}); return view;
}
}
Ok。
第二十八篇-Fragment静态用法的更多相关文章
- Android UI开发第二十八篇——Fragment中使用左右滑动菜单
Fragment实现了Android UI的分片管理,尤其在平板开发中,好处多多.这一篇将借助Android UI开发第二十六篇——Fragment间的通信. Android UI开发第二十七篇——实 ...
- 第二十九篇-Fragment动态用法
效果图: 上节学习了静态添加Fragment的方法,这节学习动态添加方法. 主页面 layout.xml Fragment页面 layout2.xml 实现功能,当点击主页面的button时,将Fra ...
- Python之路(第二十八篇) 面向对象进阶:类的装饰器、元类
一.类的装饰器 类作为一个对象,也可以被装饰. 例子 def wrap(obj): print("装饰器-----") obj.x = 1 obj.y = 3 obj.z = 5 ...
- Android UI开发第二十六篇——Fragment间的通信
为了重用Fragment的UI组件,创建的每个Fragment都应该是自包含的.有它自己的布局和行为的模块化组件.一旦你定义了这些可重用的Fragment,你就可以把它们跟一个Activity关联,并 ...
- Python之路【第二十八篇】:django视图层、模块层
1.视图函数 文件在view_demo 一个视图函数简称视图,是一个简单的Python 函数,它接受Web请求并且返回Web响应.响应可以是一张网页的HTML内容,一个重定向,一个404错误,一个XM ...
- Python之路【第二十八篇】:生成器与迭代器
#!/usr/bin/env python # -*- coding:utf-8 -*- #只要函数的代码里面出现了yield关键字,这个函数就不再是一个普通的函数了,叫做生成器函数 #执行生成器函数 ...
- 第二十八篇:SOUI中自定义控件开发过程
在SOUI中已经提供了大部分常用的控件,但是内置控件不可能满足用户的所有要求,因此一个真实的应用少不得还要做一些自定义控件. 学习一个新东西,最简单的办法就是依葫芦画瓢.事实上在SOUI系统中内置控件 ...
- 第二十八篇、自定义的UITableViewCell上有图片需要显示,要求网络网络状态为WiFi时,显示图片高清图;网络状态为蜂窝移动网络时,显示图片缩略图
1)SDWebImage会自动帮助开发者缓存图片(包括内存缓存,沙盒缓存),所以我们需要设置用户在WiFi环境下下载的高清图,下次在蜂窝网络状态下打开应用也应显示高清图,而不是去下载缩略图. 2)许多 ...
- flask第二十八篇——HTML【1】table标签
请关注公众号:自动化测试实战 以下内容参考:http://www.w3school.com.cn/tags/tag_table.asp <!DOCTYPE html> <html l ...
随机推荐
- Visual Studio2012调试时无法命中断点
今天在调试代码的时候发现在Debug模式下无法命中断点,然后一步步去检查原因,最后发现是在项目-->属性-->生成-->高级-->调试信息被设置为None,然后在选项中将其选择 ...
- python设计模式第二十二天【备忘录模式】
1.应用场景 (1)能保存对象的状态,并能够恢复到之前的状态 2.代码实现 #!/usr/bin/env python #! _*_ coding:UTF-8 _*_ class Originator ...
- Vue之变量、数据绑定、事件绑定使用举例
vue1.html <!DOCTYPE html> <html lang="en" xmlns:v-bind="http://www.w3.org/19 ...
- mybatis:数据持久层框架
mybatis是一个持久层的框架,是Apache下的顶级项目. mybatis托管到goolecode下,再后来托管到GitHub下:https://github.com/mybatis/mybati ...
- AtCoder Beginner Contest 122 解题报告
手速选手成功混进rated only里面的前30名,但是总排名就到110+了... A - Double Helix #include <bits/stdc++.h> #define ll ...
- Nginx 减少磁盘读写次数
L:133
- Qt QLabel的使用
QLabel类主要用来文本和图像的显示,没有提供用户交互功能.QLabel对象的视觉外观可以由用户自定义配置. 它还可以为另外一个可获得焦点的控件作为焦点助力器. QLabel可以显示下列的所有类型: ...
- github-share报错无法读取远程仓库
报错:github Could not read from remote repository 1.github创建仓库成功,而push报告此错误 2.考虑远程仓库名与本地项目名/文件夹名不匹配 3. ...
- 【AGC006C】Rabbit Exercise 置换
题目描述 有\(n\)只兔子站在数轴上.为了方便,将这些兔子标号为\(1\ldots n\).第\(i\)只兔子的初始位置为\(a_i\). 现在这些兔子会按照下面的规则做若干套体操.每一套体操由\( ...
- Educational Codeforces Round 58 A,B,C,D,E,G
A. Minimum Integer 链接:http://codeforces.com/contest/1101/problem/A 代码: #include<bits/stdc++.h> ...