0

我有以下问题。我有一个带有 viewpager 和两个标签的应用程序。这两个选项卡是两个列表片段。现在我希望如果用户在第一个选项卡中选择一个项目,则在第二个选项卡中选择第 5 个项目。(第二个选项卡是第一项文本的翻译)。我的问题是,当我在第一个选项卡中选择一个项目,现在切换到第二个选项卡时,没有任何选择。当我单击第一个选项卡中的某些内容时,我调用了 Parent 活动中的一个函数,该函数调用了第二个片段中的一个函数,该函数应该选择第 5 个项目。这是片段活动的代码:

public class Dailyquran extends FragmentActivity implements ActionBar.TabListener {


    SectionsPagerAdapter mSectionsPagerAdapter;
    ViewPager mViewPager;


    public  ArrayList<Tweet> tweets = new ArrayList<Tweet>(); 
    public  ArrayList<Tweet> tweets2 = new ArrayList<Tweet>(); 
    //private static ArrayList<String> roomList;

    public String a;
    public static int laenge_inhalt;

    public static int selected;

    public static String sure_media;
    public static String vers_media;
    String koran_filename;
    //int tabwahl=0;

    int playstatus=0;
    MediaPlayer mp = new MediaPlayer();
    MenuItem playMenu;

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        MenuInflater inflater = getMenuInflater();
        inflater.inflate(R.menu.activity_dailyquran, menu);
        playMenu = menu.findItem(R.id.menu_play);
        return true;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setTitle("Daily Quran");
        setContentView(R.layout.activity_dailyquran);        
        mSectionsPagerAdapter = new SectionsPagerAdapter(getSupportFragmentManager());
        // Set up the action bar.
        final ActionBar actionBar = getActionBar();
        // set the app icon as an action to go home
        actionBar.setDisplayHomeAsUpEnabled(true); 
        //enable tabs in actionbar
        actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);

     // Set up the ViewPager with the sections adapter.
        mViewPager = (ViewPager) findViewById(R.id.pager);

        mViewPager.setAdapter(mSectionsPagerAdapter);

     // When swiping between different sections, select the corresponding tab.
        // We can also use ActionBar.Tab#select() to do this if we have a reference to the
        // Tab.
        mViewPager.setOnPageChangeListener(new ViewPager.SimpleOnPageChangeListener() {
            @Override
            public void onPageSelected(int position) {
                actionBar.setSelectedNavigationItem(position);
            }
        });

        for (int i = 0; i < mSectionsPagerAdapter.getCount(); i++) {
            // Create a tab with text corresponding to the page title defined by the adapter.
            // Also specify this Activity object, which implements the TabListener interface, as the
            // listener for when this tab is selected.
            actionBar.addTab(
                    actionBar.newTab()
                            .setText(mSectionsPagerAdapter.getPageTitle(i))
                            .setTabListener(this));
        }




          main(); 

    }

    /*  WEITER UNTEN SIND DIE TABSELECTED FUNKTIONEN !!!!
    //@Override
    public void onTabUnselected(ActionBar.Tab tab, FragmentTransaction fragmentTransaction) {
    }

   // @Override
    public void onTabSelected(ActionBar.Tab tab, FragmentTransaction fragmentTransaction) {
        // When the given tab is selected, switch to the corresponding page in the ViewPager.
        mViewPager.setCurrentItem(tab.getPosition());
    }

   // @Override
    public void onTabReselected(ActionBar.Tab tab, FragmentTransaction fragmentTransaction) {
    }    
    */

    public class SectionsPagerAdapter extends FragmentPagerAdapter {

        private FragmentTransaction mCurTransaction = null;

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


        @Override
        public int getItemPosition(Object object) {
           return POSITION_NONE; //To make notifyDataSetChanged() do something
       }

        @Override      
        public Fragment getItem(int i) {
          //  Fragment building_fragment = new BuildingFragment();
            Fragment room_fragment = new RoomFragment();
            Fragment transl_fragment = new TransliterationFragment();

          //  Fragment device_fragment = new DeviceFragment();
            Bundle args = new Bundle();

            switch(i){
            case 0:

                room_fragment.setArguments(args);

                return room_fragment;
            case 1:           

                return transl_fragment;
            case 2:  
            //  args.putInt(RoomFragment.ARG_SECTION_NUMBER, i);
            //    room_fragment.setArguments(args);
                return room_fragment;
            default: return null;
            } 
        }
        @Override
        public int getCount() {
            return 2;
        }

        @Override
        public CharSequence getPageTitle(int position) {
            switch (position) {
                case 0: return "a";
                case 1: return "b";
                case 2: return "c";
            }
            return null;
        }

        @Override
        public Object instantiateItem(View container, int position) {
            if (mCurTransaction == null) {
                mCurTransaction = getSupportFragmentManager().beginTransaction();
            }
            // TODO Auto-generated method stub
            Fragment fragment = getItem(position);

            if (fragment!=null){
                System.out.println("Fragment Found!");
                mCurTransaction.attach(fragment);
                }


            return fragment;//super.instantiateItem(container, position);
        }


    }


    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle item selection
        switch (item.getItemId()) {
            case R.id.menu_play:
                play();
                return true;



            default:
                return super.onOptionsItemSelected(item);
        }
    }

    @Override
    public boolean onPrepareOptionsMenu(Menu menu) {
        MenuItem switchButton = menu.findItem(R.id.menu_play);     



    }


    public class Tweet {  

        String content;  
        String sure;
        String vers;

}  

    public class Tweet2 {  

        String content;  
        String sure;
        String vers;

}  

public void onTabReselected(Tab tab, android.app.FragmentTransaction ft) {
        // TODO Auto-generated method stub

    }

    public void onTabSelected(Tab tab, android.app.FragmentTransaction ft) {
        // TODO Auto-generated method stub

        mViewPager.setCurrentItem(tab.getPosition());

    }

    public void onTabUnselected(Tab tab, android.app.FragmentTransaction ft) {
        // TODO Auto-generated method stub

    }



//HERE IS THE CODE FOR SELECTING THE SECOND ITEM

public void one_changed()
    {
        TransliterationFragment fragment_meaning = (TransliterationFragment) getSupportFragmentManager().findFragmentById(R.id.myfragment2);                
        Toast.makeText(getApplicationContext(),"Change Selection", Toast.LENGTH_SHORT).show(); 


        fragment_meaning.change_selected(); // do what updates are required   

    }

这是第二个片段的相关代码,我可以在控制台中看到字符串“Hallo”,因此确实调用了该函数,但未选择该项目。

public void change_selected()
    {

        System.out.println("Hallo");
        ListView list=getListView();  
        list.setSelection(4);
    }
4

1 回答 1

0

当我在父活动的函数 on_changed 中添加此代码时,它现在可以工作:

TransliterationFragment fragment_meaning = (TransliterationFragment) getSupportFragmentManager().findFragmentByTag("android:switcher:"+R.id.pager+":1");      
fragment_meaning.change_selected(); // do what updates are required 
于 2013-01-14T20:37:51.067 回答