发表于: 2018-10-20 22:49:22
0 828
一、今天完成的事情
listview是Android里的重点,切换标签时可以采用fragment页面切换的方式,还可以通过不切换页面,只切换列表内数据的方式达到同样的效果,两种方式分别应该怎么做?
1.通过Fragment切换
标签部分可以使用RadioGroup + RadioButton或者第三方库来实现,也可以自定义标签设置好监听就行了,这部分不是重点,重要的是Fragment的切换过程,即在Activtiy中动态加载Fragment。动态加载分以下几步:
(1)创建Fragmnet实例
过程类似于Activity,但和Activity还是有区别的。
先创建布局;
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/white_eff0f4">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="50dp"
android:text="@string/home"
android:textSize="30sp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</android.support.constraint.ConstraintLayout>
再创建一个类继承Fragment 并加载的布局;
public class HomeFragment extends Fragment {
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle saveInsatnceState) {
return inflater.inflate(R.layout.fragment_home, container, false);
}
}
这样一个简单的Fragment就创建好了。
(2)在Fragment所处的Activity中获取FragmentManager并开启事务来操控Fragment
FragmentManager fm = getFragmentManager();
FragmentTransaction transaction = fm.beginTransaction();
(3)操作Fragment
transaction.add()
往Activity中添加一个Fragment;
transaction.remove()
从Activity中移除一个Fragment,如果被移除的Fragment没有添加到回退栈,其实例将会被销毁;
transaction.replace()
使用另一个Fragment替换当前的Fragment,也就是remove()+ add();
transaction.hide()
隐藏当前的Fragment,仅仅是设为不可见,并不会销毁;
transaction.show()
显示之前隐藏的Fragment;
detach()
将此Fragment从Activity中分离,会销毁其布局,但不会销毁该实例;
attach()
将从Activity中分离的Fragment,重新关联到该Activity,重新创建其视图层次;
(4)提交事务
transatcion.commit()
提交一个事务(提交操作)。
似乎使用replace()来切换Fragment非常方便,但是这样做存在一个问题,每次切换的时候Fragment都会重新实例化,重新加载数据,这样会拖累性能、浪费流量,除非这个Fragment不需要了,否则最好不要使用这种方法。一般的做法是先add()Fragment,切换时hide()当前的Fragment,然后show()下个Fragment。
/*
添加或者显示Fragment
*/
private void addOrShowFragment(FragmentTransaction transaction, Fragment fragment) {
if (currentFragment == fragment)
return;
if (!fragment.isAdded()) {
transaction.hide(currentFragment).add(R.id.showfragment_explore, fragment).commit();
} else {
transaction.hide(currentFragment).show(fragment).commit();
}
currentFragment = fragment;
}
另外,可以通过ViewPager来实现滑动切换Fragment的效果,在Activity的布局中添加ViewPager作为Fragment的容器
<android.support.v4.view.ViewPager
android:id="@+id/showfragment_main"
android:layout_width="match_parent"
android:layout_height="0dp"
app:layout_constraintBottom_toTopOf="@+id/line_bottom"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
然后在Activity中初始化Fragment和ViewPager后,为ViewPager设置一个滑动监听
//设置监听
viewPager.addOnPageChangeListener(new MyOnPageChangeListener()) ;
/**
* 设置页面滑动事件监听
*/
public class MyOnPageChangeListener implements ViewPager.OnPageChangeListener {
@Override
public void onPageSelected(int position) {
switch(position)
//滑动到页面0(主页home)
case 0:
if (currentIndex == 1) { //从页面1到页面0
resetBottomTab();
setBottomTab(position);
} else if(currentIndex == 2) {//从页面2到页面0
resetBottomTab();
setBottomTab(position);
}
break;
//滑动到页面1(浏览页explore)
case 1:
if (currentIndex == 0) { //从页面0到页面1
resetBottomTab();
setBottomTab(position);
} else if(currentIndex == 2) {//从页面2到页面1
resetBottomTab();
setBottomTab(position);
}
break;
//滑动到页面2(我的页me)
case 2:
if (currentIndex == 0) { //从页面0到页面2
resetBottomTab();
setBottomTab(position);
} else if(currentIndex == 1) {//从页面1到页面2
resetBottomTab();
setBottomTab(position);
}
break;
}
currentIndex = position;
}
@Override
public void onPageScrolled(int position, float positionOffset, intpositionOffsetPixels) { }
@Override
public void onPageScrollStateChanged(int state) { }
}
2.通过切换列表内的数据
写两个子布局,让适配器加载不同的数据
public class WorkerPostAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
private LayoutInflater layoutInflater;
private Context context;
private List<WorkerPost> workerList;
static final int TYPE_NORMAL = 0;
static final int TYPE_FOOT = 1;
static final int NO_MORE = 0;
static final int LOADING_MORE = 1;
private int footer_state = 0;
public WorkerPostAdapter(Context context, List<WorkerPost> workerList) {
layoutInflater = LayoutInflater.from(context);
this.context = context;
this.workerList = workerList;
}
/*
NormalViewHolder
*/
public static class NormalViewHolder extends RecyclerView.ViewHolder {
TextView workerNameTV;
TextView workerLevelTV;
TextView workerAddressTV;
NormalViewHolder(View view) {
super(view);
workerNameTV = (TextView)view.findViewById(R.id.tv_workerName);
workerLevelTV = (TextView)view.findViewById(R.id.tv_workerLevel);
workerAddressTV = (TextView)view.findViewById(R.id.tv_workerAddress);
}
}
/*
FooterViewHolder
*/
public static class FooterViewHolder extends RecyclerView.ViewHolder {
View footerView;
ProgressBar footerLoadPB;
TextView footerLoadTV;
FooterViewHolder(View view) {
super(view);
footerView = view.findViewById(R.id.footerload);
footerLoadPB = (ProgressBar)view.findViewById(R.id.pb_footerload);
footerLoadTV = (TextView)view.findViewById(R.id.tv_footerload);
}
}
@Override
//onCreateViewHolder根据不同的viewType返回不同的布局
public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view;
if (viewType == TYPE_NORMAL) {
view = layoutInflater.inflate(R.layout.workerpost_item, parent, false);
return new NormalViewHolder(view);
} else {
view = layoutInflater.inflate(R.layout.recyclerview_footer, parent, false);
return new FooterViewHolder(view);
}
}
@Override
//绑定数据
public void onBindViewHolder(RecyclerView.ViewHolder holder, int position)
if (holder instanceof NormalViewHolder) {
WorkerPost worker = workerList.get(position);
((NormalViewHolder)holder).workerNameTV.setText(worker.getWorkerName());
((NormalViewHolder)holder).workerLevelTV.setText(worker.getWorkerLevel());
((NormalViewHolder)holder).workerAddressTV.setText(worker.getWorkerAddress());
} else if (holder instanceof FooterViewHolder) {
FooterViewHolder footerViewHolder = (FooterViewHolder) holder;
if (position == 0) {
footerViewHolder.footerLoadPB.setVisibility(GONE);
footerViewHolder.footerLoadTV.setVisibility(GONE);
}
switch (footer_state) {
case NO_MORE:
footerViewHolder.footerLoadPB.setVisibility(GONE);
footerViewHolder.footerLoadTV.setText("");
break;
case LOADING_MORE:
footerViewHolder.footerLoadPB.setVisibility(View.VISIBLE);
footerViewHolder.footerLoadTV.setText(R.string.loading);
break;
default:
break;
}
}
}
二、明天计划的事情
学习HttpURLConnection和OkHttp
三、遇到的问题
四、收获
黑发不知勤学早,白首方悔读书迟。
评论