1,主布局文件 activity_design.xml
<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout 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">
<android.support.design.widget.AppBarLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<android.support.v7.widget.Toolbar
android:id="@+id/toolBar"
android:layout_width="match_parent"
android:layout_height="?actionBarSize"
app:layout_scrollFlags="scroll|enterAlways"
app:title="toolBar" />
</android.support.design.widget.AppBarLayout>
<FrameLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="@string/appbar_scrolling_view_behavior">
<android.support.v7.widget.RecyclerView
android:id="@+id/recyclerView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@android:color/white"
android:scrollbars="vertical"
tools:listitem="@layout/item_feed" />
<RelativeLayout
android:id="@+id/suspension_bar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@android:color/white">
<ImageView
android:id="@+id/iv_avatar"
android:layout_width="55dp"
android:layout_height="55dp"
android:padding="8dp"
android:src="@drawable/system_logo" />
<TextView
android:id="@+id/tv_nickname"
android:layout_width="wrap_content"
android:layout_height="55dp"
android:layout_marginStart="8dp"
android:layout_toEndOf="@id/iv_avatar"
android:gravity="center_vertical"
android:text="NetEase"
android:textSize="14sp" />
<View
android:id="@+id/top_divider"
android:layout_width="match_parent"
android:layout_height="0.2dp"
android:layout_below="@id/tv_nickname"
android:background="#33000000" />
</RelativeLayout>
</FrameLayout>
</android.support.design.widget.CoordinatorLayout>
2,recyclerview item 布局 item_feed.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<!--<com.cyq.materialdesign3.CircleImageView
android:id="@+id/iv_avatar"
android:layout_width="55dp"
android:layout_height="55dp"
android:padding="8dp"
android:src="@drawable/avatar1" />-->
<ImageView
android:id="@+id/iv_avatar"
android:layout_width="55dp"
android:layout_height="55dp"
android:padding="8dp"
android:src="@drawable/system_logo" />
<TextView
android:id="@+id/tv_nickname"
android:layout_width="wrap_content"
android:layout_height="55dp"
android:layout_marginLeft="8dp"
android:layout_toRightOf="@id/iv_avatar"
android:gravity="center_vertical"
android:text="NetEase"
android:textSize="14sp" />
<View
android:id="@+id/top_divider"
android:layout_width="match_parent"
android:layout_height="0.2dp"
android:layout_below="@id/tv_nickname"
android:background="#33000000" />
<ImageView
android:id="@+id/iv_content"
android:layout_width="match_parent"
android:layout_height="350dp"
android:layout_below="@id/iv_avatar"
android:scaleType="centerCrop"
android:src="@drawable/taeyeon_one" />
</RelativeLayout>
3,recyclerview adapter FeedAdapter
public class FeedAdapter extends RecyclerView.Adapter<FeedAdapter.FeedHolder> {
@NonNull
@Override
public FeedHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
View view = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.item_feed, viewGroup, false);
return new FeedHolder(view);
}
@Override
public void onBindViewHolder(@NonNull FeedHolder viewHolder, int i) {
//用户头像
Picasso.with(viewHolder.itemView.getContext())
.load(getAvatarResId(i))
.centerInside()
.fit()
.into(viewHolder.mIvAvatar);
//内容图片
Picasso.with(viewHolder.itemView.getContext())
.load(getContentResId(i))
.centerInside()
.fit()
.into(viewHolder.mIvContent);
//文本
viewHolder.mTvNickname.setText("NetEase"+i);
}
private int getAvatarResId(int position) {
switch (position % 4) {
case 0:
return R.drawable.system_logo;
case 1:
return R.drawable.system_logo;
case 2:
return R.drawable.system_logo;
case 3:
return R.drawable.system_logo;
}
return 0;
}
private int getContentResId(int posstion) {
switch (posstion % 4) {
case 0:
return R.drawable.taeyeon_one;
case 1:
return R.drawable.taeyeon_two;
case 2:
return R.drawable.taeyeon_three;
case 3:
return R.drawable.taeyeon_four;
}
return 0;
}
@Override
public int getItemCount() {
return 100;
}
public static class FeedHolder extends RecyclerView.ViewHolder {
ImageView mIvAvatar;
ImageView mIvContent;
TextView mTvNickname;
public FeedHolder(@NonNull View itemView) {
super(itemView);
mIvAvatar = itemView.findViewById(R.id.iv_avatar);
mIvContent = itemView.findViewById(R.id.iv_content);
mTvNickname = itemView.findViewById(R.id.tv_nickname);
}
}
}
4,主activity DesignActivity
public class DesignActivity extends AppCompatActivity {
private RecyclerView mRecyclerView;
private FeedAdapter mAdapter;
private RelativeLayout mSuspensionBar;
private TextView mSuspensionTv;
private ImageView mSuspensionIv;
private int mSuspensionHeight;
private int mCurrentPosition;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_design);
mSuspensionBar = findViewById(R.id.suspension_bar);
mSuspensionTv = findViewById(R.id.tv_nickname);
mSuspensionIv = findViewById(R.id.iv_avatar);
mRecyclerView = findViewById(R.id.recyclerView);
final LinearLayoutManager manager = new LinearLayoutManager(this);
mRecyclerView.setLayoutManager(manager);
mAdapter = new FeedAdapter();
mRecyclerView.setAdapter(mAdapter);
mRecyclerView.setHasFixedSize(true);
mRecyclerView.addOnScrollListener(new RecyclerView.OnScrollListener() {
@Override
public void onScrollStateChanged(@NonNull RecyclerView recyclerView, int newState) {
super.onScrollStateChanged(recyclerView, newState);
//获取悬浮条的高度
mSuspensionHeight = mSuspensionBar.getHeight();
}
@Override
public void onScrolled(@NonNull RecyclerView recyclerView, int dx, int dy) {
super.onScrolled(recyclerView, dx, dy);
//监听滚动对悬浮条的位置进行调整
//找到下一个item
View view = manager.findViewByPosition(mCurrentPosition + 1);
if (view != null) {//如果view不等于空就去取view距离recyclerview顶端的距离
if (view.getTop() <= mSuspensionHeight) {
//需要对悬浮条进行移动
mSuspensionBar.setY(-(mSuspensionHeight - view.getTop()));//向上移动 所以是负值
} else {
//保存在原来的位置
mSuspensionBar.setY(0);
}
}
//更改悬浮条上的数据
if (mCurrentPosition != manager.findFirstVisibleItemPosition()) {
mCurrentPosition = manager.findFirstVisibleItemPosition();
updateSuspensionBar();
}
}
});
updateSuspensionBar();
}
/**
* 更新试图方法
*/
private void updateSuspensionBar() {
//用户头像
Picasso.with(this)
.load(getAvatarResId(mCurrentPosition))
.centerInside()
.fit()
.into(mSuspensionIv);
//文本
mSuspensionTv.setText("NetEase" + mCurrentPosition);
}
private int getAvatarResId(int position) {
switch (position % 4) {
case 0:
return R.drawable.system_logo;
case 1:
return R.drawable.system_logo;
case 2:
return R.drawable.system_logo;
case 3:
return R.drawable.system_logo;
}
return 0;
}
private int getContentResId(int posstion) {
switch (posstion % 4) {
case 0:
return R.drawable.taeyeon_one;
case 1:
return R.drawable.taeyeon_two;
case 2:
return R.drawable.taeyeon_three;
case 3:
return R.drawable.taeyeon_four;
}
return 0;
}
}
网友评论