使用主活动中片段的视图

时间:2017-07-19 09:07:34

标签: java android fragment

我在Android开发方面相当新,我对片段和主要活动之间的沟通有一些疑问。

在我的特定情况下,我没有成功解决我的问题。我想要做的是在从一个片段滚动到具有此视图的片段时修改片段内的视图的alpha(类似于卡片)。我尝试了不同的东西,但我失败了(空指针异常),你可以在评论中看到一些实验。这是我的代码和我的架构:

带有视图的片段:

public class  VideoviewFragment extends BaseFragment {

/* public View card;

public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    View rootview = inflater.inflate(R.layout.fragment_videoview, container, false);
    card = (View) rootview.findViewById(R.id.card_video);
    return rootview;
}

 public void setalphacard(float value){
    card.setAlpha(value);
}*/


public static VideoviewFragment create() {

    return new VideoviewFragment();
}

@Override
public int getLayoutResId() {
    return R.layout.fragment_videoview;
}

@Override
public void inOnCreateView(View root, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {


}
}

基础片段:

public abstract class BaseFragment extends Fragment {

private View mRoot;

@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
    mRoot = inflater.inflate(getLayoutResId(), container, false);
    inOnCreateView(mRoot, container, savedInstanceState);
    return mRoot;
}

@LayoutRes
public abstract int getLayoutResId();

public abstract void inOnCreateView(View root, @Nullable ViewGroup container,@Nullable Bundle savedInstanceState);
}

主要活动:

public class MainActivity extends AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    // Viewpager
    final ViewPager viewPager = (ViewPager) findViewById(R.id.am_view_pager);
    final MainPagerAdapter adapter = new MainPagerAdapter(getSupportFragmentManager());
    viewPager.setAdapter(adapter);

    // tabview et viewpager
    viewPager.setCurrentItem(1);

    // Changement couleurs swipe
    viewPager.setBackgroundColor(Color.rgb(135,206,250));

    viewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
        @Override
        public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            if(position == 1) {

                viewPager.setBackgroundColor((Integer) new ArgbEvaluator().evaluate(positionOffset, Color.rgb(135,206,250),Color.rgb(240,15,45)));
                //videoviewFragment.setalphacard(positionOffset);

            }

            if(position == 0) {

                viewPager.setBackgroundColor((Integer) new ArgbEvaluator().evaluate(positionOffset, Color.TRANSPARENT,Color.rgb(135,206,250)));

            }

            }

        @Override
        public void onPageSelected(int position) {
        }

        @Override
        public void onPageScrollStateChanged(int state) {

        }
    });

}
}

适配器:

public class MainPagerAdapter extends FragmentPagerAdapter {


public MainPagerAdapter(FragmentManager fm) {
    super(fm);
}

@Override
public Fragment getItem(int position) {

     switch (position){

         case 0:
             return CameraFragment.create();
         case 1:
             return OptionFragment.create();
         case 2:
             return VideoviewFragment.create();
     }

     return null;
}

@Override
public int getCount() {
    return 3;
}
}

XML VideoviewFragment

<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/relativeLayout0"
android:layout_width="match_parent"
android:layout_height="match_parent">

    <View
    android:id="@+id/card_video"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_marginTop="70dp"
    android:background="@drawable/card_background_video"/>


</RelativeLayout>

(我知道此活动中没有我片段的实例...) 我在StackOverflow上发现了很少类似于Use view from fragment xml file的主题,但我做错了...... 谢谢你的帮助!

0 个答案:

没有答案