下载链接 https://share.weiyun.com/5F53RTL
依赖
implementation 'com.android.support:support-v4:26.1.0'
implementation 'com.android.support:design:26.1.0'
implementation 'com.github.CymChad:BaseRecyclerViewAdapterHelper:2.9.44'
public class MainActivity extends AppCompatActivity {
private TabLayout tableLayout;
private ViewPager viewPager;
List<Fragment> list_fragment = new ArrayList<>();
private TabAdapter tabAdapter;
String[] favourites = {"页面一", "页面二", "页面三"};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initView();
initData();
}
private void initData() {
list_fragment.add(new DemoFragment());
list_fragment.add(new DemoFragment());
list_fragment.add(new DemoFragment());
tabAdapter = new TabAdapter(getSupportFragmentManager());
tableLayout.setupWithViewPager(viewPager);
viewPager.setAdapter(tabAdapter);
}
class TabAdapter extends FragmentPagerAdapter {
public TabAdapter(FragmentManager fm) {
super(fm);
}
@Override
public Fragment getItem(int position) {
return list_fragment.get(position);
}
@Override
public int getCount() {
return list_fragment.size();
}
@Override
public CharSequence getPageTitle(int position) {
return favourites[position];
}
}
private void initView() {
tableLayout = (TabLayout) findViewById(R.id.tabs);
viewPager = (ViewPager) findViewById(R.id.viewpager);
}
}
/**
* Created by sloop on 2019/4/23.
*/
public class DemoFragment extends Fragment {
private RecyclerView recyclerView;
private RvAdapter rvAdpater;
List<String> mDatas = new ArrayList<>();
private LinearLayoutManager mLinearLayoutManager;
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_demo, container, false);
recyclerView = view.findViewById(R.id.rv);
mLinearLayoutManager = new LinearLayoutManager(getActivity());
return view;
}
@Override
public void onActivityCreated(@Nullable Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
for (int i = 0; i < 30; i++) {
mDatas.add(i + "");
}
rvAdpater = new RvAdapter(R.layout.item_rv, mDatas);
recyclerView.setLayoutManager(mLinearLayoutManager);
//分割线
DividerItemDecoration itemDecoration = new DividerItemDecoration(getActivity(), DividerItemDecoration.VERTICAL);
itemDecoration.setDrawable(getResources().getDrawable(R.drawable.divider_inset));
recyclerView.addItemDecoration(itemDecoration);
//加载动画
rvAdpater.openLoadAnimation();
rvAdpater.openLoadAnimation(BaseQuickAdapter.SCALEIN);
//空视图
rvAdpater.setEmptyView(getview());
recyclerView.setAdapter(rvAdpater);
rvAdpater.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
@Override
public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
Toast.makeText(getActivity(), "点击了" + position, Toast.LENGTH_SHORT).show();
}
});
}
public View getview(){
return LayoutInflater.from(getActivity()).inflate(R.layout.null_layou,null);
}
}
public class RvAdapter extends BaseQuickAdapter<String, BaseViewHolder> {
public RvAdapter(int layoutResId, @Nullable List<String> data) {
super(layoutResId, data);
}
@Override
protected void convert(BaseViewHolder helper, String item) {
helper.setText(R.id.tv_item, item);
}
}
divider_inset.xml
<?xml version="1.0" encoding="utf-8"?>
<inset xmlns:android="http://schemas.android.com/apk/res/android"
android:drawable="@drawable/shape_line" />
shape_line.xml
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<size android:height="0.5dp" />
<solid android:color="#FFE8E8E8" />
</shape>
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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:layout_width="match_parent"
android:orientation="vertical"
android:layout_height="match_parent"
tools:context="com.sloop.tabdemo.MainActivity">
<android.support.design.widget.TabLayout
android:id="@+id/tabs"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
app:tabSelectedTextColor="#f00"
app:tabTextColor="#000"
app:tabIndicatorHeight="3dp" />
<android.support.v4.view.ViewPager
android:id="@+id/viewpager"
android:layout_width="match_parent"
android:layout_height="match_parent">
</android.support.v4.view.ViewPager>
</LinearLayout>
fragment_demo.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<android.support.v7.widget.RecyclerView
android:id="@+id/rv"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</LinearLayout>
item_rv.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<TextView
android:id="@+id/tv_item"
android:layout_width="match_parent"
android:gravity="center"
android:text="数据"
android:layout_height="60dp" />
</LinearLayout>
null_layou.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:orientation="vertical">
<ImageView
android:id="@+id/iv_no"
android:layout_width="50dp"
android:layout_height="50dp"
android:src="@mipmap/no_data" />
<TextView
android:id="@+id/tv_no"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:text="NoData"
android:textColor="#000000"
android:textSize="14sp" />
</LinearLayout>
网友评论