Hello friends
If you want add decoration in your recyclerview so follow below steps
Decoration
If you want add decoration in your recyclerview so follow below steps
Decoration
public class MarginDecoration extends RecyclerView.ItemDecoration {
private int margin;
public MarginDecoration(Context context) {
margin = context.getResources().getDimensionPixelSize(R.dimen.item_margin);
}
@Override
public void getItemOffsets(
Rect outRect, View view, RecyclerView parent, RecyclerView.State state) {
outRect.set(margin, margin, margin, margin);
}
}
Above decoration add margin
Mainactivity.java
package com.samset.recyclerviewmargindecoration;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.AppCompatCheckBox;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.View;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import com.samset.recyclerviewmargindecoration.decoration.ColorFullDecoration;
import com.samset.recyclerviewmargindecoration.decoration.MarginDecoration;
import com.samset.recyclerviewmargindecoration.decoration.VerticalDecoration;
import java.util.ArrayList;
import java.util.List;
public class MainActivity extends AppCompatActivity {
private RecyclerView recyclerView;
private RadioGroup radioGroup;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initView();
radioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(RadioGroup radioGroup, int i) {
View radioButton = radioGroup.findViewById(i);
int index = radioGroup.indexOfChild(radioButton);
RadioButton r = (RadioButton) radioGroup.getChildAt(index);
if (r.getText().toString().equalsIgnoreCase("Simple")) {
recyclerView.addItemDecoration(new MarginDecoration(MainActivity.this));
} else if (r.getText().toString().equalsIgnoreCase("Vertical")) {
recyclerView.addItemDecoration(new VerticalDecoration(MainActivity.this));
} else if (r.getText().toString().equalsIgnoreCase("With Border")) {
recyclerView.addItemDecoration(new ColorFullDecoration());
}
}
});
setRecyclerView();
}
private void setRecyclerView() {
recyclerView.setHasFixedSize(true);
recyclerView.setLayoutManager(new LinearLayoutManager(this));
recyclerView.setAdapter(new DecorateAdapter(getList()));
}
private List<String> getList() {
List<String> lists = new ArrayList<String>();
for (int i = 0; i < 50; ++i) {
lists.add("Item " + String.valueOf(i));
}
return lists;
}
private void initView() {
radioGroup = findViewById(R.id.rg);
recyclerView = (RecyclerView) findViewById(R.id.recyclerview);
}
}
activitymain.xml
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout 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="com.samset.recyclerviewmargindecoration.MainActivity">
<LinearLayout
android:id="@+id/parent"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:padding="5dp"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent">
<RadioGroup
android:id="@+id/rg"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<RadioButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Simple" />
<RadioButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Vertical" />
<RadioButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="With Border" />
</RadioGroup>
</LinearLayout>
<android.support.v7.widget.RecyclerView
android:id="@+id/recyclerview"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:layout_constraintTop_toBottomOf="@+id/parent" />
</android.support.constraint.ConstraintLayout>
Thank you
Enjoy
See full sorce code
See live sample
No comments:
Post a Comment