博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
在Fragment中加一个嵌套了ListView的ScrollView(一)
阅读量:5025 次
发布时间:2019-06-12

本文共 8977 字,大约阅读时间需要 29 分钟。

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

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

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

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

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

布局---activity_main.xml

布局---but1_layout.xml

另一个布局里面啥也没写

 

主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
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;    }}

 

转载于:https://www.cnblogs.com/Liang-Blog/p/5789233.html

你可能感兴趣的文章
Nginx 基本 安装..
查看>>
【凸优化】保留凸性的几个方式(交集、仿射变换、投影、线性分式变换)
查看>>
NYOJ-613//HDU-1176-免费馅饼,数字三角形的兄弟~~
查看>>
TFS --- GrantBackup Plan Permissions Error
查看>>
傅里叶级数与积分方程
查看>>
软工作业3:用户体验分析——以“南通大学教务管理系统微信公众号”为例
查看>>
Css:背景色透明,内容不透明之终极方法!兼容所有浏览器
查看>>
我们前端跟后端是怎么合作的
查看>>
mysql存储过程
查看>>
洛谷P2556 [AHOI2002] 黑白图像压缩 [模拟]
查看>>
letecode [136] - Single Number
查看>>
linux下设置固定IP的方法
查看>>
VMware虚拟机下Linux系统的全屏显示
查看>>
net core体系-web应用程序-4asp.net core2.0 项目实战(任务管理系统)-2项目搭建
查看>>
高效的jQuery
查看>>
ubuntu 16.04 (软件应用)-输入法
查看>>
windos7修复引导扇区
查看>>
Leetcode总结之Backtracking
查看>>
Android开发学习之路-图片颜色获取器开发(1)
查看>>
StackExchange.Redis 官方文档(一) Basics
查看>>