最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

Android實現(xiàn)圖片輪播列表

 更新時間:2019年06月25日 11:39:11   作者:free5156  
這篇文章主要為大家詳細(xì)介紹了Android實現(xiàn)圖片輪播列表,具有一定的參考價值,感興趣的小伙伴們可以參考一下

這個效果在交友app中比較常見,一般作為首頁使用,頂部是一個自動輪播的ViewPager,下面放一個橫向LinearLayout,最下面要放一個ListView,但是注意這三個部分都支持滑動,應(yīng)該和固定在頂部的標(biāo)題欄和底部的操作欄分開,也就是把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="首頁"
      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,也就是向右滑動一次
      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.實例化容器 2.實例化適配器 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é)家,足球運動員");
      candidate.setTrait("成就:哥本哈根學(xué)派的創(chuàng)始人,1922年獲得諾貝爾物理學(xué)獎");
      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<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è)為無窮大
  public int getCount() {
    return Integer.MAX_VALUE;
  }

  @Override
  //固定寫法,不覆蓋會報錯
  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);
    //將實例加入父控件
    container.addView(imageView);
    return imageView;
  }

  @Override
  //使用接口將position回傳
  public void onClick(View v) {
    mCallback.onClick(v, (int) v.getTag());
  }
}

大功告成,實現(xiàn)效果如下:

以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

  • Android zxing如何識別反轉(zhuǎn)二維碼詳解

    Android zxing如何識別反轉(zhuǎn)二維碼詳解

    這篇文章主要給大家介紹了關(guān)于Android zxing如何識別反轉(zhuǎn)二維碼的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2018-09-09
  • Android實現(xiàn)倒計時30分鐘功能

    Android實現(xiàn)倒計時30分鐘功能

    這篇文章主要為大家詳細(xì)介紹了Android實現(xiàn)倒計時30分鐘功能,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2017-05-05
  • Android編程實現(xiàn)播放視頻時切換全屏并隱藏狀態(tài)欄的方法

    Android編程實現(xiàn)播放視頻時切換全屏并隱藏狀態(tài)欄的方法

    這篇文章主要介紹了Android編程實現(xiàn)播放視頻時切換全屏并隱藏狀態(tài)欄的方法,結(jié)合實例形式分析了Android視頻播放事件響應(yīng)及相關(guān)屬性設(shè)置操作技巧,需要的朋友可以參考下
    2017-08-08
  • Flutter應(yīng)用集成極光推送的實現(xiàn)示例

    Flutter應(yīng)用集成極光推送的實現(xiàn)示例

    這篇文章主要介紹了Flutter應(yīng)用集成極光推送的實現(xiàn)示例,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2020-02-02
  • Android實現(xiàn)點擊WebView界面中圖片滑動瀏覽與保存圖片功能

    Android實現(xiàn)點擊WebView界面中圖片滑動瀏覽與保存圖片功能

    大家在日常使用spp流量文章的時候經(jīng)常會遇到這樣的一個功能,點擊文章的圖片進(jìn)入圖片的瀏覽模式,可以左右滑動圖片瀏覽,并且可以實現(xiàn)保存圖片的功能,所以本文主要就介紹了在Android如何實現(xiàn)點擊WebView界面中圖片滑動瀏覽與保存圖片功能,需要的朋友可以參考下。
    2017-04-04
  • 快速了解Android?Room使用細(xì)則進(jìn)階

    快速了解Android?Room使用細(xì)則進(jìn)階

    這篇文章主要為大家介紹了快速了解Android?Room使用細(xì)則進(jìn)階,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2023-03-03
  • Android WebView 緩存詳解

    Android WebView 緩存詳解

    這篇文章主要介紹了 Android WebView 緩存詳解的相關(guān)資料,需要的朋友可以參考下
    2017-06-06
  • Android應(yīng)用讀取Excel文件的方法

    Android應(yīng)用讀取Excel文件的方法

    這篇文章主要介紹了Android應(yīng)用讀取Excel文件的方法,涉及Android針對Excel文件的讀寫保存等相關(guān)技巧,具有一定參考借鑒價值,需要的朋友可以參考下
    2015-12-12
  • Android開發(fā)之BottomSheetDialog組件的使用

    Android開發(fā)之BottomSheetDialog組件的使用

    BottomSheetDialog是底部操作控件,可在屏幕底部創(chuàng)建一個支持滑動關(guān)閉視圖。本文將通過示例詳細(xì)講解它的使用,感興趣的小伙伴可以了解一下
    2023-01-01
  • Android中使用Toast.cancel()方法優(yōu)化toast內(nèi)容顯示的解決方法

    Android中使用Toast.cancel()方法優(yōu)化toast內(nèi)容顯示的解決方法

    做程序員的,基本一看api就知道,用這個可以取消上一個toast的顯示,然后顯示下一個,這樣就能解決出現(xiàn)的問題。可是在測試的過程中,發(fā)現(xiàn)卻沒有想象中的那么簡單,不信可以百度一下,很多很多人發(fā)現(xiàn)toast的cancel()方法不起作用
    2013-05-05

最新評論

黑龙江省| 治县。| 望江县| 义马市| 永城市| 洞口县| 南康市| 宜昌市| 凤凰县| 蓬安县| 九江县| 怀宁县| 满洲里市| 云林县| 志丹县| 东方市| 祁门县| 六枝特区| 铁岭县| 出国| 阳西县| 额尔古纳市| 彩票| 华蓥市| 巴林右旗| 龙州县| 新巴尔虎右旗| 昌吉市| 绿春县| 思南县| 乐东| 西峡县| 巴青县| 苗栗市| 沾益县| 吴旗县| 溧阳市| 千阳县| 栖霞市| 淮北市| 龙泉市|