RecyclerView 配合布局的简单使用-->悬浮titleBar--文件
Android System do what
framework discuss
代码
public class MainActivity extends AppCompatActivity {
private RecyclerView mRcyclerView;
private FeedAdapter mFeedAdapter;
//悬浮条布局
private RelativeLayout mSuspensionBar;
private TextView mSuspensionTv;
private int mSuspensionHeight;
private int mCurrentPosition;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mSuspensionBar = findViewById(R.id.suspension_bar);
mSuspensionTv = findViewById(R.id.tv_nickname);
mRcyclerView = findViewById(R.id.recyclerview);
LinearLayoutManager layoutManager = new LinearLayoutManager(this);
mRcyclerView.setLayoutManager(layoutManager);
mFeedAdapter = new FeedAdapter();
mRcyclerView.setAdapter(mFeedAdapter);
mRcyclerView.setHasFixedSize(true);
mRcyclerView.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);
//监听调整悬浮条位置
//找到下一个itemView
View view = layoutManager.findViewByPosition(mCurrentPosition+1);
if(view!=null){
if(view.getTop() <= mSuspensionHeight){
//需要对悬浮条进行移动
mSuspensionBar.setY(-(mSuspensionHeight-view.getTop()));
}else {
//保持在原来的位置
mSuspensionBar.setY(0);
}
if(mCurrentPosition != layoutManager.findFirstVisibleItemPosition()){
mCurrentPosition = layoutManager.findFirstVisibleItemPosition();
//初始化
updateSuspensionBar();
}
}
}
});
updateSuspensionBar();
}
private void updateSuspensionBar() {
mSuspensionTv.setText("tww");
}
private int [] mInts = {R.drawable.a,R.drawable.b,R.drawable.bb6a0410ef,R.drawable.ic_launcher_foreground,R.drawable.b};
public class FeedAdapter extends RecyclerView.Adapter<FeedAdapter.FeedHolder>{
@NonNull
@Override
public FeedHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_feed,parent,false);
return new FeedHolder(view);
}
@Override
public void onBindViewHolder(@NonNull FeedHolder holder, int position) {
holder.mTvNickname.setText( "tww"+position);
holder.mIvContent.setImageResource(mInts[position]);
}
@Override
public int getItemCount() {
return mInts.length;
}
public class FeedHolder extends RecyclerView.ViewHolder{
ImageView mIvContent;
TextView mTvNickname;
public FeedHolder(@NonNull View itemView) {
super(itemView);
mIvContent = itemView.findViewById(R.id.iv_content);
mTvNickname = itemView.findViewById(R.id.tv_nickname);
}
}
}
}
布局
//activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<androidx.coordinatorlayout.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"
tools:context=".MainActivity">
<com.google.android.material.appbar.AppBarLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<androidx.appcompat.widget.Toolbar
android:id="@+id/girls"
android:layout_width="match_parent"
android:layout_height="?actionBarSize"
app:layout_scrollFlags="scroll|enterAlways"
app:title="toolbar"/>
</com.google.android.material.appbar.AppBarLayout>
<FrameLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="@string/appbar_scrolling_view_behavior"
>
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/recyclerview"
android:background="@color/white"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:scrollbars="vertical"
tools:listitem="@layout/item_feed"
/>
<RelativeLayout
android:background="@color/white"
android:id="@+id/suspension_bar"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:id="@+id/tv_nickname"
android:layout_width="wrap_content"
android:layout_height="44dp"
android:layout_marginLeft="8dp"
android:text="NetEase"
android:textColor="@color/purple_500"
android:gravity="center_vertical"
android:textSize="12sp"
/>
<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>
</androidx.coordinatorlayout.widget.CoordinatorLayout>
//item_feed.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/tv_nickname"
android:layout_width="wrap_content"
android:layout_height="44dp"
android:layout_marginLeft="8dp"
android:text="NetEase"
android:textColor="@color/purple_500"
android:gravity="center_vertical"
android:textSize="12sp"
/>
<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/top_divider"
android:scaleType="centerCrop"
android:src="@drawable/b"
/>
</RelativeLayout>
文件传入传出
//读取文件(字节流)
FileInputStream in = new FileInputStream("d:\\1.txt");
//写入相应的文件
FileOutputStream out = new FileOutputStream("d:\\2.txt");
//读取数据
//一次性取多少字节
byte[] bytes = new byte[2048];
//接受读取的内容(n就代表的相关数据,只不过是数字的形式)
int n = -1;
//循环取出数据
while ((n = in.read(bytes,0,bytes.length)) != -1) {
//转换成字符串
String str = new String(bytes,0,n,"UTF-8"); #这里可以实现字节到字符串的转换,比较实用
System.out.println(str);
//写入相关文件
out.write(bytes, 0, n);
//清除缓存向文件写入数据
out.flush();
}
//关闭流
in.close();
out.close();
//写入文件 方法1
public class WriteFileExample {
public static void main(String[] args) {
FileOutputStream fop = null;
File file;
String content = "This is the text content";
try {
file = new File("c:/newfile.txt");
fop = new FileOutputStream(file);
// if file doesnt exists, then create it
if (!file.exists()) {
file.createNewFile();
}
// get the content in bytes
byte[] contentInBytes = content.getBytes();
fop.write(contentInBytes);
fop.flush();
fop.close();
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if (fop != null) {
fop.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】凌霞软件回馈社区,博客园 & 1Panel & Halo 联合会员上线
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】博客园社区专享云产品让利特惠,阿里云新客6.5折上折
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 微软正式发布.NET 10 Preview 1:开启下一代开发框架新篇章
· 没有源码,如何修改代码逻辑?
· NetPad:一个.NET开源、跨平台的C#编辑器
· PowerShell开发游戏 · 打蜜蜂
· 凌晨三点救火实录:Java内存泄漏的七个神坑,你至少踩过三个!