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