首先介绍一下这个程序的功能:

1.顶部有两个可以切换Fragment的Button

2.在其中一个Fragment中里有个ScrollView,ScrollView中有ViewFlipper,ListView。(另一个Fragment中就随意了)

 随着listView的滚动,ViewFlipper中的内容也会滚动。

3.两个布局(主布局,一个Fragment的布局(另一个没写,其实都一样)),一个Fragment,一个主Activity,重写ListView(不重写的话,不会随着ViewFlipper滚动而滚动),至于为啥重写,咱们后面再细说哈(尴尬脸)。

布局---activity_main.xml

  1. <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
  2. android:layout_width="match_parent"
  3. android:layout_height="match_parent">
  4.  
  5. <!--用于切换的Buuton-->
  6. <LinearLayout
  7. android:id="@+id/btn_linear"
  8. android:layout_width="match_parent"
  9. android:layout_height="55dp"
  10. android:background="#222222"
  11. android:orientation="horizontal">
  12.  
  13. <Button
  14. android:id="@+id/btn1"
  15. android:layout_width="0dp"
  16. android:layout_height="match_parent"
  17. android:layout_weight="1"
  18. android:background="#00000000"
  19. android:text="按钮1"
  20. android:textColor="#ffffff"
  21. android:textSize="18sp" />
  22.  
  23. <Button
  24. android:id="@+id/btn2"
  25. android:layout_width="0dp"
  26. android:layout_height="match_parent"
  27. android:layout_weight="1"
  28. android:background="#00000000"
  29. android:text="按钮2"
  30. android:textColor="#ffffff"
  31. android:textSize="18sp" />
  32.  
  33. </LinearLayout>
  34.  
  35. <!--内容部分,fragment的切换-->
  36. <LinearLayout
  37. android:id="@+id/content_fragment"
  38. android:layout_width="match_parent"
  39. android:layout_height="match_parent"
  40. android:layout_below="@id/btn_linear"
  41. android:orientation="vertical">
  42. </LinearLayout>
  43.  
  44. </RelativeLayout>

布局---but1_layout.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:orientation="vertical">
  6. <!-- android:fillViewport="true"可以显示多个Item-->
  7. <ScrollView
  8. android:id="@+id/scrollview"
  9. android:layout_width="match_parent"
  10. android:layout_height="match_parent"
  11. android:fillViewport="true">
  12.  
  13. <LinearLayout
  14. android:layout_width="match_parent"
  15. android:layout_height="match_parent"
  16. android:orientation="vertical">
  17.  
  18. <!--里面的图片资源自己加哦-->
  19. <ViewFlipper
  20. android:id="@+id/viewflipper"
  21. android:layout_width="match_parent"
  22. android:layout_height="150dp"
  23. android:flipInterval="2000">
  24.  
  25. <ImageView
  26. android:id="@+id/img1"
  27. android:layout_width="match_parent"
  28. android:layout_height="150dp"
  29. android:scaleType="fitXY"
  30. android:src="@drawable/viewflipper_1" />
  31.  
  32. <ImageView
  33. android:id="@+id/img2"
  34. android:layout_width="match_parent"
  35. android:layout_height="150dp"
  36. android:scaleType="fitXY"
  37. android:src="@drawable/viewflipper_2" />
  38.  
  39. <ImageView
  40. android:id="@+id/img3"
  41. android:layout_width="match_parent"
  42. android:layout_height="150dp"
  43. android:scaleType="fitXY"
  44. android:src="@drawable/viewflipper_3" />
  45. </ViewFlipper>
  46.  
  47. <!--my为App的名字-->
  48. <com.example.liang.my.NestedListView
  49. android:id="@+id/listview_1"
  50. android:layout_width="match_parent"
  51. android:layout_height="match_parent"/>
  52. </LinearLayout>
  53. </ScrollView>
  54. </LinearLayout>

另一个布局里面啥也没写

主Activity---MainActivity

  1. package com.example.liang.my;
  2.  
  3. import android.support.v4.app.Fragment;
  4. import android.support.v4.app.FragmentActivity;
  5. import android.os.Bundle;
  6. import android.support.v4.app.FragmentTransaction;
  7. import android.view.View;
  8. import android.widget.Button;
  9.  
  10. public class MainActivity extends FragmentActivity implements View.OnClickListener{
  11.  
  12. //按钮
  13. private Button btn1,btn2;
  14. //用于切换的fragment和记录当前的Fragment
  15. private Fragment btn1Fragment,btn2Fragment,currentFragment;
  16.  
  17. @Override
  18. protected void onCreate(Bundle savedInstanceState) {
  19. super.onCreate(savedInstanceState);
  20. setContentView(R.layout.activity_main);
  21. initUI();
  22. initTab();
  23. }
  24.  
  25. /**
  26. * 初始化UI
  27. */
  28. private void initUI(){
  29. btn1=(Button)findViewById(R.id.btn1);
  30. btn2=(Button)findViewById(R.id.btn2);
  31.  
  32. btn1.setOnClickListener(this);
  33. btn2.setOnClickListener(this);
  34.  
  35. }
  36.  
  37. /**
  38. * 初始化顶部标签
  39. */
  40. private void initTab(){
  41. //如果碎片没有创建则先创建
  42. if(btn1Fragment==null){
  43. btn1Fragment=new Btn1iFragment();
  44. }
  45. getSupportFragmentManager().beginTransaction()
  46. .add(R.id.content_fragment,btn1Fragment).commit();
  47.  
  48. //记录当前的Fragment
  49. currentFragment=btn1Fragment;
  50. }
  51.  
  52. @Override
  53. public void onClick(View view) {
  54. switch(view.getId()){
  55. case R.id.btn1:
  56. clickTab1();
  57. break;
  58. case R.id.btn2:
  59. clickTab2();
  60. break;
  61. }
  62. }
  63.  
  64. public void clickTab1(){
  65. if(btn1Fragment==null){
  66. btn1Fragment=new Btn1Fragment();
  67. }
  68. addOrShowFragment(getSupportFragmentManager().beginTransaction(),btn1Fragment);
  69. }
  70. public void clickTab2(){
  71.  
  72. if(btn2Fragment==null){
  73. btn2Fragment=new Btn2Fragment();
  74. }
  75. addOrShowFragment(getSupportFragmentManager().beginTransaction(),btn2Fragment);
  76. }
  77. /**
  78. * 添加或显示碎片
  79. * @param transaction
  80. * @param fragment
  81. */
  82. private void addOrShowFragment(FragmentTransaction transaction, Fragment fragment) {
  83.  
  84. if (currentFragment == fragment)
  85. return;
  86. // 如果当前fragment未被添加,则添加到Fragment管理器中
  87. if (!fragment.isAdded()) {
  88. transaction.hide(currentFragment)
  89. .add(R.id.content_fragment, fragment).commit();
  90. } else {
  91. transaction.hide(currentFragment).show(fragment).commit();
  92. }
  93.  
  94. currentFragment = fragment;
  95. }
  96. }

Fragment---Bt1Fragment

  1. package com.example.liang.my.fragment;
  2.  
  3. import android.os.Bundle;
  4.  
  5. import android.support.v4.app.Fragment;
  6. import android.view.LayoutInflater;
  7. import android.view.View;
  8. import android.view.ViewGroup;
  9. import android.widget.AdapterView;
  10. import android.widget.ArrayAdapter;
  11. import android.widget.ScrollView;
  12. import android.widget.ViewFlipper;
  13.  
  14. import com.example.liang.my.R;
  15. import com.example.liang.my.listview.NestedListView;
  16.  
  17. /**
  18. * Created by liang on 2016/8/18.
  19. */
  20. public class Bt1Fragment extends Fragment implements AdapterView.OnItemClickListener {
  21.  
  22. private NestedListView mListView;
  23. private ScrollView mScrollView;
  24. private ViewFlipper flipper;
  25.  
  26. private ArrayAdapter<String> mAdapter;
  27.  
  28. @Override
  29. public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
  30. View view=inflater.inflate(R.layout.bt1_layout,container,false);
  31. mScrollView = (ScrollView) view.findViewById(R.id.scrollview);
  32. mListView = (NestedListView) view.findViewById(R.id.listview_1);
  33. flipper=(ViewFlipper)view.findViewById(R.id.viewflipper);
  34. //启动图片切换
  35. flipper.startFlipping();
  36.  
  37. //数据部分
  38. String[] array = new String[] { "刘一 ", "陈二", "张三", "李四", "王五", "赵六",
  39. "孙七", "周八", "吴九", "郑十","刘一 ", "陈二", "张三", "李四", "王五", "赵六",
  40. "孙七", "周八", "吴九", "郑十" };
  41. mAdapter = new ArrayAdapter<>(getActivity(),
  42. android.R.layout.simple_list_item_1, array);
  43.  
  44. mListView.setAdapter(mAdapter);
  45.  
  46. //解决未滑动时聚焦listview的问题
  47. mListView.setFocusable(false);
  48.  
  49. return view;
  50. }
  51.  
  52. @Override
  53. public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
  54.  
  55. }
  56. }

Fragment---Btn1Fragment(啥也没写)

  1. package com.example.liang.my.fragment;
  2.  
  3. import android.os.Bundle;
  4. import android.support.annotation.Nullable;
  5. import android.support.v4.app.Fragment;
  6. import android.view.LayoutInflater;
  7. import android.view.View;
  8. import android.view.ViewGroup;
  9. import android.widget.AdapterView;
  10.  
  11. /**
  12. * Created by liang on 2016/8/18.
  13. */
  14. public class Btn2Fragment extends Fragment implements AdapterView.OnItemClickListener {
  15. @Nullable
  16. @Override
  17. public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
  18.  
  19. return super.onCreateView(inflater, container, savedInstanceState);
  20. }
  21.  
  22. @Override
  23. public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
  24.  
  25. }
  26. }

重写的ListView---NestedListView

  1. package com.example.liang.my.listview;
  2.  
  3. import android.content.Context;
  4. import android.util.AttributeSet;
  5. import android.view.MotionEvent;
  6. import android.view.View;
  7. import android.view.View.OnTouchListener;
  8. import android.view.ViewGroup;
  9. import android.widget.AbsListView;
  10. import android.widget.AbsListView.OnScrollListener;
  11. import android.widget.ListAdapter;
  12. import android.widget.ListView;
  13.  
  14. public class NestedListView extends ListView implements OnTouchListener,
  15. OnScrollListener {
  16.  
  17. private int listViewTouchAction;
  18. private static final int MAXIMUM_LIST_ITEMS_VIEWABLE = 99;
  19.  
  20. public NestedListView(Context context, AttributeSet attrs) {
  21. super(context, attrs);
  22. listViewTouchAction = -1;
  23. setOnScrollListener(this);
  24. setOnTouchListener(this);
  25. }
  26.  
  27. @Override
  28. public void onScroll(AbsListView view, int firstVisibleItem,
  29. int visibleItemCount, int totalItemCount) {
  30. if (getAdapter() != null
  31. && getAdapter().getCount() > MAXIMUM_LIST_ITEMS_VIEWABLE) {
  32. if (listViewTouchAction == MotionEvent.ACTION_MOVE) {
  33. scrollBy(0, -1);
  34. }
  35. }
  36. }
  37.  
  38. @Override
  39. public void onScrollStateChanged(AbsListView view, int scrollState) {
  40. }
  41.  
  42. @Override
  43. protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
  44. super.onMeasure(widthMeasureSpec, heightMeasureSpec);
  45.  
  46. int newHeight = 0;
  47. final int heightMode = MeasureSpec.getMode(heightMeasureSpec);
  48. int heightSize = MeasureSpec.getSize(heightMeasureSpec);
  49. if (heightMode != MeasureSpec.EXACTLY) {
  50. ListAdapter listAdapter = getAdapter();
  51. if (listAdapter != null && !listAdapter.isEmpty()) {
  52. int listPosition = 0;
  53. for (listPosition = 0; listPosition < listAdapter.getCount()
  54. && listPosition < MAXIMUM_LIST_ITEMS_VIEWABLE; listPosition++) {
  55. View listItem = listAdapter.getView(listPosition, null,
  56. this);
  57. // now it will not throw a NPE if listItem is a ViewGroup
  58. // instance
  59. if (listItem instanceof ViewGroup) {
  60. listItem.setLayoutParams(new LayoutParams(
  61. LayoutParams.WRAP_CONTENT,
  62. LayoutParams.WRAP_CONTENT));
  63. }
  64. listItem.measure(widthMeasureSpec, heightMeasureSpec);
  65. newHeight += listItem.getMeasuredHeight();
  66. }
  67. newHeight += getDividerHeight() * listPosition;
  68. }
  69. if ((heightMode == MeasureSpec.AT_MOST) && (newHeight > heightSize)) {
  70. if (newHeight > heightSize) {
  71. newHeight = heightSize;
  72. }
  73. }
  74. } else {
  75. newHeight = getMeasuredHeight();
  76. }
  77. setMeasuredDimension(getMeasuredWidth(), newHeight);
  78. }
  79.  
  80. @Override
  81. public boolean onTouch(View v, MotionEvent event) {
  82. if (getAdapter() != null
  83. && getAdapter().getCount() > MAXIMUM_LIST_ITEMS_VIEWABLE) {
  84. if (listViewTouchAction == MotionEvent.ACTION_MOVE) {
  85. scrollBy(0, 1);
  86. }
  87. }
  88. return false;
  89. }
  90. }

在Fragment中加一个嵌套了ListView的ScrollView(一)的更多相关文章

  1. Fragment中启动一个新的Activity

    最近遇到一个小问题,就是我在主界面中用的是Fragment,其中四个Fragment,然后打算在其中一个里边,写一个TextView(准确地说是Linearout)的单击事件,然后跳转到另外一个Act ...

  2. 在robotframework里面,怎么在已有的字典中加一个键值对呢

  3. Android Fragment使用(四) Toolbar使用及Fragment中的Toolbar处理

    Toolbar作为ActionBar使用介绍 本文介绍了在Android中将Toolbar作为ActionBar使用的方法. 并且介绍了在Fragment和嵌套Fragment中使用Toolbar作为 ...

  4. 无需SherlockActionbar的SlidingMenu使用详解(二)——向Fragment中添加ViewPager和Tab

    之前我们对大体框架有了一定的认识,现在我们来做Fragment界面,其实这里面和这个框架的关系就不大了,但因为有些同学对于在SlidingMenu中切换fragment还是有问题,所以我就在本篇进行详 ...

  5. Android Toolbar使用及Fragment中的Toolbar处理

    Toolbar作为ActionBar使用介绍 本文介绍了在Android中将Toolbar作为ActionBar使用的方法.并且介绍了在Fragment和嵌套Fragment中使用Toolbar作为A ...

  6. Fragment中监听onKey事件,没你想象的那么难。

    项目中越来越多的用到Fragment,在用Fragment取代TabHost的时候遇到了一个问题,我们都知道,TabHost的Tab为Activity实例,有OnKey事件,但是Fragment中没有 ...

  7. 在Fragment中获取Activity中数据

    今天要做一个功能,用Fragment显示从其所在的Acitivity1中获取到的数据.这个Activity1是从另一个带有参数Activity2跳转过来的,所以要获得的是这些参数.因为之前没遇到过,所 ...

  8. wemall app商城源码Fragment中监听onKey事件

    wemall-mobile是基于WeMall的android app商城,只需要在原商城目录下上传接口文件即可完成服务端的配置,客户端可定制修改.本文分享android开发Fragment中监听onK ...

  9. [Android] Android ViewPager 中加载 Fragment的两种方式 方式(二)

    接上文: https://www.cnblogs.com/wukong1688/p/10693338.html Android ViewPager 中加载 Fragmenet的两种方式 方式(一) 二 ...

随机推荐

  1. Hadoop资源调度器

    hadoop调度器的作用是将系统中空闲的资源按一定策略分配给作业.调度器是一个可插拔的模块,用户可以根据自己的实际应用要求设计调度器.Hadoop中常见的调度器有三种,分别为: 1.基于队列的FIFO ...

  2. 如何调试ubifs文件系统

    注意内核版本为4.9 在drivers/mtd/ubi/debug.h中加入DEBUG的定义,如下 #ifndef __UBI_DEBUG_H__#define __UBI_DEBUG_H__#def ...

  3. MQ的前世今生

    1983年孟买26岁的工程师Vivek Ranadive设想一种软件总线,同年Teknekron诞生了.     最初用于高盛,用于解决金融交易.于是发布订阅的MQ The Information B ...

  4. Lucene 的 Scoring 评分机制

    转自: http://www.oschina.net/question/5189_7707  Lucene 评分体系/机制(lucene scoring)是 Lucene 出名的一核心部分.它对用户来 ...

  5. Java并发之synchronized深入

    一句话总结synchronized: JVM会自动通过使用monitor来加锁和解锁,保证了同时只有一个线程可以执行指定代码,从而保证了线程安全,同时具有可重入和不可中断的性质. 一.synchron ...

  6. PHP对象的使用,什么时候可以用中括号[], 什么时候可以用箭头->

    $orderTPLMessage = (object)array( 'touser' => '这里填open id', 'template_id' => 'oQDOldy7q6CdaYw2 ...

  7. JavaScript权威指南--词法结构

    使用广泛,所有的浏览器(桌面.手机.屏蔽等等)都配有相应的JavaScript解析器. JavaScript解析器如何工作? 浏览器在读取HTML文件的时候,只有当遇到<script>标签 ...

  8. python异常列表

    http://www.runoob.com/python/python-exceptions.html https://www.cnblogs.com/zhangyingai/p/7097920.ht ...

  9. Template、ItemsPanel、ItemContainerStyle、ItemTemplate (部分内容有待验证)

    以下摘自“CSDN”的某人博客,部分内容有待验证,需注意“辨别学之....” 1.Template是指控件的样式 在WPF中所有继承自contentcontrol类的控件都含有此属性,(继承自Fram ...

  10. Shell_NotifyIcon托盘图标闪烁

    之前的同事留下了一个程序会莫名闪退的bug,今天终于发现是托盘图标闪烁使得gdi资源耗尽导致的. 先定义 #include "shellapi.h" //托盘图标引用 NOTIFY ...