我在我的应用程序中有3个页面,我通过在屏幕上进行滑动运动来遍历。ViewPager无法正确显示图片
整个应用程序只有1个ImageView共享3页/片段。 每个人都有自己的形象。
当我滑动到每个页面时,问题就出现了。当从第1页 - > 2 - > 3并回到2 - > 1 - > 2并最终回到1时,第1页中的ImageView会显示第2页中的ImageView。我不明白为什么会发生这种情况,即使我放入的各种日志清楚地表明我处于正确的页面并且调用了正确的功能。
我用较短的版本替换了此问题中的代码,但基本上也展示了相同的问题。这些是我从网站引用的代码,我不记得在搜索如何执行多个页面的划动时。
该项目的zip文件是here
下面是MainActivity.java代码,
package com.example.swiping;
import java.util.Locale;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.os.Bundle;
import android.support.v4.view.ViewPager;
import android.support.v7.app.ActionBarActivity;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
public class MainActivity extends ActionBarActivity
{
static final int NUM_PAGE = 3;
/**
* 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}.
*/
SectionsPagerAdapter mSectionsPagerAdapter;
/**
* The {@link ViewPager} that will host the section contents.
*/
public static ViewPager mViewPager;
static ImageView ImgView1;
@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.pager); //This is from the activity_main.xml's one and only ViewPager
mViewPager.setAdapter(mSectionsPagerAdapter);
}
/**
* 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 x number of pages.
return NUM_PAGE;
}
@Override
public CharSequence getPageTitle(int position) {
Locale l = Locale.getDefault();
switch (position) {
case 0:
return getString(R.string.title_section1).toUpperCase(l);
case 1:
return getString(R.string.title_section2).toUpperCase(l);
case 2:
return getString(R.string.title_section3).toUpperCase(l);
}
return null;
}
}//SectionsPagerAdapter
/**
* 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";
/**
* 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_main, container,false);
TextView textView = (TextView) rootView.findViewById(R.id.section_label);
textView.setText
(Integer.toString(getArguments().getInt(ARG_SECTION_NUMBER)));
ImgView1 = (ImageView) rootView.findViewById(R.id.imageView1);
Log.i("Swiping", "Current Item is " + mViewPager.getCurrentItem());
mViewPager.setOnPageChangeListener(new ViewPager.SimpleOnPageChangeListener()
{
@Override
public void onPageSelected(int position)
{
Log.i("Swiping", "Current Item is " + mViewPager.getCurrentItem());
if(mViewPager.getCurrentItem() == 0)
{
ImgView1.setImageDrawable(getResources().getDrawable(R.drawable.qnmark));
}
else if(mViewPager.getCurrentItem() == 1)
{
ImgView1.setImageDrawable(getResources().getDrawable(R.drawable.genieus));
}
else if(mViewPager.getCurrentItem() == 2)
{
ImgView1.setImageDrawable(getResources().getDrawable(R.drawable.community));
}
}
});//setOnPageChangeListener
return rootView;
}//onCreateView
}//PlaceholderFragment
}//MainActivity
在我的一个项目中,我遇到了这个问题....如果你还在努力,让我知道 – GvSharma 2014-09-29 08:51:34