向View-pager添加点指示符

时间:2018-04-27 10:42:07

标签: android android-layout

如何在项目中添加点指示符。 我尝试了很多教程和解决方案,但所有解决方案都需要将片段添加到我不想要的项目中。

SlideAdapter.java

public class SlideAdapter extends PagerAdapter {

Context context;
LayoutInflater inflater;


public int[] lst_images = {
        R.drawable.iconslide1,
        R.drawable.iconslide2,
        R.drawable.iconslide3,
        R.drawable.iconslide2
};

public String[] lst_title = {
        "COSMONAUT",
        "SATELITE",
        "GALAXY",
        "ROCKET"
}   ;

public String[] lst_description = {
        "a",
        "b",
        "c",
        "d"
};

public int[]  lst_backgroundcolor = {
        Color.rgb(55,55,55),
        Color.rgb(239,85,85),
        Color.rgb(110,49,89),
        Color.rgb(1,188,212)
};
public SlideAdapter(Context context) {
    this.context = context;
}

@Override
public int getCount() {
    return lst_title.length;
}
@Override
public boolean isViewFromObject(View view, Object object) {
    return (view==(LinearLayout)object);
}
@Override
public Object instantiateItem(ViewGroup container, int position) {
    inflater = (LayoutInflater) context.getSystemService(context.LAYOUT_INFLATER_SERVICE);
    View v = inflater.inflate(R.layout.slide , container , false);
    LinearLayout layoutslide = (LinearLayout) v.findViewById(R.id.slidelinearlayout);
    ImageView imgslide = (ImageView)  v.findViewById(R.id.slideimg);
    TextView txttitle= (TextView) v.findViewById(R.id.txttitle);
    TextView description = (TextView) v.findViewById(R.id.txtdescription);
    layoutslide.setBackgroundColor(lst_backgroundcolor[position]);
    imgslide.setImageResource(lst_images[position]);
    txttitle.setText(lst_title[position]);
    description.setText(lst_description[position]);
    container.addView(v);
    return v;
}
@Override
public void destroyItem(ViewGroup container, int position, Object object) {
    container.removeView((LinearLayout)object);
}}

我在main_Slider java类中做了什么

main_Slider.java

 private ViewPager viewPager;
    private SlideAdapter myadapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.slide_main);
        viewPager = (ViewPager) findViewById(R.id.viewpager);
        myadapter = new SlideAdapter(this);
        viewPager.setAdapter(myadapter);}

所以我需要修改我的代码来添加点指示符。

0 个答案:

没有答案