尚硅谷Android項目之_硅谷商城項目全套源碼解析(五、發現)

一、簡介

承接上文:尚硅谷Android項目之_硅谷商城項目全套源碼解析(四、分類) 

上篇博客概括的介紹了硅谷商城項目的分類模塊技術要點。本篇內容給大家講解硅谷商城項目發現模塊,發現模塊用的技術包括:採用TabLayout實現標題的切換、採用OpenDanmaku實現彈幕功能、數據解析採用Gson。


 二、詳細資源地址

由於篇幅所限,詳情情況見如下地址視頻和筆記

項目免費視頻講解下載地址:http://www.atguigu.com/download.shtml

github地址:https://github.com/atguigu01/Shopping

作者:大海哥


三、效果演示:


四、技術詳情

1、採用TabLayout實現標題的切換

1)導包

compile'com.android.support:design:24.2.1'

2)佈局文件

<?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"
>

    <LinearLayout
       
android:layout_width="match_parent"
       
android:layout_height="55dp"
       
android:gravity="center_vertical"
       
android:orientation="horizontal"
       
android:padding="5dp"
>

        <ImageButton
           
android:id="@+id/ib_community_icon"
           
android:layout_width="40dp"
           
android:scaleType="fitXY"
           
android:layout_height="40dp"
           
android:background="@drawable/top_btn"
/>

        <TextView
           
android:textSize="18sp"
           
android:textColor="@android:color/background_dark"
           
android:gravity="center"
           
android:text="社區"
           
android:layout_weight="1"
           
android:layout_width="wrap_content"
           
android:layout_height="wrap_content"
/>

        <ImageButton
           
android:id="@+id/ib_community_message"
           
android:layout_width="wrap_content"
           
android:layout_height="wrap_content"
           
android:background="@drawable/community_message_icon"
/>
    </LinearLayout>
    <View
       
android:layout_width="match_parent"
       
android:layout_height="2dp"
       
android:background="#22000000"
/>

     <android.support.design.widget.TabLayout
        android:id="@+id/tablayout"
       
android:layout_width="wrap_content"
       
android:layout_height="wrap_content"
       
android:layout_gravity="center_horizontal"
       
android:layout_marginLeft="10dp"
       
android:layout_marginRight="10dp"
/>

    <android.support.v4.view.ViewPager
       
android:id="@+id/view_pager"
       
android:layout_width="match_parent"
       
android:layout_height="match_parent"
/>
</LinearLayout>

3)設置樣式

<android.support.design.widget.TabLayout
  
android:id="@+id/tablayout"
  
style="@style/MyCustomTabLayout"
   android:layout_width="wrap_content"
  
android:layout_height="wrap_content"
  
android:layout_gravity="center_horizontal"
  
android:layout_marginLeft="10dp"
  
android:layout_marginRight="10dp"
/>

// 在styles.xml文件裏面

<stylename="MyCustomTabLayout"parent="Widget.Design.TabLayout">

    <item name="tabMaxWidth">52dp</item>
    <item name="tabMinWidth">52dp</item>
    <item name="tabIndicatorColor">#ff0000</item>
    <item name="tabIndicatorHeight">2dp</item>
    <item name="tabTextAppearance">@style/MyCustomTabTextAppearance</item>
    <item name="tabSelectedTextColor">@android:color/holo_red_light</item>
</style>

<style name="MyCustomTabTextAppearance"parent="TextAppearance.Design.Tab">
    <item name="android:textSize">18sp</item>
    <item name="android:textColor">@android:color/black</item>
    <item name="textAllCaps">false</item>
</style>

4)代碼:

tablayout= (TabLayout) view.findViewById(R.id.tablayout);

@Override
public void initData() {
    super.initData();
    CommunityViewPagerAdapter adapter = new CommunityViewPagerAdapter(getFragmentManager());
    viewPager.setAdapter(adapter);
    tablayout.setupWithViewPager(viewPager);
    //如果有多個ViewPager頁面
   
tablayout.setTabMode(TabLayout.MODE_SCROLLABLE);
}


2、採用OpenDanmaku實現彈幕功能

詳情使用情況見:https://github.com/linsea/OpenDanmaku


3、數據解析採用Gson

private void processData(String json) {
    HotPostBean hotPostBean = JSONObject.parseObject(json,HotPostBean.class);
    result = hotPostBean.getResult();
}

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章