step1:
implementation 'com.google.android.material:material:1.1.0'
step2: activity
package com.example.mepositry;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentActivity;
import androidx.viewpager.widget.ViewPager;
import android.os.Bundle;
import com.google.android.material.tabs.TabLayout;
import java.util.ArrayList;
import java.util.List;
public class TabDataActivity extends FragmentActivity {
List<Fragment> fragmentList = new ArrayList<>();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_tab);
List<String> curnewslist = new ArrayList<>();
curnewslist.add("娱乐");
curnewslist.add("军事");
curnewslist.add("财经");
curnewslist.add("体育");
fragmentList.add(new EntertainmentFragment());
fragmentList.add(new MilitaryFragment());
fragmentList.add(new FinanceFragment());
fragmentList.add(new SportFragment());
TabLayout tabLayout = findViewById(R.id.main_tablayout);
ViewPager viewPager = findViewById(R.id.main_newspager);
viewPager.setAdapter(new FragmentAdapter(getSupportFragmentManager(), fragmentList, curnewslist));
tabLayout.setupWithViewPager(viewPager);
}
}
step3: xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/maindrawerlayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<androidx.viewpager.widget.ViewPager
android:id="@+id/main_newspager"
android:layout_width="match_parent"
android:layout_below="@+id/main_tablayout"
android:layout_height="match_parent" />
<com.google.android.material.tabs.TabLayout
android:id="@+id/main_tablayout"
android:layout_width="match_parent"
android:layout_height="50dp"
app:tabGravity="fill"
app:tabIndicatorFullWidth="false"
app:tabIndicatorHeight="2dp"
app:tabMode="fixed"
app:tabBackground="@color/blue"
app:tabIndicatorColor="@color/orange"
app:tabSelectedTextColor="@color/red"
app:tabTextColor="@color/white"
app:tabPaddingStart="0dp"
app:tabPaddingEnd="0dp"
app:tabRippleColor="@android:color/transparent" />
</RelativeLayout>
step4:? adapter
package com.example.mepositry;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentPagerAdapter;
import java.util.List;
public class FragmentAdapter extends FragmentPagerAdapter {
List<Fragment> fragmentList;
List<String> newstypeList;
public FragmentAdapter(@NonNull FragmentManager fm, List<Fragment> fragmentListin, List<String> newstypeListin) {
super(fm);
this.fragmentList = fragmentListin;
this.newstypeList = newstypeListin;
}
@NonNull
@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 newstypeList.get(position);
}
}
step5:? fragment
package com.example.mepositry;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
public class EntertainmentFragment extends Fragment {
@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_entertainment, container, false);
return view;
}
}
end
|