Android 频道管理(简单)

1 导入依赖

compile 'com.github.andyoom:draggrid:v1.0.1'

maven { url "https://jitpack.io" } ----------> (在项目的build.gradle导入)

 allprojects {
    repositories {
        google()
        jcenter()
        maven { url "https://jitpack.io" }
    }
}

2 简单布局
Android 频道管理(简单)_第1张图片

3 在MainActivity中

public class MainActivity extends AppCompatActivity {

//定义集合 用来存放频道管理 和 Fragment

ArrayList channelBeanlist = new ArrayList<>();
ArrayList fraglist = new ArrayList<>();
private ViewPager viewPager;
private TabLayout tablayout;
private Button button;
private MyAdapter myAdapter;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    
    tablayout = findViewById(R.id.tablayout);
    viewPager = findViewById(R.id.viewpager);
    intData();
    
    button = findViewById(R.id.button);
    button.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            ChannelActivity.startChannelActivity(MainActivity.this,channelBeanlist);
        }
    });
    myAdapter = new MyAdapter(getSupportFragmentManager());
    viewPager.setAdapter(myAdapter);
    tablayout.setupWithViewPager(viewPager);

}

//
private void intData() {
channelBeanlist.add(new ChannelBean(“热点”, true));
channelBeanlist.add(new ChannelBean(“旅游”, true));
channelBeanlist.add(new ChannelBean(“条目一”, false));
channelBeanlist.add(new ChannelBean(“条目二”, false));
channelBeanlist.add(new ChannelBean(“条目三”, false));
channelBeanlist.add(new ChannelBean(“条目四”, false));

    for (int i = 0; i 

@Override
protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
super.onActivityResult(requestCode, resultCode, data);
String s = data.getStringExtra(ChannelActivity.RESULT_JSON_KEY);
Type type = new TypeToken() {}.getType();

        Gson gson = new Gson();
        channelBeanlist = gson.fromJson(s, type);

        tablayout.removeAllTabs();
        fraglist.clear();

        for (int i = 0; i < channelBeanlist.size(); i++) {
               if ( channelBeanlist.get(i).isSelect())
               {
                   String name = channelBeanlist.get(i).getName();
                   tablayout.addTab(tablayout.newTab().setText(name));

                   if ("热点".equals(channelBeanlist.get(i).getName()))
                   {
                       fraglist.add(new Frag_01());

                   }else if("旅游".equals(channelBeanlist.get(i).getName()))
                   {
                       fraglist.add(new Frag_02());
                   }else
                   {
                       fraglist.add(new Blank());
                   }
               }
        }
        myAdapter.notifyDataSetChanged();
    }

class MyAdapter extends FragmentPagerAdapter{

    public MyAdapter(FragmentManager fm) {
        super(fm);
    }

    @Override
    public Fragment getItem(int i) {
        return fraglist.get(i);
    }

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

    @Nullable
    @Override
    public CharSequence getPageTitle(int position) {
        return channelBeanlist.get(position).getName();
    }
}

//如果在fragment 中打频道管理 需要在他所依赖的activity中 打一下代码
@Override
protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
super.onActivityResult(requestCode, resultCode, data);
FragmentManager manager = getSupportFragmentManager();
Fragment frag01 = manager.findFragmentByTag(“frag”);
frag01.onActivityResult(requestCode, resultCode, data);
}

你可能感兴趣的:(Android 频道管理(简单))