Recyclerview's row update
public class RecyclerviewActivity extends AppCompatActivity {
private List<SampleValue> versionsList = new ArrayList<>();
private RecyclerView mRecyclerView;
private DividerItemDecoration mDividerItemDecoration;
private NestedScrollView mNestedScrollView;
private BottomSheetBehavior mBottomSheetBehavior;
private Button mDelete;
private MyRecyclerViewAdapter myRecyclerViewAdapter;
@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 = 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);
}
});
myRecyclerViewAdapter.setRootClickOnListener(new MyRecyclerViewAdapter.CustomClickListener() {
@Override
public void update(int position, SampleValue sampleValue) {
Toast.makeText(RecyclerviewActivity.this, "changing values..."+sampleValue.getPostId()+" "+
sampleValue.getPosition(), Toast.LENGTH_SHORT).show();
Intent editRowIntent = new Intent(RecyclerviewActivity.this,EditRowActivity.class);
editRowIntent.putExtra("postId",sampleValue.getPostId());
editRowIntent.putExtra("position",position-1);
startActivityForResult(editRowIntent,100);
}
});
}
private void fillRawDataToList() {
for (int i = 0; i <50 ; i++) {
SampleValue sampleValue = new SampleValue();
sampleValue.setPostId(i);
sampleValue.setPosition(i);
versionsList.add(sampleValue);
}
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == 100) {
Toast.makeText(this, "Got data"+data.getStringExtra("postId"), Toast.LENGTH_SHORT).show();
versionsList.get(data.getIntExtra("position",0)).setPostId(Integer.parseInt(data.getStringExtra("postId")));
myRecyclerViewAdapter.notifyDataSetChanged();
}
}
}
public class MyRecyclerViewAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
private List<SampleValue> versionsList;
private Context mContext;
private static final int HEADER = 0;
private static final int ITEM= 1;
private Button mDelete;
private SampleValue mSelectedObject;
private BottomSheetBehavior mBottomSheetBehavior;
private CustomClickListener customClickListener;
private SampleValue sampleValue;
public MyRecyclerViewAdapter(List<SampleValue> 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 SampleValue sampleValue= getItem(position);
myItem.tvVersionName.setText("Version "+sampleValue.getPostId());
((MyItem) holder).btEdit.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
mSelectedObject = sampleValue;
mBottomSheetBehavior.setState(BottomSheetBehavior.STATE_EXPANDED);
// versionsList.remove(integer);
// notifyDataSetChanged();
}
});
mDelete.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
versionsList.remove(mSelectedObject);
notifyDataSetChanged();
mBottomSheetBehavior.setState(BottomSheetBehavior.STATE_COLLAPSED);
}
});
((MyItem)holder).mRootView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
customClickListener.update(position,sampleValue);
}
});
}
}
@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 SampleValue getItem(int position) {
return versionsList.get(position -1);
}
private class MyItem extends RecyclerView.ViewHolder {
private TextView tvVersionName;
private Button btEdit;
private View mRootView;
public MyItem(View itemView) {
super(itemView);
mRootView = 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);
}
}
interface CustomClickListener{
void update(int position,SampleValue sampleValue);
}
public void setRootClickOnListener(CustomClickListener customClickListener) {
this.customClickListener = customClickListener;
}
}
public class EditRowActivity extends AppCompatActivity {
private EditText etChangeValue;
private int mPostId,mPosition;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_edit_row);
etChangeValue = (EditText) findViewById(R.id.activity_edit_row_et);
mPostId = getIntent().getIntExtra("postId",0);
mPosition = getIntent().getIntExtra("position",0);
etChangeValue.setText(""+mPostId);
}
public void finishActivity(View veiw) {
Intent resultIntent = new Intent();
resultIntent.putExtra("postId",etChangeValue.getText().toString());
resultIntent.putExtra("position",mPosition);
setResult(200,resultIntent);
finish();
}
}
public class SampleValue {
int postId;
public int getPosition() {
return position;
}
public void setPosition(int position) {
this.position = position;
}
int position;
public int getPostId() {
return postId;
}
public void setPostId(int postId) {
this.postId = postId;
}
}
activity_edit_row.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/activity_edit_row"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:orientation="vertical"
tools:context="sciens.com.viewdeletionactivity.recyclerview_header.EditRowActivity">
<EditText
android:id="@+id/activity_edit_row_et"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="raw data"/>
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Change and submit"
android:onClick="finishActivity"/>
</LinearLayout>
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
<?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>
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>