今天上课,看到大家都把页面做出来了。马上就要毕业的我们,我今天慌了,今天连夜要把这篇文章写完,希望对我们班做的项目app有点帮助;今天算是一个小案例吧,用前面说过的RecyclerView和TabLayout,但是也加入了新的元素viewpager和Fragment。可以说这几个都是最基础,最常用的控件。先看一下效果图吧:
先说一下我的这个页面,上面是一个viewpager,中间是一个Fragment,最下面是一个TabLayout,而且我在Fragment里面加入一个RecyclerView。现在具体说一下步骤:
1、首先看一下主布局(activity_main.xml)
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity"
android:orientation="vertical">
<android.support.design.widget.TabLayout
android:id="@+id/title_tablayout"
android:layout_height="0dp"
android:layout_width="wrap_content"
android:layout_weight="1"
android:layout_gravity="center"
app:tabBackground="@android:color/white"
app:tabTextColor="@android:color/black"
app:tabSelectedTextColor="@android:color/holo_red_dark"
app:tabIndicatorColor="@android:color/holo_red_dark"/>
<android.support.v4.view.ViewPager
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="7"
android:id="@+id/content_viewpager">
</android.support.v4.view.ViewPager>
<android.support.design.widget.BottomNavigationView
android:id="@+id/bottom_navigationView"
android:layout_height="0dp"
android:layout_width="match_parent"
android:layout_weight="1"
app:itemTextColor="@color/selectot_bnv"
app:menu="@layout/item"/>
</LinearLayout>
前面说过东西我就不再这里说了(因为再说就有点啰嗦了),现在主要说说TabLayout里面的tabIndicatorColor这是文字下面那个横条条的颜色。
2、我们先看fragment的写法;
- 首先写个布局(fragment.xml)
<?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">
<FrameLayout
android:id="@+id/fragment_layout"
android:layout_width="match_parent"
android:layout_height="match_parent">
</FrameLayout>
</LinearLayout>
FrameLayout里面的内容是可以变化的(叫做动态添加碎片),当然还有静态添加碎片,那就要用到fragment。
- 接着我在里面放了一个recyclerview(新建recycler_fragment.xml)
<?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.support.v7.widget.RecyclerView
android:id="@+id/recycler"
android:layout_width="match_parent"
android:layout_height="match_parent">
</android.support.v7.widget.RecyclerView>
</LinearLayout>
- 接着就要写内容的布局了,我只加一段文字(新建recycler.xml)
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:id="@+id/content"
android:background="@android:color/holo_orange_dark"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:textSize="35sp"
android:textColor="@android:color/holo_blue_dark"/>
</RelativeLayout>
- 然后就是建立配置器(这个是Fragment的配置器:FragmentAdapter)
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import java.util.List;
public class FragmentAdapter extends FragmentPagerAdapter {
private List<Fragment> mFragment;
private String[] title = {" 首页 "," 动态 "," 我的 "};
public FragmentAdapter(FragmentManager fm, List<Fragment> fragment) {
super(fm);
this.mFragment=fragment;
}
@Override
public Fragment getItem(int position) {
return mFragment.get(position);
}
@Override
public int getCount() {
return mFragment.size();
}
public CharSequence getPageTitle(int position){
return title[position];
}
}
这里面也有几个必须要实现的方法:getItem(position)返回第position个fragment;
getcount()为了得到有几个fragment实例;
这里面还有一个charsequence getPageTitle()这个方法根据第几个title得到对应的fragment,前提要实现viewpager和tablayout的连接(后面我展现代码并说明);
- 建立fragment 实例并在里面填充数据(我这个例子需要建三个,分别为Fragment1,Fragment2,Fragment3)
import android.annotation.SuppressLint;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import java.util.ArrayList;
import java.util.List;
@SuppressLint("ValidFragment")
class Fragment2 extends Fragment {
@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View view = LayoutInflater.from(getContext()).inflate(R.layout.recycler_fragment,container,false);
RecyclerView recyclerView = (RecyclerView)view.findViewById(R.id.recycler);
LinearLayoutManager linearLayoutManager=new LinearLayoutManager(getActivity());
recyclerView.setLayoutManager(linearLayoutManager);
RecyclerViewAdapter adapter =new RecyclerViewAdapter(getcontent());
recyclerView.setAdapter(adapter);
return view;
}
public List<content> getcontent() {
List<content> newsList = new ArrayList<>();
for(int i=0;i<=20;i++){
content bottom = new content();
bottom.setContent("哈哈"+i);
newsList.add(bottom);
}
return newsList;
}
}
其他二个代码和这个相似,这个代码前面也说过。
- 我们还需要最后一个小步骤建立一个Java类(content)来设置属性;
public class content {
String content;
public String getContent() {
return content;
}
public void setContent(String content) {
this.content = content;
}
}
- 最后就是在mainActivity里面实现它,后面我一块贴出来
3、现在我们说一下ViewPager,这是一个可以实现滑动的界面,这个小例子的主心骨,它连接着上面控件TabLayout和下面的BottomNativationView控件,实现它主要也是一个配置器(ViewpagerAdapter)
package com.example.jiangyou.xiangmu;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import java.util.ArrayList;
import java.util.List;
public class ViewAdapter extends FragmentPagerAdapter {
private List<Fragment> fragments = new ArrayList<>();
public ViewAdapter(FragmentManager fm) {
super(fm);
}
@Override
public Fragment getItem(int position) {
return fragments.get(position);
}
@Override
public int getCount() {
return fragments.size();
}
public void addFragment(Fragment fragment) {
fragments.add(fragment);
}
}
我觉得配置器好像都是这几个方法(可能我是初学者,见到少的缘故吧,哈哈哈),那就直接看一下addFragment():像list中添加fragment实例(main方法会实现)。
4、现在建立最后一个配置器(RecyclerAdapter)
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import java.util.List;
public class RecyclerViewAdapter extends RecyclerView.Adapter<RecyclerViewAdapter.ViewHolder> {
private List<content> mContent;
static class ViewHolder extends RecyclerView.ViewHolder {
TextView contentName;
public ViewHolder(View view) {
super(view);
contentName=(TextView)view.findViewById(R.id.content);
}
}
public RecyclerViewAdapter(List<content> contents) {
this.mContent = contents;
}
@NonNull
@Override
public RecyclerViewAdapter.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.recycler,parent,false);
final ViewHolder holder = new ViewHolder(view);
return holder;
}
@Override
public void onBindViewHolder(@NonNull RecyclerViewAdapter.ViewHolder holder, int position) {
content c = mContent.get(position);
holder.contentName.setText(c.getContent());
}
@Override
public int getItemCount() {
return mContent.size();
}
}
我觉得这个配置器以后要还要经常看,我先做还是记不太清意思。
5、建立一些前面提过的布局文件,这里我直接把带面贴出来了,前面有说过的。
item.xml
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<item
android:id="@+id/one"
android:title="首页"/>
<item
android:id="@+id/two"
android:title="动态"/>
<item
android:id="@+id/three"
android:title="我的"/>
</menu>
selectot_bnv.xml
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<item android:color="#e20f0f"
android:state_checked="true">
</item>
<item
android:color="#0e0e0e">
</item>
</selector>
6.最后一步了,在MainActicity把各个控件进行连接,还有点击事件逻辑。
import android.support.annotation.NonNull;
import android.support.design.widget.BottomNavigationView;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.app.FragmentTransaction;
import android.support.v4.app.FragmentTransitionImpl;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.MenuItem;
import android.view.View;
import java.util.ArrayList;
import java.util.List;
public class MainActivity extends AppCompatActivity {
private TabLayout tabLayout;
private Fragment1 fragment1;
private Fragment2 fragment2;
private Fragment3 fragment3;
private ViewAdapter viewAdapter;
private ViewPager vp;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
fragment1 = new Fragment1();//1、这里我写的有点麻烦了,
fragment2 = new Fragment2();
fragment3 = new Fragment3();
List<Fragment> mFragment = new ArrayList<Fragment>();
mFragment.add(fragment1);//这段代码可以和1一起合成mFragment.add(new Fragment1)
mFragment.add(fragment2);
mFragment.add(fragment3);
viewAdapter =new ViewAdapter(getSupportFragmentManager());
viewAdapter.addFragment(fragment1);//这段代码可以去看viewAdapter,里面有这个方法
viewAdapter.addFragment(fragment2);
viewAdapter.addFragment(fragment3);
FragmentPagerAdapter fragmentPagerAdapter = new FragmentAdapter(getSupportFragmentManager(), mFragment);
vp =(ViewPager)findViewById(R.id.content_viewpager);
vp.setAdapter(viewAdapter);
vp.setAdapter(fragmentPagerAdapter);
tabLayout=(TabLayout)findViewById(R.id.title_tablayout);
tabLayout.setupWithViewPager(vp);
tabLayout.setTabMode(TabLayout.MODE_SCROLLABLE);
final BottomNavigationView bottom=(BottomNavigationView)findViewById(R.id.bottom_navigationView);
bottom.setOnNavigationItemSelectedListener(new BottomNavigationView.OnNavigationItemSelectedListener() {
@Override
public boolean onNavigationItemSelected(@NonNull MenuItem item) {
int itemID=item.getItemId();
switch (itemID){
case R.id.one:
vp.setCurrentItem(0);
break;
case R.id.two:
vp.setCurrentItem(1);
break;
case R.id.three:
vp.setCurrentItem(2);
break;
}
return true;
}
});
//为viewpager添加一个监听器
vp.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
@Override
public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
}
@Override
//根据bottom的item得到对应的viewpager;
public void onPageSelected(int position) {
bottom.getMenu().getItem(position).setChecked(true);
}
@Override
public void onPageScrollStateChanged(int state) {
}
});
}
}
结语:
这个小例子我今天讲的有点乱,不知道你们有没有看懂。大三快要结束了,不知道以后我们会是什么样,现在有点迷茫了,不知道自己不选择考研是不是对的选择。总之大家既然都选择了道路,就坚持下去。——一个android初学者