1

我是 android 新手,我正面临一个奇怪的情况,我没有找到 setupWithViewPager。当我调用 objectTabLayout.setupWithViewPager 然后 setupWithViewPager 没有找到..我该怎么办?

我的 XML 代码

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:background="@color/colorBlack"
    android:id="@+id/favoriteFragment_RL"
    tools:context="Fragments.Favorite">

   <com.google.android.material.tabs.TabLayout
       android:layout_width="match_parent"
       android:layout_height="wrap_content"
       android:id="@+id/favoriteFragment_TabLayout"
       android:minHeight="?actionBarSize"
       app:tabGravity="fill"
       app:tabIndicatorColor="@color/colorWhite"
       app:tabIndicatorHeight="4dp"
       app:tabBackground="@color/colorPrimary"
       app:tabMode="fixed"
       app:tabTextColor="@color/colorWhite"/>
    
    <androidx.viewpager.widget.ViewPager
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:id="@+id/favoriteFragment_viewPager"
        android:layout_below="@id/favoriteFragment_TabLayout"/>


</RelativeLayout>

my java code

     package Fragments;

import android.os.Bundle;

import androidx.fragment.app.Fragment;
import androidx.viewpager.widget.ViewPager;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TableLayout;
import android.widget.Toast;

import com.example.connectsocialmediaapp.R;

import AdapterClasses.FavoriteStatusTabAdapter;


public class Favorite extends Fragment {


    //XML Variables
    private View parent;
    private TableLayout objectTablayout;
    private ViewPager objectViewPager;

    //Class Variables
    favoriteImageStatusFragment objectFavoriteImageStatusFragment;
    FavoriteStatusTabAdapter objectFavoriteStatusTabAdapter;
    favoriteTextStatusFragment objectFavoriteTextStatusFragment;

    private int[] tabIcons= {
            R.drawable.ic_text, R.drawable.ic_image
    };
    public Favorite() {
        // Required empty public constructor
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        parent= inflater.inflate(R.layout.fragment_favorite, container, false);
        initializeVariables();
        return parent;
    }

    private void addFragmentToTablayout()
    {
    try
    {
        objectFavoriteStatusTabAdapter=new FavoriteStatusTabAdapter(getChildFragmentManager());
        objectFavoriteStatusTabAdapter.addFragments(objectFavoriteTextStatusFragment);

        objectFavoriteStatusTabAdapter.addFragments(objectFavoriteImageStatusFragment);
        objectViewPager.setAdapter(objectFavoriteStatusTabAdapter);


        objectTablayout.(objectViewPager);
        objectViewPager.setSaveFromParentEnabled(false);

    }
    catch (Exception e)
    {
        Toast.makeText(getContext(), e.getMessage(), Toast.LENGTH_SHORT).show();
    }
    }


    private void initializeVariables()
    {
        try
        {
            objectFavoriteTextStatusFragment=new favoriteTextStatusFragment();
            objectFavoriteImageStatusFragment=new favoriteImageStatusFragment();

            objectTablayout=parent.findViewById(R.id.favoriteFragment_TabLayout);

            objectViewPager=parent.findViewById(R.id.favoriteFragment_viewPager);
        }
        catch (Exception e)
        {
            Toast.makeText(getContext(), e.getMessage(), Toast.LENGTH_SHORT).show();
            
        }
    }
}

有没有其他方法可以访问 setupWithViewPager 或者我应该如何访问它。

4

1 回答 1

1

似乎您混淆了使用 TabLayout 的 xml 和使用 TableLayout 的 Java 代码。在Java代码中修改TableLayoutOut并运行,你的问题已经解决了

public class Favorite extends Fragment {


//XML Variables
private View parent;
private TabLayout objectTablayout;
private ViewPager objectViewPager;

//Class Variables
favoriteImageStatusFragment objectFavoriteImageStatusFragment;
FavoriteStatusTabAdapter objectFavoriteStatusTabAdapter;
favoriteTextStatusFragment objectFavoriteTextStatusFragment;

private int[] tabIcons= {
        R.drawable.ic_text, R.drawable.ic_image
};
public Favorite() {
    // Required empty public constructor
}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    // Inflate the layout for this fragment
    parent= inflater.inflate(R.layout.fragment_favorite, container, false);
    initializeVariables();
    return parent;
}

private void addFragmentToTablayout()
{
try
{
    objectFavoriteStatusTabAdapter=new FavoriteStatusTabAdapter(getChildFragmentManager());
    objectFavoriteStatusTabAdapter.addFragments(objectFavoriteTextStatusFragment);

    objectFavoriteStatusTabAdapter.addFragments(objectFavoriteImageStatusFragment);
    objectViewPager.setAdapter(objectFavoriteStatusTabAdapter);


    objectTablayout.(objectViewPager);
    objectViewPager.setSaveFromParentEnabled(false);

}
catch (Exception e)
{
    Toast.makeText(getContext(), e.getMessage(), Toast.LENGTH_SHORT).show();
}
}


private void initializeVariables()
{
    try
    {
        objectFavoriteTextStatusFragment=new favoriteTextStatusFragment();
        objectFavoriteImageStatusFragment=new favoriteImageStatusFragment();

        objectTablayout=parent.findViewById(R.id.favoriteFragment_TabLayout);

        objectViewPager=parent.findViewById(R.id.favoriteFragment_viewPager);
    }
    catch (Exception e)
    {
        Toast.makeText(getContext(), e.getMessage(), Toast.LENGTH_SHORT).show();
        
    }
}

}

于 2021-04-19T01:22:27.677 回答