Skip to content

Commit

Permalink
AutoSet for Button
Browse files Browse the repository at this point in the history
  • Loading branch information
ThisIsNSH committed Aug 21, 2019
1 parent d7ce8fa commit be80663
Show file tree
Hide file tree
Showing 9 changed files with 198 additions and 149 deletions.
235 changes: 121 additions & 114 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,12 +1,16 @@
![](https://github.com/ThisIsNSH/CandyView/blob/master/asset/main.jpg?raw=true)


The easiest way to implement RecyclerView with just 1 LOC.
### The easiest way to implement RecyclerView in just 1 Line.

* Eliminate creation of big adapter classes & usage of `findViewById` for declaration of adapter views. 
* Get rid of different listeners and manage all view methods with single `SugarListener`.
* Automatically put data from Model class to variable without any linking code.
* Forget about lengthy RecyclerView and make delicious `CandyView`.
Delicious Facts
--------

* Forget creation of tasteless `Adapters` & monotonous `findViewById` for your views in adapter.
* Text data `String, int, float ...` is automatically set into `TextView` & `Button`
* Image URL `String` and Image Drawable `(int) R.drawable.id` is automatically set into `ImageView`
* Its all done without any extra line of code. [How?](#automatically-set-data)
* Manage properties of all view using single `SugarListener` or just set attributes for specific views.


Download via Gradle
Expand All @@ -16,121 +20,120 @@ Download via Gradle
Add it in your Project build.gradle at the end of repositories:
```groovy
allprojects {
repositories {
maven { url 'https://jitpack.io' }
}
repositories {
maven { url 'https://jitpack.io' }
}
}
```


Add the dependency in your App build.gradle:
```groovy
dependencies {
implementation 'com.github.ThisIsNSH:CandyView:1.2'
// add these for internal functioning
implementation 'com.squareup.picasso:picasso:{latest version}'
implementation 'com.android.support:recyclerview-v7:{build version}'
implementation 'com.github.ThisIsNSH:CandyView:1.3'
// add these for internal functioning
implementation 'com.squareup.picasso:picasso:{latest version}'
implementation 'com.android.support:recyclerview-v7:{build version}'
}
```

Get Started
--------


Add the code to create `RecyclerView` using CandyView:
#### 1. Add the code in Activity to create `CandyView`:
```java
class ExampleActivity extends Activity {

@Override
protected void onCreate(Bundle savedInstanceState) {
...
// Initialize CandyView object
CandyView candyView = findViewById(R.id.candy);
// Create RecyclerView using make(Context content, int Layout, List<Model> Data, Model Class)
candyView.make(this, R.layout.adapter, dataList, Model.class);
}
@Override
protected void onCreate(Bundle savedInstanceState) {
...

// Initialize CandyView object
CandyView candyView = findViewById(R.id.candy);

// Create RecyclerView using make(Context content, int Layout, List<Model> Data, Model Class)
candyView.make(this, R.layout.adapter, dataList, Model.class);

}

}
```


Add `SugarListener` for extended functionalities:
#### 2. Add `SugarListener` to explicitly set properties of views accessed using `getViewById`:
```java
class ExampleActivity extends Activity implements CandyView.SugarListener {

TextView textView;

@Override
protected void onCreate(Bundle savedInstanceState) {
...
candyView.addSugarListener(this);
}

@Override
public void onCandyMade() {

// Initialize required adapter views
textView = (TextView) candyView.getViewById(R.id.text);

}

@Override
public void onCandyRecycled(View view, int position) {

// Set attributes of adapter views for all positions
textView.setTextColor(getResources().getColor(R.color.colorPrimary));

// Set attributes of adapter views for particular position
if (position == 1)
textView.setTextColor(getResources().getColor(R.color.colorAccent));

}


TextView textView;

@Override
protected void onCreate(Bundle savedInstanceState) {
...
candyView.addSugarListener(this);
}

@Override
public void onCandyMade() {

// Initialize required adapter views
textView = (TextView) candyView.getViewById(R.id.text);

}
```

@Override
public void onCandyRecycled(View view, int position) {

Set data in views without any code:
// Set attributes of adapter views for all positions
textView.setTextColor(getResources().getColor(R.color.colorPrimary));

Create `Model Class` with data members having name same as ID of views in adapter view.
// Set attributes of adapter views at a particular position
if (position == 1)
textView.setTextColor(getResources().getColor(R.color.colorAccent));

}

}
```

#### Automatically Set Data
#### 3. Create `Model Class` with variables having same names as IDs of views in adapter view.
```java
public class Model {

// Declare data variables
String image;
String name;

public Model(String image, String name) {
this.image = image;
this.name = name;
}
// Declare data variables
String image;
String name;

// Getter & Setter for the variables
public String getImage() {
return image;
}
public Model(String image, String name) {
this.image = image;
this.name = name;
}

public void setImage(String image) {
this.image = image;
}
// Getter & Setter for the variables
public String getImage() {
return image;
}

public String getName() {
return name;
}
public void setImage(String image) {
this.image = image;
}

public void setName(String name) {
this.name = name;
}
public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}

}
```


Passing the above Model class and below adapter view automatically sets the value of `name` (String) in `name` (TextView). Not only this but `image` (String URL) is set to `image` (ImageView). `R.drawable.id` format is also supported for ImageView. Make sure the variable name in Model class and id in Layout is same.
This example sets the value of **name** `String` in **name** `TextView`. **image** `String URL` is set to **image** `ImageView`. `R.drawable.id` format is also supported for ImageView.
Make sure the variable name in `Model` and `ID` in layout.xml is same.


```XML
<?xml version="1.0" encoding="utf-8"?>
Expand All @@ -140,44 +143,48 @@ android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">

<android.support.v7.widget.CardView
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:cardBackgroundColor="#fff"
app:cardCornerRadius="16dp"
app:cardElevation="4dp"
app:cardPreventCornerOverlap="true"
app:cardUseCompatPadding="true">

<RelativeLayout
android:layout_width="match_parent"
android:id="@+id/relative"
android:layout_height="wrap_content"
android:gravity="center_vertical">

<ImageView
android:id="@+id/image"
android:layout_width="100dp"
android:layout_height="100dp"
android:layout_margin="8dp"
android:scaleType="centerCrop" />

<TextView
android:id="@+id/name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="8dp"
android:layout_toRightOf="@id/image"
android:textColor="#000"
android:textSize="18sp"/>

</RelativeLayout>

</android.support.v7.widget.CardView>
<android.support.v7.widget.CardView
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:cardBackgroundColor="#fff"
app:cardCornerRadius="16dp"
app:cardElevation="4dp"
app:cardPreventCornerOverlap="true"
app:cardUseCompatPadding="true">

<RelativeLayout
android:layout_width="match_parent"
android:id="@+id/relative"
android:layout_height="wrap_content"
android:gravity="center_vertical">

<ImageView
android:id="@+id/image"
android:layout_width="100dp"
android:layout_height="100dp"
android:layout_margin="8dp"
android:scaleType="centerCrop" />

<TextView
android:id="@+id/name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="8dp"
android:layout_toRightOf="@id/image"
android:textColor="#000"
android:textSize="18sp"/>

</RelativeLayout>

</android.support.v7.widget.CardView>

</LinearLayout>
```


Confused?
--------

* Download the sample app and test.


Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

Expand Down Expand Up @@ -57,10 +58,10 @@ public class CreamAdapter extends RecyclerView.Adapter<CreamAdapter.SuperViewHol
/**
* Constructor to use when creating an adapter.
*
* @param context The Context the view is running in, through which it can
* access the current theme, resources, etc.
* @param context The Context the view is running in, through which it can
* access the current theme, resources, etc.
* @param adapterLayout The ID of the layout for the adapter.
* @param context The List of Maps which contains the data stored as List of Model in calling Activity.
* @param context The List of Maps which contains the data stored as List of Model in calling Activity.
*/
public CreamAdapter(Context context, int adapterLayout, List<Map<String, Object>> dataMap) {
this.adapterLayout = adapterLayout;
Expand All @@ -85,11 +86,11 @@ public SuperViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {

/**
* Override method of RecyclerView.Adapter class.
*
* <p>
* Whenever the views are recycled or are about to
* recycle this method is called. The viewList is iterated
* and the data present at the position i is put in the views.
*
* <p>
* Map contains the view id and data. So the iteration puts the
* data in the correct views by checking their IDs.
*/
Expand All @@ -113,13 +114,18 @@ public void onBindViewHolder(@NonNull SuperViewHolder superViewHolder, int i) {
Picasso.get().load((String) dataMap.get(i).get(imageView.getResources().getResourceEntryName(imageView.getId()))).into(imageView);
}
}
} else if (viewList.get(counter) instanceof Button) {
Button button = (Button) viewList.get(counter);
if (dataMap.get(i).get(button.getResources().getResourceEntryName(button.getId())) != null) {
button.setText(String.valueOf(dataMap.get(i).get(button.getResources().getResourceEntryName(button.getId()))));
}
}
}
}

/**
* Override method of RecyclerView.Adapter class.
*
* <p>
* This returns the count of views required.
*/
@Override
Expand Down Expand Up @@ -149,7 +155,6 @@ public SuperViewHolder(@NonNull View itemView) {
* @param v The View is the view generated by the LayoutInflator
* in onBindViewHolder, through which we get all the
* child views in the layout and add them to viewList.
*
* @return result The child view list is returned.
*/
public List<View> getAllChildren(View v) {
Expand All @@ -176,7 +181,6 @@ public List<View> getAllChildren(View v) {
* @param v The View is the view generated by the LayoutInflator
* in onBindViewHolder, through which we get all the
* child and parent views in the layout and add them to viewList.
*
* @return result The child and parent view list is returned.
*/
public List<View> getAllChildrenWithParent(View v) {
Expand Down Expand Up @@ -228,8 +232,8 @@ public interface CreamAdapterListener {
* Method provides current position of the candy view
* & child views in that adapter view.
*
* @param view The View gives all the views recycled for
* a position.
* @param view The View gives all the views recycled for
* a position.
* @param position The int gives the current position of
* the candy view.
*/
Expand Down
2 changes: 1 addition & 1 deletion SugarLibrary/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
<resources>
<string name="app_name">ChocoBeetle</string>
<string name="app_name">SugarLibrary</string>
</resources>
3 changes: 1 addition & 2 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,8 @@ android {

dependencies {
implementation fileTree(dir: 'libs', include: ['*.jar'])
compile project(":SugarLibrary")
implementation 'com.android.support:appcompat-v7:28.0.0'
// implementation 'com.github.ThisIsNSH:Candy-View:1.2'
implementation 'com.github.ThisIsNSH:CandyView:1.3'
implementation 'com.squareup.picasso:picasso:2.71828'
implementation 'com.android.support:cardview-v7:28.0.0'
implementation 'com.android.support:recyclerview-v7:28.0.0'
Expand Down
Loading

0 comments on commit be80663

Please sign in to comment.