2017-01-06 58 views
1

基本上我有一個叫做ComputerOptions的活動。在ComputerOptions內部有按鈕,您可以點擊它將您帶到選項卡式活動。此TabbedActivity具有不同的片段,其中包含一組選項的Listview。每次我設置意圖時,它只會加載Tab1,並不會加載到Tab2中。我需要使用什麼代碼,所以如果單擊我們在ComputerOptions上的button2,它會加載tabbedActivity中的tab2和片段Listview?這裏是代碼和圖像。正如你可以告訴我新的android開發,所以任何幫助,將不勝感激。如何使用onClick方法加載到特定的Fragment/Viewpager?

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); 
      } 
     } 
    } 
    ); 

} 
0123在TabbedActivity現在

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

}

+0

試試這個http://stackoverflow.com/questions/15150587/how-to-change-tab-on-button-click-in-android –

回答

0

試試這個:

通格位置上的每個按鈕點擊

//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); 
     } 
    } 
0

你可以捆綁PARAMS發送頁/分片號當創建意圖時,像這樣:

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

然後在TabbedActivity onCreate方法讀取捆綁PARAM:

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

然後你有內部TabbedActivity所需的起始頁訂單號碼,這樣你就可以將它設置等。無論您將PageAdapter。

+0

對不起,我在與rafsanahmad007同時輸入它,他也是提供了答案 – daneejela

+0

男人不要對不起。我感謝我能得到的所有幫助!我剛剛嘗試了你的方法,但在TabbedActivity中,.getExtra給了我一個錯誤。 – techixo

+0

我編輯它(它是getIntExtra()而不是getExtra()..) – daneejela

相關問題