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