頻道管理 第三方外掛
阿新 • • 發佈:2018-12-20
** 專案的(不是 module) build.grade
在 allprojects–repositories 裡 加上
maven {url “https://jitpack.io”} **
本module繼承依賴
implementation 'com.github.andyoom:draggrid:v1.0.1’
在Activity 裡
package com.example.test01; import android.annotation.SuppressLint; import android.content.Intent; import android.support.annotation.Nullable; import android.support.design.widget.TabLayout; import android.support.v4.app.Fragment; import android.support.v4.view.ViewPager; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.view.View; import android.widget.Button; import com.andy.library.ChannelActivity; import com.andy.library.ChannelBean; import com.example.test01.Adapter.PinDao_FA; import com.example.test01.Fragment.BaseFragment.BlankFragment; import com.example.test01.Fragment.BaseFragment.VP_Fragment1; import com.example.test01.Fragment.BaseFragment.VP_Fragment2; import com.example.test01.Fragment.BaseFragment.VP_Fragment3; import com.google.gson.Gson; import com.google.gson.reflect.TypeToken; import java.io.Serializable; import java.lang.reflect.Type; import java.util.ArrayList; import java.util.List; public class PinDaoActivity extends AppCompatActivity { private List<ChannelBean> channelBeanList = new ArrayList<>(); private List<Fragment> fragmentList = new ArrayList<>(); private TabLayout tabLayout; private ViewPager viewPager; private PinDao_FA adapter; @SuppressLint("WrongViewCast") @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_pindao); // tabLayout = findViewById(R.id.pindao_tab); viewPager = findViewById(R.id.pindao_pager); // Button btn = findViewById(R.id.pindao_jia); // initData(); // btn.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { ChannelActivity.startChannelActivity(PinDaoActivity.this,channelBeanList); } }); // //新增Fragment for (int i = 0; i < channelBeanList.size(); i++) { //如果被選中 就會新建Fragment 並且 給tab 新增標籤 if(channelBeanList.get(i).isSelect()){ String name = channelBeanList.get(i).getName(); tabLayout.addTab(tabLayout.newTab().setText(name)); if (i == 0) { fragmentList.add(new VP_Fragment1()); }else if(i == 1){ fragmentList.add(new VP_Fragment2()); }else if(i == 2){ fragmentList.add(new VP_Fragment3()); }else { //fragmentList.add(new BlankFragment()); } } } //// adapter = new PinDao_FA(getSupportFragmentManager(), fragmentList, channelBeanList); viewPager.setAdapter(adapter); tabLayout.setupWithViewPager(viewPager); } private void initData() { channelBeanList.add(new ChannelBean("熱點",true)); channelBeanList.add(new ChannelBean("北京",true)); channelBeanList.add(new ChannelBean("房價",true)); channelBeanList.add(new ChannelBean("旅遊",false)); channelBeanList.add(new ChannelBean("條目1",false)); channelBeanList.add(new ChannelBean("條目2",false)); } @Override protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) { super.onActivityResult(requestCode, resultCode, data); //01 String json = data.getStringExtra(ChannelActivity.RESULT_JSON_KEY); //02 Type type = new TypeToken<ArrayList<ChannelBean>>(){}.getType(); //03 channelBeanList = new Gson().fromJson(json, type); //04 tabLayout.removeAllTabs(); fragmentList.clear(); //05迴圈 for (int i = 0; i < channelBeanList.size(); i++) { //06拿出一個bean ChannelBean channelBean = channelBeanList.get(i); //07判斷 選中 if (channelBean.isSelect()) { //08拿出個名字 String tabName = channelBeanList.get(i).getName(); //給tab標籤賦值 tabLayout.addTab(tabLayout.newTab().setText(tabName)); //如果是熱點就例項一個Fragment if ("熱點".equals(channelBean.getName())) { fragmentList.add(new VP_Fragment1()); }else if("北京".equals(channelBean.getName())){ fragmentList.add(new VP_Fragment2()); }else if("房價".equals(channelBean.getName())){ fragmentList.add(new VP_Fragment3()); }else { fragmentList.add(new BlankFragment()); } } } //因為集合中的Fragment數量改變了; adapter.notifyDataSetChanged(); } }
介面卡
package com.example.test01.Adapter; import android.support.annotation.Nullable; import android.support.v4.app.Fragment; import android.support.v4.app.FragmentManager; import android.support.v4.app.FragmentPagerAdapter; import com.andy.library.ChannelBean; import java.nio.channels.Channel; import java.util.List; public class PinDao_FA extends FragmentPagerAdapter { private List<Fragment> fragmentList; private List<ChannelBean> channelBeanList; public PinDao_FA(FragmentManager fm, List<Fragment> fragmentList, List<ChannelBean> channelBeanList) { super(fm); this.fragmentList = fragmentList; this.channelBeanList = channelBeanList; } @Override public Fragment getItem(int i) { return fragmentList.get(i); } @Override public int getCount() { return fragmentList.size(); } @Nullable @Override public CharSequence getPageTitle(int position) { return channelBeanList.get(position).getName(); } }