我为个人资料创建了tablayout片段。这个TabLayout如下所示
<?xml version="1.0" encoding="utf-8"?>
<layout xmlns:app="http://schemas.android.com/apk/res-auto">
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<include
android:id="@+id/layout"
layout="@layout/common_act" />
<ScrollView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:scrollbars="none">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@color/color5"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:padding="@dimen/medium">
<RelativeLayout
android:id="@+id/layoutPic"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<ImageView
android:id="@+id/profilePic"
android:layout_width="@dimen/size_profile_pic"
android:layout_height="@dimen/size_profile_pic"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:gravity="center"
android:scaleType="fitCenter"
android:src="@drawable/img_profile" />
<TextView
android:id="@+id/name"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/profilePic"
android:layout_marginTop="@dimen/gap_medium"
android:background="@null"
android:gravity="center"
android:hint="@string/first_name"
android:lines="1"
android:paddingLeft="5dp"
android:singleLine="true"
android:textColor="@color/veryDarkGrey"
android:textColorHint="@color/darkGrey"
android:textSize="@dimen/normal"
app:font="@{@string/font_roboto_bold}" />
<TextView
android:id="@+id/mobile"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/name"
android:layout_marginTop="@dimen/gap_small"
android:background="@null"
android:gravity="center"
android:hint="@string/mobile_number"
android:lines="1"
android:paddingLeft="5dp"
android:singleLine="true"
android:textColor="@color/veryDarkGrey"
android:textColorHint="@color/darkGrey"
android:textSize="@dimen/normal"
app:font="@{@string/font_roboto_bold}" />
<TextView
android:id="@+id/type"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/mobile"
android:layout_marginTop="@dimen/gap_small"
android:background="@null"
android:gravity="center"
android:hint="@string/mobile_number"
android:lines="1"
android:paddingLeft="5dp"
android:singleLine="true"
android:textColor="@color/veryDarkGrey"
android:textColorHint="@color/darkGrey"
android:textSize="@dimen/normal"
app:font="@{@string/font_roboto_bold}" />
</RelativeLayout>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="@dimen/gap_small"
android:background="@null"
android:gravity="center"
android:hint="Complete your profile "
android:lines="1"
android:paddingLeft="5dp"
android:singleLine="true"
android:textColor="@color/veryDarkGrey"
android:textColorHint="@color/darkGrey"
android:textSize="@dimen/normal"
app:font="@{@string/font_roboto_bold}" />
<com.xx.xx.ui.widget.SeekBarWithLabel
android:id="@+id/seekBarCustomer"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:performanceProgress="20" />
</LinearLayout>
<android.support.design.widget.TabLayout
android:id="@+id/tabs"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@color/color3"
app:tabGravity="fill"
app:tabIndicatorHeight="4dp"
app:tabMode="fixed"
app:tabSelectedTextColor="@color/colorAccent" />
<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" />
</LinearLayout>
</ScrollView>
</LinearLayout>
然后我为此创建了2个片段 作为简介和研讨会。
然后我像这样创建了主片段
public class ProfileFragment extends BaseFragment {
private MyprofileFragBinding mBinding;
public static ProfileFragment newInstance() {
return new ProfileFragment();
}
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
super.onCreateView(inflater, container, savedInstanceState);
mBinding = DataBindingUtil.inflate(inflater, R.layout.myprofile_frag, container, false);
mBinding.tabs.setupWithViewPager(mBinding.viewpager);
setupViewPager(mBinding.viewpager);
mBinding.layout.navMenu.setOnClickListener(view -> openTradeDrawer());
return mBinding.getRoot();
}
private void setupViewPager(ViewPager viewPager) {
ViewPagerAdapter adapter = new ViewPagerAdapter(getChildFragmentManager());
adapter.addFragment(new ProfileViewFragment(), getString(R.string.profile_view));
adapter.addFragment(new WorkShopFragment(), getString(R.string.workshop_profile));
viewPager.setAdapter(adapter);
}
}
同样明智的我已经创建了布局和类。 这里在主tablayout中添加了个人资料图片和textview
在第一个子片段中,我正在调用API所以,我如何将图像和细节设置到tablayout中。
答案 0 :(得分:1)
在你的布局xml中,像这样定义你的TabLayout和ViewPager:
<android.support.v4.view.ViewPager
android:id="@+id/view_pager"
android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.design.widget.TabLayout
android:id="@+id/tab_layout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="top"
app:tabGravity="fill"
app:tabIndicatorHeight="4dp"
app:tabMode="fixed"/>
</android.support.v4.view.ViewPager>
然后定义你的适配器:
public class MyAdapter extends FragmentStatePagerAdapter {
public static final int LOGIN_INDEX = 0;
public static final int WORKSHOP_INDEX = 1;
private final Context mContext;
private final List<Fragment> mFragments;
public MyAdapter(Context context, FragmentManager fm, List<Fragment> fragments) {
super(fm);
mContext = context;
mFragments = fragments;
}
@Override
public CharSequence getPageTitle(int position) {
switch (position) {
case LOGIN_INDEX:
return mContext.getString(R.string.login);
case WORKSHOP_INDEX:
return mContext.getString(R.string.workshop);
default:
throw new IllegalArgumentException("too many tabs");
}
}
@Override
public Fragment getItem(int position) {
return mFragments.get(position);
}
@Override
public int getCount() {
return mFragments.size();
}
定义要由ProfileFragment实现的接口:
public interface Listener {
void displayInformations(String firstName, String name, String mobileNumber);
}
像这样修改ProfileFragment
并使其实现界面:
@Override
@Nullable
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
super.onCreateView(inflater, container, savedInstanceState);
mViewBindings = DataBindingUtil.inflate(inflater, R.layout.myprofile_frag, container, false);
List<Fragment> fragments = new ArrayList<>(2);
fragments.add(LoginFragment.newInstance());
fragments.add(WorkshopFragment.newInstance());
MyAdapter adapter = new MyAdapter(getContext(), getSupportFragmentManager(), fragments);
mViewBindings.viewPager.setAdapter(adapter);
mViewBindings.tabLayout.setupWithViewPager(mViewBindings.viewPager);
return mViewBindings.getRoot();
}
@Override
public void displayInformations(String firstName, String name, String mobileNumber){
// display the informations
}
像这样更改LoginFragment
:
public class ProfileFragment extends Fragment {
private Listener mListener;
@Override
public void onAttach(Context context) {
super.onAttach(context);
if (context instanceof Listener) {
mListener = (Listener) context;
} else {
throw new IllegalArgumentException("the parent should implement Listener");
}
}
public void setInformations(String firstName, String name, String mobileNumber) {
mListener.displayInformations(firstName, name, mobileNumber);
}
}