viewpager2实现fragment的加载

master
fangshicai 3 years ago
parent eb80cb2935
commit 06fe27021b

@ -64,4 +64,7 @@ dependencies {
implementation "androidx.fragment:fragment:$fragment_version"
}

@ -24,6 +24,7 @@ public class HomePageActivity extends AppCompatActivity implements BottomNavigat
private FragmentTransaction fragmentTransaction;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);

@ -82,6 +82,7 @@ public class LoginActivity extends AppCompatActivity implements LoginListener, V
this.responseData = responseData;
login();
Log.e("responseData",String.valueOf(responseData.getData().getId()));
}

@ -0,0 +1,40 @@
package com.android.activity.adapter;
import android.os.Bundle;
import androidx.annotation.NonNull;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
import androidx.lifecycle.Lifecycle;
import androidx.viewpager2.adapter.FragmentStateAdapter;
import com.android.activity.fragment.GoodsTypeFragment;
import com.android.bean.Goods;
import com.android.bean.GoodsType;
import java.util.List;
public class GoodsTypeFSAdapter extends FragmentStateAdapter{
private List<GoodsType> goodsTypeList;
private List<Fragment> fragmentList;
public GoodsTypeFSAdapter(@NonNull FragmentManager fragmentManager,
@NonNull Lifecycle lifecycle,
// List<GoodsType> goodsTypeList, List<Goods> goodsList,
List<Fragment> fragmentList){
super(fragmentManager, lifecycle);
this.fragmentList = fragmentList;
}
@NonNull
@Override
public Fragment createFragment(int position) {
return fragmentList.get(position);
}
@Override
public int getItemCount() {
return fragmentList.size();
}
}

@ -0,0 +1,73 @@
package com.android.activity.fragment;
import android.os.Bundle;
import androidx.fragment.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import com.android.R;
/**
* A simple {@link Fragment} subclass.
* Use the {@link GoodsTypeFragment#newInstance} factory method to
* create an instance of this fragment.
*/
public class GoodsTypeFragment extends Fragment {
// TODO: Rename parameter arguments, choose names that match
// the fragment initialization parameters, e.g. ARG_ITEM_NUMBER
private static final String ARG_TEXT = "param1";
View rootView;
// TODO: Rename and change types of parameters
private String mParam1;
public GoodsTypeFragment() {
// Required empty public constructor
}
/**
* Use this factory method to create a new instance of
* this fragment using the provided parameters.
*
* @param param1 Parameter 1.
* @return A new instance of fragment GoodsTypeFragment.
*/
// TODO: Rename and change types and number of parameters
public static GoodsTypeFragment newInstance(String param1) {
GoodsTypeFragment fragment = new GoodsTypeFragment();
Bundle args = new Bundle();
args.putString(ARG_TEXT, param1);
fragment.setArguments(args);
return fragment;
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if (getArguments() != null) {
mParam1 = getArguments().getString(ARG_TEXT);
}
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
if(rootView == null)
rootView = inflater.inflate(R.layout.fragment_goods_type, container, false);
initView();
return rootView;
}
private void initView() {
TextView view = rootView.findViewById(R.id.tv_goods);
view.setText(mParam1);
}
}

@ -7,6 +7,7 @@ import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import androidx.viewpager2.widget.ViewPager2;
import android.util.Log;
import android.view.LayoutInflater;
@ -17,11 +18,15 @@ import android.widget.Button;
import com.android.R;
import com.android.activity.adapter.GoodsTypeAdapter;
import com.android.activity.adapter.GoodsTypeFSAdapter;
import com.android.bean.GoodsType;
import com.android.bean.ResponseData;
import com.android.model.goods.GoodsListener;
import com.android.model.goods.GoodsModelimpl;
import com.google.android.material.tabs.TabLayout;
import com.google.android.material.tabs.TabLayoutMediator;
import java.util.ArrayList;
import java.util.List;
/**
@ -34,9 +39,12 @@ public class HomePageFragment extends Fragment implements View.OnClickListener,
private Button button;
private GoodsModelimpl goodsModelimpl;
private List<GoodsType> goodsTypeList;
private GoodsTypeAdapter goodsTypeAdapter = null;
private RecyclerView recyclerView;
// private GoodsTypeAdapter goodsTypeAdapter = null;
// private RecyclerView recyclerView;
private View rootView;
private GoodsTypeFSAdapter goodsTypeFSAdapter;
private ViewPager2 viewPager;
@ -78,19 +86,34 @@ public class HomePageFragment extends Fragment implements View.OnClickListener,
@Override
public void onGetAllGoodsType(ResponseData<List<GoodsType>> responseData) {
this.goodsTypeList = responseData.getData();
this.goodsTypeList.add(new GoodsType(0,"全部"));
initRecyclerview();
}
private void initRecyclerview() {
recyclerView = rootView.findViewById(R.id.lv_goodsType_list);
viewPager = rootView.findViewById(R.id.view_pager);
// goodsTypeAdapter = new GoodsTypeAdapter(goodsTypeList,context,R.layout.list_good_stype_item);
// viewPager.setAdapter(goodsTypeAdapter);
ArrayList<Fragment> fragments = new ArrayList<>();
for (int i = 0; i < goodsTypeList.size(); i++) {
fragments.add(GoodsTypeFragment.newInstance(goodsTypeList.get(i).getType()));
}
goodsTypeAdapter = new GoodsTypeAdapter(goodsTypeList,context,R.layout.list_good_stype_item);
goodsTypeFSAdapter = new GoodsTypeFSAdapter(getActivity().getSupportFragmentManager(), getLifecycle(),fragments);
viewPager.setAdapter(goodsTypeFSAdapter);
//把TabLayout选项卡布局和ViewPager2绑定在一起。特别说明一下下面这行代码是官方给的特别好使。
TabLayout tablayout = rootView.findViewById(R.id.tabLayout);
new TabLayoutMediator(tablayout, viewPager,
(tab, position) -> tab.setText(goodsTypeList.get(position).getType())).attach();
LinearLayoutManager llm = new LinearLayoutManager(context);
recyclerView.setLayoutManager(llm);
recyclerView.setAdapter(goodsTypeAdapter);
}
@Override

@ -4,6 +4,14 @@ public class GoodsType {
private Integer id;
private String type;
public GoodsType() {
}
public GoodsType(Integer id, String type) {
this.id = id;
this.type = type;
}
public Integer getId() {
return id;
}

@ -0,0 +1,15 @@
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".activity.fragment.GoodsTypeFragment">
<!-- TODO: Update blank fragment layout -->
<TextView
android:id="@+id/tv_goods"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="@string/hello_blank_fragment" />
</FrameLayout>

@ -1,15 +1,24 @@
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".activity.fragment.HomePageFragment">
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/lv_goodsType_list"
android:layout_width="wrap_content"
<com.google.android.material.tabs.TabLayout
android:id="@+id/tabLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:isScrollContainer="true"/>
app:tabMode="scrollable"
/>
<androidx.viewpager2.widget.ViewPager2
android:id="@+id/view_pager"
android:layout_width="match_parent"
android:layout_height="match_parent"
/>

@ -3,7 +3,7 @@
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".activity.fragment.ShopPageFragment">
tools:context=".activity.fragment.ShopCarPageFragment">
<!-- TODO: Update blank fragment layout -->
<TextView

@ -8,6 +8,7 @@
android:id="@+id/tv_goods_type_item"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:text="商品"
/>

Loading…
Cancel
Save