ViewPager未在Fragment中显示第一个选项卡

时间:2017-11-12 08:09:36

标签: java android android-viewpager

我有两个标签,现在我想在两个标签中显示相同的数据,但只在第二个标签中显示数据,在第一个标签中则不显示。当我只使用一个标签时,它显示完美。

我在Stack Overflow中尝试了很多答案,但我找不到任何答案。

StatusAdapter.Java

package com.example.android.navdrawer1;

import android.content.Context;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;

public class StatusAdapter extends FragmentPagerAdapter {

    private Context mContext;

    public StatusAdapter(Context context, FragmentManager fm) {
        super(fm);
        mContext = context;
    }

    //Return the {@link Fragment} that should be displayed for the given page number.
    @Override
    public Fragment getItem(int position) {
        if (position == 0) {
            return new CatalogFragment();
        } else {
            return new CatalogFragment();
        }
    }

    //Return the total number of pages.
    @Override
    public int getCount() {
        return 2;
    }

    @Override
    public CharSequence getPageTitle(int position) {
        if (position == 0) {
            return mContext.getString(R.string.active);
        } else {
            return mContext.getString(R.string.inactive);
        }
    }

}

CatalogActivity.Java

import android.content.ContentValues;
import android.net.Uri;
import android.os.Bundle;
import android.support.design.widget.TabLayout;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.Toast;

import com.example.android.navdrawer1.data.PayersiteContract;
import com.example.android.navdrawer1.data.PayersiteContract.PayersiteEntry;
import com.example.android.navdrawer1.data.PayersiteDbHelper;

import static com.example.android.navdrawer1.data.PayersiteContract.PayersiteEntry.COLUMN_PAYERSITE_DAYS;

public class CatalogActivity extends AppCompatActivity{
    @Override
    protected void onCreate(Bundle savedInstanceState){
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_catalog);

        // Find the view pager that will allow the user to swipe between fragments
        ViewPager viewPager = (ViewPager) findViewById(R.id.viewpager);
        viewPager.setCurrentItem(0);

        // Create an adapter that knows which fragment should be shown on each page
        StatusAdapter adapter = new StatusAdapter(this, getSupportFragmentManager());

        // Set the adapter onto the view pager
        viewPager.setAdapter(adapter);

        // Find the tab layout that shows the tabs
        TabLayout tabLayout = (TabLayout) findViewById(R.id.tabs);


        tabLayout.setupWithViewPager(viewPager);

    }
}

CatalogFragment

import android.content.ContentUris;
import android.content.ContentValues;
import android.content.Intent;
import android.database.Cursor;
import android.net.Uri;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.LoaderManager;
import android.support.v4.content.CursorLoader;
import android.support.v4.widget.CursorAdapter;
import android.support.v4.content.Loader;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import com.example.android.navdrawer1.data.PayersiteContract;
import com.example.android.navdrawer1.data.PayersiteContract.*;
import com.example.android.navdrawer1.data.PayersiteDbHelper;

import static com.example.android.navdrawer1.data.PayersiteContract.PayersiteEntry.COLUMN_PAYERSITE_DAYS;

/**
 * A simple {@link Fragment} subclass.
 */
public class CatalogFragment extends Fragment implements LoaderManager.LoaderCallbacks<Cursor>{

    // Initializing variable for the loader
    private static final int PAYERSITE_LOADER = 0;

    // Declaring cursoradapter variable
    PayersiteCursorAdapter mCursorAdapter;

    // Database helper which will provide access to the database
    private PayersiteDbHelper mDbHelper;



    public CatalogFragment() {
        // Required empty public constructor
    }


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

        View rootView = inflater.inflate(R.layout.list_only, container, false);

        // Find the ListView which will be populated with the pet data
        ListView payersiteListView = (ListView) rootView.findViewById(R.id.list);
        // Setup an adapter to create a list item for each row of pet data in the cursor.
        // There is no pet data yet (until the loader finishes) so pass in null for the cursor.
        mCursorAdapter = new PayersiteCursorAdapter(getActivity(), null);
        payersiteListView.setAdapter(mCursorAdapter);
//
        // setting up the listener when the list item clicks
        payersiteListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int position, long id) {
                Intent intent = new Intent(getActivity(), EditorActivity.class);

                // Create a current URI that tells us that which list item is clicked. becoz we need to pass on the list details to activity
                Uri currentpayersiteUri = ContentUris.withAppendedId(PayersiteContract.PayersiteEntry.CONTENT_URI, id);

                // Set the URI on the data field of the intent
                intent.setData(currentpayersiteUri);

                // Launch the activity to display the data for the current pet.
                startActivity(intent);
            }
        });
        // Kick of the loader
        getActivity().getSupportLoaderManager().initLoader(PAYERSITE_LOADER, null, this);
        return rootView;
    }
}

0 个答案:

没有答案