在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的两种方式 方式(一) 二 ...
随机推荐
- Hadoop资源调度器
hadoop调度器的作用是将系统中空闲的资源按一定策略分配给作业.调度器是一个可插拔的模块,用户可以根据自己的实际应用要求设计调度器.Hadoop中常见的调度器有三种,分别为: 1.基于队列的FIFO ...
- 如何调试ubifs文件系统
注意内核版本为4.9 在drivers/mtd/ubi/debug.h中加入DEBUG的定义,如下 #ifndef __UBI_DEBUG_H__#define __UBI_DEBUG_H__#def ...
- MQ的前世今生
1983年孟买26岁的工程师Vivek Ranadive设想一种软件总线,同年Teknekron诞生了. 最初用于高盛,用于解决金融交易.于是发布订阅的MQ The Information B ...
- Lucene 的 Scoring 评分机制
转自: http://www.oschina.net/question/5189_7707 Lucene 评分体系/机制(lucene scoring)是 Lucene 出名的一核心部分.它对用户来 ...
- Java并发之synchronized深入
一句话总结synchronized: JVM会自动通过使用monitor来加锁和解锁,保证了同时只有一个线程可以执行指定代码,从而保证了线程安全,同时具有可重入和不可中断的性质. 一.synchron ...
- PHP对象的使用,什么时候可以用中括号[], 什么时候可以用箭头->
$orderTPLMessage = (object)array( 'touser' => '这里填open id', 'template_id' => 'oQDOldy7q6CdaYw2 ...
- JavaScript权威指南--词法结构
使用广泛,所有的浏览器(桌面.手机.屏蔽等等)都配有相应的JavaScript解析器. JavaScript解析器如何工作? 浏览器在读取HTML文件的时候,只有当遇到<script>标签 ...
- python异常列表
http://www.runoob.com/python/python-exceptions.html https://www.cnblogs.com/zhangyingai/p/7097920.ht ...
- Template、ItemsPanel、ItemContainerStyle、ItemTemplate (部分内容有待验证)
以下摘自“CSDN”的某人博客,部分内容有待验证,需注意“辨别学之....” 1.Template是指控件的样式 在WPF中所有继承自contentcontrol类的控件都含有此属性,(继承自Fram ...
- Shell_NotifyIcon托盘图标闪烁
之前的同事留下了一个程序会莫名闪退的bug,今天终于发现是托盘图标闪烁使得gdi资源耗尽导致的. 先定义 #include "shellapi.h" //托盘图标引用 NOTIFY ...