在Fragment中加一个嵌套了ListView的ScrollView(一)
首先介绍一下这个程序的功能:
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(一)的更多相关文章
- Fragment中启动一个新的Activity
最近遇到一个小问题,就是我在主界面中用的是Fragment,其中四个Fragment,然后打算在其中一个里边,写一个TextView(准确地说是Linearout)的单击事件,然后跳转到另外一个Act ...
- 在robotframework里面,怎么在已有的字典中加一个键值对呢
- Android Fragment使用(四) Toolbar使用及Fragment中的Toolbar处理
Toolbar作为ActionBar使用介绍 本文介绍了在Android中将Toolbar作为ActionBar使用的方法. 并且介绍了在Fragment和嵌套Fragment中使用Toolbar作为 ...
- 无需SherlockActionbar的SlidingMenu使用详解(二)——向Fragment中添加ViewPager和Tab
之前我们对大体框架有了一定的认识,现在我们来做Fragment界面,其实这里面和这个框架的关系就不大了,但因为有些同学对于在SlidingMenu中切换fragment还是有问题,所以我就在本篇进行详 ...
- Android Toolbar使用及Fragment中的Toolbar处理
Toolbar作为ActionBar使用介绍 本文介绍了在Android中将Toolbar作为ActionBar使用的方法.并且介绍了在Fragment和嵌套Fragment中使用Toolbar作为A ...
- Fragment中监听onKey事件,没你想象的那么难。
项目中越来越多的用到Fragment,在用Fragment取代TabHost的时候遇到了一个问题,我们都知道,TabHost的Tab为Activity实例,有OnKey事件,但是Fragment中没有 ...
- 在Fragment中获取Activity中数据
今天要做一个功能,用Fragment显示从其所在的Acitivity1中获取到的数据.这个Activity1是从另一个带有参数Activity2跳转过来的,所以要获得的是这些参数.因为之前没遇到过,所 ...
- wemall app商城源码Fragment中监听onKey事件
wemall-mobile是基于WeMall的android app商城,只需要在原商城目录下上传接口文件即可完成服务端的配置,客户端可定制修改.本文分享android开发Fragment中监听onK ...
- [Android] Android ViewPager 中加载 Fragment的两种方式 方式(二)
接上文: https://www.cnblogs.com/wukong1688/p/10693338.html Android ViewPager 中加载 Fragmenet的两种方式 方式(一) 二 ...
随机推荐
- 重新想,重新看——CSS3变形,过渡与动画④
最后,我们来探讨一下CSS3的动画属性. 之前提到过,实际上过渡也算作动画的一种.但过渡作为动画的缺陷在于,只能使元素属性从一个值“过渡”至另一个值,但如果想要使元素的属性值根据需要在时间轴上不断变化 ...
- SQL调优简介及调优方式
引导语:我曾有一种感觉,不管何种调优方式,索引是最根本的方法,是一切优化手法的内功,所以一下我们 将讨论一些和索引相关的调优方式.索引是提高数据库性能的常用方法,它可以令数据库服务器以比没有索引快得多 ...
- zabbix3.2通过snmp v2采集Dell服务器iDRAC口信息监控硬件
模板下载 https://files.cnblogs.com/files/LuckWJL/zbx_export_templates.xml 模板源代码 <?xml version="1 ...
- 在vim中的复制,剪切,粘贴
1. 剪切和粘贴 定位鼠标到剪切的开始位置 输入v键开始选择剪切的字符,或者V键是为了选择 整行 移动方向键到结束的地方 d键是剪切,y键是复制 移动鼠标到粘贴的位置 输入P是在鼠标位置前粘贴,输入p ...
- tiny4412的烧录工具minitool安装【学习笔记】
烧录了半天,在win10下一直就是烧录不进去,但是在Ubuntuh环境却可以,找了很久终于找到了,原来在win10安装minitool驱动的时候没有注意到报了错误,错误内容是驱动的数字签名问题,后来禁 ...
- pyspider—爬取视频链接
#!/usr/bin/env python # -*- encoding: utf-8 -*- # Created on 2015-03-20 09:46:20 # Project: fly_spid ...
- Object.assign() 从一个或多个源对象复制到目标对象
Object.assign()方法用于将所有可枚举属性的值从一个或多个源对象复制到目标对象.它将返回目标对象. 1.语法: Object.assign(target, ... , sources) 参 ...
- 最优化问题 Optimization Problems & 动态规划 Dynamic Programming
2018-01-12 22:50:06 一.优化问题 优化问题用数学的角度来分析就是去求一个函数或者说方程的极大值或者极小值,通常这种优化问题是有约束条件的,所以也被称为约束优化问题. 约束优化问题( ...
- Python面向过程和面向对象基础
总结一下: 面向过程编程:根据业务逻辑从上到下的写代码-----就是一个project写到底,重复利用性比较差 函数式:将某些特定功能代码封装到函数中------方便日后调用 面向对象:对函数进行分类 ...
- 关于.net4.0中的Action委托
在使用委托时,若封装的方法无返回值,并且参数在0-7个,可考虑使用.Net4.0中的Action委托,建议使用系统自带的,减少自定义 public delegate void Action<in ...