在Fragment裡面寫TabLayout橫向滑動
阿新 • • 發佈:2018-12-10
import android.os.Bundle; import android.support.annotation.NonNull; import android.support.annotation.Nullable; import android.support.design.widget.TabLayout; import android.support.v4.app.Fragment; import android.support.v4.app.FragmentPagerAdapter; import android.support.v4.view.ViewPager; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import java.util.ArrayList; import java.util.List; public class ChannelFragment extends Fragment { private TabLayout tb_news; private List<String> titles; private List<Fragment> fragmentList; private List<String> urls; private ViewPager vp; @Nullable @Override public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) { View v = inflater.inflate(R.layout.item_channel,container,false); vp = v.findViewById(R.id.vp); tb_news = v.findViewById(R.id.tl_title); return v; } @Override public void onActivityCreated(@Nullable Bundle savedInstanceState) { super.onActivityCreated(savedInstanceState); titles = new ArrayList<>(); titles.add("全部"); titles.add("國內"); titles.add("國際"); titles.add("科技"); titles.add("股市"); titles.add("美股"); titles.add("財經"); titles.add("社會"); titles.add("體育"); titles.add("娛樂"); titles.add("軍事"); fragmentList = new ArrayList<>(); urls = new ArrayList<>(); urls.add("https://feed.mix.sina.com.cn/api/roll/get?pageid=153&lid=2509&k=&num=20&page="); urls.add("https://feed.mix.sina.com.cn/api/roll/get?pageid=153&lid=2510&k=&num=20&page="); urls.add("https://feed.mix.sina.com.cn/api/roll/get?pageid=153&lid=2511&k=&num=20&page="); urls.add("https://feed.mix.sina.com.cn/api/roll/get?pageid=153&lid=2515&k=&num=20&page="); urls.add("https://feed.mix.sina.com.cn/api/roll/get?pageid=153&lid=2517&k=&num=20&page="); urls.add("https://feed.mix.sina.com.cn/api/roll/get?pageid=153&lid=2518&k=&num=20&page="); urls.add("https://feed.mix.sina.com.cn/api/roll/get?pageid=153&lid=2516&k=&num=20&page="); urls.add("https://feed.mix.sina.com.cn/api/roll/get?pageid=153&lid=2669&k=&num=20&page="); urls.add("https://feed.mix.sina.com.cn/api/roll/get?pageid=153&lid=2512&k=&num=20&page="); urls.add("https://feed.mix.sina.com.cn/api/roll/get?pageid=153&lid=2513&k=&num=20&page="); urls.add("https://feed.mix.sina.com.cn/api/roll/get?pageid=153&lid=2514&k=&num=20&page="); for (int i = 0; i <urls.size() ; i++) { NewsFragment newsFragment = new NewsFragment(); Bundle bundle = new Bundle(); bundle.putString("urls",urls.get(i)); newsFragment.setArguments(bundle); fragmentList.add(newsFragment); } tb_news.setTabMode(TabLayout.MODE_SCROLLABLE); FragmentPagerAdapter adapter = new FragmentPagerAdapter(getChildFragmentManager()) { @Override public Fragment getItem(int position) { return fragmentList.get(position); } @Override public int getCount() { return fragmentList.size(); } @Nullable @Override public CharSequence getPageTitle(int position) { return titles.get(position); } }; vp.setAdapter(adapter); tb_news.setupWithViewPager(vp); } }