频道管理

1、添加依赖
compile ‘com.github.andyoom:draggrid:v1.0.1’
2、在项目的build.gradle中添加(注,编译会爆错,尝试着添加下面的步骤)

allprojects {
    repositories {
        jcenter()
//下面这句才是加上的,上面的自带 的
        maven {url "https://jitpack.io"}
    }
 }

3、XML布局

    <android.support.design.widget.TabLayout
        android:id="@+id/Tab_Layout"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        app:tabMode="scrollable" />

    <TextView
        android:id="@+id/Add_Fragment"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentRight="true"
        android:layout_centerInParent="true"
        android:text="+"
        android:textSize="40sp" />
</RelativeLayout>

<android.support.v4.view.ViewPager
    android:id="@+id/View_Pager"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
    
</android.support.v4.view.ViewPager>

4、MainActivity

public class MainActivity extends AppCompatActivity implements View.OnClickListener {
private TabLayout Tab_Layout;
private TextView Image_Btn;
private ArrayList<ChannelBean> channelBeens;
String jsonStr = "";
private Gson gson;
private Button test_btn;
private ArrayList<Fragment> mList = new ArrayList<>();
private ViewPager mPager;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    initView();
    initData();
}


private void initView() {
    Tab_Layout = (TabLayout) findViewById(R.id.Tab_Layout);
    Image_Btn = findViewById(R.id.Add_Fragment);
    mPager = findViewById(R.id.View_Pager);
    Image_Btn.setOnClickListener(this);
}

private void initData() {
    //准备栏目数据
    channelBeens = new ArrayList<ChannelBean>();
    channelBeens.add(new ChannelBean("热点", true));
    channelBeens.add(new ChannelBean("军事", true));
    channelBeens.add(new ChannelBean("八卦", true));
    channelBeens.add(new ChannelBean("游戏", true));
    channelBeens.add(new ChannelBean("宠物", true));
    channelBeens.add(new ChannelBean("汽车", true));
    channelBeens.add(new ChannelBean("热卖", true));
    channelBeens.add(new ChannelBean("外卖", true));
    channelBeens.add(new ChannelBean("条目1", true));
    channelBeens.add(new ChannelBean("条目2", true));
    channelBeens.add(new ChannelBean("条目3", false));
    channelBeens.add(new ChannelBean("条目4", false));
    channelBeens.add(new ChannelBean("条目5", false));
    channelBeens.add(new ChannelBean("条目6", false));
    channelBeens.add(new ChannelBean("条目7", false));
    channelBeens.add(new ChannelBean("条目8", false));
    mList.add(new AFragment());
    mList.add(new BFragment());
    mList.add(new AFragment());
    mList.add(new BFragment());
    mList.add(new AFragment());
    mList.add(new BFragment());
    mList.add(new AFragment());
    mList.add(new BFragment());
    mList.add(new AFragment());
    mList.add(new AFragment());
    mList.add(new BFragment());
    mList.add(new AFragment());
    mList.add(new AFragment());
    mList.add(new AFragment());
    mList.add(new AFragment());
    mList.add(new AFragment());
    //把选择的栏目(true)数据配置给tablayout
    for (int i = 0; i < channelBeens.size(); i++) {
        if (channelBeens.get(i).isSelect()) {
            Tab_Layout.addTab(Tab_Layout.newTab().setText(channelBeens.get(i).getName()));
        }
    }
    mPager.setAdapter(new FragmentPagerAdapter(getSupportFragmentManager()) {
        @Override
        public Fragment getItem(int i) {
            return mList.get(i);
        }

        @Override
        public int getCount() {
            int count = 0;
            for (int i = 0; i < channelBeens.size(); i++) {
                if (channelBeens.get(i).isSelect()) {
                    count++;
                }
            }
            return count;
        }

        @Nullable
        @Override
        public CharSequence getPageTitle(int position) {
            return channelBeens.get(position).getName();
        }
    });
    Tab_Layout.setupWithViewPager(mPager);
}

@Override
public void onClick(View v) {
    switch (v.getId()) {
        case R.id.Add_Fragment:
            ChannelActivity.startChannelActivity(MainActivity.this, channelBeens);
            break;
    }
}

@Override
protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
    super.onActivityResult(requestCode, resultCode, data);

    if (requestCode == ChannelActivity.REQUEST_CODE && resultCode == ChannelActivity.RESULT_CODE) {//为true表示是频道管理回调回来的
        jsonStr = data.getStringExtra(ChannelActivity.RESULT_JSON_KEY);//得到栏目管理的结果
        Toast.makeText(this, jsonStr, Toast.LENGTH_SHORT).show();
        Log.i("main", jsonStr);
        Tab_Layout.removeAllTabs();//清空之前的栏目
        //把新选择的栏目结果更新到tablayout上
        gson = new Gson();
        //进行json解析
        Type type = new TypeToken<ArrayList<ChannelBean>>() {
        }.getType();
        channelBeens = gson.fromJson(jsonStr, type);
        //遍历结果,更新tablayout
        for (int i = 0; i < channelBeens.size(); i++) {
            if (channelBeens.get(i).isSelect()) {
                try {
                    Tab_Layout.addTab(Tab_Layout.newTab().setText(channelBeens.get(i).getName()));
                } catch (Exception e) {
                }

            }
    	    }
      	  mPager.getAdapter().notifyDataSetChanged();
   		 }
  }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值