Android 开发
高仿支付宝,美团页面的伸缩Title,代码简单谁都能看懂。
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"
tools:context=".Main2Activity">
<android.support.design.widget.AppBarLayout
android:id="@+id/appbar"
android:layout_width="match_parent"
android:layout_height="160dp"
android:theme="@style/AppTheme.AppBarOverlay">
<!--android:background="@mipmap/image_avatar_6"-->
<android.support.design.widget.CollapsingToolbarLayout
android:id="@+id/toolbarLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#0F6AFF"
app:layout_scrollFlags="scroll|exitUntilCollapsed|snap">
<LinearLayout
android:id="@+id/main_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginTop="?android:attr/actionBarSize"
android:gravity="center_vertical"
android:orientation="horizontal">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:src="@mipmap/erweima1" />
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:src="@mipmap/tiaoxingma1" />
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:src="@mipmap/erweima1" />
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:src="@mipmap/erweima1" />
</LinearLayout>
<android.support.v7.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?android:attr/actionBarSize"
app:contentInsetStart="0dp"
app:layout_collapseMode="pin"
app:titleTextColor="@color/colorAccent">
<FrameLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<RelativeLayout
android:id="@+id/main_layout1"
android:layout_width="match_parent"
android:layout_height="?android:attr/actionBarSize">
<EditText
android:layout_width="300dp"
android:layout_height="35dp"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:background="#FFFFFFFF"
android:drawableRight="@mipmap/fangdajing"
android:hint="搜索"
android:paddingStart="10dp"
android:paddingEnd="10dp"
android:textColorHint="#CFCFCF"
android:textSize="13sp" />
</RelativeLayout>
<LinearLayout
android:id="@+id/main_layout2"
android:layout_width="match_parent"
android:layout_height="?android:attr/actionBarSize"
android:alpha="0"
android:gravity="center_vertical"
android:orientation="horizontal">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:src="@mipmap/erweima" />
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:src="@mipmap/tiaoxingma" />
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:src="@mipmap/erweima" />
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:src="@mipmap/erweima" />
</LinearLayout>
</FrameLayout>
</android.support.v7.widget.Toolbar>
</android.support.design.widget.CollapsingToolbarLayout>
</android.support.design.widget.AppBarLayout>
<android.support.v7.widget.RecyclerView
android:id="@+id/recyclerView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:overScrollMode="never"
app:layout_behavior="@string/appbar_scrolling_view_behavior"
tools:listitem="@android:layout/simple_list_item_2" />
</android.support.design.widget.CoordinatorLayout>
代码部分:
public class Main2Activity extends Activity {
private LinearLayout main_layout;
private RelativeLayout main_layout1;
private LinearLayout main_layout2;
private AppBarLayout appbar;
private enum Item {
one,
two,
three,
four,
five,
six,
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main2);
RecyclerView recyclerView = (RecyclerView) findViewById(R.id.recyclerView);
appbar = findViewById(R.id.appbar);
main_layout = findViewById(R.id.main_layout);
main_layout1 = findViewById(R.id.main_layout1);
main_layout2 = findViewById(R.id.main_layout2);
recyclerView.setLayoutManager(new LinearLayoutManager(this));
recyclerView.addItemDecoration(new DividerItemDecoration(this, VERTICAL));
recyclerView.setItemAnimator(new DefaultItemAnimator());
List<Item> items = new ArrayList<>();
items.addAll(Arrays.asList(Item.values()));
items.addAll(Arrays.asList(Item.values()));
recyclerView.setAdapter(new BaseRecyclerAdapter<>(items, this));
// 监听滑动事件
appbar.addOnOffsetChangedListener(new AppBarLayout.BaseOnOffsetChangedListener() {
// 加速消失
float layoutfraction = 0f;
@Override
public void onOffsetChanged(AppBarLayout appBarLayout, int i) {
// 消失速度
float num = 0.2f;
int scrollRange = appBarLayout.getTotalScrollRange();
float fraction = 1f * (scrollRange + i) / scrollRange;
if (fraction <= 1) {
num = num + num;
layoutfraction = fraction - num;
if (layoutfraction >= 0) {
main_layout.setAlpha(layoutfraction);
}
}
main_layout1.setAlpha(fraction);
main_layout2.setAlpha(1f - fraction);
Log.d("fraction", "onOffsetChanged: " + fraction);
if(fraction == 1){
main_layout.setAlpha(1f);
main_layout1.setAlpha(1f);
main_layout2.setAlpha(0f);
}else if (fraction==0){
main_layout.setAlpha(0f);
main_layout1.setAlpha(0f);
main_layout2.setAlpha(1f);
}
}
});
}
}
Adapter:
class RecyclerAdapter<Item> extends RecyclerView.Adapter<RecyclerAdapter.MViewHolder> {
private List<Item> items;
private Context context;
public RecyclerAdapter(List<Item> items, Context context){
this.items = items;
this.context = context;
}
@NonNull
@Override
public RecyclerAdapter.MViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
View view = LayoutInflater.from(context).inflate(R.layout.layout,viewGroup ,false);
return new MViewHolder(view);
}
@Override
public void onBindViewHolder(@NonNull RecyclerAdapter.MViewHolder viewHolder, int i) {
viewHolder.textView.setText(items.get(i)+"");
}
@Override
public int getItemCount() {
return items.size();
}
class MViewHolder extends RecyclerView.ViewHolder{
TextView textView ;
public MViewHolder(@NonNull View itemView) {
super(itemView);
textView = itemView.findViewById(R.id.itemtext);
}
}
}
不会的小白可以使用这一套代码,大神们就别看了! ^ 0 ^ ^ 0 ^ ^ 0 ^
这套代码时参照 “这个网址” 里的内容修改的
觉得代码简单的话,点这里可以关注我哦 !!!!以后发现新的简单实用内容,我还会继续更新的!