BottomSheet recyclerview
I love this, understand life as it is.
Download app:
MyRecyclerViewAdapter.class
MyRecyclerViewAdapter.class
public class MyRecyclerViewAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
private static final int HEADER = 0;
private static final int ITEM = 1;
private List<Integer> versionsList;
private Context mContext;
private Button mDelete;
private Integer mSelectedObject;
private BottomSheetBehavior mBottomSheetBehavior;
public MyRecyclerViewAdapter(List<Integer> versionsList, Button mDelete, BottomSheetBehavior mBottomSheetBehavior) {
this.versionsList = versionsList;
this.mDelete = mDelete;
this.mBottomSheetBehavior = mBottomSheetBehavior;
}
@Override
public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
if (viewType == HEADER) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.header_layout, parent, false);
return new MyHeader(view);
} else if (viewType == ITEM) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.inflator_layout, parent, false);
return new MyItem(view);
}
throw new RuntimeException("there is no type that matches the type " + viewType + " + make sure your using types correctly");
}
@Override
public void onBindViewHolder(RecyclerView.ViewHolder holder, final int position) {
if (holder instanceof MyHeader) {
MyHeader myHeader = (MyHeader) holder;
myHeader.tvHeader1.setText("Hi! Lokesh");
} else if (holder instanceof MyItem) {
MyItem myItem = (MyItem) holder;
final Integer integer = getItem(position);
myItem.tvVersionName.setText("Version " + integer);
((MyItem) holder).btEdit.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
mSelectedObject = integer;
mBottomSheetBehavior.setState(BottomSheetBehavior.STATE_EXPANDED);
}
});
mDelete.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
versionsList.remove(mSelectedObject);
notifyDataSetChanged();
mBottomSheetBehavior.setState(BottomSheetBehavior.STATE_COLLAPSED);
}
});
}
}
@Override
public int getItemCount() {
return versionsList.size() + 1;
}
@Override
public int getItemViewType(int position) {
if (isPositionHeader(position))
return HEADER;
else return ITEM;
}
public boolean isPositionHeader(int position) {
return position == 0;
}
private Integer getItem(int position) {
return versionsList.get(position - 1);
}
private class MyItem extends RecyclerView.ViewHolder {
private TextView tvVersionName;
private Button btEdit;
public MyItem(View itemView) {
super(itemView);
tvVersionName = (TextView) itemView.findViewById(R.id.inflator_name);
btEdit = (Button) itemView.findViewById(R.id.inflator_edit);
}
}
private class MyHeader extends RecyclerView.ViewHolder {
private TextView tvHeader1;
public MyHeader(View itemView) {
super(itemView);
tvHeader1 = (TextView) itemView.findViewById(R.id.header_layout_tv1);
}
}
}
RecyclerviewActivity.class
RecyclerviewActivity.class
public class RecyclerviewActivity extends AppCompatActivity {
private List<Integer> versionsList = new ArrayList<>();
private RecyclerView mRecyclerView;
private DividerItemDecoration mDividerItemDecoration;
private NestedScrollView mNestedScrollView;
private BottomSheetBehavior mBottomSheetBehavior;
private Button mDelete;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_recyclerview);
mRecyclerView = (RecyclerView) findViewById(R.id.activity_recyclerview_rl);
mNestedScrollView = (NestedScrollView) findViewById(R.id.activity_recyclerview_nsv);
mDelete = (Button) findViewById(R.id.activity_main_delete);
mBottomSheetBehavior = BottomSheetBehavior.from(mNestedScrollView);
fillRawDataToList();
MyRecyclerViewAdapter myRecyclerViewAdapter = new MyRecyclerViewAdapter(versionsList,mDelete
,mBottomSheetBehavior);
RecyclerView.LayoutManager mLayoutManager = new LinearLayoutManager(this);
mRecyclerView.setLayoutManager(mLayoutManager);
mRecyclerView.setItemAnimator(new DefaultItemAnimator());
mRecyclerView.setAdapter(myRecyclerViewAdapter);
mRecyclerView.setOnScrollListener(new RecyclerView.OnScrollListener() {
@Override
public void onScrollStateChanged(RecyclerView recyclerView, int newState) {
super.onScrollStateChanged(recyclerView, newState);
mBottomSheetBehavior.setState(BottomSheetBehavior.STATE_COLLAPSED);
}
@Override
public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
super.onScrolled(recyclerView, dx, dy);
}
});
}
private void fillRawDataToList() {
for (int i = 0; i <50 ; i++) {
versionsList.add(i);
}
}
}
activity_recyclerview.xml
activity_recyclerview.xml
<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/activity_recyclerview"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/cardview_light_background"
tools:context="sciens.com.viewdeletionactivity.recyclerview_header.RecyclerviewActivity">
<android.support.v7.widget.RecyclerView
android:id="@+id/activity_recyclerview_rl"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
<android.support.v4.widget.NestedScrollView
android:id="@+id/activity_recyclerview_nsv"
android:layout_width="match_parent"
android:layout_height="100dp"
app:behavior_hideable="true"
app:behavior_peekHeight="1dp"
app:layout_behavior="@string/bottom_sheet_behavior">
<Button
android:id="@+id/activity_main_delete"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Delete"/>
</android.support.v4.widget.NestedScrollView>
</android.support.design.widget.CoordinatorLayout>
header_layout.xml
header_layout.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:id="@+id/header_layout_tv1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="header"
android:textSize="22sp"
android:gravity="center_horizontal"
android:padding="10dp"
android:background="@color/colorAccent"/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="hello"
android:id="@+id/header_layout_hello_tv"
android:textSize="22sp"
android:gravity="center_horizontal"
android:padding="10dp"
android:background="@color/colorPrimary"/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="header"
android:textSize="22sp"
android:gravity="center_horizontal"
android:padding="10dp"
android:background="@color/colorAccent"/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="hello"
android:textSize="22sp"
android:gravity="center_horizontal"
android:padding="10dp"
android:background="@color/colorPrimaryDark"/>
</LinearLayout>
inflator_layout.xml
inflator_layout.xml
<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.CardView android:layout_width="match_parent"
android:layout_height="wrap_content"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:card_view="http://schemas.android.com/tools"
app:cardElevation="4dp"
card_view:cardCornerRadius="4dp"
android:layout_marginBottom="5dp"
android:layout_marginLeft="5dp"
android:layout_marginRight="5dp"
xmlns:android="http://schemas.android.com/apk/res/android">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:weightSum="2">
<TextView
android:id="@+id/inflator_name"
android:layout_width="0dp"
android:layout_weight="1"
android:layout_margin="5dp"
android:textSize="25dp"
android:textColor="@color/colorPrimary"
android:layout_height="wrap_content"
android:text="name"/>
<Button
android:layout_gravity="center_horizontal"
android:id="@+id/inflator_edit"
android:layout_width="0dp"
android:textColor="@color/colorAccent"
android:layout_weight="1"
android:layout_height="wrap_content"
android:text="Options"/>
</LinearLayout>
</android.support.v7.widget.CardView>