我试图建立一个包含三个带有相同片段的选项卡的视图寻呼机。
但是它没有按预期工作。我想显示具有不同背景颜色的单个选项卡,并根据我使用bundle和arguments提供的ID来显示textview来显示其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);
}
}


输出:

android - 设置具有相同 fragment 的viewpager无法正常工作-LMLPHP

最佳答案

您在此处将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;
    }

关于android - 设置具有相同 fragment 的viewpager无法正常工作,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/49753221/

10-13 03:25