Android开发工程师文集-Fragment,适配器,轮播图,
Android开发工程师文集-Fragment,适配器,轮播图,ScrollView,Gallery 图片浏览器,Android常用布局样式
Fragment
FragmentManager fragmentManager = getFragmentManager();
FragmentTransaction beginTransaction = fragmentManager.beginTransaction();
事务
add(),remove(),replace(),commit()
MyFragment2 fragment2=new MyFragment2();
FragmentManager fragmentManager = getFragmentManager();
FragmentTransaction beginTransaction = fragmentManager.beginTransaction();
beginTransaction.add(R.id.frame,fragement2);
beginTransaction.commit();
fragment的生命周期
Created
onAttach()->onCreate()->onCreateView()->onActivityCreated()
Started
onStart()
Resumed
onResume()
Paused
onPause()
Stopped
onStop()
Destroyed
onDestroyView()->onDestroy()->onDetach()
//当Fragment被添加到Activity的时候回调这个方法,并且只调用一次
@Override
public void onAttach(Activity activity){
super.onAttach(activity);
}
//创建Fragment时会回调,只调用一次
@Override
public void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
}
//每次创建回调
@Override
public View onCreateView(## ##){
}
//当Fragment所在的Activity启动完成后调用
@Override
public void onActivityCreated(Bundle savedInstanceState){
super.onActivityCreated(savedInstanceState);
}
//启动onStart()
@Override
public void onStart(){
super.onStart();
}
//onResume
@Override
public void onResume(){
super.onResume();
}
//onPause 暂停
@Override
public void onPause(){
super.onPause();
}
//onStop 停止
@Override
public void onStop(){
super.onStop();
}
//last
//onDestroyView
@Override
public void onDestroyView(){
super.onDestroyView();
}
//销毁
//onDestroy
@Override
public void onDestroy(){
super.onDestroy();
}
重点
删除Fragment
//onDetach
@Override
public void onDetach(){
super.onDetach();
}
启动Fragment
onAttach()->onCreate()->onCreateView()->onActivityCreated()->onStart()->onResume()
暂停Fragment - //暂停和停止的方法
onPause()->onStop()
重新看到Fragment
onStart()->onResume()
Fragment1 -> Fragment2
Fragment1: onPause()->onStop()->onDestroyView->onDestroy()->onDetach()
Fragment2: onAttach()->onCreate()->onCreateView()->onActivityCreated()->onStart()->onResume()
Fragment2回到主界面时
onPause()->onStop()
又重新看见
//Fragment2回到界面
onStart()->onResume()
Fragment2 退出
onPause()->onStop()->onDestroyView()->onDestroy()->onDetach()
Fragment与Activity
- Fragment是通过调用getActivity()的方法获取Activity的;
- Activity是通过调用FragmentManager的findFramentById()的,也可以用findFragmentByTag()方法获取Fragment片段的。
重点
public class MyFragment extends Fragment{
@Override
public View onCreateView(LayoutInflater inflater,ViewGrop container,Bundle savedInstanceState){
return super.onCreateView(inflater,container,savedInstanceState);
//View view = inflater.inflate(R.layout.fragment, container, false);
//TextView tv = view.findViewById(R.id.textview);
//return view;
}
}
一个Activity调用Fragment的界面显示数据内容
String text = editext.getText().toString();
MyFragment fragment = new MyFragment();
Bundle bundle = new Bundle();
bundle.putString("name",text);
fragment.setArguments(bundle);
FragmentManager fragmentManager = getFragmentManager();
FragmentTransaction beginTransaction = fragmentManager.beginTransaction();
//beginTransaction.add(containerViewId,fragment,tag)
beginTransaction.add(R.id.layout.textview,fragment,"fragment");
beginTransaction.commit();
在Fragement中设置
public class MyFragment extends Fragment{
@0verride
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState){
View view = inflater.infate(R.layout.fragement,container,false);
TextView tv=view.findViewById(R.id.textview);
String text = getArguments().get("name")+"";
tv.setText(text);
}
}
Fragment
private String name = "v";
public interface Myvoid
{
public void name(String name);
}
Activity
implements Myvoid
fragment
@Override
public void onAttach(Activity activity){
myvoid = activity;
super.onAttach(activity);
}
回到Activity
覆盖方法
另类
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="@android:color/white"
android:orientation="vertical">
</LinearLayout>
Fragment
public class MyinfoFragment extends Fragment implements View.OnClickListener{
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
return inflater.inflate(R.layout.fragment_myinfo, null);
}
@Override
public void onViewCreated(View view, Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
}
@Override
public void onClick(View v) {
}
}
MainActivity
public class MainActivity extends FragmentActivity implements View.OnClickListener{
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initView();
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
setMain();
}
@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
}
private void setMain() {
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
}
private void setSelectStatus(int index) {
switch (index){
}
}
private void initView() {
}
@Override
public void onClick(View v) {
switch (v.getId()){
}
}
}
继续
FragmentManager fragmentManager = getFragmentManager();
Fragment findFragmentById = fragmentManager.findFragmentById(R.id.fragment);
ViewPager左右滑动
<android.support.v4.view.ViewPager
android:id="@+id/viewpager"
android:...
android:...>
</android.support.v4.view.ViewPager>
ViewPager是一种容器,可以盛装两种类型的对象:一种是View对象; 也可以放置Fragment。
getLayoutInflater().form(this);
getLayoutInflater().form(this).inflate(resource,root);
配置Adapter:
准备数据源 -> 准备适配器 ->加载适配器
List<View>
PagerAdapter
ViewPager的声明:<android.support.v4.view.ViewPager>
是为了解决版本兼容性问题
过程
我们创建好布局文件,然后在MainActivity中添加,进行实例
准备数据源
private List<View> viewList;
viewList = new ArrayList<View>();
显示
View view = View.inflate(this, R.layout.view, null);
添加
viewList.add(view);
准备适配器
@Override
public Object instantiateItem(ViewGroup container, int position){
}
搭建构造方法
public MyPagerAdapter(List<View> viewList){
this.viewList=viewList;
}
container.addview();
return viewList.get(position);
container.removeView(viweList.get(position));
Adapter
创建Adapter
PagerAdapter pagerAdapter = new PagerAdapter(viewList);
setAdapter(pagerAdapter);
对象
PagerTabStrip和PagerTitleStrip
一个View显示ViewPager,然后在ViewPager中添加PagerTabStrip
<android.support.v4.view.ViewPager
...
<android.support.v4.view.PagerTabStrip
...>
</android.support.v4.view.PagerTabStrip>
</android.support.v4.view.ViewPager>
然后加入标题,private List<String> titleList;
同理,ViewPager添加题目
titleList = new ArrayList<String>();
titleList.add("第一题");
titleList.add("第二题");
titleList.add("第三题");
titleList.add("第五题");
在加入PagerAdapter中,创建Adapter下创建,MyPagerAdapter.java
MyPagerAdapter.java
public class MyPagerAdapter extends PagerAdapter{
private List<View> viewList;
private List<String> titleList;
public MyPagerAdapter(List<View> viewList, List<String> titleList){
this.viewList=viewList;
this.titleList=titleList;
}
}
加入题目数量第几题
显示标题
getPageTitle(),显示所对应的标题
@Override
public CharSequence getPageTitle(int position){
//return super.getPageTitle(position);
return titleList.get(position);
}
PagerTabStrip的属性
背景
setBackgroundColor(Color.RED);
文本
setTextColor();
下划线
setDrawFullUnderline(false);
分割线
setTabIndicatorColor();
PagerTitleStrip 与 PagerTabStrip 类似
适配器
FragmentPagerAdapter
import android.os.Bundle;
import android.support.v4.app.Fragment;
public class ExercisesFragment extends Fragment {
private ListView lvList;//来源fragment
private ListItemAdapter adapter; //适配器
private List<> ebl; //列表集合
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
return inflater.inflate(R.layout.fragment, null);
}
@Override
public void onViewCreated(View view, Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
initData();
lvList = view.findViewById(R.id.lv_list);
adapter = new ListItemAdapter(getActivity());
adapter.setData(ebl);
lvList.setAdapter(adapter);
}
private void initData(){
ebl = new ArrayList<Bean>();
for (int i=0;i<10;i++){
Bean bean = new Bean();
bean.id=(i+1);
switch (i){
case 0:
case 9:
default:
break;
}
}
}
}
设置MyFragment继承Fragment
public class MyFragment extends Fragment{
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState){
return inflater.infate(R.layout.view, container, false);
//return inflater.infate(R.layout.view, null);
}
}
点击跳转Fragment
public void onClick(View v) {
switch (v.getId()){
case R.id.bottom1:
//getSupportFragmentManager().beginTransaction().add(R.id.main_body,new CFragment()).commit();
break;
case R.id.bottom2:
//getSupportFragmentManager().beginTransaction().add(R.id.main_body,new EFragment()).commit();
break;
case R.id.bottom3:
//getSupportFragmentManager().beginTransaction().add(R.id.main_body,new MFragment()).commit();
break;
}
}
实例
private List<Fragment> fragmentList;
Fragment作为数据源
添加Fragment
fragmentList = new ArrayList<Fragment>();
fragmentList.add(new Fragment());
添加创建的Fragment
//fragmentList.add(new Fragment%());
导入的包
import android.support.v4.app.Fragment;
配置数据适配器
如果用FragmentPagerAdapter,一次性加载
同理
public class MyFragment extends FragmentPagerAdapter{
private List<Fragment> fragmentList;
private List<String> titleList;
public MyFragment(FragmentManager fm,List<Fragment> fragmentList,List<String> titleList){
super(fm)
this.fragmentList=fragmentList;
this.titleList=titleList;
}
}
MainActivity.java
public class MainActivity extends FragmentActivity implements View.OnClickListener{
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
private void setMain() {
}
private void setSelectStatus(int index) {
switch (index){
}
}
private void initView() {
}
@Override
public void onClick(View v) {
switch (v.getId()){
//getSupportFragmentManager().beginTransaction().add(R.id.main_body,new CourseFragment()).commit();
//getSupportFragmentManager().beginTransaction().add(R.id.main_body,new ExercisesFragment()).commit();
//getSupportFragmentManager().beginTransaction().add(R.id.main_body,new MyinfoFragment()).commit();
}
}
}
FragmentPagerAdapter adapter=new FragmentPagerAdapter(getSupportFragmentManager());
轮播图
ViewFlipper
自动播放
静态的方法导入:
<ViewFlipper
android:id="@+id/flipper"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:flipInterval="4000">
<ImageView
android:scaleType="matrix"
android:id="@+id/ex2"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
<ImageView
android:scaleType="matrix"
android:id="@+id/ex3"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
<ImageView
android:scaleType="matrix"
android:id="@+id/ex4"
android:android:layout_width="match_parent"
android:layout_height="wrap_content"/>
</ViewFlipper>
动态导入:
flipper = findViewById(R.id.flipper);
flipper.addView(getImageView(R.drawable.picture));
自己定义动态效果,一般用anim的文件夹表示:
然后创建布局界面:
创建轮播图
<ViewFlipper
android:id="@+id/flipper"
android:layout_width="match_parent"
android:layout_height="match_parent">
</ViewFlipper>
MainActivity.java
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
}
}
声明变量
private ViewFlipper flipper;
进行初始化
flipper = findViewById(R.id.flipper);
动态导入:addView(getImageView(R.drawable.picture));
轮播图有多张图片,创建数组,分别导入
获取图片
private ImageView getImageView(int imageId){
ImageView image = new ImageView(this);
image.setImageResource(imageId);
return image;
}
for(int i=0; i<imageId.length;i++){
flipper.addView(getImageView( 数组[i] ));
}
手势滑动的方法
@Override
public boolean onTouchEvent(MotionEvent event){
return super.onTouchEvent(event);
}
自动播放效果:
flipper.setFlipInterval(3000);
flipper.startFlipping();
ScrollView
HorizontalScrollView,ScrollView
滚动视图
不能滚动,因为布局不是ScrollVeiw,而是<TextView>
tv.setText(getResources().getString(R.string.content));
如果是这样就可以
<ScrollView
android:layout_width="match_parent"
android:layout_height="wrap_content"
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
</ScrollView>
setOnTouchListener的方法
scroll.setOnTouchListener(new OnTouchListener){
}
记得 scrollBy 和 scrollTo 的区别
Gallery 图片浏览器
Gallery像一个走廊,ImageSwitcher像幻灯片
显示图片,就要对图片进行封装,声明数组
布局
<Gallery
android:...
/>
老样子
图片id,private int[] res 定义数组
我们学过的适配器,都是继承BaseAdapter
ArrayAdapter<String> adapter;
SimpleAdapter adapter;
继承BaseAdapter中必须要实现的方法
-
public int getCount()
-
public long getItemId(int position)
-
public Object getItem(int position)
-
public View gtView(int position, View convertView, ViewGroup parent)
如果自己定义一个适配器,定义后,实现BaseAdapter中的方法,
然后创建构造函数
private int[] images;
public ImageAdapter(int []images)
{
this.images = images;
}
说明
获取视图
private Context context;
public ImageAdapter(int[] images, Context context){
this.images = images;
this.context = context;
}
public View gtView(int position, View convertView, ViewGroup parent)
{
//ImageView image=new ImageView(this);
ImageView image = new ImageView(context);
image.setBackgroundResource(数组[position]);
//设置图片大小
image.setLayoutParams(new Gallery.LayoutParams(400,300));
return image;
}
加载适配器 MainActivity.java
声明适配器: private ImageAdapter adapter;
初始化: adapter = new ImageAdapter(数组的变量名,上下文(this));
gallery.setAdapter(adapter);
无限制浏览
return Interger.MAX_VALUE;
监听器
setOnItemSelectedListener(this);
同样<ImageSwitcher>也要布局
声明 private ImageSwitcher is;
进行初始化 is = findViewById(R.id.is);
监听器:gallery.setOnItemSelectedListener(this);
现实的接口:implements OnItemSelectedListener,ViewFactory
public View makeView(){
}
is.setFactory(this);
public View makeView(){
ImageView image = new ImageView(this);
return image;
}
SeekBar 拖动进度条
setOnSeekBarChangeListener(this);
<SeekBar
style="@android:style/Widget.SeekBar"
/>
<selecter xmlns:android="http...."
<item
android:android:state_pressed="true"
android:state_window_focused="true"
andeoid:drawable=""/>
Android常用布局样式
线性布局
LinearLayout
相对布局
RelativeLayout
表格布局
TableLayout
可以使用GridView替代
绝对布局
AbsoluteLayout
帧布局(主要布局叠加)
FrameLayout
多用LinearLayout和RelativeLayout
少用AbsoluteLayout
定位:分享 Android&Java 知识点