這篇文章給大家分享的是有關(guān)Android如何實(shí)現(xiàn)圖片輪播列表的內(nèi)容。小編覺(jué)得挺實(shí)用的,因此分享給大家做個(gè)參考,一起跟隨小編過(guò)來(lái)看看吧。
成都創(chuàng)新互聯(lián)專注于彭水苗族土家族網(wǎng)站建設(shè)服務(wù)及定制,我們擁有豐富的企業(yè)做網(wǎng)站經(jīng)驗(yàn)。 熱誠(chéng)為您提供彭水苗族土家族營(yíng)銷型網(wǎng)站建設(shè),彭水苗族土家族網(wǎng)站制作、彭水苗族土家族網(wǎng)頁(yè)設(shè)計(jì)、彭水苗族土家族網(wǎng)站官網(wǎng)定制、微信小程序開(kāi)發(fā)服務(wù),打造彭水苗族土家族網(wǎng)絡(luò)公司原創(chuàng)品牌,更為您提供彭水苗族土家族網(wǎng)站排名全網(wǎng)營(yíng)銷落地服務(wù)。
這個(gè)效果在交友a(bǔ)pp中比較常見(jiàn),一般作為首頁(yè)使用,頂部是一個(gè)自動(dòng)輪播的ViewPager,下面放一個(gè)橫向LinearLayout,最下面要放一個(gè)ListView,但是注意這三個(gè)部分都支持滑動(dòng),應(yīng)該和固定在頂部的標(biāo)題欄和底部的操作欄分開(kāi),也就是把ViewPager和LinearLayout以及ListView都放在父控件ScrollView里面
布局文件代碼:
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical"> <include layout="@layout/title_bar"/> <ScrollView android:layout_width="match_parent" android:id="@+id/sv" android:layout_height="0dp" android:layout_weight="1"> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="vertical"> <android.support.v4.view.ViewPager android:id="@+id/vp" android:layout_width="match_parent" android:layout_height="200dp"/> <LinearLayout android:id="@+id/ivs" android:layout_width="match_parent" android:layout_height="60dp" android:layout_margin="10dp" android:orientation="horizontal"> <ImageView android:id="@+id/iv1" android:layout_weight="1" android:layout_marginStart="30dp" android:layout_width="60dp" android:layout_height="60dp" android:src="@drawable/index_icon"/> <ImageView android:id="@+id/iv2" android:layout_weight="1" android:layout_width="60dp" android:layout_height="60dp" android:src="@drawable/index_icon"/> <ImageView android:id="@+id/iv3" android:layout_weight="1" android:layout_width="60dp" android:layout_height="60dp" android:src="@drawable/index_icon"/> <ImageView android:id="@+id/iv4" android:layout_weight="1" android:layout_marginEnd="30dp" android:layout_width="60dp" android:layout_height="60dp" android:src="@drawable/index_icon"/> </LinearLayout> <com.oridway.www.uiframe.utils.ListViewForScrollView android:id="@+id/lvfsv" android:layout_width="match_parent" android:layout_height="wrap_content"/> </LinearLayout> </ScrollView> <LinearLayout android:id="@+id/toolbar" android:layout_width="match_parent" android:layout_height="wrap_content" android:gravity="center_horizontal" android:orientation="horizontal"> <TextView android:id="@+id/index" android:layout_width="60dp" android:layout_height="wrap_content" android:drawableTop="@drawable/ic_home_black_24dp" android:gravity="center" android:text="首頁(yè)" android:textColor="@color/black" /> <TextView android:id="@+id/message" android:layout_width="60dp" android:layout_height="wrap_content" android:drawableTop="@drawable/ic_message_black_24dp" android:gravity="center" android:text="消息" android:textColor="@color/black" /> <TextView android:id="@+id/community" android:layout_width="60dp" android:layout_height="wrap_content" android:drawableTop="@drawable/ic_people_black_24dp" android:gravity="center" android:text="社區(qū)" android:textColor="@color/black" /> <TextView android:id="@+id/self" android:layout_width="60dp" android:layout_height="wrap_content" android:drawableTop="@drawable/ic_person_black_24dp" android:gravity="center" android:text="我" android:textColor="@color/black" /> </LinearLayout> </LinearLayout>
主窗口代碼:
public class IndexActivity extends AppCompatActivity implements View.OnClickListener{ private Context mContext; private List<Integer> mImageList; private List<Candidate> mCandidateList; private ViewPagerAdapter mPagerAdapter; private CandidateListAdapter mListAdapter; @SuppressLint("HandlerLeak") private Handler handler = new Handler() { @Override public void handleMessage(Message msg) { //每次將當(dāng)前的位置加1,也就是向右滑動(dòng)一次 vp.setCurrentItem(vp.getCurrentItem() + 1); //遞歸無(wú)限循環(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ù)源,固定寫(xiě)法 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)動(dòng)員"); candidate.setTrait("成就:哥本哈根學(xué)派的創(chuàng)始人,1922年獲得諾貝爾物理學(xué)獎(jiǎng)"); mCandidateList.add(candidate); } mListAdapter.notifyDataSetChanged(); } private void initView() { tvTitleMiddle.setText("輪播列表"); //手動(dòng)設(shè)置ScrollView的位置 scrollView.smoothScrollTo(0, 0); } //初始化監(jiān)聽(tīng) 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 /** * 重寫(xiě)該方法,達(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<Integer> mList; private Callback mCallback; public ViewPagerAdapter(List<Integer> 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è)為無(wú)窮大 public int getCount() { return Integer.MAX_VALUE; } @Override //固定寫(xiě)法,不覆蓋會(huì)報(bào)錯(cuò) public void destroyItem(@NonNull ViewGroup container, int position, @NonNull Object object) { container.removeView((View) object); } @Override //固定寫(xiě)法 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()); } }
大功告成,實(shí)現(xiàn)效果如下:
感謝各位的閱讀!關(guān)于“Android如何實(shí)現(xiàn)圖片輪播列表”這篇文章就分享到這里了,希望以上內(nèi)容可以對(duì)大家有一定的幫助,讓大家可以學(xué)到更多知識(shí),如果覺(jué)得文章不錯(cuò),可以把它分享出去讓更多的人看到吧!
當(dāng)前題目:Android如何實(shí)現(xiàn)圖片輪播列表
文章路徑:http://www.chinadenli.net/article38/jdhhsp.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供python、網(wǎng)站改版、虛擬主機(jī)、搜索引擎優(yōu)化、ChatGPT、建站公司
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來(lái)源: 創(chuàng)新互聯(lián)