使用相同片段设置viewpager无法按预期工作

时间:2018-04-10 12:06:38

标签: android android-fragments android-viewpager

我正在尝试设置一个包含三个带有相同片段的标签的视图寻呼机。 但它没有按预期工作。我想显示具有不同背景颜色的单个选项卡,textview根据我使用包和参数提供的ID显示其ID。

我提供代码和图片以便更好地理解。

MainActivity.xml

<?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"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.bdtask.myapplication.MainActivity">

<LinearLayout
    android:id="@+id/tabviewholder"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical">

    <android.support.design.widget.TabLayout
        android:id="@+id/viewpagertab"
        android:layout_width="match_parent"
        android:layout_height="48dp"
        android:background="@android:color/white"
      />


    <android.support.v4.view.ViewPager
        android:id="@+id/viewpager"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_below="@id/viewpagertab" />


</LinearLayout>

BlankFragment.xml

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:id="@+id/container"
android:layout_height="match_parent"
tools:context="com.bdtask.myapplication.BlankFragment">

<!-- TODO: Update blank fragment layout -->
<TextView
    android:gravity="center"
    android:textSize="40sp"
    android:id="@+id/textview"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:text="@string/hello_blank_fragment" />

MainActivity.java

package com.bdtask.myapplication;

import android.support.design.widget.TabLayout;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.TabHost;
import android.widget.TableLayout;

public class MainActivity extends AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    ViewPagerAdapter viewPagerAdapter = new 
    ViewPagerAdapter(getSupportFragmentManager());
    ViewPager viewPager = findViewById(R.id.viewpager);
    TabLayout tabLayout = findViewById(R.id.viewpagertab);


    for(int i = 0 ; i<3 ; i++){
        Bundle bundle = new Bundle();
        bundle.putString("id",String .valueOf(i));
        BlankFragment blankFragment = new BlankFragment();
        blankFragment.setArguments(bundle);
        viewPagerAdapter.getFragments(blankFragment, "tab "+i);
    }


    viewPager.setAdapter(viewPagerAdapter);
    tabLayout.setupWithViewPager(viewPager);






}
}

BlankFragment.java

package com.bdtask.myapplication;


import android.graphics.Color;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.FrameLayout;
import android.widget.TextView;


/**
 * A simple {@link Fragment} subclass.
 */
public class BlankFragment extends Fragment {


TextView textView;
FrameLayout frameLayout;
String id;
public BlankFragment() {
    // Required empty public constructor

}


@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    // Inflate the layout for this fragment
    //

    id = getArguments().getString("id");

    return inflater.inflate(R.layout.fragment_blank, container, false);
}

@Override
public void onActivityCreated(@Nullable Bundle savedInstanceState) {
    super.onActivityCreated(savedInstanceState);


    frameLayout = getActivity().findViewById(R.id.container);
    textView = getActivity().findViewById(R.id.textview);
    setbackgroundandtext();
}


  public void setbackgroundandtext() {
    if(id.equals("0")){
        frameLayout.setBackgroundColor(Color.parseColor("#ab44ab"));
        textView.setText(id);

    }


    if(id.equals("1")){
        frameLayout.setBackgroundColor(Color.parseColor("#ea32ea"));
        textView.setText(id);

    }


    if(id.equals("2")){
        frameLayout.setBackgroundColor(Color.parseColor("#67ae34"));
        textView.setText(id);
    }

}
}

ViewPagerAdapter.java

package com.bdtask.myapplication;

import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentStatePagerAdapter;

import java.util.ArrayList;

/**
 * Created by Jibunnisa on 5/20/2017.
 */

public class ViewPagerAdapter extends FragmentStatePagerAdapter {

ArrayList<Fragment> fragments = new ArrayList<Fragment>();
ArrayList<String> titles = new ArrayList<String>();

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



public void getFragments(Fragment fragment, String title) {

    this.fragments.add(fragment);
    this.titles.add(title);

}

@Override
public Fragment getItem(int position) {
    return fragments.get(position);
}

@Override
public int getCount() {
    return fragments.size();
}
@Override
public CharSequence getPageTitle(int position) {
    return titles.get(position);
}
}

输出:

enter image description here

1 个答案:

答案 0 :(得分:2)

您在此处将ID设置为0,1,2:

for(int i = 0 ; i<3 ; i++) {
    Bundle bundle = new Bundle();
    bundle.putString("id",String .valueOf(i));
    BlankFragment blankFragment = new BlankFragment();
    blankFragment.setArguments(bundle);
    viewPagerAdapter.getFragments(blankFragment, "tab "+i);
}

但是你在这里检查它是否是1,2,3:

public void setbackgroundandtext() {
    if(id.equals("1")){
        frameLayout.setBackgroundColor(Color.parseColor("#ab44ab"));
        textView.setText(id);

    }


    if(id.equals("2")){
        frameLayout.setBackgroundColor(Color.parseColor("#ea32ea"));
        textView.setText(id);

    }


    if(id.equals("3")){
        frameLayout.setBackgroundColor(Color.parseColor("#67ae34"));
        textView.setText(id);
    }

}

因此,请检查它是否为0,1,2。

修改

您需要在片段内找到视图,而不是通过活动,将findViewById代码从onActivityCreated移到onCreateView

@Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        //

        id = getArguments().getString("id");

        View v =inflater.inflate(R.layout.fragment_blank, container, false);
        frameLayout = v.findViewById(R.id.container);
        textView = v.findViewById(R.id.textview);


         return v;
    }