viewpager+fragment 动态添加删除

1简介

viewpager+fragment 动态更新页面没什么好介绍的 就一点 当后台数据变更时遍历删除所有fragment 再重新赋值. 简单粗暴请码友们收下....

package com.refl.wind.myapplication;

import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.AdapterViewFlipper;
import android.widget.Button;

import java.util.ArrayList;

public class HomeActivity extends FragmentActivity implements View.OnClickListener  {
    private Button b1, b2;
    private ArrayList<String> mlist;
    private ViewPager mviewpager;
    private MyFragmenetAdapter vfAdapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        b1 = (Button) findViewById(R.id.b1);
        b2 = (Button) findViewById(R.id.b2);
        mviewpager = (ViewPager) findViewById(R.id.viewP);
        b1.setOnClickListener(this);
        b2.setOnClickListener(this);
        mlist = new ArrayList<>();
        for (int i = 0; i < 2; i++) {
            mlist.add(i + Text.stri);
        }
        vfAdapter = new MyFragmenetAdapter(getSupportFragmentManager(),mlist);
        mviewpager.setAdapter(vfAdapter);
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.b1:
                mlist.add("-------->添加数据");
                vfAdapter.updateData(mlist);
                break;

            case R.id.b2:
                mlist.clear();
                vfAdapter.updateData(mlist);
                break;

        }
    }


}
2. fragment
package com.refl.wind.myapplication;

import android.app.Activity;
import android.content.Context;
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.TextView;

public class ShowFragment extends Fragment {
    // 标志位,标志已经初始化完成。
    public static final String msg = "msg";
    private Activity mact;
    private View mView;
    private String msgShow;
    private TextView content;

    @Override
    public void onAttach(Context context) {
        super.onAttach(context);
        mact = getActivity();

    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        mView = inflater.inflate(R.layout.homefragment, container, false);
        Bundle args = getArguments();
        if (args != null) {
            msgShow = getArguments().getString(msg);
        }
        initView();
        initData();
        return mView;
    }

    private void initView() {
        content = (TextView) mView.findViewById(R.id.martitle);
        content.setText(msgShow);
    }

    private void initData() {

    }


}
3 Adapter

package com.refl.wind.myapplication;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.app.FragmentTransaction;
import android.util.Log;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by PVer on 2017/4/1.
 */
public class MyFragmenetAdapter extends FragmentPagerAdapter {
    public ArrayList<Fragment> mFragmentList;
    private FragmentManager mFragmentManager;

    public MyFragmenetAdapter(FragmentManager fm, List<String> beans) {
        super(fm);
        this.mFragmentManager = fm;
        mFragmentList = new ArrayList<>();
        for (String msg : beans) {
           ShowFragment showfragment = new ShowFragment();
            Bundle bundle = new Bundle();
            bundle.putString("msg", msg.toString());
            showfragment.setArguments(bundle);
            mFragmentList.add(showfragment);
        }
        setFragments(mFragmentList);
    }

    public ArrayList<Fragment> getArrayList() {
        return mFragmentList;
    }

    @Override
    public int getCount() {
        return this.mFragmentList.size();
    }

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

    @Override
    public Fragment getItem(int position) {
        return mFragmentList.get(position);
    }

    public void updateData(List<String> beans) {
        ArrayList<Fragment> fragments = new ArrayList<>();
        for (String msg : beans) {
            ShowFragment showfragment = new ShowFragment();
            Bundle bundle = new Bundle();
            bundle.putString("msg", msg.toString());
            showfragment.setArguments(bundle);
            fragments.add(showfragment);
        }
        setFragments(fragments);
    }


    public void setFragments(ArrayList<Fragment> mFList) {
        if (this.mFragmentList != null) {
            FragmentTransaction fragmentTransaction = mFragmentManager.beginTransaction();
            for (Fragment f : this.mFragmentList) {
                fragmentTransaction.remove(f);
            }
            fragmentTransaction.commit();
            mFragmentManager.executePendingTransactions();

        }
        this.mFragmentList = mFList;
        notifyDataSetChanged();
    }


}



代码链接 http://download.csdn.net/detail/qq_38800775/9847913




评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值