适配器:
public class MyViewAdapter extends StaticPagerAdapter {
private SharedPreferences sharedPreferences; private Context context; private ArrayList<Integer> list; private Button bt; public MyViewAdapter(MainActivity mainActivity, ArrayList<Integer> list, Button bt, SharedPreferences sp) { this.context=mainActivity; this.list=list; this.bt=bt; this.sharedPreferences=sp; }public View getView(final ViewGroup container, int position) { View view= LayoutInflater.from(context).inflate(R.layout.a,null); ImageView imageView= (ImageView) view.findViewById(R.id.img); imageView.setImageResource(list.get(position)); if(position==list.size()-1){
bt.setVisibility(View.VISIBLE);
}
else { bt.setVisibility(View.GONE); } bt.setOnClickListener(new View.OnClickListener() { public void onClick(View view) { Intent intent=new Intent(context,Main2Activity.class); context.startActivity(intent); SharedPreferences.Editor editor=sharedPreferences.edit(); editor.putInt("count",1); editor.commit(); } }); return view; }public int getCount() { return list.size();
----------------------------------------------------------------------------------------------------------
Activity:
package jiyun.com.myapplication;
import android.content.Intent;
import android.content.SharedPreferences; import android.os.Bundle; import android.support.v7.app.AppCompatActivity; import android.widget.Button; import android.widget.ListView;import com.jude.rollviewpager.RollPagerView;
import java.util.ArrayList;
public class MainActivity extends AppCompatActivity {
private ListView lv;
private RollPagerView rollPagerView; private ArrayList<Integer> list = new ArrayList<>(); int[] arr = {R.mipmap.ic_launcher, R.mipmap.ic_launcher, R.mipmap.ic_launcher}; private Button bt; public SharedPreferences sp; public int count; protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); initView();initListener();
}
private void initListener() {
count=sp.getInt("count",0); if(count>0){ startActivity(new Intent(MainActivity.this,Main2Activity.class)); } else { initData(); initAdapter(); } }private void initData() {
for (int i = 0; i < arr.length; i++) { list.add(arr[i]); } }private void initAdapter() {
MyViewAdapter my = new MyViewAdapter(this, list,bt,sp); rollPagerView.setAdapter(my); } private void initView() {rollPagerView = (RollPagerView) findViewById(R.id.rollPagerView);
bt = (Button) findViewById(R.id.bt);
sp=getSharedPreferences("con",MODE_PRIVATE); } }