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

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

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

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

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

布局---activity_main.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"> <!--用于切换的Buuton-->
<LinearLayout
android:id="@+id/btn_linear"
android:layout_width="match_parent"
android:layout_height="55dp"
android:background="#222222"
android:orientation="horizontal"> <Button
android:id="@+id/btn1"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:background="#00000000"
android:text="按钮1"
android:textColor="#ffffff"
android:textSize="18sp" /> <Button
android:id="@+id/btn2"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:background="#00000000"
android:text="按钮2"
android:textColor="#ffffff"
android:textSize="18sp" /> </LinearLayout> <!--内容部分,fragment的切换-->
<LinearLayout
android:id="@+id/content_fragment"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="@id/btn_linear"
android:orientation="vertical">
</LinearLayout> </RelativeLayout>

布局---but1_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"
android:orientation="vertical">
<!-- android:fillViewport="true"可以显示多个Item-->
<ScrollView
android:id="@+id/scrollview"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fillViewport="true"> <LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"> <!--里面的图片资源自己加哦-->
<ViewFlipper
android:id="@+id/viewflipper"
android:layout_width="match_parent"
android:layout_height="150dp"
android:flipInterval="2000"> <ImageView
android:id="@+id/img1"
android:layout_width="match_parent"
android:layout_height="150dp"
android:scaleType="fitXY"
android:src="@drawable/viewflipper_1" /> <ImageView
android:id="@+id/img2"
android:layout_width="match_parent"
android:layout_height="150dp"
android:scaleType="fitXY"
android:src="@drawable/viewflipper_2" /> <ImageView
android:id="@+id/img3"
android:layout_width="match_parent"
android:layout_height="150dp"
android:scaleType="fitXY"
android:src="@drawable/viewflipper_3" />
</ViewFlipper> <!--my为App的名字-->
<com.example.liang.my.NestedListView
android:id="@+id/listview_1"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
</LinearLayout>
</ScrollView>
</LinearLayout>

另一个布局里面啥也没写

主Activity---MainActivity

package com.example.liang.my;

import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.os.Bundle;
import android.support.v4.app.FragmentTransaction;
import android.view.View;
import android.widget.Button; public class MainActivity extends FragmentActivity implements View.OnClickListener{ //按钮
private Button btn1,btn2;
//用于切换的fragment和记录当前的Fragment
private Fragment btn1Fragment,btn2Fragment,currentFragment; @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initUI();
initTab();
} /**
* 初始化UI
*/
private void initUI(){
btn1=(Button)findViewById(R.id.btn1);
btn2=(Button)findViewById(R.id.btn2); btn1.setOnClickListener(this);
btn2.setOnClickListener(this); } /**
* 初始化顶部标签
*/
private void initTab(){
//如果碎片没有创建则先创建
if(btn1Fragment==null){
btn1Fragment=new Btn1iFragment();
}
getSupportFragmentManager().beginTransaction()
.add(R.id.content_fragment,btn1Fragment).commit(); //记录当前的Fragment
currentFragment=btn1Fragment;
} @Override
public void onClick(View view) {
switch(view.getId()){
case R.id.btn1:
clickTab1();
break;
case R.id.btn2:
clickTab2();
break;
}
} public void clickTab1(){
if(btn1Fragment==null){
btn1Fragment=new Btn1Fragment();
}
addOrShowFragment(getSupportFragmentManager().beginTransaction(),btn1Fragment);
}
public void clickTab2(){ if(btn2Fragment==null){
btn2Fragment=new Btn2Fragment();
}
addOrShowFragment(getSupportFragmentManager().beginTransaction(),btn2Fragment);
}
/**
* 添加或显示碎片
* @param transaction
* @param fragment
*/
private void addOrShowFragment(FragmentTransaction transaction, Fragment fragment) { if (currentFragment == fragment)
return;
// 如果当前fragment未被添加,则添加到Fragment管理器中
if (!fragment.isAdded()) {
transaction.hide(currentFragment)
.add(R.id.content_fragment, fragment).commit();
} else {
transaction.hide(currentFragment).show(fragment).commit();
} currentFragment = fragment;
}
}

Fragment---Bt1Fragment

package com.example.liang.my.fragment;

import android.os.Bundle;

import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ScrollView;
import android.widget.ViewFlipper; import com.example.liang.my.R;
import com.example.liang.my.listview.NestedListView; /**
* Created by liang on 2016/8/18.
*/
public class Bt1Fragment extends Fragment implements AdapterView.OnItemClickListener { private NestedListView mListView;
private ScrollView mScrollView;
private ViewFlipper flipper; private ArrayAdapter<String> mAdapter; @Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view=inflater.inflate(R.layout.bt1_layout,container,false);
mScrollView = (ScrollView) view.findViewById(R.id.scrollview);
mListView = (NestedListView) view.findViewById(R.id.listview_1);
flipper=(ViewFlipper)view.findViewById(R.id.viewflipper);
//启动图片切换
flipper.startFlipping(); //数据部分
String[] array = new String[] { "刘一 ", "陈二", "张三", "李四", "王五", "赵六",
"孙七", "周八", "吴九", "郑十","刘一 ", "陈二", "张三", "李四", "王五", "赵六",
"孙七", "周八", "吴九", "郑十" };
mAdapter = new ArrayAdapter<>(getActivity(),
android.R.layout.simple_list_item_1, array); mListView.setAdapter(mAdapter); //解决未滑动时聚焦listview的问题
mListView.setFocusable(false); return view;
} @Override
public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) { }
}

Fragment---Btn1Fragment(啥也没写)

package com.example.liang.my.fragment;

import android.os.Bundle;
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.AdapterView; /**
* Created by liang on 2016/8/18.
*/
public class Btn2Fragment extends Fragment implements AdapterView.OnItemClickListener {
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { return super.onCreateView(inflater, container, savedInstanceState);
} @Override
public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) { }
}

重写的ListView---NestedListView

package com.example.liang.my.listview;

import android.content.Context;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnTouchListener;
import android.view.ViewGroup;
import android.widget.AbsListView;
import android.widget.AbsListView.OnScrollListener;
import android.widget.ListAdapter;
import android.widget.ListView; public class NestedListView extends ListView implements OnTouchListener,
OnScrollListener { private int listViewTouchAction;
private static final int MAXIMUM_LIST_ITEMS_VIEWABLE = 99; public NestedListView(Context context, AttributeSet attrs) {
super(context, attrs);
listViewTouchAction = -1;
setOnScrollListener(this);
setOnTouchListener(this);
} @Override
public void onScroll(AbsListView view, int firstVisibleItem,
int visibleItemCount, int totalItemCount) {
if (getAdapter() != null
&& getAdapter().getCount() > MAXIMUM_LIST_ITEMS_VIEWABLE) {
if (listViewTouchAction == MotionEvent.ACTION_MOVE) {
scrollBy(0, -1);
}
}
} @Override
public void onScrollStateChanged(AbsListView view, int scrollState) {
} @Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
super.onMeasure(widthMeasureSpec, heightMeasureSpec); int newHeight = 0;
final int heightMode = MeasureSpec.getMode(heightMeasureSpec);
int heightSize = MeasureSpec.getSize(heightMeasureSpec);
if (heightMode != MeasureSpec.EXACTLY) {
ListAdapter listAdapter = getAdapter();
if (listAdapter != null && !listAdapter.isEmpty()) {
int listPosition = 0;
for (listPosition = 0; listPosition < listAdapter.getCount()
&& listPosition < MAXIMUM_LIST_ITEMS_VIEWABLE; listPosition++) {
View listItem = listAdapter.getView(listPosition, null,
this);
// now it will not throw a NPE if listItem is a ViewGroup
// instance
if (listItem instanceof ViewGroup) {
listItem.setLayoutParams(new LayoutParams(
LayoutParams.WRAP_CONTENT,
LayoutParams.WRAP_CONTENT));
}
listItem.measure(widthMeasureSpec, heightMeasureSpec);
newHeight += listItem.getMeasuredHeight();
}
newHeight += getDividerHeight() * listPosition;
}
if ((heightMode == MeasureSpec.AT_MOST) && (newHeight > heightSize)) {
if (newHeight > heightSize) {
newHeight = heightSize;
}
}
} else {
newHeight = getMeasuredHeight();
}
setMeasuredDimension(getMeasuredWidth(), newHeight);
} @Override
public boolean onTouch(View v, MotionEvent event) {
if (getAdapter() != null
&& getAdapter().getCount() > MAXIMUM_LIST_ITEMS_VIEWABLE) {
if (listViewTouchAction == MotionEvent.ACTION_MOVE) {
scrollBy(0, 1);
}
}
return false;
}
}

在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. 【前端】display: box布局教程 [转]

    css display:box 新属性   一.display:box; 在元素上设置该属性,可使其子代排列在同一水平上,类似display:inline-block;. 二.可在其子代设置如下属性 ...

  2. layui和bootstrap 对比

    layui和bootstrap 对比 这两个都属于UI渲染框架. layui是国人开发的一套框架,2016年出来的,现在已更新到2.X版本了.比较新,轻量级,样式简单好看. bootstrap 相对来 ...

  3. webservice的SOAP代理设置

    我们的服务器是可以访问的 设置的代理同样也可以访问 接下来设置代理 接着打开TCP窗口 最后来看数据

  4. sstream头文件-getline 函数 和 stringstream函数 和string的常见用法

    2017-08-12 19:50:50 writer:pprp getline函数可以读入一行的字符,不论有没有空格 第一个参数,流 第二个参数 ,将流读入的地方 第三个参数,当读到某个字符的时候停止 ...

  5. centos mysql忘记密码

    1.停止mysql 服务:service mysqld stop; 2.vim命令打开mysql配置文件my.cnf(位置一般为:/etc/my.cnf) 3.在mysqld进程配置文件中添加skip ...

  6. hiho#1080 更为复杂的买卖房屋姿势 线段树+区间更新

    #1080 : 更为复杂的买卖房屋姿势 时间限制:10000ms 单点时限:1000ms 内存限制:256MB 描述 小Hi和小Ho都是游戏迷,“模拟都市”是他们非常喜欢的一个游戏,在这个游戏里面他们 ...

  7. Asp.Net MVC向视图View传值的三种方法

    本文将总结Asp.Net MVC向视图View传值的三种常见的方法: ----------------------------------------------------------------- ...

  8. 【Jmeter】压测mysql数据库中间件mycat

    背景 因为博主所负责测试的项目需要数据库有较大的吞吐量,在最近进行了升级,更新了一个数据库中间件 - - mycat.查询了一些资料,了解到这是阿里的一个开源项目,基于mysql,是针对磁盘的读与写, ...

  9. 共用y轴的双图形绘制

    实现这种形式的图形,可通过matplotlib和pandas的实现,相比下pandas实现方便的多. 我数据分析的时候主要是stacked bar.bar和line形式的放在一张图上.stacked ...

  10. Neutron之OVS

    OVS即开放虚拟交换标准,不仅仅是为了支持OpenFlow协议,而是为了给虚拟化平台上运行的虚拟机实例提供一套纯软件实现的路由交换协议栈.具体点说,Open vSwitch是在开源的Apache2.0 ...