仿b站demo(代码篇)

引导页,代码很简单,用个viewpager就搞定了,这里给出一个延时跳转的代码。我是通过pos判断当前是第几张图片,然后进行延时跳转。
仿b站demo(代码篇)
文章图片



MainTabActivity(主界面): package com.apk.bilibili.activity ;
import android.os.Bundle ;
import android.support.v4.app.Fragment ;
import android.support.v4.app.FragmentActivity ;
import android.support.v4.app.FragmentManager ;
import android.support.v4.app.FragmentTransaction ;
import android.view.View ;
import android.view.Window ;
import android.widget.ImageButton ;
import android.widget.LinearLayout ;
import com.apk.bilibili.R ;
import com.apk.bilibili.fragment.HomeFragment ;
import com.apk.bilibili.fragment.MineFragment ;
import com.apk.bilibili.fragment.NoticeFragment ;
import butterknife.ButterKnife ;
import butterknife.InjectView ;

public class MainTabActivity extends FragmentActivity implements View.OnClickListener{
@InjectView(R.id. tab_home) LinearLayout mTabHome ;
@InjectView(R.id. tab_mine) LinearLayout mTabMine ;
@InjectView(R.id. tab_notice) LinearLayout mTabNotice ;
@InjectView(R.id. tab_home_img) ImageButton mImgHome ;
@InjectView(R.id. tab_notice_img) ImageButton mImgNotice ;
@InjectView(R.id. tab_mine_img) ImageButton mImgMine ;
private Fragment mHome ;
private Fragment mNotice ;
private Fragment mMine ;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState) ;
requestWindowFeature(Window. FEATURE_NO_TITLE) ;
setContentView(R.layout. activity_main_tab) ;
ButterKnife. inject( this) ;
setSelect( 0) ;
initEvent() ;
}
private void initEvent() {
mTabHome.setOnClickListener( this) ;
mTabNotice.setOnClickListener( this) ;
mTabMine.setOnClickListener( this) ;
}
private void setSelect( int i)
{
FragmentManager fm = getSupportFragmentManager() ;
FragmentTransaction transaction = fm.beginTransaction() ;
hideFragment(transaction) ;
switch (i)
{
case 0:
if ( mHome == null)
{
mHome = new HomeFragment() ;
transaction.add(R.id. id_content , mHome) ;
} else
{
transaction.show( mHome) ;
}
mImgHome.setImageResource(R.drawable. home_pressed) ;
break;
case 1:
if ( mNotice == null)
{
mNotice = new NoticeFragment() ;
transaction.add(R.id. id_content , mNotice) ;
} else
{
transaction.show( mNotice) ;
}
mImgNotice.setImageResource(R.drawable. notice_pressed) ;
break;
case 2:
if ( mMine == null)
{
mMine = new MineFragment() ;
transaction.add(R.id. id_content , mMine) ;
} else
{
transaction.show( mMine) ;
}
mImgMine.setImageResource(R.drawable. mine_pressed) ;
break;
default:
break;
}
transaction.commit() ;
}
private void hideFragment(FragmentTransaction transaction)
{
if ( mHome != null)
{
transaction.hide( mHome) ;
}

if ( mNotice != null)
{
transaction.hide( mNotice) ;
}
if ( mMine != null)
{
transaction.hide( mMine) ;
}
}
@Override
public void onClick(View v)
{
resetImgs() ;
switch (v.getId())
{
case R.id. tab_home:
setSelect( 0) ;
break;
case R.id. tab_notice:
setSelect( 1) ;
break;
case R.id. tab_mine:
setSelect( 2) ;
break;
default:
break;
}
}

/**
* 切换图片至暗色
*/
private void resetImgs() {
mImgHome.setImageResource(R.drawable. home_normal) ;
mImgNotice.setImageResource(R.drawable. notice_normal) ;
mImgMine.setImageResource(R.drawable. mine_normal) ;
}
} 布局文件:
仿b站demo(代码篇)
文章图片

tab_button:
android :layout_width= "match_parent"
android :layout_height= "65dp"
android :orientation= "horizontal"
android :background= "#FFFFFF" >
android :id= "@+id/tab_home"
android :layout_width= "0dp"
android :layout_height= "fill_parent"
android :layout_weight= "1"
android :gravity= "center"
android :orientation= "vertical" >
android :background= "#FFFFFF"
android :id= "@+id/tab_home_img"
android :layout_width= "fill_parent"
android :layout_height= "fill_parent"
android :clickable= "false"
android :src= "https://www.it610.com/article/@drawable/home_pressed" />

android :id= "@+id/tab_notice"
android :layout_width= "0dp"
android :layout_height= "fill_parent"
android :layout_weight= "1"
android :gravity= "center"
android :orientation= "vertical" >
android :background= "#FFFFFF"
android :id= "@+id/tab_notice_img"
android :layout_width= "fill_parent"
android :layout_height= "fill_parent"
android :clickable= "false"
android :src= "https://www.it610.com/article/@drawable/notice_normal" />

android :id= "@+id/tab_mine"
android :layout_width= "0dp"
android :layout_height= "fill_parent"
android :layout_weight= "1"
android :gravity= "center"
android :orientation= "vertical" >
android :background= "#FFFFFF"
android :id= "@+id/tab_mine_img"
android :layout_width= "fill_parent"
android :layout_height= "fill_parent"
android :clickable= "false"
android :src= "https://www.it610.com/article/@drawable/mine_normal" />

Fragment: 1、HomeFragment: package com.apk.bilibili.fragment ;

import android.os.Bundle ;
import android.support.v4.app.Fragment ;
import android.support.v4.view.ViewPager ;
import android.view.LayoutInflater ;
import android.view.View ;
import android.view.ViewGroup ;

import com.apk.bilibili.R ;
import com.apk.bilibili.utils.TabAdapter ;
import com.viewpagerindicator.TabPageIndicator ;

import java.util.ArrayList ;
import java.util.List ;


public class HomeFragment extends Fragment {
private ViewPager mViewPager ;
private TabPageIndicator mTabPageIndicator ;
private TabAdapter mAdapter ;
private List mFragments = new ArrayList() ;

@Override
public View onCreateView(LayoutInflater inflater , ViewGroup container ,
Bundle savedInstanceState)
{
View view = inflater.inflate(R.layout. home , container , false) ;
mViewPager=(ViewPager) view.findViewById(R.id. id_viewpager) ;
mTabPageIndicator = (TabPageIndicator) view.findViewById(R.id. id_indicator) ;
mAdapter = new TabAdapter(getFragmentManager()) ;
mViewPager.setAdapter( mAdapter) ;
mTabPageIndicator.setViewPager( mViewPager , 0) ;
Recommend_Fragment first_tab = new Recommend_Fragment() ;
Animation_Fragment second_tab = new Animation_Fragment() ;
mFragments.add(first_tab) ;
mFragments.add(second_tab) ;
return view ;
}

}
NoticeFragment: package com.apk.bilibili.fragment ;

import android.content.Intent ;
import android.os.Bundle ;
import android.support.v4.app.Fragment ;
import android.view.LayoutInflater ;
import android.view.View ;
import android.view.ViewGroup ;
import android.widget.Button ;


import com.apk.bilibili.R ;
import com.apk.bilibili.activity.SignIn ;


public class NoticeFragment extends Fragment {
private Button mNotice_sign_in ;
@Override
public View onCreateView(LayoutInflater inflater , ViewGroup container ,
Bundle savedInstanceState)
{
View view = inflater.inflate(R.layout. notice , container , false) ;
mNotice_sign_in=(Button) view.findViewById(R.id. notice_sign_in) ;
mNotice_sign_in.setOnClickListener( new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent intent = new Intent(getActivity() ,SignIn. class) ;
startActivity(intent) ;

}
}) ;
return view ;
}
}
MineFragment: package com.apk.bilibili.fragment ;

import android.content.Intent ;
import android.os.Bundle ;
import android.support.v4.app.Fragment ;
import android.view.LayoutInflater ;
import android.view.View ;
import android.view.ViewGroup ;
import android.widget.ImageButton ;

import com.apk.bilibili.R ;
import com.apk.bilibili.activity.MineFourthImageButton ;
import com.apk.bilibili.activity.MineImageButton ;
import com.apk.bilibili.activity.MineSecondImageButton ;
import com.apk.bilibili.activity.MineThridImageButton ;



public class MineFragment extends Fragment {
private ImageButton mfourth_imageButton ;
private ImageButton mthrid_imageButton ;
private ImageButton mimageButton ;
private ImageButton msecond_imageButton ;

@Override
public View onCreateView(LayoutInflater inflater , ViewGroup container ,
Bundle savedInstanceState)
{

View view = inflater.inflate(R.layout. mine , container , false) ;
mfourth_imageButton=(ImageButton) view.findViewById(R.id. fourth_imageButton) ;
mfourth_imageButton.setOnClickListener( new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent intent = new Intent(getActivity() ,MineFourthImageButton. class) ;
startActivity(intent) ;
}
}) ;
mthrid_imageButton=(ImageButton)view.findViewById(R.id. thrid_imageButton) ;
mthrid_imageButton.setOnClickListener( new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent intent = new Intent(getActivity() ,MineThridImageButton. class) ;

startActivity(intent) ;


}
}) ;
mimageButton=(ImageButton)view.findViewById(R.id. imageButton) ;
mimageButton.setOnClickListener( new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent intent = new Intent(getActivity() ,MineImageButton. class) ;

startActivity(intent) ;
}
}) ;
msecond_imageButton=(ImageButton)view.findViewById(R.id. second_imageButton) ;
msecond_imageButton.setOnClickListener( new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent intent = new Intent(getActivity() ,MineSecondImageButton. class) ;

startActivity(intent) ;
}
}) ;
return view ;
}

} Recommend_Fragment: package com.apk.bilibili.fragment ;

import android.app.Activity ;
import android.content.Context ;
import android.content.Intent ;
import android.os.Bundle ;
import android.support.v4.app.Fragment ;

import android.view.LayoutInflater ;
import android.view.View ;
import android.view.ViewGroup ;
import android.widget.AdapterView ;
import android.widget.GridView ;
import android.widget.SimpleAdapter ;


import com.apk.bilibili.R ;
import com.apk.bilibili.activity.Show ;
import com.apk.bilibili.utils.History ;

import java.util.ArrayList ;
import java.util.HashMap ;
import java.util.Map ;

import cn.bmob.v3.Bmob ;
import cn.bmob.v3.listener.SaveListener ;


/**
* Created by Administrator on 2016/6/5.
*/
public class Recommend_Fragment extends Fragment {
protected Context mContext ;
protected static ArrayList> mListItems ;
static {
mListItems = new ArrayList>() ;

Map, Object> map = new HashMap, Object>() ;
map.put( "title" , "[排行向]二次元眼罩萌娘排行") ;
map.put( "img" ,R.drawable. recommend_item_first) ;
Map, Object> second_map = new HashMap, Object>() ;
second_map.put( "title" , "漫画《鬼刀》宣传视频 英文版") ;
second_map.put( "img" ,R.drawable. recommend_item_second) ;
Map, Object> thrid_map = new HashMap, Object>() ;
thrid_map.put( "title" , "【耳机福利】【中国风原创】") ;
thrid_map.put( "img" ,R.drawable. recommend_item_thrid) ;
Map, Object> fourth_map = new HashMap, Object>() ;
fourth_map.put( "title" , "【AMV】SUPERNATURAL 超异常") ;
fourth_map.put( "img" ,R.drawable. recommend_item_fourth) ;
Map, Object> fifth_map = new HashMap, Object>() ;
fifth_map.put( "title" , "【多素材/催泪向】Dear,在最美的年华遇见你") ;
fifth_map.put( "img" ,R.drawable. animation_item_first) ;
Map, Object> sixth_map = new HashMap, Object>() ;
sixth_map.put( "title" , "【多素材/黑暗向】nightmare/夜不能寐吗") ;
sixth_map.put( "img" ,R.drawable. animation_item_second) ;
Map, Object> seventh_map = new HashMap, Object>() ;
seventh_map.put( "title" , "【多素材/治愈向】樱花飞舞的时候,献给即将远行") ;
seventh_map.put( "img" ,R.drawable. animation_item_thrid) ;
Map, Object> eighth_map = new HashMap, Object>() ;
eighth_map.put( "title" , "【综漫/高燃向】以我热血燃剑气,凭我豪情破苍穹") ;
eighth_map.put( "img" ,R.drawable. animation_item_fourth) ;
Map, Object> ninth_map = new HashMap, Object>() ;
ninth_map.put( "title" , "【Ryu邪道长】红心皇后(Queen Of Heart)") ;
ninth_map.put( "img" ,R.drawable. music_item_first) ;
Map, Object> tenth_map = new HashMap, Object>() ;
tenth_map.put( "title" , "【甲苯】九九八十一feat.凯玟桑") ;
tenth_map.put( "img" ,R.drawable. music_item_second) ;
Map, Object> eleventh_map = new HashMap, Object>() ;
eleventh_map.put( "title" , "【凯玟桑】Girlgirl eye") ;
eleventh_map.put( "img" ,R.drawable. music_item_thrid) ;
Map, Object> twelfth_map = new HashMap, Object>() ;
twelfth_map.put( "title" , "【Ryu邪道长】秒速5厘米") ;
twelfth_map.put( "img" ,R.drawable. music_item_fourth) ;
Map, Object> thirteenth_map = new HashMap, Object>() ;
thirteenth_map.put( "title" , "【直播录像】Dead by Daylight黎明杀鸡翻车") ;
thirteenth_map.put( "img" ,R.drawable. game_item_first) ;
Map, Object> fourteenth_map = new HashMap, Object>() ;
fourteenth_map.put( "title" , "【徐老师讲故事】04:亚索与塔利亚的破事儿") ;
fourteenth_map.put( "img" ,R.drawable. game_item_second) ;
Map, Object> fifteenth_map = new HashMap, Object>() ;
fifteenth_map.put( "title" , "pi.mc,神圣世界的体验part2") ;
fifteenth_map.put( "img" ,R.drawable. game_item_thrid) ;
Map, Object> sixteenth_map = new HashMap, Object>() ;
sixteenth_map.put( "title" , "徐老师来巡山68:上单老船长一Q崩死下路") ;
sixteenth_map.put( "img" ,R.drawable. game_item_fourth) ;
Map, Object> seventeenth_map = new HashMap, Object>() ;
seventeenth_map.put( "title" , "【暴走看啥片儿第三季】42翻译官杨幂万人迷,魔") ;
seventeenth_map.put( "img" ,R.drawable. pleasure_item_first) ;
Map, Object> eighteenth_map = new HashMap, Object>() ;
eighteenth_map.put( "title" , "【暴走大事件第四季】48 朋友圈谣言的正确解读,") ;
eighteenth_map.put( "img" ,R.drawable. pleasure_item_second) ;
Map, Object> nineteenth_map = new HashMap, Object>() ;
nineteenth_map.put( "title" , "每日一暴合集99") ;
nineteenth_map.put( "img" ,R.drawable. pleasure_item_thrid) ;
Map, Object> twentieth_map = new HashMap, Object>() ;
twentieth_map.put( "title" , "恐怖故事系列特辑") ;
twentieth_map.put( "img" ,R.drawable. pleasure_item_fourth) ;


mListItems.add(map) ; mListItems.add(second_map) ;
mListItems.add(thrid_map) ; mListItems.add(fourth_map) ;
mListItems.add(fifth_map) ; mListItems.add(sixth_map) ;
mListItems.add(seventh_map) ; mListItems.add(eighth_map) ;
mListItems.add(ninth_map) ; mListItems.add(tenth_map) ;
mListItems.add(eleventh_map) ; mListItems.add(twelfth_map) ;
mListItems.add(thirteenth_map) ; mListItems.add(fourteenth_map) ;
mListItems.add(fifteenth_map) ; mListItems.add(sixteenth_map) ;
mListItems.add(seventeenth_map) ; mListItems.add(eighteenth_map) ;
mListItems.add(nineteenth_map) ; mListItems.add(twentieth_map) ;


}
public static Recommend_Fragment newInstance(){
Recommend_Fragment fragment= new Recommend_Fragment() ;
return fragment ;
}
public void onAttach(Activity activity) {
super.onAttach(activity) ;
mContext = activity.getApplicationContext() ;
}



public View onCreateView(LayoutInflater inflater , ViewGroup container , Bundle savedInstanceState)
{
View view = inflater.inflate(R.layout. recommend , null) ;

GridView mGridView=(GridView) view.findViewById(R.id. gridView) ;
SimpleAdapter adapter = new SimpleAdapter( mContext , mListItems ,R.layout. recommend_item ,
new String[] { "title" , "img"} ,
new int[] {R.id. title , R.id. image}) ;
mGridView.setAdapter(adapter) ;
Bmob. initialize( mContext , "4abc415ca666a3fa6301ad90a0d2fa8d") ;
mGridView.setOnItemClickListener( new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView adapterView , View view , int i , long l) {

if (i== 0){
String a= "http://www.bilibili.com/video/av4960178/" ;
Intent intent = new Intent(getActivity() ,Show. class) ;
intent.putExtra( "url" , a) ;
submit( "[排行向]二次元眼罩萌娘排行") ;
startActivity(intent) ;
}
if (i== 1){
String b= "http://www.bilibili.com/video/av4975125/" ;
Intent intent = new Intent(getActivity() ,Show. class) ;
intent.putExtra( "url" , b) ;
startActivity(intent) ;
submit( "漫画《鬼刀》宣传视频 英文版") ;
}
}
}) ;

return view ;
}
public void submit( final String record){


History history = new History() ;
history.setHistory_record(record) ;
history.save( mContext , new SaveListener() {
@Override
public void onSuccess() {

}

@Override
public void onFailure( int i , String s) {

}
}) ;
}


}
Animation_Fragment: package com.apk.bilibili.fragment ;

import android.app.Activity ;
import android.content.Context ;
import android.os.Bundle ;
import android.support.v4.app.Fragment ;
import android.view.LayoutInflater ;
import android.view.View ;
import android.view.ViewGroup ;
import android.widget.ListView ;
import android.widget.SimpleAdapter ;

import com.apk.bilibili.R ;

import java.util.ArrayList ;
import java.util.HashMap ;
import java.util.Map ;


public class Animation_Fragment extends Fragment {
protected Context mContext ;
protected static ArrayList> mListItems ;
static {
mListItems = new ArrayList>() ;

Map, Object> map = new HashMap, Object>() ;
map.put( "title" , "Re:从零开始的异世界生活") ;
map.put( "content" , "诅咒上身,倒数重开。绿林孤影犄角下,是失去理智的温柔...") ;
map.put( "img" ,R.drawable. item_first) ;
Map, Object> second_map = new HashMap, Object>() ;
second_map.put( "title" , "秒速五厘米") ;
second_map.put( "content" , "[你知道樱花飘落的速度吗?]") ;
second_map.put( "img" ,R.drawable. item_second) ;
Map, Object> thrid_map = new HashMap, Object>() ;
thrid_map.put( "title" , "请问您今天要来点兔子吗??") ;
thrid_map.put( "content" , "没有什么事是看一话点兔解决不了的...如果有,那就看两话!") ;
thrid_map.put( "img" ,R.drawable. item_thrid) ;
Map, Object> fourth_map = new HashMap, Object>() ;
fourth_map.put( "title" , "一周的朋友") ;
fourth_map.put( "content" , "一周的友谊→重启→陌生...一次又一次。愿有朝一日,你也能记得这份感觉。") ;
fourth_map.put( "img" ,R.drawable. item_fourth) ;
Map, Object> fifth_map = new HashMap, Object>() ;
fifth_map.put( "title" , "线上游戏的老婆不可能是女生? 10") ;
fifth_map.put( "content" , "学院祭来啦!我们要把胜利的旗帜插在那座城池上(╯`□′)╯") ;
fifth_map.put( "img" ,R.drawable. item_fifth) ;
Map, Object> sixth_map = new HashMap, Object>() ;
sixth_map.put( "title" , "花开伊吕波") ;
sixth_map.put( "content" , "为改变、为了崭新的自己,离开诚实,旅馆工作开始!少女们怀揣梦想,在同一屋檐下成长,绽放!") ;
sixth_map.put( "img" ,R.drawable. item_sixth) ;
Map, Object> seventh_map = new HashMap, Object>() ;
seventh_map.put( "title" , "四月是你的谎言") ;
seventh_map.put( "content" , "又到四月,从一个美丽的谎言开始。") ;
seventh_map.put( "img" ,R.drawable. item_seventh) ;
Map, Object> eighth_map = new HashMap, Object>() ;
eighth_map.put( "title" , "罪恶王冠") ;
eighth_map.put( "content" , "[人为何互相伤害 彼此争斗呢]承受宿命之重 冠以王者之名") ;
eighth_map.put( "img" ,R.drawable. item_eighth) ;
Map, Object> ninth_map = new HashMap, Object>() ;
ninth_map.put( "title" , "我们大家的河合庄") ;
ninth_map.put( "content" , "本宿舍自带三餐,自带奇葩舍友_(:з」∠)_还自带暗恋对象") ;
ninth_map.put( "img" ,R.drawable. item_ninth) ;
mListItems.add(map) ;
mListItems.add(second_map) ;
mListItems.add(thrid_map) ;
mListItems.add(fourth_map) ;
mListItems.add(fifth_map) ;
mListItems.add(sixth_map) ;
mListItems.add(seventh_map) ;
mListItems.add(eighth_map) ;
mListItems.add(ninth_map) ;


}
public static Animation_Fragment newInstance(){
Animation_Fragment fragment= new Animation_Fragment() ;
return fragment ;
}
public void onAttach(Activity activity) {
super.onAttach(activity) ;
mContext = activity.getApplicationContext() ;
}
@Override
public View onCreateView(LayoutInflater inflater , ViewGroup container , Bundle savedInstanceState)
{
View view = inflater.inflate(R.layout. animation , null) ;
ListView mListView=(ListView) view.findViewById(R.id. animation_ListView) ;
SimpleAdapter adapter = new SimpleAdapter( mContext , mListItems ,R.layout. animation_item ,
new String[] { "title" , "content" , "img"} ,
new int[] {R.id. title_animation , R.id. content_animation ,R.id. img_animation}) ;
mListView.setAdapter(adapter) ;
return view ;
}
} TabAdapter: package com.apk.bilibili.utils ;

import android.support.v4.app.Fragment ;
import android.support.v4.app.FragmentManager ;
import android.support.v4.app.FragmentPagerAdapter ;

import com.apk.bilibili.fragment.Recommend_Fragment ;
import com.apk.bilibili.fragment.Animation_Fragment ;

public class TabAdapter extends FragmentPagerAdapter
{

public static String[] TITLES = new String[]
{ "推荐" , "动画"} ;

public TabAdapter(FragmentManager fm)
{
super(fm) ;
}

@Override
public Fragment getItem( int position)
{
switch (position) {
case 0:
return Recommend_Fragment. newInstance() ;

case 1:
return Animation_Fragment. newInstance() ;


}

return null;
}

@Override
public int getCount()
{
return TITLES. length ;
}

@Override
public CharSequence getPageTitle( int position)
{
return TITLES[position] ;
}


} 界面的代码就只有这些(还有 bmob初始化用的 Application ID你们需要改一下这里用的是我的) 至于那些图片的资源文件,都是些我自己剪的,还有一些样式。有需要的,或者需要源代码的可以找我要。这里只是展示一下大致的效果,viewpager和fragment怎么使用,如何使用viewpagerindicator。 这里向大家推荐一下bmob,比如账户,系统消息,历史记录都有用到bmob。 下一篇将是这个demo的一些功能实现

【仿b站demo(代码篇)】

    推荐阅读