From f971248d17642b4454e3237790a98d7d9d460ddf Mon Sep 17 00:00:00 2001 From: chiemy Date: Sat, 30 Aug 2014 16:33:39 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E6=94=B9item=E8=83=8C=E6=99=AF?= =?UTF-8?q?=E3=80=81=E5=86=85=E5=AE=B9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- res/drawable-mdpi/card_bg.9.png | Bin 0 -> 212 bytes res/layout/frag_layout.xml | 17 ++++--- .../example/cviewpager/JellyPagerAdapter.java | 13 +++++ src/com/example/cviewpager/MainActivity.java | 46 +++++++++++------- 4 files changed, 51 insertions(+), 25 deletions(-) create mode 100644 res/drawable-mdpi/card_bg.9.png create mode 100644 src/com/example/cviewpager/JellyPagerAdapter.java diff --git a/res/drawable-mdpi/card_bg.9.png b/res/drawable-mdpi/card_bg.9.png new file mode 100644 index 0000000000000000000000000000000000000000..e12fe1df48560b2976126314436170b5be9fc453 GIT binary patch literal 212 zcmV;_04x8AP)FJV z&TV^1MP=>RxgW@TKYC_KDM=|sYNCDGuX7=U;lP;roz_+EB*rNFhR@@F^l7yloo%S| z-Fw^sPYQO57dv&`viRf{43FoFuxK}>n#W;e0feDBJ O0000 - - + + diff --git a/src/com/example/cviewpager/JellyPagerAdapter.java b/src/com/example/cviewpager/JellyPagerAdapter.java new file mode 100644 index 0000000..20bee82 --- /dev/null +++ b/src/com/example/cviewpager/JellyPagerAdapter.java @@ -0,0 +1,13 @@ +package com.example.cviewpager; + +import android.view.View; + +public interface JellyPagerAdapter{ + + public int getCount(); + + public Object getItem(int position); + + public View getView(int position,View convertView); + +} diff --git a/src/com/example/cviewpager/MainActivity.java b/src/com/example/cviewpager/MainActivity.java index 49f7944..53aa29b 100644 --- a/src/com/example/cviewpager/MainActivity.java +++ b/src/com/example/cviewpager/MainActivity.java @@ -1,37 +1,57 @@ package com.example.cviewpager; -import android.graphics.Color; import android.os.Bundle; import android.support.v4.app.FragmentActivity; +import android.support.v4.view.ViewPager.OnPageChangeListener; import android.view.LayoutInflater; import android.view.View; -import android.view.ViewGroup; -import android.widget.ImageView; import android.widget.TextView; +import android.widget.Toast; public class MainActivity extends FragmentActivity { private LayoutInflater inflater; - MyViewPager pager; + JellyViewPager pager; int currentItem; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); inflater = LayoutInflater.from(this); - pager = (MyViewPager) findViewById(R.id.myViewPager1); + pager = (JellyViewPager) findViewById(R.id.myViewPager1); pager.setAdapter(new MyAdapter()); + pager.setOnPageChangeListener(new OnPageChangeListener() { + @Override + public void onPageSelected(int arg0) { + Toast.makeText(MainActivity.this, "" + arg0, Toast.LENGTH_SHORT).show(); + } + + @Override + public void onPageScrolled(int arg0, float arg1, int arg2) { + } + + @Override + public void onPageScrollStateChanged(int arg0) { + } + }); } public void onClick(View view) { - pager.setCurrentItem(pager.getCurrentItem() + 1); + switch(view.getId()){ + case R.id.preBtn: + pager.showPre(); + break; + case R.id.nextBtn: + pager.showNext(); + break; + } } - public class MyAdapter implements SampleAdapter{ + public class MyAdapter implements JellyPagerAdapter{ @Override public int getCount() { - return 5; + return 10; } @Override @@ -40,20 +60,12 @@ public Integer getItem(int position) { } @Override - public long getItemId(int position) { - return position; - } - - @Override - public View getView(int position,View convertView,ViewGroup container) { + public View getView(int position,View convertView) { if(convertView == null){ convertView = inflater.inflate(R.layout.frag_layout, null); - convertView.setBackgroundColor(Color.BLUE); } - ImageView iv = ViewHolder.get(convertView, R.id.imageView1); TextView tv = ViewHolder.get(convertView,R.id.tv); tv.setText(position + ""); - iv.setImageResource(getItem(position)); return convertView; }