我一直在試圖實現CollapsingToolbar與使用RecyclerViewCollapsingToolbarLayout與RecyclerView
<android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/main_content"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true">
<android.support.design.widget.AppBarLayout
android:id="@+id/appbar"
android:layout_width="match_parent"
android:layout_height="300dp"
android:fitsSystemWindows="true"
android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar">
<android.support.design.widget.CollapsingToolbarLayout
android:id="@+id/collapsing_toolbar"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
app:contentScrim="?attr/colorPrimary"
app:expandedTitleMarginEnd="64dp"
app:expandedTitleMarginStart="48dp"
app:layout_scrollFlags="scroll|exitUntilCollapsed">
<ImageView
android:id="@+id/imageView_list"
android:layout_width="match_parent"
android:layout_height="275dp"
app:layout_collapseMode="parallax"
android:fitsSystemWindows="true"
android:scaleType="fitXY"
android:src="@mipmap/bg_profile_01" />
<android.support.v7.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
app:layout_collapseMode="pin"
app:popupTheme="@style/ThemeOverlay.AppCompat.Light" />
</android.support.design.widget.CollapsingToolbarLayout>
</android.support.design.widget.AppBarLayout>
<android.support.v7.widget.RecyclerView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/list"/>
</android.support.design.widget.CoordinatorLayout>
list.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="50dp"
android:orientation="vertical">
<ImageView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/item_img"
android:adjustViewBounds="true"
/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/item_txt"
android:gravity="center"
android:textAppearance="?android:attr/textAppearanceLarge"
android:paddingTop="5dp"
android:paddingBottom="5dp"
android:layout_centerInParent="true"
/>
適配器
public class ViewAdapter extends RecyclerView.Adapter<ViewAdapter.ViewHolder> {
Context context;
private List<ViewModel> items;
private int itemLayout;
public ViewAdapter(Context context,List<ViewModel> items,int itemLayout) {
this.context = context;
this.items = items;
this.itemLayout = itemLayout;
}
@Override
public int getItemCount() {
return items.size();
}
@Override
public void onBindViewHolder(ViewHolder holder, int position) {
ViewModel item = items.get(position);
holder.itemView.setTag(item);
holder.text.setText(item.getTitle());
}
@Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(itemLayout, parent, false);
/*if(parent.getHeight() > 0){
view.getLayoutParams().height = parent.getHeight()/6;
}*/
ViewHolder vh = new ViewHolder(view, new ViewHolder.AdapterClickListener() {
@Override
public void onAadapterItemClick(View view, int position) {
items.remove(position);
notifyItemRemoved(position);
}
});
return vh;
}
public static class ViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
public ImageView image;
public TextView text;
public AdapterClickListener adapterClickListener;
public ViewHolder(View itemView, AdapterClickListener adapterClickListener) {
super(itemView);
this.adapterClickListener = adapterClickListener;
image = (ImageView)itemView.findViewById(R.id.item_img);
text=(TextView)itemView.findViewById(R.id.item_txt);
itemView.setOnClickListener(this);
}
@Override
public void onClick(View v) {
adapterClickListener.onAadapterItemClick(v, getAdapterPosition());
}
public static interface AdapterClickListener {
public void onAadapterItemClick(View view, int position);
}
}
主要
private void init(){
recyclerView = (RecyclerView)findViewById(R.id.list);
recyclerView.setHasFixedSize(true);
recyclerView.setAdapter(new ViewAdapter(getApplicationContext(), CreateItemList(), R.layout.item_list));
recyclerView.setItemAnimator(new DefaultItemAnimator());
recyclerView.setLayoutManager(new LinearLayoutManager(this));
}
private List<ViewModel> CreateItemList(){
List<ViewModel> items = new ArrayList<ViewModel>();
for(int i=0;i<20;i++){
ViewModel model = new ViewModel();
model.setTitle("title_" + i);
items.add(model);
}
return items;
}
但RecyclerView佔據了整個屏幕
我不知道什麼時候開始出問題。
對不起,我真的在英語不好ㅠ_ㅠ
試試這個https://github.com/chrisbanes/cheesesquare – MHossein
哇!非常非常感謝您 – kimtaehun