我想用Android Studio制作图像滑块。
我发现了一个新的滑块代码,使用ViewPager
构建,我尝试了它并且工作了..
但我想为下一个和上一个添加2个按钮..
单击按钮时,必须转到下一张图片或上一张图片。
这里是mainactivity
,
package org.trial.slider;
import android.app.Activity;
import android.os.Bundle;
import android.support.v4.view.ViewPager;
public class MainActivity extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ViewPager viewPager = (ViewPager) findViewById(R.id.view_pager);
ImageAdapter adapter = new ImageAdapter(this);
viewPager.setAdapter(adapter);
}
}
ImageAdapter.java
档案,
package org.trial.slider;
import android.content.Context;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;
/**
* Created by ismail on 30.03.16.
*/
public class ImageAdapter extends PagerAdapter {
Context context;
private int[] GalImages = new int[] {
R.drawable.one,
R.drawable.two,
R.drawable.three
};
ImageAdapter(Context context){
this.context=context;
}
@Override
public int getCount() {
return GalImages.length;
}
@Override
public boolean isViewFromObject(View view, Object object) {
return view == ((ImageView) object);
}
@Override
public Object instantiateItem(ViewGroup container, int position) {
ImageView imageView = new ImageView(context);
int padding = context.getResources().getDimensionPixelSize(R.dimen.padding_medium);
imageView.setPadding(padding, padding, padding, padding);
imageView.setScaleType(ImageView.ScaleType.FIT_XY);
imageView.setImageResource(GalImages[position]);
((ViewPager) container).addView(imageView, 0);
return imageView;
}
@Override
public void destroyItem(ViewGroup container, int position, Object object) {
((ViewPager) container).removeView((ImageView) object);
}
}
我有2个XML文件; fragment_pager
和swipe_fragment
答案 0 :(得分:2)
方法名称viewPager.setCurrentItem(position)
此方法将整数值作为索引并使用此方法在viewPager对象上设置,如果是pager项,则可以更改位置。
答案 1 :(得分:1)
MainActivity
和activity_main.xml
button.setonclicklister(this);
OnCreate()
此处的示例代码:
@Override
public void onClick(View v) {
int id = v.getId();
switch (id) {
//prev button
case R.id.btn_list_detail_img_prev:
viewPager.setCurrentItem(mPager.getCurrentItem()-1,true);
break;
//nextbutton, 1st argument : pageindex, 2nd argument : Enable smooth
case R.id.btn_list_detail_img_next:
viewPager.setCurrentItem(mPager.getCurrentItem()+1,true);
break;
}
答案 2 :(得分:0)
class MyClass extends Fragment implements ViewPager.OnPageChangeListener {
private int selectedViewPager = 0;
public void onViewClicked(View view) {
switch (view.getId()) {
case R.id.backward:
if (selectedViewPager > 0) {
viewPager.setCurrentItem(selectedViewPager - 1, true);
}
break;
case R.id.fwd:
if (viewPager.getAdapter().getCount() > selectedViewPager) {
viewPager.setCurrentItem(selectedViewPager + 1, true);
}
break;
}
}
@Override
public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
}
@Override
public void onPageSelected(int position) {
selectedViewPager = position;
}
@Override
public void onPageScrollStateChanged(int state) {
}
}