Tablayout_Viewpager
Viewpager 适配器
package com.example.weekthree_test01.Adapter.FragmentAdapter;
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.example.weekthree_test01.Fragment.WoDe_Fragment;
import com.example.weekthree_test01.Fragment.XinWen_Fragment;
import com.example.weekthree_test01.Fragment.TuPian_Fragment;
public class My_main_tab_FragmentAdapter extends FragmentPagerAdapter {
private String[] main_str = new String[]{
"图片","新闻","我的"
};
public My_main_tab_FragmentAdapter(FragmentManager fm) {
super(fm);
}
@Override
public Fragment getItem(int i) {
switch (i){
case 0:
return new TuPian_Fragment();
case 1:
return new XinWen_Fragment();
case 2:
return new WoDe_Fragment();
default: return new Fragment();
}
}
@Nullable
@Override
public CharSequence getPageTitle(int position) {
return main_str[position];
}
@Override
public int getCount() {
return main_str.length;
}
}
TabLayout 与ViewPager操作
//获取ID
TabLayout main_tab = findViewById(R.id.main_tab);
ViewPager main_viewpager = findViewById(R.id.main_pager);
//添加适配器
main_viewpager.setAdapter(new My_main_tab_FragmentAdapter(getSupportFragmentManager()));
//绑定
main_tab.setupWithViewPager(main_viewpager);
二级采样
//二次采样
public static Bitmap scaleBitmap(String imagePath, int width, int height){
//先得到 BitmapFactory.options
BitmapFactory.Options options = new BitmapFactory.Options();
//02让他只加载边框--说白的就是加载图片的所有信息 就是不加载图片
options.inJustDecodeBounds=true;
//03 把信息和图片给这个工厂
BitmapFactory.decodeFile(imagePath,options);
//04 我需要 规定一下宽高 所以需要传参宽高过来
//就是--- 改变好宽高的一个架子
options.inSampleSize=Math.max(options.outWidth/width,options.outHeight/height);
//05加载出来这个图片
options.inJustDecodeBounds = false;
//06 把图片的路径imagpath 用工厂factory给它 加上之前定义的那个规则options
return BitmapFactory.decodeFile(imagePath,options);
}
初始化ImageLoader
ImageLoader.getInstance().init(
new ImageLoaderConfiguration.Builder(this)
.diskCacheSize(50 * 1024 * 1024)
.memoryCacheSizePercentage(10)
.defaultDisplayImageOptions(
new DisplayImageOptions.Builder()
.cacheInMemory(true)
.cacheOnDisk(true)
.showImageOnLoading(R.mipmap.ic_launcher)
.showImageOnFail(R.mipmap.ic_launcher)
.showImageForEmptyUri(R.mipmap.ic_launcher)
.display(圆形或圆角)
.build()
)
.build()
);
添加Fragment
判断是在Activity还是Fragmen
第一个参数是占位符的 id
getSupportFragmentManager().beginTransaction()
.add(R.id.left_drawer, new LeftFragment())
.commit();
基类Activity
public abstract class BaseActivity extends AppCompatActivity {
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(getLayoutResId());
//
initView(savedInstanceState);
initData();
}
protected abstract void initData();
protected abstract void initView(Bundle saveInstanceState);
protected abstract int getLayoutResId();
}
Fragment基类
public abstract class BaseFragment extends Fragment {
@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
return inflater.inflate(getLayoutResId(), container, false);
}
@Override
public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
initView(view);
initData();
}
protected abstract void initData();
protected abstract void initView(View view);
protected abstract int getLayoutResId();
}