当前位置:   article > 正文

Android开发工程师文集-Fragment,适配器,轮播图,ScrollView,Gallery 图片浏览器,Android常用布局样式_android 开发 scrollview 浏览选择图片

android 开发 scrollview 浏览选择图片

Android开发工程师文集-Fragment,适配器,轮播图,ScrollView,Gallery 图片浏览器,Android常用布局样式

Fragment

  1. FragmentManager fragmentManager = getFragmentManager();
  2. FragmentTransaction beginTransaction = fragmentManager.beginTransaction();

事务

add(),remove(),replace(),commit()

  1. MyFragment2 fragment2=new MyFragment2();
  2. FragmentManager fragmentManager = getFragmentManager();
  3. FragmentTransaction beginTransaction = fragmentManager.beginTransaction();
  4. beginTransaction.add(R.id.frame,fragement2);
  5. beginTransaction.commit();

fragment的生命周期

Created

onAttach()->onCreate()->onCreateView()->onActivityCreated()

Started

onStart()

Resumed

onResume()

Paused

onPause()

Stopped

onStop()

Destroyed

onDestroyView()->onDestroy()->onDetach()
  1. //当Fragment被添加到Activity的时候回调这个方法,并且只调用一次
  2. @Override
  3. public void onAttach(Activity activity){
  4. super.onAttach(activity);
  5. }
  1. //创建Fragment时会回调,只调用一次
  2. @Override
  3. public void onCreate(Bundle savedInstanceState){
  4. super.onCreate(savedInstanceState);
  5. }
  1. //每次创建回调
  2. @Override
  3. public View onCreateView(## ##){
  4. }
  1. //当Fragment所在的Activity启动完成后调用
  2. @Override
  3. public void onActivityCreated(Bundle savedInstanceState){
  4. super.onActivityCreated(savedInstanceState);
  5. }
  1. //启动onStart()
  2. @Override
  3. public void onStart(){
  4. super.onStart();
  5. }
  1. //onResume
  2. @Override
  3. public void onResume(){
  4. super.onResume();
  5. }
  1. //onPause 暂停
  2. @Override
  3. public void onPause(){
  4. super.onPause();
  5. }
  1. //onStop 停止
  2. @Override
  3. public void onStop(){
  4. super.onStop();
  5. }

//last

  1. //onDestroyView
  2. @Override
  3. public void onDestroyView(){
  4. super.onDestroyView();
  5. }

//销毁

  1. //onDestroy
  2. @Override
  3. public void onDestroy(){
  4. super.onDestroy();
  5. }

重点
删除Fragment
//onDetach

  1. @Override
  2. public void onDetach(){
  3. super.onDetach();
  4. }

启动Fragment

onAttach()->onCreate()->onCreateView()->onActivityCreated()->onStart()->onResume()

暂停Fragment - //暂停和停止的方法

onPause()->onStop()

重新看到Fragment

onStart()->onResume()

Fragment1 -> Fragment2

  1. Fragment1: onPause()->onStop()->onDestroyView->onDestroy()->onDetach()
  2. Fragment2: onAttach()->onCreate()->onCreateView()->onActivityCreated()->onStart()->onResume()

Fragment2回到主界面时

onPause()->onStop()

又重新看见

  1. //Fragment2回到界面
  2. onStart()->onResume()

Fragment2 退出

onPause()->onStop()->onDestroyView()->onDestroy()->onDetach()

Fragment与Activity

  1. Fragment是通过调用getActivity()的方法获取Activity的;
  2. Activity是通过调用FragmentManager的findFramentById()的,也可以用findFragmentByTag()方法获取Fragment片段的。

重点

  1. public class MyFragment extends Fragment{
  2. @Override
  3. public View onCreateView(LayoutInflater inflater,ViewGrop container,Bundle savedInstanceState){
  4. return super.onCreateView(inflater,container,savedInstanceState);
  5. //View view = inflater.inflate(R.layout.fragment, container, false);
  6. //TextView tv = view.findViewById(R.id.textview);
  7. //return view;
  8. }
  9. }

一个Activity调用Fragment的界面显示数据内容

  1. String text = editext.getText().toString();
  2. MyFragment fragment = new MyFragment();
  3. Bundle bundle = new Bundle();
  4. bundle.putString("name",text);
  5. fragment.setArguments(bundle);
  6. FragmentManager fragmentManager = getFragmentManager();
  7. FragmentTransaction beginTransaction = fragmentManager.beginTransaction();
  8. //beginTransaction.add(containerViewId,fragment,tag)
  9. beginTransaction.add(R.id.layout.textview,fragment,"fragment");
  10. beginTransaction.commit();

在Fragement中设置

  1. public class MyFragment extends Fragment{
  2. @0verride
  3. public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState){
  4. View view = inflater.infate(R.layout.fragement,container,false);
  5. TextView tv=view.findViewById(R.id.textview);
  6. String text = getArguments().get("name")+"";
  7. tv.setText(text);
  8. }
  9. }

Fragment

  1. private String name = "v";
  2. public interface Myvoid
  3. {
  4. public void name(String name);
  5. }

Activity

implements Myvoid

fragment

  1. @Override
  2. public void onAttach(Activity activity){
  3. myvoid = activity;
  4. super.onAttach(activity);
  5. }

回到Activity

覆盖方法

另类

xml

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3. android:layout_width="match_parent"
  4. android:layout_height="match_parent"
  5. android:background="@android:color/white"
  6. android:orientation="vertical">
  7. </LinearLayout>

Fragment

  1. public class MyinfoFragment extends Fragment implements View.OnClickListener{
  2. @Override
  3. public View onCreateView(LayoutInflater inflater, ViewGroup container,
  4. Bundle savedInstanceState) {
  5. return inflater.inflate(R.layout.fragment_myinfo, null);
  6. }
  7. @Override
  8. public void onViewCreated(View view, Bundle savedInstanceState) {
  9. super.onViewCreated(view, savedInstanceState);
  10. }
  11. @Override
  12. public void onClick(View v) {
  13. }
  14. }

MainActivity

  1. public class MainActivity extends FragmentActivity implements View.OnClickListener{
  2. @Override
  3. protected void onCreate(Bundle savedInstanceState) {
  4. super.onCreate(savedInstanceState);
  5. setContentView(R.layout.activity_main);
  6. initView();
  7. setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
  8. setMain();
  9. }
  10. @Override
  11. public boolean onKeyDown(int keyCode, KeyEvent event) {
  12. }
  13. private void setMain() {
  14. }
  15. @Override
  16. protected void onActivityResult(int requestCode, int resultCode, Intent data) {
  17. super.onActivityResult(requestCode, resultCode, data);
  18. }
  19. private void setSelectStatus(int index) {
  20. switch (index){
  21. }
  22. }
  23. private void initView() {
  24. }
  25. @Override
  26. public void onClick(View v) {
  27. switch (v.getId()){
  28. }
  29. }
  30. }

继续

  1. FragmentManager fragmentManager = getFragmentManager();
  2. Fragment findFragmentById = fragmentManager.findFragmentById(R.id.fragment);

ViewPager左右滑动

  1. <android.support.v4.view.ViewPager
  2. android:id="@+id/viewpager"
  3. android:...
  4. android:...>
  5. </android.support.v4.view.ViewPager>

ViewPager是一种容器,可以盛装两种类型的对象:一种是View对象; 也可以放置Fragment。

  1. getLayoutInflater().form(this);
  2. 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);

准备适配器

  1. @Override
  2. public Object instantiateItem(ViewGroup container, int position){
  3. }

搭建构造方法

  1. public MyPagerAdapter(List<View> viewList){
  2. this.viewList=viewList;
  3. }
  1. container.addview();
  2. return viewList.get(position);
container.removeView(viweList.get(position));

Adapter

创建Adapter

PagerAdapter pagerAdapter = new PagerAdapter(viewList);

setAdapter(pagerAdapter);

对象

PagerTabStrip和PagerTitleStrip

  1. 一个View显示ViewPager,然后在ViewPager中添加PagerTabStrip
  2. <android.support.v4.view.ViewPager
  3. ...
  4. <android.support.v4.view.PagerTabStrip
  5. ...>
  6. </android.support.v4.view.PagerTabStrip>
  7. </android.support.v4.view.ViewPager>

然后加入标题,private List<String> titleList;

同理,ViewPager添加题目

  1. titleList = new ArrayList<String>();
  2. titleList.add("第一题");
  3. titleList.add("第二题");
  4. titleList.add("第三题");
  5. titleList.add("第五题");

在加入PagerAdapter中,创建Adapter下创建,MyPagerAdapter.java

MyPagerAdapter.java

  1. public class MyPagerAdapter extends PagerAdapter{
  2. private List<View> viewList;
  3. private List<String> titleList;
  4. public MyPagerAdapter(List<View> viewList, List<String> titleList){
  5. this.viewList=viewList;
  6. this.titleList=titleList;
  7. }
  8. }

加入题目数量第几题
显示标题
getPageTitle(),显示所对应的标题

  1. @Override
  2. public CharSequence getPageTitle(int position){
  3. //return super.getPageTitle(position);
  4. return titleList.get(position);
  5. }

PagerTabStrip的属性
背景

setBackgroundColor(Color.RED);
文本
setTextColor();
下划线
setDrawFullUnderline(false);
分割线
setTabIndicatorColor();

PagerTitleStrip 与 PagerTabStrip 类似

适配器

FragmentPagerAdapter

  1. import android.os.Bundle;
  2. import android.support.v4.app.Fragment;
  3. public class ExercisesFragment extends Fragment {
  4. private ListView lvList;//来源fragment
  5. private ListItemAdapter adapter; //适配器
  6. private List<> ebl; //列表集合
  7. @Override
  8. public View onCreateView(LayoutInflater inflater, ViewGroup container,
  9. Bundle savedInstanceState) {
  10. return inflater.inflate(R.layout.fragment, null);
  11. }
  12. @Override
  13. public void onViewCreated(View view, Bundle savedInstanceState) {
  14. super.onViewCreated(view, savedInstanceState);
  15. initData();
  16. lvList = view.findViewById(R.id.lv_list);
  17. adapter = new ListItemAdapter(getActivity());
  18. adapter.setData(ebl);
  19. lvList.setAdapter(adapter);
  20. }
  21. private void initData(){
  22. ebl = new ArrayList<Bean>();
  23. for (int i=0;i<10;i++){
  24. Bean bean = new Bean();
  25. bean.id=(i+1);
  26. switch (i){
  27. case 0:
  28. case 9:
  29. default:
  30. break;
  31. }
  32. }
  33. }
  34. }

设置MyFragment继承Fragment

  1. public class MyFragment extends Fragment{
  2. @Override
  3. public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState){
  4. return inflater.infate(R.layout.view, container, false);
  5. //return inflater.infate(R.layout.view, null);
  6. }
  7. }

点击跳转Fragment

  1. public void onClick(View v) {
  2. switch (v.getId()){
  3. case R.id.bottom1:
  4. //getSupportFragmentManager().beginTransaction().add(R.id.main_body,new CFragment()).commit();
  5. break;
  6. case R.id.bottom2:
  7. //getSupportFragmentManager().beginTransaction().add(R.id.main_body,new EFragment()).commit();
  8. break;
  9. case R.id.bottom3:
  10. //getSupportFragmentManager().beginTransaction().add(R.id.main_body,new MFragment()).commit();
  11. break;
  12. }
  13. }

实例

private List<Fragment> fragmentList;

Fragment作为数据源

  1. 添加Fragment
  2. fragmentList = new ArrayList<Fragment>();
  3. fragmentList.add(new Fragment());
  4. 添加创建的Fragment
  5. //fragmentList.add(new Fragment%());

导入的包

import android.support.v4.app.Fragment;

配置数据适配器

如果用FragmentPagerAdapter,一次性加载
同理

  1. public class MyFragment extends FragmentPagerAdapter{
  2. private List<Fragment> fragmentList;
  3. private List<String> titleList;
  4. public MyFragment(FragmentManager fm,List<Fragment> fragmentList,List<String> titleList){
  5. super(fm)
  6. this.fragmentList=fragmentList;
  7. this.titleList=titleList;
  8. }
  9. }

MainActivity.java

  1. public class MainActivity extends FragmentActivity implements View.OnClickListener{
  2. @Override
  3. protected void onCreate(Bundle savedInstanceState) {
  4. super.onCreate(savedInstanceState);
  5. setContentView(R.layout.activity_main);
  6. }
  7. private void setMain() {
  8. }
  9. private void setSelectStatus(int index) {
  10. switch (index){
  11. }
  12. }
  13. private void initView() {
  14. }
  15. @Override
  16. public void onClick(View v) {
  17. switch (v.getId()){
  18. //getSupportFragmentManager().beginTransaction().add(R.id.main_body,new CourseFragment()).commit();
  19. //getSupportFragmentManager().beginTransaction().add(R.id.main_body,new ExercisesFragment()).commit();
  20. //getSupportFragmentManager().beginTransaction().add(R.id.main_body,new MyinfoFragment()).commit();
  21. }
  22. }
  23. }
FragmentPagerAdapter adapter=new FragmentPagerAdapter(getSupportFragmentManager());

轮播

ViewFlipper

自动播放

静态的方法导入:

  1. <ViewFlipper
  2. android:id="@+id/flipper"
  3. android:layout_width="match_parent"
  4. android:layout_height="wrap_content"
  5. android:layout_marginTop="10dp"
  6. android:flipInterval="4000">
  7. <ImageView
  8. android:scaleType="matrix"
  9. android:id="@+id/ex2"
  10. android:layout_width="match_parent"
  11. android:layout_height="wrap_content"/>
  12. <ImageView
  13. android:scaleType="matrix"
  14. android:id="@+id/ex3"
  15. android:layout_width="match_parent"
  16. android:layout_height="wrap_content"/>
  17. <ImageView
  18. android:scaleType="matrix"
  19. android:id="@+id/ex4"
  20. android:android:layout_width="match_parent"
  21. android:layout_height="wrap_content"/>
  22. </ViewFlipper>

动态导入:

flipper = findViewById(R.id.flipper);
flipper.addView(getImageView(R.drawable.picture));

自己定义动态效果,一般用anim的文件夹表示:

然后创建布局界面:
创建轮播图

  1. <ViewFlipper
  2. android:id="@+id/flipper"
  3. android:layout_width="match_parent"
  4. android:layout_height="match_parent">
  5. </ViewFlipper>

MainActivity.java

  1. public class MainActivity extends Activity {
  2. @Override
  3. protected void onCreate(Bundle savedInstanceState){
  4. super.onCreate(savedInstanceState);
  5. setContentView(R.layout.main);
  6. }
  7. }

声明变量

private ViewFlipper flipper;

进行初始化

flipper = findViewById(R.id.flipper);

动态导入:addView(getImageView(R.drawable.picture));

轮播图有多张图片,创建数组,分别导入

获取图片

  1. private ImageView getImageView(int imageId){
  2. ImageView image = new ImageView(this);
  3. image.setImageResource(imageId);
  4. return image;
  5. }
  1. for(int i=0; i<imageId.length;i++){
  2. flipper.addView(getImageView( 数组[i] ));
  3. }

手势滑动的方法

  1. @Override
  2. public boolean onTouchEvent(MotionEvent event){
  3. return super.onTouchEvent(event);
  4. }

自动播放效果:

  1. flipper.setFlipInterval(3000);
  2. flipper.startFlipping();

ScrollView

HorizontalScrollView,ScrollView

滚动视图

不能滚动,因为布局不是ScrollVeiw,而是<TextView>

tv.setText(getResources().getString(R.string.content));

如果是这样就可以

  1. <ScrollView
  2. android:layout_width="match_parent"
  3. android:layout_height="wrap_content"
  4. <TextView
  5. android:layout_width="match_parent"
  6. android:layout_height="wrap_content"/>
  7. </ScrollView>

setOnTouchListener的方法

  1. scroll.setOnTouchListener(new OnTouchListener){
  2. }

记得 scrollBy 和 scrollTo 的区别

Gallery 图片浏览器

Gallery像一个走廊,ImageSwitcher像幻灯片

显示图片,就要对图片进行封装,声明数组

  1. 布局
  2. <Gallery
  3. android:...
  4. />
  5. 老样子

图片id,private int[] res 定义数组

我们学过的适配器,都是继承BaseAdapter

  1. ArrayAdapter<String> adapter;
  2. SimpleAdapter adapter;

继承BaseAdapter中必须要实现的方法

  1. public int getCount()

  2. public long getItemId(int position)

  3. public Object getItem(int position)

  4. public View gtView(int position, View convertView, ViewGroup parent)

如果自己定义一个适配器,定义后,实现BaseAdapter中的方法,
然后创建构造函数

  1. private int[] images;
  2. public ImageAdapter(int []images)
  3. {
  4. this.images = images;
  5. }

说明

获取视图

  1. private Context context;
  2. public ImageAdapter(int[] images, Context context){
  3. this.images = images;
  4. this.context = context;
  5. }

public View gtView(int position, View convertView, ViewGroup parent)

  1. {
  2. //ImageView image=new ImageView(this);
  3. ImageView image = new ImageView(context);
  4. image.setBackgroundResource(数组[position]);
  5. //设置图片大小
  6. image.setLayoutParams(new Gallery.LayoutParams(400,300));
  7. return image;
  8. }

加载适配器 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

  1. public View makeView(){
  2. }

is.setFactory(this);

  1. public View makeView(){
  2. ImageView image = new ImageView(this);
  3. return image;
  4. }

SeekBar 拖动进度条

setOnSeekBarChangeListener(this);

  1. <SeekBar
  2. style="@android:style/Widget.SeekBar"
  3. />
  1. <selecter xmlns:android="http...."
  2. <item
  3. android:android:state_pressed="true"
  4. android:state_window_focused="true"
  5. andeoid:drawable=""/>

Android常用布局样式

线性布局
LinearLayout

相对布局
RelativeLayout

表格布局
TableLayout

可以使用GridView替代

绝对布局
AbsoluteLayout

帧布局(主要布局叠加)
FrameLayout

多用LinearLayout和RelativeLayout

少用AbsoluteLayout

定位:分享 Android&Java 知识点

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/Li_阴宅/article/detail/862518
推荐阅读
相关标签
  

闽ICP备14008679号