简体   繁体   中英

How do I load into a specific Fragment / Viewpager with an onClick method?

Basically I have an activity called ComputerOptions. Inside ComputerOptions there are buttons you can click that will take you to a tabbed activity. This TabbedActivity has diffrernt fragments that include the Listview for a set of options. Everytime I set the intent it will only load Tab1 and won't load into Tab2. What code do I need to use so if I click let's say button2 on ComputerOptions it would load tab2 and the fragments Listview in TabbedActivity? Here is the code and image. As you can tell I'm new to android development so any help would be appreciated.

ComputerOptions.java

public class ComputerOptions extends AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.computer_options);



    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
    TextView toolTitle = (TextView) findViewById(R.id.toolbar_title);
    toolTitle.setText("Computer");
    toolTitle.setTextColor(Color.parseColor("#FFFFFF"));
    setSupportActionBar(toolbar);


    // add back arrow to toolbar_layout
    if (getSupportActionBar() != null){
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);
        getSupportActionBar().setDisplayShowHomeEnabled(true);
    }

}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    // handle arrow click here
    if (item.getItemId() == android.R.id.home) {
        finish(); // close this activity and return to preview activity (if there is any)
    }
    return super.onOptionsItemSelected(item);
}

public void Button1(View view) {
    Intent intent = new Intent(view.getContext(), TabbedActivity.class);
    startActivityForResult(intent, 0);
}

public void Button2(View view) {

    /** I think I do something like this in the intent to load tab2 
    Intent i = new Intent(this, TabbedActivity.class);
    i.putExtra("frgToLoad", 1);
    // Now start your activity
    startActivity(i);
     */
}

}

PageAdapter.java

class PagerAdapter extends FragmentStatePagerAdapter {
int mNumOfTabs;

public PagerAdapter(FragmentManager fm, int NumOfTabs) {
    super(fm);
    this.mNumOfTabs = NumOfTabs;
}

@Override
public Fragment getItem(int position) {

    switch (position) {
        case 0:
            FragmentOne tab1 = new FragmentOne();
            return tab1;
        case 1:
            FragmentTwo tab2 = new FragmentTwo();
            return tab2;
        case 2:
            FragmentThree tab3 = new FragmentThree();
            return tab3;
        case 3:
            FragmentFour  tab4 = new FragmentFour();
            return tab4;
        case 4:
            FragmentFive tab5 = new FragmentFive();
            return tab5;
        default:
            return null;
    }
}

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

}

Button2.java

public class Button2 extends Fragment {


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


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


    return inflater.inflate(R.layout.tab_fragment_2, container, false);


}

@Override
public void onActivityCreated(Bundle savedInstanceState) {
    super.onActivityCreated(savedInstanceState);

    ListView lv = (ListView)getActivity().findViewById(R.id.listView2);


    String[] values = new String[] { "Ex1", "Ex2", "Ex3",
            "Ex4", "Ex5", "Ex6", "Ex7", "Ex8",
            "Ex9", "Ex10" };
    ArrayAdapter<String> adapter = new ArrayAdapter<>(getActivity(),
            android.R.layout.simple_list_item_1, values);

    lv.setAdapter(adapter);


    // Handles items being clicked
    lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> adapterView, View view, int position, long l) {
            if (position == 0) {
                Intent intent = new Intent(getContext(), ComputerOptions.class);
                startActivity(intent);
            }
        }
    }
    );

}

}

try this:

pass tab position on each button click:

Intent intent = new Intent(view.getContext(), TabbedActivity.class);
final Bundle bundle = new Bundle();
bundle.putString("TabNumber", tab_number);
intent.putExtras(bundle);
startActivity(intent);

now in TabbedActivity:

 //get tab number
  final Intent intent = getIntent();
        if (intent.hasExtra("TabNumber")) {
            String tab = intent.getExtras().getString("TabNumber");
            Log.e("TabNumberFriendList",tab);
            switchToTab(tab);
        }
 public void switchToTab(String tab){
         if(tab.equals("0")){
             viewPager.setCurrentItem(0);
         }else if(tab.equals("1")){
             viewPager.setCurrentItem(1);
         }else if(tab.equals("2")){
             viewPager.setCurrentItem(2);
         }
     }

You could send page/fragment number in bundle params when creating intent, like this:

Intent intent = new Intent(view.getContext(), TabbedActivity.class);
intent.putExtra("page_number", 2);  //2 or whatever you want
startActivityForResult(intent, 0);

And then in TabbedActivity onCreate method read that param from bundle:

int startPage = getIntent().getIntExtra("page_number", 0);

Then you have the desired startPage order number inside TabbedActivity, so you can set it whereever you set your PageAdapter.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM