我对Android很新,我试图自定义android studio的默认视图寻呼机。 。我试图获取查看寻呼机的页码,例如为每个页面设置不同的图像。
package blabla.example.com.bla;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity {
/**
* The {@link android.support.v4.view.PagerAdapter} that will provide
* fragments for each of the sections. We use a
* {@link FragmentPagerAdapter} derivative, which will keep every
* loaded fragment in memory. If this becomes too memory intensive, it
* may be best to switch to a
* {@link android.support.v4.app.FragmentStatePagerAdapter}.
*/
private SectionsPagerAdapter mSectionsPagerAdapter;
/**
* The {@link ViewPager} that will host the section contents.
*/
private ViewPager mViewPager;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// Create the adapter that will return a fragment for each of the three
// primary sections of the activity.
mSectionsPagerAdapter = new SectionsPagerAdapter(getSupportFragmentManager());
// Set up the ViewPager with the sections adapter.
mViewPager = (ViewPager) findViewById(R.id.container);
mViewPager.setAdapter(mSectionsPagerAdapter);
}
/**
* A placeholder fragment containing a simple view.
*/
public static class PlaceholderFragment extends Fragment {
/**
* The fragment argument representing the section number for this
* fragment.
*/
private static final String ARG_SECTION_NUMBER = "section_number";
public PlaceholderFragment() {
}
/**
* Returns a new instance of this fragment for the given section
* number.
*/
public static PlaceholderFragment newInstance(int sectionNumber) {
PlaceholderFragment fragment = new PlaceholderFragment();
Bundle args = new Bundle();
args.putInt(ARG_SECTION_NUMBER, sectionNumber);
fragment.setArguments(args);
return fragment;
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_main, container, false);
TextView textView = (TextView) rootView.findViewById(R.id.section_label);
textView.setText(getString(R.string.section_format, getArguments().getInt(ARG_SECTION_NUMBER)));
ImageView levelImage = (ImageView) rootView.findViewById(R.id.imageView3);
if (!ARG_SECTION_NUMBER.equals("1")) {
levelImage.setImageResource(R.drawable.example1);
}
else if (!ARG_SECTION_NUMBER.equals("2")) {
levelImage.setImageResource(R.drawable.example2);
}
else if (!ARG_SECTION_NUMBER.equals("3")) {
levelImage.setImageResource(R.drawable.example3);
}
else if (!ARG_SECTION_NUMBER.equals("4")) {
levelImage.setImageResource(R.drawable.example4);
}
else {
levelImage.setImageResource(R.drawable.example5);
}
return rootView;
}
}
/**
* A {@link FragmentPagerAdapter} that returns a fragment corresponding to
* one of the sections/tabs/pages.
*/
public class SectionsPagerAdapter extends FragmentPagerAdapter {
public SectionsPagerAdapter(FragmentManager fm) {
super(fm);
}
@Override
public Fragment getItem(int position) {
// getItem is called to instantiate the fragment for the given page.
// Return a PlaceholderFragment (defined as a static inner class below).
return PlaceholderFragment.newInstance(position + 1);
}
@Override
public int getCount() {
// Show 5 total pages.
return 5;
}
@Override
public CharSequence getPageTitle(int position) {
switch (position) {
case 0:
return "SECTION 1";
case 1:
return "SECTION 2";
case 2:
return "SECTION 3";
case 3:
return "SECTION 4";
case 4:
return "SECTION 5";
}
return null;
}
}
}
你可以看到我到目前为止尝试了这个:))
ImageView levelImage = (ImageView) rootView.findViewById(R.id.imageView3);
if (!ARG_SECTION_NUMBER.equals("1")) {
levelImage.setImageResource(R.drawable.example51);
}
else if (!ARG_SECTION_NUMBER.equals("2")) {
levelImage.setImageResource(R.drawable.example2);
}
else if (!ARG_SECTION_NUMBER.equals("3")) {
levelImage.setImageResource(R.drawable.example3);
}
else if (!ARG_SECTION_NUMBER.equals("4")) {
levelImage.setImageResource(R.drawable.example4);
}
else {
levelImage.setImageResource(R.drawable.example5);
}
但它没有用。我该怎么办?
答案 0 :(得分:1)
变量ARG_SECTION_NUMBER
包含String
,您只是将其用作在getArguments()
捆绑中查找值的键。所以你应该改变你的代码:
if (getArguments().getInt(ARG_SECTION_NUMBER) == 1) {
...
}
else if (getArguments().getInt(ARG_SECTION_NUMBER) == 2) {
...
}
当然,一个更好的解决方案是将图像资源id直接放入包中,而不是仅仅放入一个节号,然后创建一个巨大的if / else树。像这样:
public static PlaceholderFragment newInstance(int sectionNumber, int drawableId) {
PlaceholderFragment fragment = new PlaceholderFragment();
Bundle args = new Bundle();
args.putInt(ARG_SECTION_NUMBER, sectionNumber);
args.putInt(ARG_DRAWABLE_ID, drawableId);
fragment.setArguments(args);
return fragment;
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
...
ImageView levelImage = (ImageView) rootView.findViewById(R.id.imageView3);
levelImage.setImageResource(getArguments().getInt(ARG_DRAWABLE_ID));
return rootView;
}