2017-08-19 12 views
0

プロファイルの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> 

以下のようなこのTabLayoutは、それから私は、プロファイルとワークショップとして、この のための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に追加されました

最初の子フラグメントの中に私はAPIを呼び出しています。どうすれば画像と詳細をtablayoutに設定できますか?あなたのレイアウトXMLで

答えて

1

、このようなあなたの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をこのように変更して、inter顔:

@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変更

:これらのイメージと私のTabLayoutで

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); 
    } 

} 
+0

は、スクロール可能なレイアウトとして作成することが可能ですか? – CarinaMj

+0

@CarinaMj私はちょうど私の答えを編集しました。 – Benjamin

+0

画像全体にスクロールビューを追加することは可能ですか? – CarinaMj

関連する問題