现在的位置: 首页 > 综合 > 正文

Activity中的按钮去控制ViewPager与Fragment结合后,Fragment在ViewPager中的动态增加删除现实

2017年11月11日 ⁄ 综合 ⁄ 共 7015字 ⁄ 字号 评论关闭

     最近研究ViewPager+Fragment,遇到一个问题,就是外面的按钮怎么去控制Fragment动态的增加删除。让界面看起来好像有两个ViewPager在动态切换一样。研究老半天,效果实现了,写个博客做个笔记。

    MainActivity的布局文件:

   <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent" >

<android.support.v4.view.ViewPager
android:id="@+id/pager"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_above="@+id/ll"
android:layout_below="@+id/titlelayout" >
</android.support.v4.view.ViewPager>

<LinearLayout
android:id="@+id/titlelayout"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentTop="true" >

<RadioGroup
android:id="@+id/tab_menu"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="@drawable/tab_bg_normal"
android:orientation="horizontal" >

<RadioButton
android:id="@+id/title1"
style="@style/tab_style"
android:checked="true"
android:drawableTop="@drawable/tab_activity_selector"
android:text="活动" />

<RadioButton
android:id="@+id/title2"
style="@style/tab_style"
android:drawableTop="@drawable/tab_friend"
android:text="工作" />

<RadioButton
android:id="@+id/title3"
style="@style/tab_style"
android:drawableTop="@drawable/tab_friend"
android:text="圈子" />
<RadioButton
android:id="@+id/title4"
style="@style/tab_style"
android:visibility="gone"
android:drawableTop="@drawable/tab_activity_selector"
android:text="金刚" />

<RadioButton
android:id="@+id/title5"
style="@style/tab_style"
android:visibility="gone"
android:drawableTop="@drawable/tab_friend"
android:text="红星" />

<RadioButton
android:id="@+id/title6"
style="@style/tab_style"
android:visibility="gone"
android:drawableTop="@drawable/tab_friend"
android:text="屋子" />
</RadioGroup>
</LinearLayout>

<LinearLayout
   android:id="@+id/ll"
   android:layout_width="match_parent"
   android:layout_height="wrap_content"
   android:orientation="vertical"
   android:layout_alignParentBottom="true" >
   
   <Button 
       android:id="@+id/bt1"
       android:text="壹"
       android:layout_width="match_parent"
       android:layout_height="wrap_content"/>
   <Button 
       android:id="@+id/bt2"
       android:text="贰"
       android:layout_width="match_parent"
       android:layout_height="wrap_content"/>
   
</LinearLayout>

</RelativeLayout>

    上面六个按钮,通过Visible与gone控制每次只显示三个。下面两个button,用来控制Fragment动态显示隐藏。

package com.llb.view;

import java.util.ArrayList;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v4.view.ViewPager.OnPageChangeListener;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.RadioButton;

import com.llb.adapter.MyViewPagerAdapter;
import com.llb.fragment.ActivityFragment;
import com.llb.fragment.FriendFragment;
import com.llb.fragment.HongXingFragment;
import com.llb.fragment.JinGangFragment;
import com.llb.fragment.JobFragment;
import com.llb.fragment.WuZiFragment;

public class MainActivity extends FragmentActivity implements OnPageChangeListener {
private ViewPager pager;
private PagerAdapter mAdapter;

private ArrayList<Fragment> fragments;
private ArrayList<RadioButton> title = new ArrayList<RadioButton>();// 三个标题
private RadioButton rb1,rb2,rb3,rb4,rb5,rb6;
private Button bt1,bt2;
private int a=1;

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
bt1=(Button)findViewById(R.id.bt1);
bt2=(Button)findViewById(R.id.bt2);

rb1=(RadioButton)findViewById(R.id.title1);
rb2=(RadioButton)findViewById(R.id.title2);
rb3=(RadioButton)findViewById(R.id.title3);
rb4=(RadioButton)findViewById(R.id.title4);
rb5=(RadioButton)findViewById(R.id.title5);
rb6=(RadioButton)findViewById(R.id.title6);

initView();// 初始化控件
initTitle();
initViewPager();

bt1.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
fragments.clear();
fragments.add(new ActivityFragment());
fragments.add(new JobFragment());
fragments.add(new FriendFragment());

rb1.setVisibility(View.VISIBLE);
rb2.setVisibility(View.VISIBLE);
rb3.setVisibility(View.VISIBLE);
rb4.setVisibility(View.GONE);
rb5.setVisibility(View.GONE);
rb6.setVisibility(View.GONE);
title.clear();
title.add(rb1);
title.add(rb2);
title.add(rb3);

rb1.setChecked(true);
pager.setCurrentItem(0);
mAdapter.notifyDataSetChanged();

}
});
bt2.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
fragments.clear();
fragments.add(new JinGangFragment());
fragments.add(new HongXingFragment());
fragments.add(new WuZiFragment());

rb1.setVisibility(View.GONE);
rb2.setVisibility(View.GONE);
rb3.setVisibility(View.GONE);
rb4.setVisibility(View.VISIBLE);
rb5.setVisibility(View.VISIBLE);
rb6.setVisibility(View.VISIBLE);
title.clear();
title.add(rb4);
title.add(rb5);
title.add(rb6);

rb4.setChecked(true);
pager.setCurrentItem(0);
mAdapter.notifyDataSetChanged();
}
});
}

/**
* 初始化视图
*/
private void initView() {
pager = (ViewPager) findViewById(R.id.pager);// 初始化控件

fragments = new ArrayList<Fragment>();// 初始化数据
fragments.add(new ActivityFragment());
fragments.add(new JobFragment());
fragments.add(new FriendFragment());
}

/**
* 初始化ViewPager
*/
private void initViewPager() {
mAdapter = new MyViewPagerAdapter(getSupportFragmentManager(), fragments);
pager.setAdapter(mAdapter);
pager.setOnPageChangeListener(this);
pager.setCurrentItem(0);// 设置成当前第一个
}

/**
* 初始化几个用来显示title的RadioButton
*/
private void initTitle() {
title.add(rb1);// 三个title标签
title.add(rb2);
title.add(rb3);
}

/**
* 下面三个是OnPageChangeListener的接口函数
*/
@Override
public void onPageScrollStateChanged(int arg0) {
}

@Override
public void onPageScrolled(int arg0, float arg1, int arg2) {
}

@Override
public void onPageSelected(int arg0) {
title.get(arg0).setChecked(true);// 保持页面跟按钮的联动
}
}

MainActivity重点就是mAdapter.notifyDataSetChanged();这个东西在数据变化的时候调用。

要让notifyDataSetChanged有效,重点就是adapter的写法。

package com.llb.adapter;

import java.util.ArrayList;

import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.app.FragmentStatePagerAdapter;
import android.support.v4.view.PagerAdapter;
/**
 * 这个adapter里面有Fragment数组
 * @author llb
 *
 */
public class MyViewPagerAdapter extends FragmentStatePagerAdapter {

private ArrayList<Fragment> fragments;//需要添加到上面的Fragment

public MyViewPagerAdapter(FragmentManager fm) {
super(fm);
}
/**
* 自定义的构造函数
* @param fm
* @param fragments ArrayList<Fragment>
*/
public MyViewPagerAdapter(FragmentManager fm,ArrayList<Fragment> fragments) {
super(fm);
this.fragments = fragments;
}
@Override
public Fragment getItem(int arg0) {
return fragments.get(arg0);//返回Fragment对象
}
@Override
public int getCount() {
return fragments.size();//返回Fragment的个数
}

@Override
public int getItemPosition(Object object) {
        return PagerAdapter.POSITION_NONE;
}

}

Adapter必须要继承自FragmentStatePagerAdapter ,而不是FragmentPagerAdapter,具体的原因就是ViewPager切换时,有预加载,用 FragmentPagerAdapter是达不到Fragment及时隐藏的效果的。同时,注意重写

       public int getItemPosition(Object object) {
             return PagerAdapter.POSITION_NONE;
}

  算做个笔记吧,里面几个原因为什么会这样,我也不太清楚,慢慢研究。

Demo下载:http://download.csdn.net/detail/xxlwanmm/8233617

抱歉!评论已关闭.