今天就跟大家聊聊有關(guān)Android中怎么實(shí)現(xiàn)一個(gè)圖片輪播列表,可能很多人都不太了解,為了讓大家更加了解,小編給大家總結(jié)了以下內(nèi)容,希望大家根據(jù)這篇文章可以有所收獲。
我們提供的服務(wù)有:網(wǎng)站設(shè)計(jì)制作、成都網(wǎng)站建設(shè)、微信公眾號開發(fā)、網(wǎng)站優(yōu)化、網(wǎng)站認(rèn)證、無極ssl等。為上1000+企事業(yè)單位解決了網(wǎng)站和推廣的問題。提供周到的售前咨詢和貼心的售后服務(wù),是有科學(xué)管理、有技術(shù)的無極網(wǎng)站制作公司
布局文件代碼:
主窗口代碼:
public class IndexActivity extends AppCompatActivity implements View.OnClickListener{ private Context mContext; private List mImageList; private List mCandidateList; private ViewPagerAdapter mPagerAdapter; private CandidateListAdapter mListAdapter; @SuppressLint("HandlerLeak") private Handler handler = new Handler() { @Override public void handleMessage(Message msg) { //每次將當(dāng)前的位置加1,也就是向右滑動一次 vp.setCurrentItem(vp.getCurrentItem() + 1); //遞歸無限循環(huán)調(diào)用 handler.sendEmptyMessageDelayed(0x123, 2000); } }; @Override protected void onCreate(@Nullable Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_index); ButterKnife.bind(this); initData(); initView(); intListener(); } //初始化數(shù)據(jù)源,固定寫法 1.實(shí)例化容器 2.實(shí)例化適配器 3.設(shè)置適配器 private void initData() { mContext = this; mImageList = new ArrayList<>(); mCandidateList = new ArrayList<>(); mListAdapter = new CandidateListAdapter(mCandidateList); mPagerAdapter = new ViewPagerAdapter(mImageList); lvfsv.setAdapter(mListAdapter); vp.setAdapter(mPagerAdapter); getListData(10); getPagerData(); //間隔2秒發(fā)送一次信息 handler.sendEmptyMessageDelayed(0x123, 2000); } //生成ViewPager數(shù)據(jù)源 private void getPagerData() { mImageList.add(R.drawable.bm1); mImageList.add(R.drawable.bm2); mImageList.add(R.drawable.bm3); mImageList.add(R.drawable.bm4); mImageList.add(R.drawable.bm5); mImageList.add(R.drawable.bm6); mPagerAdapter.notifyDataSetChanged(); //初始的位置在正中間 vp.setCurrentItem(mPagerAdapter.getCount() / 2); } //生成ListView數(shù)據(jù)源 private void getListData(int num) { for (int i = 0; i < num; i++) { Candidate candidate = new Candidate(); candidate.setName("姓名:尼爾斯·亨利克·戴維·玻爾"); candidate.setInfo("職業(yè):學(xué)者,物理學(xué)家,足球運(yùn)動員"); candidate.setTrait("成就:哥本哈根學(xué)派的創(chuàng)始人,1922年獲得諾貝爾物理學(xué)獎(jiǎng)"); mCandidateList.add(candidate); } mListAdapter.notifyDataSetChanged(); } private void initView() { tvTitleMiddle.setText("輪播列表"); //手動設(shè)置ScrollView的位置 scrollView.smoothScrollTo(0, 0); } //初始化監(jiān)聽 private void intListener() { mPagerAdapter.setmCallback((v, position) -> { Toast.makeText(mContext, "position: " + position, Toast.LENGTH_SHORT).show(); }); lvfsv.setOnItemClickListener((parent, view, position, id) -> { Toast.makeText(mContext, "position: " + position, Toast.LENGTH_SHORT).show(); }); for (int i = 0; i < 4; i++) { ivs.getChildAt(i).setOnClickListener(this); } } @Override public void onClick(View v) { switch (v.getId()){ case R.id.iv1: case R.id.iv2: case R.id.iv3: case R.id.iv4: Toast.makeText(mContext, "此處跳轉(zhuǎn)", Toast.LENGTH_SHORT).show(); } }}
ListView需要覆蓋onMeasure方法,代碼如下:
public class ListViewForScrollView extends ListView { public ListViewForScrollView(Context context) { super(context); } public ListViewForScrollView(Context context, AttributeSet attrs) { super(context, attrs); } public ListViewForScrollView(Context context, AttributeSet attrs, int defStyle) { super(context, attrs, defStyle); } @Override /** * 重寫該方法,達(dá)到使ListView適應(yīng)ScrollView的效果 */ protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { int expandSpec = MeasureSpec.makeMeasureSpec(Integer.MAX_VALUE >> 2, MeasureSpec.AT_MOST); super.onMeasure(widthMeasureSpec, expandSpec); }}
ViewPager適配器代碼:
public class ViewPagerAdapter extends PagerAdapter implements View.OnClickListener { //圖片的資源id列表 private List mList; private Callback mCallback; public ViewPagerAdapter(List mList) { this.mList = mList; } public void setmCallback(Callback mCallback) { this.mCallback = mCallback; } public interface Callback { void onClick(View v, int position); } @Override //將適配器中的數(shù)據(jù)設(shè)為無窮大 public int getCount() { return Integer.MAX_VALUE; } @Override //固定寫法,不覆蓋會報(bào)錯(cuò) public void destroyItem(@NonNull ViewGroup container, int position, @NonNull Object object) { container.removeView((View) object); } @Override //固定寫法 public boolean isViewFromObject(@NonNull View view, @NonNull Object object) { return view == object; } @NonNull @Override public Object instantiateItem(@NonNull ViewGroup container, int position) { LayoutInflater inflater = LayoutInflater.from(container.getContext()); ImageView imageView = (ImageView) inflater.inflate(R.layout.item_image_pager, null); //將position轉(zhuǎn)換成余數(shù) int realPosition = position % mList.size(); imageView.setImageResource(mList.get(realPosition)); imageView.setOnClickListener(this); //tag放跳轉(zhuǎn)需要的數(shù)據(jù) imageView.setTag(realPosition); //將實(shí)例加入父控件 container.addView(imageView); return imageView; } @Override //使用接口將position回傳 public void onClick(View v) { mCallback.onClick(v, (int) v.getTag()); }}
看完上述內(nèi)容,你們對Android中怎么實(shí)現(xiàn)一個(gè)圖片輪播列表有進(jìn)一步的了解嗎?如果還想了解更多知識或者相關(guān)內(nèi)容,請關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道,感謝大家的支持。
網(wǎng)站標(biāo)題:Android中怎么實(shí)現(xiàn)一個(gè)圖片輪播列表
轉(zhuǎn)載來于:
http://weahome.cn/article/piosdi.html