赞
踩
高仿简书个人中心页面
Demo下载地址:
先贴上效果图
1. 步骤
1.1 把APP的主题改为NoAction
1.2 引入Material Design 包
implementation 'com.google.android.material:material:1.0.0'
1.3 新建activity_home_page.xml布局文件
/*
*/
下边讲解一些属性:
scrollFlags的效果
scrollFlags一共有五个值,一些值是可以组合作用在View上的。五个值分别是:
scroll:子View随ScrollView一起滚动
enterAlways:只要ScrollView向下移动,子View立即响应滚动
enterAlwaysCollapsed:当ScrollView滚动最顶层时,子View响应滚动事件,直至子View完全显示
exitUtilCollapsed:只要ScrollView向上滚动,子View立即响应滚动,直到达到最小高度
snap:当Scrollview滚动到最顶层时,子View响应滚动事件。松开手指时,依据AppBarLayout移出屏幕区域与生育可视区域对比,自动移向占比大的区域。
titleMarginStart 标题距离(开始)左边的距离
CollapsingToolbarLayout控件 以下的几个属性:
app:contentScrim=”?attr/colorPrimary” 当上滑到toolbar 高度期间直到达到toolbar高度时,给toolbar设置的背景色,以及过渡颜色。当然,这里不仅仅可以设置颜色,也可以设置图片。 如果不设置该属性,标题栏会过渡为以之前的图片为背景。
app:layout_scrollFlags=”scroll” 和介绍 AppBarLayout 时,给Toolbar 设置和配置一样。
app:expandedTitleGravity=”center_horizontal” 从单词意思可以看出,这是展示后,title的位置。
app:expandedTitleMarginStart=”50dp” 从单词意思可以看出,这是展示后,title 距离开始位置的边距。
app:expandedTitleTextAppearance=”@style/transparentText” 展开后标题文字的样式
app:collapsedTitleTextAppearance=”@style/ToolbarTitle” 折叠后标题文字的样式
1.4 新建HomePageActivity.java文件
import android.os.Bundle;
import android.view.View;
import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.Toolbar;
import androidx.fragment.app.Fragment;
import androidx.viewpager.widget.ViewPager;
import com.google.android.material.tabs.TabLayout;
import java.util.ArrayList;
import java.util.List;
/**
* 用户主页
* 展示用户个人信息
*/
public class HomePageActivity extends AppCompatActivity {
private TabLayout tab_layout;
private ViewPager viewPager;
private MyTabAdapter myFragmentPagerAdapter;
private ListmList;
private Liststrings;
private Toolbar toolbar;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_home_page);
initDate();
initView();
}
private void initDate(){
mList = new ArrayList<>();
strings = new ArrayList<>();
mList.add(new FragmentA());
strings.add("动态");
mList.add(new FragmentB());
strings.add("视频");
mList.add(new FragmentC());
strings.add("更多");
}
private void initView(){
toolbar = findViewById(R.id.toolBar);
toolbar.setTitle("Mr.H");
toolbar.setNavigationIcon(R.drawable.nav_ic_back);
//点击左边返回按钮监听事件
toolbar.setNavigationOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
onBackPressed();
}
});
tab_layout = findViewById(R.id.tl_tabLayout);
viewPager = findViewById(R.id.vp_viewPager);
myFragmentPagerAdapter = new MyTabAdapter(mList,strings,getSupportFragmentManager());
viewPager.setAdapter(myFragmentPagerAdapter);
//联动ViewPager
tab_layout.setupWithViewPager(viewPager);
}
}
1.5 一些java类Adapter、Fragment
FragmentA.java
package org.gpnu.mineui;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.DividerItemDecoration;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
/**
* 用户动态
*/
public class FragmentA extends Fragment {
private RecyclerView rv_track;
@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_a,container,false);
rv_track = view.findViewById(R.id.mRecyclerView);
MyAdapter adapter = new MyAdapter();
LinearLayoutManager layoutManager = new LinearLayoutManager(this.getContext());
rv_track.setLayoutManager(layoutManager);
rv_track.addItemDecoration(new DividerItemDecoration(this.getContext(), layoutManager.getOrientation()));//用类设置分割线
rv_track.setAdapter(adapter);
return view;
}
}
MyTabAdapter.java
package org.gpnu.mineui;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentPagerAdapter;
import java.util.List;
public class MyTabAdapter extends FragmentPagerAdapter {
private ListmList;
private Liststrings;
public MyTabAdapter(ListmList, Liststrings, @NonNull FragmentManager fm) {
super(fm);
this.mList = mList;
this.strings = strings;
}
@NonNull
@Override
public Fragment getItem(int position) {
return mList.get(position);
}
@Override
public int getCount() {
return strings.size();
}
@Nullable
@Override
public CharSequence getPageTitle(int position) {
return strings.get(position);
}
}
MyAdapter.java
package org.gpnu.mineui;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;
public class MyAdapter extends RecyclerView.Adapter{
@NonNull
@Override
public MyViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_view,parent,false);
return new MyViewHolder(view);
}
@Override
public void onBindViewHolder(@NonNull MyViewHolder holder, int position) {
}
@Override
public int getItemCount() {
return 40;
}
class MyViewHolder extends RecyclerView.ViewHolder{
TextView textView;
public MyViewHolder(@NonNull View itemView) {
super(itemView);
textView = itemView.findViewById(R.id.tv_textview);
}
}
}
1.6 其他用的到一些布局文件
fragment_a.xml
item_view.xml
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。