片段不会显示在viewpager中

时间:2015-05-14 08:47:05

标签: android android-fragments tabs android-viewpager

PFB我的代码,我的要求和我的研究

要求:

我有4个标签(标签A,B,C,D)有4个不同的片段(每个相应标签上有片段A,B,C,D),它们附有viewpager

我可以使用FragmentStatePagerAdapter帮助滑动标签,它可以保存要刷卡的片段的状态

现在我在片段中有一个列表说片段A,它有一个按钮,我需要调用它来说新片段A1。

我已经参考了堆栈网站链接:Replace Fragment inside a ViewPager

但是TabA上的FragmentA消失了,并且没有在标签A上显示FragmentA1是什么问题。请帮助

我的PageAdapter .java类

import java.util.List;

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

/**
* The <code>PagerAdapter</code> serves the fragments when paging.
* @author mwho
*/
public class PagerAdapter extends FragmentStatePagerAdapter {

   private List<Fragment> fragments;
   private final FragmentManager mFragmentManager = null;
   private Fragment mFragmentAtPos0;

   /**
    * @param fm
    * @param fragments
    */
   public PagerAdapter(FragmentManager fm, List<Fragment> fragments) {
       super(fm);
       this.fragments = fragments;
   }
   /* (non-Javadoc)
    * @see android.support.v4.app.FragmentPagerAdapter#getItem(int)
    */
   @Override
   public Fragment getItem(int position) {


       if (position == 0)
       {
           if (mFragmentAtPos0 == null)
           {
               mFragmentAtPos0 = FarmerInformationFragment.newInstance(new FirstPageFragmentListener()
               {
                   public void onSwitchToNextFragment()
                   {
                       mFragmentManager.beginTransaction().remove(mFragmentAtPos0).commit();
                       mFragmentAtPos0 = FieldVisitFragment.newInstance();
                       notifyDataSetChanged();
                   }
               });
           }
           return mFragmentAtPos0;
       }
       else
           return FieldVisitFragment.newInstance();




    //   return this.fragments.get(position);
   }

   /* (non-Javadoc)
    * @see android.support.v4.view.PagerAdapter#getCount()
    */
   @Override
   public int getCount() {
       return this.fragments.size();
   }


   @Override
   public int getItemPosition(Object object)
   {
       if (object instanceof FarmerInformationFragment && mFragmentAtPos0 instanceof FieldVisitFragment)
           return POSITION_NONE;
       return POSITION_UNCHANGED;
   }



}

界面

public interface FirstPageFragmentListener {
    void onSwitchToNextFragment();
}

FragmentA1

import com.example.fms.R;

import android.annotation.SuppressLint;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;

@SuppressLint("NewApi")
public class FieldVisitFragment extends Fragment {



    public static FieldVisitFragment newInstance() {
        FieldVisitFragment f = new FieldVisitFragment();
        return f;
    }

    /** (non-Javadoc)
     * @see android.support.v4.app.Fragment#onCreateView(android.view.LayoutInflater, android.view.ViewGroup, android.os.Bundle)
     */
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        if (container == null) {
            // We have different layouts, and in one of them this
            // fragment's containing frame doesn't exist.  The fragment
            // may still be created from its saved state, but there is
            // no reason to try to create its view hierarchy because it
            // won't be displayed.  Note this is not needed -- we could
            // just run the code below, where we would create and return
            // the view hierarchy; it would just never be used.
            return null;
        }
        return (LinearLayout)inflater.inflate(R.layout.yield_estimation_screen2_layout, container, false);
    }

}

片段A

import com.example.fms.R;

import android.annotation.SuppressLint;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.Toast;

@SuppressLint("NewApi")
public class FarmerInformationFragment extends Fragment {

    ListView mLandHoldingList , mFamilyDetailsList , mBankDetailsList , mOtherDetailsList;
    ImageView mArrowLandHolding  , mArrowFamilyHolding , mArrowBankHolding , mArrowOtherHolding ;
    RelativeLayout mLandHoldingRelativeLayout , mFamilyDetailsRelativeLayout , mBankDetailsRelativeLayout , mOtherDetailsRelativeLayout ;
    static boolean mLandFlg =false , mFamilyFlg = false , mBankFlg = false , mOtherDetailsFlg = false ;
    BroadT mScreenTimeOutReceiver;
    BroadcastReceiver mBroadcastReceiver;

    static FirstPageFragmentListener firstPageFragmentListenerFrag;



    public static FarmerInformationFragment newInstance(FirstPageFragmentListener firstPageFragmentListener) {
        FarmerInformationFragment f = new FarmerInformationFragment();
        firstPageFragmentListenerFrag = firstPageFragmentListener;
        return f;
    }


    /** (non-Javadoc)
     * @see android.support.v4.app.Fragment#onCreateView(android.view.LayoutInflater, android.view.ViewGroup, android.os.Bundle)
     */
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        if (container == null) {
            // We have different layouts, and in one of them this
            // fragment's containing frame doesn't exist.  The fragment
            // may still be created from its saved state, but there is
            // no reason to try to create its view hierarchy because it
            // won't be displayed.  Note this is not needed -- we could
            // just run the code below, where we would create and return
            // the view hierarchy; it would just never be used.
            return null;
        }


        View view = inflater.inflate(R.layout.tab_frag_farmer_info_layout, container,false);
        mScreenTimeOutReceiver = new BroadT();
        mLandHoldingList = (ListView)view.findViewById(R.id.landHoldingList);
        mFamilyDetailsList = (ListView)view.findViewById(R.id.familyDetailsList);
        mBankDetailsList = (ListView)view.findViewById(R.id.bankDetailsList);
        mOtherDetailsList = (ListView)view.findViewById(R.id.otherDetailsList);


        mLandHoldingList.setAdapter(new LandHoldingListAdapter(getActivity(),null ,mScreenTimeOutReceiver,FarmerInformationFragment.this.getActivity()));



        mArrowLandHolding = (ImageView)view.findViewById(R.id.arrowLandHolding);
        mArrowFamilyHolding = (ImageView)view.findViewById(R.id.arrowFamily);
        mArrowBankHolding = (ImageView)view.findViewById(R.id.arrowBank);
        mArrowOtherHolding = (ImageView)view.findViewById(R.id.arrowOther);


        mLandHoldingRelativeLayout = (RelativeLayout)view.findViewById(R.id.landrel);
        mFamilyDetailsRelativeLayout = (RelativeLayout)view.findViewById(R.id.familyrel);
        mBankDetailsRelativeLayout = (RelativeLayout)view.findViewById(R.id.bankrel);
        mOtherDetailsRelativeLayout = (RelativeLayout)view.findViewById(R.id.otherrel);



        // Land 

        mLandHoldingRelativeLayout.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View arg0) {

                if(mLandFlg)
                {
                    mArrowLandHolding.setImageDrawable(getResources().getDrawable(R.drawable.down_aarow));
                    mLandHoldingList.setVisibility(View.VISIBLE);
                    mLandFlg = false;

                }
                else
                {

                    mArrowLandHolding.setImageDrawable(getResources().getDrawable(R.drawable.up_aarow));
                    mLandHoldingList.setVisibility(View.GONE);
                    mLandFlg = true;


                }

            }
        });


        // Family 

        mFamilyDetailsRelativeLayout.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View arg0) {

                if(mFamilyFlg)
                {
                    mArrowFamilyHolding.setImageDrawable(getResources().getDrawable(R.drawable.down_aarow));
                    mFamilyDetailsList.setVisibility(View.VISIBLE);
                    mFamilyFlg = false;

                }
                else
                {
                    mArrowFamilyHolding.setImageDrawable(getResources().getDrawable(R.drawable.up_aarow));
                    mFamilyDetailsList.setVisibility(View.GONE);
                    mFamilyFlg = true;

                }

            }
        });


        // Bank 

        mBankDetailsRelativeLayout.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View arg0) {

                if(mBankFlg)
                {
                    mArrowBankHolding.setImageDrawable(getResources().getDrawable(R.drawable.down_aarow));
                    mBankDetailsList.setVisibility(View.VISIBLE);
                    mBankFlg = false;

                }
                else
                {

                    mArrowBankHolding.setImageDrawable(getResources().getDrawable(R.drawable.up_aarow));
                    mBankDetailsList.setVisibility(View.GONE);
                    mBankFlg = true;


                }

            }
        });


        // Other 

        mOtherDetailsRelativeLayout.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View arg0) {

                if(mOtherDetailsFlg)
                {
                    mArrowOtherHolding.setImageDrawable(getResources().getDrawable(R.drawable.down_aarow));
                    mOtherDetailsList.setVisibility(View.VISIBLE);
                    mOtherDetailsFlg = false;

                }
                else
                {

                    mArrowOtherHolding.setImageDrawable(getResources().getDrawable(R.drawable.up_aarow));
                    mOtherDetailsList.setVisibility(View.GONE);
                    mOtherDetailsFlg = true;


                }

            }
        });



    /* mBroadcastReceiver = new BroadcastReceiver() {
                @Override
                public void onReceive(Context context, Intent intent) {
                    Toast.makeText(context, "Recived", 
                               Toast.LENGTH_LONG).show();
                    FieldVisitFragment fieldvisitFragment = new FieldVisitFragment();
                    FragmentManager fragmentManager = getFragmentManager();
                    FragmentTransaction fragmentTransaction = fragmentManager
                            .beginTransaction();
                    fragmentTransaction.replace(android.R.id.tabcontent, fieldvisitFragment);
                    fragmentTransaction.commit();
                }



            };*/













        return view;






    }


}

我的BaseAdapter来自选项卡A上的片段A列表,其上有一个按钮,我可以将其称为片段A1

import java.util.ArrayList;
import java.util.HashMap;

import com.example.fms.R;

import android.annotation.SuppressLint;
import android.annotation.TargetApi;
import android.app.Activity;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.IntentFilter;
import android.os.Build;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.webkit.WebView.FindListener;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

public class LandHoldingListAdapter extends BaseAdapter{

    private LayoutInflater mInflater;
    private ArrayList<HashMap<String, String>> allElementDetails;
    private Context con;
    BroadcastReceiver mBroadcastReceiver;
    FragmentActivity fragmentActivity;

    public LandHoldingListAdapter (Context context,
            ArrayList<HashMap<String, String>> mapData , BroadcastReceiver mBroadcastReceiver, FragmentActivity fragmentActivity)
    {
        mInflater = LayoutInflater.from(context);
        allElementDetails = mapData;
        con = context;
        this.mBroadcastReceiver = mBroadcastReceiver;
        this.fragmentActivity=fragmentActivity;     
    }

    @Override
    public int getCount() {
        /*return allElementDetails.size();*/

        return 1 ;
    }

    @Override
    public Object getItem(int position) {
         //return allElementDetails.get(position);
         return 1;
    }

    @Override
    public long getItemId(int position) {
        return 1;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {


        //HashMap<String, String> map = new HashMap<String, String>();
        //map = allElementDetails.get(position);


        convertView = mInflater.inflate(R.layout.farmer_info_land_holding_table_row, null);

        TextView rsNumberTxt = (TextView) convertView.findViewById(R.id.RSnumberTxt);
        TextView type = (TextView) convertView.findViewById(R.id.type);
        TextView year = (TextView) convertView.findViewById(R.id.year);
        ImageView visitImg = (ImageView)convertView.findViewById(R.id.visitButton);
        ImageView yieldImg = (ImageView)convertView.findViewById(R.id.yieldButton);
        ImageView landImg = (ImageView)convertView.findViewById(R.id.landButton);

        visitImg.setOnClickListener(new View.OnClickListener() {

            @SuppressWarnings("static-access")
            @TargetApi(Build.VERSION_CODES.HONEYCOMB) @SuppressLint("NewApi") @Override
            public void onClick(View arg0) {/*
                Toast.makeText(con, "Visit Clicked ", Toast.LENGTH_LONG).show();
                con.registerReceiver(mBroadcastReceiver, new IntentFilter(
                        "start.fragment.action"));
            */

                Toast.makeText(con, "clicked!", Toast.LENGTH_SHORT).show();

                FragmentTransaction ft = fragmentActivity.getSupportFragmentManager().beginTransaction();

                ft.replace(R.id.viewpager, new FieldVisitFragment().newInstance());
                ft.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN);
                ft.addToBackStack(null);

                ft.commit();

            }
        });

        return convertView;
    }

}

0 个答案:

没有答案