当用户从左向右或从右向左滑动时,我想设计带滑块的Android应用程序(如网站)我想要更改背景而不是整个视图。我需要一个像下面这样的设计。请帮助我使用本机代码示例
答案 0 :(得分:0)
请参阅此图书馆,它可能会帮助您
答案 1 :(得分:0)
您可以使用View pager。
public class ImageDownloader extends Fragment {
public ImageDownloader() {
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View mView = inflater.inflate(R.layout.frag_image, null);
Bundle bundle = getArguments();
String imageUrl = bundle.getString("imageUrl");
ImageView mView2 = (ImageView) mView.findViewById(R.id.image);
ImageLoader mImageLoader = new ImageLoader(getActivity());
mImageLoader.DisplayImage(imageUrl, mView2);
return mView;
}
}
public class FragmentAdapter extends FragmentStatePagerAdapter {
public FragmentAdapter(FragmentManager fm) {
super(fm);
}
@Override
public CharSequence getPageTitle(int position) {
return mdataStrings[position];
}
@Override
public Fragment getItem(int position) {
Bundle bundle = new Bundle();
bundle.putString("imageUrl", mdataStrings[position]);
ImageDownloader mDownloader = new ImageDownloader();
mDownloader.setArguments(bundle);
return mDownloader;
}
@Override
public int getCount() {
return mdataStrings.length;
}
}
并使用像这样:
mdataStrings = GlobalVars.stories.get(pos)
.getUpload_images().split(",");
mViewPager = (ViewPager) findViewById(R.id.viewpager);
FragmentAdapter titleAdapter = new FragmentAdapter(
getSupportFragmentManager());
mViewPager.setAdapter(titleAdapter);
for (int i = 0; i < mdataStrings.length; i++) {
LinearLayout mLinearLayout = (LinearLayout) findViewById(R.id.indicator);
ImageView mImageView = new ImageView(
StoriesDetails.this);
mImageView.setTag(i + "");
if (i == 0) {
mImageView
.setBackgroundResource(R.drawable.black_circle);
} else {
mImageView
.setBackgroundResource(R.drawable.white_circle);
}
mLinearLayout.addView(mImageView);
}
mViewPager
.setOnPageChangeListener(new OnPageChangeListener() {
@Override
public void onPageSelected(int page) {
// TODO Auto-generated method stub
System.out.println("Page : " + page);
LinearLayout mLinearLayout = (LinearLayout) findViewById(R.id.indicator);
mLinearLayout.getChildAt(page)
.setBackgroundResource(
R.drawable.black_circle);
for (int i = 0; i < mdataStrings.length; i++) {
if (page != i) {
mLinearLayout
.getChildAt(i)
.setBackgroundResource(
R.drawable.white_circle);
}
}
}
@Override
public void onPageScrolled(int arg0,
float arg1, int arg2) {
// TODO Auto-generated method stub
}
@Override
public void onPageScrollStateChanged(int page) {
// TODO Auto-generated method stub
}
});