我是Android编程新手。 我有以下布局:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<android.support.v4.view.ViewPager
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/pager"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_above="@+id/fundo"
android:background="@color/roxo"
tools:context="com.fnsp.anedotaspiadas.Anedotas" />
<LinearLayout
android:id="@+id/fundo"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/adView"
android:layout_alignParentBottom="true"
android:background="@color/roxo"
android:orientation="horizontal" >
<ImageButton
android:id="@+id/previousButton"
style="?android:attr/buttonStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/previous" />
<ImageButton
android:id="@+id/voltarButton"
style="?android:attr/buttonStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/voltar" />
<TextView
android:id="@+id/counter"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="center_horizontal"
android:text="TextView" />
<ImageButton
android:id="@+id/nextButton"
style="?android:attr/buttonStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/next" />
</LinearLayout>
</RelativeLayout>
Fragment Layout:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.fnsp.anedotaspiadas.Anedotas$PlaceholderFragment" >
<ScrollView
android:id="@+id/scrollView1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_alignParentBottom="true" >
<TextView
android:id="@+id/section_label"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="5dp"
android:padding="@dimen/padding"
android:textColor="@color/branco"
android:textSize="20sp" />
</ScrollView>
</RelativeLayout>
按钮位于片段之外,但我希望按钮能够在片段之间切换,而textView则显示片段编号。我开始使用textView,但是当我尝试打印数字时,它没有打印正确的数字,当我滑动时,他有时会跳过几个数字。
public class Anedotas extends ActionBarActivity {
DBTools dbTools = new DBTools(this);
static ArrayList<String> ar = new ArrayList<String>();
private static TextView counter;
private ImageButton previous;
private ImageButton next;
private ImageButton voltar;
SectionsPagerAdapter mSectionsPagerAdapter;
ViewPager mViewPager;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_anedotas);
Intent intent = getIntent();
Bundle extras = intent.getExtras();
int aux = Integer.parseInt(extras.getString("categoria"));
this.setTitle(dbTools.retornaNomeCategoria(aux));
counter = (TextView) findViewById(R.id.counter);
previous = (ImageButton) findViewById(R.id.previousButton);
next = (ImageButton) findViewById(R.id.nextButton);
previous = (ImageButton) findViewById(R.id.voltarButton);
//buscar as anedotas...
ar = dbTools.getAnedotasCategoria(aux);
// 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.pager);
mViewPager.setAdapter(mSectionsPagerAdapter);
//alteraBarraInferior();
}
@Override
public void onResume(){
super.onResume();
}
@Override
public void onPause(){
super.onPause();
//this.finish();
}
/**
* 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 3 total pages.
return ar.size();
}
}
/**
* A placeholder fragment containing a simple view.
*/
public static class PlaceholderFragment extends Fragment {
/**
* The fragment argument representing the section number for this
* fragment.
*/
private final static String ARG_SECTION_NUMBER = "section_number";
/**
* 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;
}
public PlaceholderFragment() {
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_anedotas, container, false);
Bundle args = getArguments();
int i = args.getInt(ARG_SECTION_NUMBER);
String help = i+"/"+ar.size();
counter.setText(help);
((TextView) rootView.findViewById(R.id.section_label)).setText(Html.fromHtml(ar.get(i-1)));
return rootView;
}
}
}
有谁知道问题是什么?甚至现在,我如何创建一个ClickListener来调用上一个或下一个片段。 我感谢你的帮助和可用性。
答案 0 :(得分:0)
为了能够打印当前的ViewPager位置,您应该使用ViewPager的OnPageChangeListener:
ViewPager pager = (ViewPager) findViewById(R.id.pager);
pager.setOnPageChangeListener(new ViewPager.SimpleOnPageChangeListener() {
@Override
public void onPageSelected(int position) {
counter.setText(String.valueOf(position));
}
});
要更改ViewPager的当前位置(片段切换),您应该使用ViewPager.setCurrentItem方法:
pager.setCurrentItem(1)
1这里是你的片段的位置。这应该从按钮的onClick侦听器调用。当然,您应该将该值更改为正确的值。
为什么从onCreateView打印当前位置不正确?
ViewPager由于其实现而一次初始化几个片段。例如,ViewPager中有5个片段,ViewPager即将显示片段#3。它将初始化片段#3,片段#2和片段#4。因此,将调用onCreateView中的这3个片段,这就是为什么你会出错的原因。