Tablayout + View-Pager zeigt das Fragment nicht an Position 0 an

Meine Anwendung verfügt über 2 Registerkarten mit zwei verschiedenen Layouts. Wenn ich die Anwendung starte, wird das Fragment, das in Tab1 (Position 0) angezeigt werden soll, in Tab2 angezeigt, und das Fragment, das in Tab2 (Position 1) angezeigt werden soll, wird nicht angezeigt. Auch wenn ich über den Bildschirm wische, ändert sich der Tabulatorfokus nicht im Tablayout

Ich habe meinen Code unten angegeben

MainActivity.java

public class MainActivity extends AppCompatActivity implements TabLayout.OnTabSelectedListener 
{
    TabLayout tabLayoutTL;
    TabLayout.Tab linearTab, gridTab;
    ViewPager viewPagerVP;
    ViewPagerAdapter viewPagerAdapter;

    @Override
    public void onTabSelected(TabLayout.Tab tab) {
        viewPagerVP.setCurrentItem(tab.getPosition());
    }

    @Override
    public void onTabUnselected(TabLayout.Tab tab) {
    }

    @Override
    public void onTabReselected(TabLayout.Tab tab) {
    }


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);

        viewPagerVP = (ViewPager)findViewById(R.id.viewPagerVP);
        viewPagerAdapter = new ViewPagerAdapter(getSupportFragmentManager());
        viewPagerVP.setAdapter(viewPagerAdapter);

        tabLayoutTL = (TabLayout)findViewById(R.id.tabLayoutTL);
        linearTab = tabLayoutTL.newTab();
        gridTab = tabLayoutTL.newTab();

        linearTab.setText("Linear");
        gridTab.setText("Grid");

        tabLayoutTL.addTab(linearTab, 0);
        tabLayoutTL.addTab(gridTab, 1);
        tabLayoutTL.setOnTabSelectedListener(this);
    }
}

MainFragment.java

public class MainFragment extends Fragment {

    private static final String FRAG_TYPE = "frag_type";
    int fragType;
    RecyclerView recyclerViewRv;

    public MainFragment() {}

    public static MainFragment newInstance(int fragType) {
        MainFragment mainFragment = new MainFragment();
        Bundle args = new Bundle();
        args.putInt(FRAG_TYPE, fragType);
        mainFragment.setArguments(args);
        return mainFragment;
    }

    private void initialize() {
        recyclerViewRv = (RecyclerView)getActivity().findViewById(R.id.recyclerViewRv);
    }

    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        initialize();
        new BackBone().execute();
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (getArguments() != null) {
            fragType = getArguments().getInt(FRAG_TYPE);
        }
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        return inflater.inflate(R.layout.fragment_linear, container, false);
    }

    @Override
    public void onAttach(Context context) {
        super.onAttach(context);
    }

    @Override
    public void onDetach() {
        super.onDetach();
    }

    class BackBone extends AsyncTask<Void, Void, ArrayList<DataRecord>> {

        ProgressDialog progressDialog;
        private static final String flickrUrl = "http://www.flickr.com/services/feeds/photos_public.gne?tags=soccer&format=json&nojsoncallback=1";

        private String getData() {
            HttpClient httpClient = new DefaultHttpClient();
            HttpGet httpGet = new HttpGet(flickrUrl);
            try {
                HttpResponse httpResponse = httpClient.execute(httpGet);
                HttpEntity httpEntity = httpResponse.getEntity();
                String data = EntityUtils.toString(httpEntity);
                return data;
            } catch (Exception e) {
                return "exception";
            }
        }

        @Override
        protected void onPreExecute() {
            super.onPreExecute();
            progressDialog = new ProgressDialog(getActivity());
            progressDialog.setTitle("Loading");
            progressDialog.setMessage("Please wait .......");
            progressDialog.show();
        }

        @Override
        protected ArrayList<DataRecord> doInBackground(Void... params) {
            ArrayList<DataRecord> dataRecords = new ArrayList<>();
            try {
                JSONObject jsonObject = new JSONObject(getData());
                JSONArray jsonArray = jsonObject.getJSONArray("items");
                for (int i = 0 ; i < jsonArray.length() ; i++) {
                    JSONObject jsonObject1 = jsonArray.getJSONObject(i);
                    DataRecord dataRecord = new DataRecord();
                    dataRecord.setName(jsonObject1.getString("title"));
                    JSONObject mediaJSONObject = jsonObject1.getJSONObject("media");
                    dataRecord.setUrl(mediaJSONObject.getString("m"));
                    dataRecords.add(dataRecord);
                }
            } catch (Exception e) {}
            return dataRecords;
        }

        @Override
        protected void onPostExecute(ArrayList<DataRecord> dataRecords) {
            super.onPostExecute(dataRecords);
            DataRecordAdapter dataRecordAdapter = new DataRecordAdapter(getActivity(), dataRecords, fragType);
            if (fragType == 1) {
                recyclerViewRv.setLayoutManager(new LinearLayoutManager(getActivity()));
                recyclerViewRv.setBackgroundColor(Color.GREEN);
            } else if (fragType == 2){
                recyclerViewRv.setLayoutManager(new GridLayoutManager(getActivity(), 2));
                recyclerViewRv.setBackgroundColor(Color.BLUE);
            }
            recyclerViewRv.setAdapter(dataRecordAdapter);
            progressDialog.dismiss();
        }

    }
}

ViewPagerAdapter.java

public class ViewPagerAdapter extends FragmentStatePagerAdapter {

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

    @Override
    public Fragment getItem(int position) {
        switch (position) {
            case 0 :
                MainFragment mainFragment = MainFragment.newInstance(1);
                return mainFragment;
            case 1 :
                MainFragment mainFragment1 = MainFragment.newInstance(2);
                return mainFragment1;
            default :
                return null;
        }
    }

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

Ich bin sicher, es gibt kein Problem mit den Fragmets. Ich denke, das Problem liegt beim Tablayout. Ich kann nicht herausfinden, wo genau das Problem ist.

DataRecordAdapter.java

public class DataRecordAdapter extends RecyclerView.Adapter<DataRecordAdapter.MyViewHolder> {

    ArrayList<DataRecord> dataRecords;
    Context context;
    int flag;
    LayoutInflater layoutInflater;

    public DataRecordAdapter(Context context, ArrayList<DataRecord> dataRecords, int flag) {
        this.context = context;
        this.dataRecords = dataRecords;
        this.flag = flag;
        layoutInflater = (LayoutInflater.from(context));
    }

    @Override
    public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view;
        if (flag == 1) {
            view = layoutInflater.inflate(R.layout.linear_data_layout, parent, false);
        } else {
            view = layoutInflater.inflate(R.layout.grid_data_layout, parent, false);
        }
        MyViewHolder myViewHolder = new MyViewHolder(view);
        return myViewHolder;
    }

    @Override
    public void onBindViewHolder(MyViewHolder holder, int position) {
        DataRecord dataRecord = dataRecords.get(position);
        holder.textViewTV.setText(dataRecord.getName());
        Picasso.with(context).load(dataRecord.getUrl()).into(holder.imageViewIV);
    }

    @Override
    public int getItemCount() {
        return dataRecords.size();
    }

    class MyViewHolder extends RecyclerView.ViewHolder {
        TextView textViewTV;
        ImageView imageViewIV;
        public MyViewHolder(View itemView) {
            super(itemView);
            textViewTV = (TextView)itemView.findViewById(R.id.textViewTV);
            imageViewIV = (ImageView)itemView.findViewById(R.id.imageViewIV);
        }
    }

}

DataRecord.java

public class DataRecord {
    String name, url;

    public String getName() {
        return name;
    }
[![enter image description here][1]][1]
    public void setName(String name) {
        this.name = name;
    }

    public String getUrl() {
        return url;
    }

    public void setUrl(String url) {
        this.url = url;
    }
}

werfen Sie einen Blick auf die Bilder Jungs. Tab 1 ist leer und Tab 2 zeigt den Inhalt, der sich auf Tab 1 befinden soll. Tab 2-Inhalt wird nicht angezeigt.

Antworten auf die Frage(4)

Ihre Antwort auf die Frage