2016-03-07 88 views
0

我試圖弄清楚如何在viewpager中從imagebutton創建活動(有3個圖片按鈕)。我是一個初學者,並創建我的第一個應用程序。在Viewpager中創建活動 - Android Studio

這裏是我的main_java

private ViewPager mViewPager; 
private DcDesignPageAdapter dcDesignPageAdapter; 

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

    SectionsPagerAdapter(getSupportFragmentManager()); 
    dcDesignPageAdapter = new DcDesignPageAdapter(getSupportFragmentManager(), getApplicationContext()); 

    mViewPager = (ViewPager) findViewById(R.id.container); 

    mViewPager.setAdapter(dcDesignPageAdapter); 
} 


@Override 
public boolean onCreateOptionsMenu(Menu menu) { 
    getMenuInflater().inflate(R.menu.menu_dc_design, menu); 
    return true; 
} 

@Override 
public boolean onOptionsItemSelected(MenuItem item) { 
    int id = item.getItemId(); 

    if (id == R.id.action_settings) { 
     return true; 
    } 

    return super.onOptionsItemSelected(item); 
} 

這裏是我的代碼片段

public class DcDesignFragment extends Fragment { 
    public static final String ImageButtonIDKey = "imagebuttonkey"; 

    @Nullable 
    @Override 
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { 
     View view = inflater.inflate(fragment_dcdesigns, container, false); 
     Bundle bundle = getArguments(); 

     if(bundle != null) { 

      int imageButtonID = bundle.getInt(ImageButtonIDKey); 

      displayValues(view, imageButtonID); 
     } 
     return view; 
    } 

    private void displayValues(View view, int imageButtonID) { 
     ImageButton imageButton = (ImageButton) view.findViewById(R.id.imageButton); 
     imageButton.setImageResource(imageButtonID); 
    } 
} 

這裏是我的PageAdapter

String[] dcdesigns; 

public DcDesignPageAdapter(FragmentManager fm, Context context) { 
    super(fm); 

    Resources resources = context.getResources(); 
    dcdesigns = resources.getStringArray(R.array.dcdesigns); 
} 

@Override 
public Fragment getItem(int position) { 
    Bundle bundle = new Bundle(); 
    bundle.putInt(DcDesignFragment.ImageButtonIDKey,getDcdesignsImageButtonID(position)); 

    DcDesignFragment dcDesignFragment = new DcDesignFragment(); 
    dcDesignFragment.setArguments(bundle); 
    return dcDesignFragment; 
} 

private int getDcdesignsImageButtonID(int position) { 
    int id = 0; 
    switch (position) { 

     case 0: 
      id = R.drawable.1; 
      break; 
     case 1: 
      id = R.drawable.2; 
      break; 
     case 2: 
      id = R.drawable.3; 
    } 

    return id; 
} 

@Override 
public CharSequence getPageTitle(int position) { 
    return dcdesigns[position]; 
} 

@Override 
public int getCount() { 
    return dcdesigns.length; 
} 

非常感謝你,任何幫助將不勝感激。

+0

那麼,什麼是您所遇到的問題? – chRyNaN

+0

我想在用戶點擊圖像按鈕時移動到另一個活動。 –

+0

將onClickListener添加到ImageButton。調用getActivity()(從Fragment中)來獲取對Activity的引用,並使用傳入Intent的startActivity方法來啓動Activity。 – chRyNaN

回答

0

在您的Fragment中,只需將一個onClickListener放置到ImageButton。您可以使用getActivity()方法獲取對父活動的引用。使用startActivity()方法啓動一個Activity。在DcDesignFragment新displayValues()方法:

private void displayValues(View view, int imageButtonID) { 
    ImageButton imageButton = (ImageButton) view.findViewById(R.id.imageButton); 
    imageButton.setImageResource(imageButtonID); 
    imageButtonnew.setOnClickListener(new View.OnClickListener() { 
     @Override 
     public void onClick(View v) { 
      //Create your intent 
      //Replace "NextActivity" with your Activity name. 
      Intent intent = new Intent(getActivity(), NextActivity.class); 
      //This will launch your new Activity 
      getActivity().startActivity(intent); 
     } 
    }); 

} 

此外,確保您的下一個活動是在AndroidManifest.xml文件中聲明。

更新:您更新displayValues()方法在問候您的評論:

private void displayValues(View view, final int imageButtonID) { 
    ImageButton imageButton = (ImageButton) view.findViewById(R.id.imageButton); 
    imageButton.setImageResource(imageButtonID); 
    imageButtonnew.setOnClickListener(new View.OnClickListener() { 
     @Override 
     public void onClick(View v) { 
      Intent intent; 
      switch(imageButtonID){ 
       default: 
       case R.drawable.1: 
        //Create your intent 
        //Replace "NextActivity1" with your Activity name. 
        intent = new Intent(getActivity(), NextActivity1.class); 
        break; 
       case R.drawable.2: 
        //Replace "NextActivity2" with your Activity name. 
        intent = new Intent(getActivity(), NextActivity2.class); 
        break; 
       case R.drawable.3: 
        //Replace "NextActivity3" with your Activity name. 
        intent = new Intent(getActivity(), NextActivity3.class); 
        break; 
      } 
      //This will launch your new Activity 
      getActivity().startActivity(intent); 
     } 
    }); 

} 
+0

完美的工作!另一個問題是,如果我想爲第二個和第三個圖像按鈕添加不同的活動,我該怎麼做? –

+0

@ Cassie.L你會做同樣的事情,你做了這個ImageButton,但更改Intent構造函數中的第二個參數。例如,而不是'Intent intent = new Intent(getActivity(),NextActivity.class);''你會做'Intent intent = new Intent(getActivity(),OtherActivity.class);' – chRyNaN

+0

我試過這個但不起作用。 「Intent intent = new Intent(getActivity(),1.class); getActivity()。startActivity(intent); Intent intent1 = new Intent(getActivity(),2.class); –