在viewpager中实现next和previous

时间:2016-09-24 11:38:40

标签: java android android-viewpager android-pageradapter

我想在textview中显示下一个和上一个内容,但是,
现在点击下一步,内容会更新,但不会在滑动或点击时显示。

单击并滑动后会更新。

我正在使用pageradapter

下面是我的代码:

pagerAdapter

public Object instantiateItem(ViewGroup container, final int position) {
 View v=mLayoutInflater.inflate(R.layout.pathavali_adapter,container,false);
 detail= (TextView) v.findViewById(R.id.tv_pathavalidetails);
        next.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                pos = pos + 1;
                if (pos < descriptions.size()) {
                    detail.setText(descriptions.get(pos));
                } else {
                    Toast.makeText(mContext, "Last record", Toast.LENGTH_SHORT).show();
                    pos = pos - 1;

                }
            }
        });   

这里的描述是arraylist,pos是arraylist中的位置。

Viewpager.java

ViewPager vp;  
Page_Adapter pa;  

public void onCreate( Bundle savedInstanceState) {  

    super.onCreate(savedInstanceState);  

    Bundle b=this.getArguments();  

    detail=b.getString("detail");  

    pos= b.getInt("pos");  

    description=(ArrayList<String>)b.getSerializable("list");  
}  


public View onCreateView(LayoutInflater inflater, ViewGroup container,    

   Bundle savedInstanceState) {

   View v=           inflater.inflate(R.layout.pathavalidetails_pager,container,false);

    vp= (ViewPager)v.findViewById(R.id.pathali_pager);  

    pa=new Page_Adapter(getActivity(),pos,detail,description);

    vp.setAdapter(pa);   

    return v;  
 }

Page_Adapter.java

public class Pathavali_Adapter extends PagerAdapter {  

Context mContext;  

LayoutInflater mLayoutInflater;  

String details;  

ArrayList<String> descriptions=new ArrayList<String>();  

TextView detail;  
static  int pos;  


public Page_Adapter(Context context,int poss,String detail,  

     ArrayList<String> description){  

    mContext=context;  

    pos=poss;  

    details=detail;  

    descriptions=description;  

    mLayoutInflater=   
    (LayoutInflater)mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}


public int getCount() {  
    return descriptions.size();  
}



public boolean isViewFromObject(View view, Object object) {  

    return view==((LinearLayout)object);  
}



public void destroyItem(ViewGroup container, int position, Object object) {  

    container.removeView((LinearLayout)object);  
}

0 个答案:

没有答案