import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.support.design.widget.TabLayout;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.widget.Toast;
import com.ansen.tablayout.fragment.FragmentTest;
public class MainActivity extends AppCompatActivity implements View.OnClickListener {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//设置标题内容以及标题文字颜色
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
toolbar.setTitle("关注公众号[Android开发者666]");
//toolbar.setTitleTextColor(获取资源.获取颜色(颜色资源id))
toolbar.setTitleTextColor(getResources().getColor(android.R.color.white));
ViewPager vPager = (ViewPager) findViewById(R.id.viewPager);
vPager.setOffscreenPageLimit(2);//设置缓存页数
vPager.setCurrentItem(0);//设置当前显示的item 0表示显示第一个
FragmentAdapter pagerAdapter = new FragmentAdapter(getSupportFragmentManager());
FragmentTest fragmentOne=new FragmentTest();
FragmentTest fragmentTwo=new FragmentTest();
FragmentTest fragmentThree=new FragmentTest();
//把Fragment一个个的添加到适配器中 参数2是选项卡的标题
pagerAdapter.addFragment(fragmentOne,"选项卡1");//
pagerAdapter.addFragment(fragmentTwo,"选项卡2");
pagerAdapter.addFragment(fragmentThree,"选项卡3");
//给ViewPager设置适配器
vPager.setAdapter(pagerAdapter);
TabLayout tabLayout = (TabLayout) findViewById(R.id.tabLayout);
//通过TabLayout的setupWithViewPager把ViewPager设置进去
tabLayout.setupWithViewPager(vPager);//tabLayout添加ViewPager分页视图进来就可以了
FloatingActionButton fab= (FloatingActionButton) findViewById(R.id.fab);
fab.setOnClickListener(this);
}
@Override
public void onClick(View view) {
Snackbar.make(view, "这是内容", Snackbar.LENGTH_SHORT).setAction
("取消", new View.OnClickListener() {
@Override
public void onClick(View view) {
Toast.makeText(MainActivity.this, "cancel",Toast.LENGTH_SHORT).show();
}
}).show();
}
}
布局
<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/coordinatorLayout"
android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.design.widget.AppBarLayout
android:id="@+id/appBarLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<android.support.v7.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@color/colorAccent"
app:layout_scrollFlags="scroll|enterAlways"/>
<!--
app:tabIndicatorColor="#ffffffff" 改变指示器下标的颜色
app:tabIndicatorHeight="2dp":设置指示器下标的高度:
app:tabSelectedTextColor="#ffffffff":.改变选中字体的颜色
app:tabTextColor="#bebebe":改变未选中字体的颜色
注意:都是用app命名的
-->
<android.support.design.widget.TabLayout
android:id="@+id/tabLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:background="#54B0E9"
app:tabIndicatorColor="#ffffffff"
app:tabIndicatorHeight="2dp"
app:tabSelectedTextColor="#ffffffff"
app:tabTextColor="#bebebe"/>
</android.support.design.widget.AppBarLayout>
<!--
注意:这个app:layout_behavior="@string/appbar_scrolling_view_behavior" 滚动行为
这个布局行为ViewPager记得要添加
-->
<android.support.v4.view.ViewPager
android:id="@+id/viewPager"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="@string/appbar_scrolling_view_behavior"/>
<android.support.design.widget.FloatingActionButton
android:id="@+id/fab"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="end|bottom"
android:layout_margin="15dp"
android:src="@mipmap/ic_launcher"/>
</android.support.design.widget.CoordinatorLayout>
2.FragmentAdapter
/**
* ViewPager适配器
* @author ansen
* @create time 2019-04-07
*/
public class FragmentAdapter extends FragmentStatePagerAdapter {
private final List<Fragment> fragmentList = new ArrayList<Fragment>();
private final List<String> fragmentTitleList = new ArrayList<String>();
public FragmentAdapter(FragmentManager fm) {
super(fm);
}
@Override
public Fragment getItem(int arg0) {
return fragmentList.get(arg0);
}
@Override
public int getCount() {
return fragmentList.size();
}
@Override
public CharSequence getPageTitle(int position) {
return fragmentTitleList.get(position);
}
public void addFragment(Fragment fragment, String title) {
fragmentList.add(fragment);
fragmentTitleList.add(title);
}
}
3.FragmentTest
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.Toolbar;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import com.ansen.tablayout.R;
/**
* Created by ansen
* Create Time 2017-04-07
*/
public class FragmentTest extends Fragment{
private RecyclerView recyclerView;
private String[] data=new String[20];
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState){
/**
* 碎片布局里面用了RecyclerView
* 这里碎片:RecyclerView都在这里面写了
*/
View rootView=LayoutInflater.from(getActivity()).inflate(R.layout.fragment_test, null);
recyclerView= (RecyclerView) rootView.findViewById(R.id.recyclerView);
initData();
recyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));
recyclerView.setAdapter(new RecyclerViewAdapter());
return rootView;
}
/**
* 这里面的话,碎片和RecyclerView适配器放在一起了(也就是碎片里面有RecyclerView)
*/
public class RecyclerViewAdapter extends RecyclerView.Adapter<RecyclerViewAdapter.MyViewHolder>{
@Override
public RecyclerViewAdapter.MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
//注意:这里是碎片类里面所以from()里面是获取当前活动
//item用的是android.R.layout.simple_list_item_1 (跟listView用的那个差不多)
MyViewHolder holder = new MyViewHolder(LayoutInflater.from(getActivity()).
inflate(android.R.layout.simple_list_item_1, parent,false));
return holder;
}
@Override
public void onBindViewHolder(RecyclerViewAdapter.MyViewHolder holder, int position) {
holder.text1.setText(data[position]);
}
@Override
public int getItemCount() {
return data.length;
}
class MyViewHolder extends RecyclerView.ViewHolder{
TextView text1;
public MyViewHolder(View view){
super(view);
text1 = (TextView) view.findViewById(android.R.id.text1);//这里用了系统的text1
}
}
}
private void initData(){
for(int i=0;i<data.length;i++){
data[i]="test"+i;//显示定义一个数组,然后依次赋值给里面的每一个值
}
}
}
布局fragmen_test.xml 里面放了一个RecyclerView
<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.RecyclerView
xmlns:android=“http://schemas.android.com/apk/res/android”
android:id="@+id/recyclerView"
android:layout_width=“match_parent”
android:layout_height=“match_parent” />


网友评论