; paras[X - 1].parentNode.insertBefore(ad1, paras[X]); } if (paras.length > X + 4) { var ad1 = document.createElement('div'); ad1.className = 'ad-auto-insert ad-first'; ad1.innerHTML = ` ; paras[X + 3].parentNode.insertBefore(ad2, paras[X + 4]); } if (isMobile && paras.length > X + 8) { var ad1 = document.createElement('div'); ad1.className = 'ad-auto-insert ad-first'; ad1.innerHTML = ` ; paras[X + 7].parentNode.insertBefore(ad3, paras[X + 8]); } });

Advertisement

Sectioned RecyclerView in android


Sectioned RecyclerView in android


Created a sample Sectioned RecyclerView Application.

I used the following Library to easily create this one.

https://github.com/afollestad/sectioned-recyclerview
Credits:Aidan Follestad














Step: 1
======
Add a dependency to the build.gradle file [or] You can download the SectionedRecyclerViewAdapter.java file and put in it your source code.

compile('com.github.afollestad:sectioned-recyclerview:0.1.0') {
transitive = true
}

Step: 2
======
Created a Sample Activity with RecyclerView in the Xml Layout file. Populated with dummy data for sample.

package com.pratap.sectionrecyclerview;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.Toolbar;

import com.pratap.sectionrecyclerview.models.DataModel;
import com.pratap.sectionrecyclerview.utils.DividerItemDecoration;

import java.util.ArrayList;
import java.util.List;

public class MainActivity extends AppCompatActivity {

private Toolbar toolbar;


List<DataModel> allSampleData;


@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

toolbar = (Toolbar) findViewById(R.id.toolbar);

allSampleData = new ArrayList<DataModel>();
if (toolbar != null) {
setSupportActionBar(toolbar);
toolbar.setTitle("Section RecyclerView");

}


populateSampleData();


RecyclerView my_recycler_view = (RecyclerView) findViewById(R.id.my_recycler_view);
my_recycler_view.addItemDecoration(
new DividerItemDecoration(this, null));


RecyclerViewSectionAdapter adapter = new RecyclerViewSectionAdapter(allSampleData);


GridLayoutManager manager = new GridLayoutManager(this,
getResources().getInteger(R.integer.grid_span_1));


/* GridLayoutManager manager = new GridLayoutManager(this,
getResources().getInteger(R.integer.grid_span_2));


GridLayoutManager manager = new GridLayoutManager(this,
getResources().getInteger(R.integer.grid_span_3));*/

my_recycler_view.setLayoutManager(manager);
adapter.setLayoutManager(manager);
my_recycler_view.setAdapter(adapter);


}

private void populateSampleData() {

for (int i = 1; i <= 10; i++) {

DataModel dm = new DataModel();

dm.setHeaderTitle("Section " + i);

ArrayList<String> singleItem = new ArrayList<>();
for (int j = 1; j <= 4; j++) {

singleItem.add("Item " + j);
}

dm.setAllItemsInSection(singleItem);

allSampleData.add(dm);

}
}
}

Step: 3
======
Created a Adapter  which extends from SectionedRecyclerViewAdapter Class provided in the Library.

package com.pratap.sectionrecyclerview;

import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.pratap.sectionrecyclerview.models.DataModel;
import com.pratap.sectionrecyclerview.utils.SectionedRecyclerViewAdapter;

import java.util.ArrayList;
import java.util.List;


public class RecyclerViewSectionAdapter extends SectionedRecyclerViewAdapter<RecyclerView.ViewHolder> {


private List<DataModel> allData;


public RecyclerViewSectionAdapter(List<DataModel> data) {

this.allData = data;
}


@Override
public int getSectionCount() {
return allData.size();
}

@Override
public int getItemCount(int section) {

return allData.get(section).getAllItemsInSection().size();

}

@Override
public void onBindHeaderViewHolder(RecyclerView.ViewHolder holder, int section) {

String sectionName = allData.get(section).getHeaderTitle();
SectionViewHolder sectionViewHolder = (SectionViewHolder) holder;
sectionViewHolder.sectionTitle.setText(sectionName);
}

@Override
public void onBindViewHolder(RecyclerView.ViewHolder holder, int section, int relativePosition, int absolutePosition) {

ArrayList<String> itemsInSection = allData.get(section).getAllItemsInSection();

String itemName = itemsInSection.get(relativePosition);

ItemViewHolder itemViewHolder = (ItemViewHolder) holder;

itemViewHolder.itemTitle.setText(itemName);

// Try to put a image . for sample i set background color in xml layout file
// itemViewHolder.itemImage.setBackgroundColor(Color.parseColor("#01579b"));
}

@Override
public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, boolean header) {
View v = null;
if (header)

{
v = LayoutInflater.from(parent.getContext())
.inflate(R.layout.list_item_section, parent, false);
return new SectionViewHolder(v);
} else {
v = LayoutInflater.from(parent.getContext())
.inflate(R.layout.list_item, parent, false);
return new ItemViewHolder(v);
}

}


// SectionViewHolder Class for Sections
public static class SectionViewHolder extends RecyclerView.ViewHolder {


final TextView sectionTitle;

public SectionViewHolder(View itemView) {
super(itemView);

sectionTitle = (TextView) itemView.findViewById(R.id.sectionTitle);


}
}

// ItemViewHolder Class for Items in each Section
public static class ItemViewHolder extends RecyclerView.ViewHolder {

final TextView itemTitle;

final ImageView itemImage;


public ItemViewHolder(View itemView) {
super(itemView);
itemTitle = (TextView) itemView.findViewById(R.id.itemTitle);
itemImage = (ImageView) itemView.findViewById(R.id.itemImage);

itemView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {

Toast.makeText(v.getContext(), itemTitle.getText(), Toast.LENGTH_SHORT).show();

}
});
}
}
}

Step: 4
======
Create a Model class to provide for the Adapter


package com.pratap.sectionrecyclerview.models;

import java.util.ArrayList;

/**
* Created by pratap.kesaboyina on 30-11-2015.
*/
public class DataModel {



private String headerTitle;
private ArrayList<String> allItemsInSection;


public DataModel() {

}
public DataModel(String headerTitle, ArrayList<String> allItemsInSection) {
this.headerTitle = headerTitle;
this.allItemsInSection = allItemsInSection;
}



public String getHeaderTitle() {
return headerTitle;
}

public void setHeaderTitle(String headerTitle) {
this.headerTitle = headerTitle;
}

public ArrayList<String> getAllItemsInSection() {
return allItemsInSection;
}

public void setAllItemsInSection(ArrayList<String> allItemsInSection) {
this.allItemsInSection = allItemsInSection;
}


}

ScreenShots
=========
























Demo 
=====







Source Code
=========
DropBoxLink


Android HttpURLConnection code for GET and POST Methods



package com.infodat.selltis2;

import android.util.Log;

import com.infodat.selltis2.utils.Constants;
import com.infodat.selltis2.utils.Utils;

import java.io.BufferedInputStream;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.io.UnsupportedEncodingException;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLEncoder;
import java.util.HashMap;
import java.util.Map;

import javax.net.ssl.HttpsURLConnection;

public class HttpWebServiceHandlerSample {

static String response = null;


public HttpWebServiceHandlerSample() {

}


/**
* Making service call
*
* @url - url to make request
* @method - http request method
* @params - http request params
*/


// Get Request using HttpURLConnection Code
public String makeGetServiceCall(String requestURL, HashMap<String, String> postDataParams) {

InputStream inputStream = null;

HttpURLConnection conn = null;

String response = "";
try {
/* forming th java.net.URL object */


String newRequestURL = requestURL + getQueryString(postDataParams);

URL url = new URL(newRequestURL);
Log.i("URL ", newRequestURL);

conn = (HttpURLConnection) url.openConnection();

/* optional request header */
conn.setRequestProperty("Content-Type", "application/json");

/* optional request header */
conn.setRequestProperty("Accept", "application/json");

// urlConnection.setConnectTimeout(100000);

/* for Get request */
conn.setRequestMethod("GET");

int statusCode = conn.getResponseCode();

/* 200 represents HTTP OK */
if (statusCode == 200) {

inputStream = new BufferedInputStream(conn.getInputStream());

response = convertInputStreamToString(inputStream);


} else {
response = Constants.NetWorkErrorMSG;
}

} catch (Exception e) {
// Log.i("Selltis N/w Error", e.getLocalizedMessage());
response = Constants.NetWorkErrorMSG;

} finally {

if (conn != null)
conn.disconnect();
}

Log.i("RESPONSE", response);
return response; // return response

}


// Convert all key/Value pairs to URL request parameters
private String getQueryString(HashMap<String, String> params) throws UnsupportedEncodingException {
StringBuilder result = new StringBuilder();
boolean first = true;
for (Map.Entry<String, String> entry : params.entrySet()) {
if (first)
first = false;
else
result.append("&");

result.append(URLEncoder.encode(entry.getKey(), "UTF-8"));
result.append("=");
result.append(URLEncoder.encode(entry.getValue(), "UTF-8"));
}

return result.toString();
}


// convert InputStream to String
private String convertInputStreamToString(InputStream inputStream) throws IOException {

BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream));

String line = "";
String result = "";

while ((line = bufferedReader.readLine()) != null) {
result += line;
}

/* Close Stream */
if (null != inputStream) {
inputStream.close();
}

return result;
}


// POST Request using HttpURLConnection Code
public String makePostServiceCall(String requestURL, String postJosnData) {
InputStream inputStream = null;
URL url;
String response = "";
HttpURLConnection conn = null;
try {
url = new URL(requestURL);

conn = (HttpURLConnection) url.openConnection();
conn.setReadTimeout(10000);
conn.setConnectTimeout(10000);
conn.setRequestMethod("POST");
conn.setDoInput(true);
conn.setDoOutput(true);

/* optional request header */
conn.setRequestProperty("Content-Type", "application/json");

/* optional request header */
conn.setRequestProperty("Accept", "application/json");

OutputStream os = conn.getOutputStream();
BufferedWriter writer = new BufferedWriter(
new OutputStreamWriter(os, "UTF-8"));
writer.write(postJosnData);

// Log.i("POST URL", url.toString());
// Log.i("POST DATA", postJosnData);

writer.flush();
writer.close();
os.close();
int responseCode = conn.getResponseCode();

if (responseCode == HttpURLConnection.HTTP_OK) {

inputStream = new BufferedInputStream(conn.getInputStream());

response = convertInputStreamToString(inputStream);


} else {
response = Utils.ServerErrorMessage;

}
} catch (Exception e) {

// Log.i("Selltis N/w Error", e.getLocalizedMessage());
response = Utils.ServerErrorMessage;
} finally {

if (conn != null)
conn.disconnect();
}

return response;

}


}


Swipe RecyclerView using AndroidSwipeLayout



Android Swipe Layout

This will be the most powerful Android swipe UI component


In one of my Project, I have a requirement to create a Swiping Layout For RecyclerView.
I looked for different Libraries. I found this Great Library Android Swipe Layout.

I have tried this Library for RecyclerView. I have done sample Project using this great Library.

This Library Supports ListView and GridView also.





Thanks to the Author for Creating this useful Library:
https://github.com/daimajia/

Library Link :
https://github.com/daimajia/AndroidSwipeLayout/

Wiki Page:
https://github.com/daimajia/AndroidSwipeLayout/wiki

Step: 1
======

Add dependency to your build.gradle file

1
2
3
4
dependencies {
compile 'com.daimajia.swipelayout:library:1.2.0@aar'

}

Step: 2
======
Create a row for RecyclerView using SwipeLayout

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
<?xml version="1.0" encoding="utf-8" ?>
<com.daimajia.swipe.SwipeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:swipe="http://schemas.android.com/apk/res-auto"
android:id="@+id/swipe"
android:layout_width="match_parent"
android:layout_height="wrap_content"
swipe:leftEdgeSwipeOffset="0dp"
swipe:rightEdgeSwipeOffset="0dp">

<!--Bottom View For Right to Left-->

<LinearLayout
android:id="@+id/bottom_wrapper"
android:layout_width="240dp"
android:layout_height="match_parent"
android:weightSum="3">

<TextView
android:id="@+id/tvEdit"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:background="#0076a5"
android:gravity="center"
android:text="Edit"
android:textColor="#fff" />

<TextView
android:id="@+id/tvShare"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:background="#003c54"
android:gravity="center"
android:text="Share"
android:textColor="#fff" />

<TextView
android:id="@+id/tvDelete"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:background="#0076a5"
android:gravity="center"
android:text="Delete"
android:textColor="#fff" />
</LinearLayout>


<!-- Another Bottom View For Left to Right -->

<LinearLayout
android:id="@+id/bottom_wrapper1"
android:layout_width="80dp"
android:layout_height="match_parent"
android:weightSum="1">

<ImageButton
android:id="@+id/btnLocation"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:background="@null"
android:gravity="center"
android:src="@drawable/ic_map" />
</LinearLayout>

<!-- Top View, Row itemView of RecyclerView -->
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="?android:selectableItemBackground"
android:elevation="5dp"
android:orientation="vertical"
android:padding="10dp">

<TextView
android:id="@+id/tvName"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="5dp"
android:text="Name"
android:textColor="@android:color/black"
android:textSize="18sp" />

<TextView
android:id="@+id/tvEmailId"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/tvName"
android:layout_margin="5dp"
android:text="Email Id"
android:textColor="@android:color/black"
android:textSize="12sp" />
</LinearLayout>

</com.daimajia.swipe.SwipeLayout>

Step: 3
======
Now, Create Adapter Class by extending RecyclerSwipeAdapter 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
package com.pratap.swipe;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageButton;
import android.widget.TextView;
import android.widget.Toast;

import com.daimajia.swipe.SwipeLayout;
import com.daimajia.swipe.adapters.RecyclerSwipeAdapter;

import java.util.ArrayList;

public class SwipeRecyclerViewAdapter extends RecyclerSwipeAdapter<SwipeRecyclerViewAdapter.SimpleViewHolder> {


private Context mContext;
private ArrayList<Student> studentList;

public SwipeRecyclerViewAdapter(Context context, ArrayList<Student> objects) {
this.mContext = context;
this.studentList = objects;
}

@Override
public SimpleViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.swipe_row_item, parent, false);
return new SimpleViewHolder(view);
}

@Override
public void onBindViewHolder(final SimpleViewHolder viewHolder, final int position) {
final Student item = studentList.get(position);

viewHolder.tvName.setText((item.getName()) + " - Row Position " + position);
viewHolder.tvEmailId.setText(item.getEmailId());


viewHolder.swipeLayout.setShowMode(SwipeLayout.ShowMode.PullOut);

// Drag From Left
viewHolder.swipeLayout.addDrag(SwipeLayout.DragEdge.Left, viewHolder.swipeLayout.findViewById(R.id.bottom_wrapper1));

// Drag From Right
viewHolder.swipeLayout.addDrag(SwipeLayout.DragEdge.Right, viewHolder.swipeLayout.findViewById(R.id.bottom_wrapper));


// Handling different events when swiping
viewHolder.swipeLayout.addSwipeListener(new SwipeLayout.SwipeListener() {
@Override
public void onClose(SwipeLayout layout) {
//when the SurfaceView totally cover the BottomView.
}

@Override
public void onUpdate(SwipeLayout layout, int leftOffset, int topOffset) {
//you are swiping.
}

@Override
public void onStartOpen(SwipeLayout layout) {

}

@Override
public void onOpen(SwipeLayout layout) {
//when the BottomView totally show.
}

@Override
public void onStartClose(SwipeLayout layout) {

}

@Override
public void onHandRelease(SwipeLayout layout, float xvel, float yvel) {
//when user's hand released.
}
});

/*viewHolder.swipeLayout.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {


if ((((SwipeLayout) v).getOpenStatus() == SwipeLayout.Status.Close)) {
//Start your activity

Toast.makeText(mContext, " onClick : " + item.getName() + " \n" + item.getEmailId(), Toast.LENGTH_SHORT).show();
}

}
});*/

viewHolder.swipeLayout.getSurfaceView().setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(mContext, " onClick : " + item.getName() + " \n" + item.getEmailId(), Toast.LENGTH_SHORT).show();
}
});


viewHolder.btnLocation.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {

Toast.makeText(v.getContext(), "Clicked on Map " + viewHolder.tvName.getText().toString(), Toast.LENGTH_SHORT).show();
}
});


viewHolder.tvShare.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {

Toast.makeText(view.getContext(), "Clicked on Share " + viewHolder.tvName.getText().toString(), Toast.LENGTH_SHORT).show();
}
});

viewHolder.tvEdit.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {

Toast.makeText(view.getContext(), "Clicked on Edit " + viewHolder.tvName.getText().toString(), Toast.LENGTH_SHORT).show();
}
});


viewHolder.tvDelete.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
mItemManger.removeShownLayouts(viewHolder.swipeLayout);
studentList.remove(position);
notifyItemRemoved(position);
notifyItemRangeChanged(position, studentList.size());
mItemManger.closeAllItems();
Toast.makeText(view.getContext(), "Deleted " + viewHolder.tvName.getText().toString(), Toast.LENGTH_SHORT).show();
}
});


// mItemManger is member in RecyclerSwipeAdapter Class
mItemManger.bindView(viewHolder.itemView, position);

}

@Override
public int getItemCount() {
return studentList.size();
}

@Override
public int getSwipeLayoutResourceId(int position) {
return R.id.swipe;
}


// ViewHolder Class

public static class SimpleViewHolder extends RecyclerView.ViewHolder {
SwipeLayout swipeLayout;
TextView tvName;
TextView tvEmailId;
TextView tvDelete;
TextView tvEdit;
TextView tvShare;
ImageButton btnLocation;

public SimpleViewHolder(View itemView) {
super(itemView);
swipeLayout = (SwipeLayout) itemView.findViewById(R.id.swipe);
tvName = (TextView) itemView.findViewById(R.id.tvName);
tvEmailId = (TextView) itemView.findViewById(R.id.tvEmailId);
tvDelete = (TextView) itemView.findViewById(R.id.tvDelete);
tvEdit = (TextView) itemView.findViewById(R.id.tvEdit);
tvShare = (TextView) itemView.findViewById(R.id.tvShare);
btnLocation = (ImageButton) itemView.findViewById(R.id.btnLocation);


}
}
}


Step: 4
======
Create an xml Layout with RecyclerView for the Activity


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">

<android.support.v7.widget.Toolbar
android:id="@+id/toolbar"
style="@style/ToolBarStyle"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@color/md_deep_orange_500"
android:elevation="8dp"
android:minHeight="?attr/actionBarSize">

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

<android.support.v7.widget.RecyclerView
android:id="@+id/my_recycler_view"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_margin="5dp"
android:layout_weight="1"
android:scrollbars="vertical" />
<TextView
android:id="@+id/empty_view"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:text="No Records"
android:visibility="gone" />


</LinearLayout>


Step: 5
======
Now , Create an Activity Class


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
package com.pratap.swipe;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.Toolbar;
import android.util.Log;
import android.view.View;
import android.widget.TextView;

import com.daimajia.swipe.util.Attributes;

import java.util.ArrayList;


public class RecyclerViewExample extends AppCompatActivity {

/**
* RecyclerView: The new recycler view replaces the list view. Its more modular and therefore we
* must implement some of the functionality ourselves and attach it to our recyclerview.
* <p/>
* 1) Position items on the screen: This is done with LayoutManagers
* 2) Animate & Decorate views: This is done with ItemAnimators & ItemDecorators
* 3) Handle any touch events apart from scrolling: This is now done in our adapter's ViewHolder
*/

private ArrayList<Student> mDataSet;

private Toolbar toolbar;

private TextView tvEmptyView;
private RecyclerView mRecyclerView;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.swiprecyclerview);
toolbar = (Toolbar) findViewById(R.id.toolbar);
tvEmptyView = (TextView) findViewById(R.id.empty_view);
mRecyclerView = (RecyclerView) findViewById(R.id.my_recycler_view);

// Layout Managers:
mRecyclerView.setLayoutManager(new LinearLayoutManager(this));

// Item Decorator:
mRecyclerView.addItemDecoration(new DividerItemDecoration(getResources().getDrawable(R.drawable.divider)));
// mRecyclerView.setItemAnimator(new FadeInLeftAnimator());


mDataSet = new ArrayList<Student>();

if (toolbar != null) {
setSupportActionBar(toolbar);
getSupportActionBar().setTitle("Android Students");

}

loadData();

if (mDataSet.isEmpty()) {
mRecyclerView.setVisibility(View.GONE);
tvEmptyView.setVisibility(View.VISIBLE);

} else {
mRecyclerView.setVisibility(View.VISIBLE);
tvEmptyView.setVisibility(View.GONE);
}


// Creating Adapter object
SwipeRecyclerViewAdapter mAdapter = new SwipeRecyclerViewAdapter(this, mDataSet);


// Setting Mode to Single to reveal bottom View for one item in List
// Setting Mode to Mutliple to reveal bottom Views for multile items in List
((SwipeRecyclerViewAdapter) mAdapter).setMode(Attributes.Mode.Single);

mRecyclerView.setAdapter(mAdapter);

/* Scroll Listeners */
mRecyclerView.addOnScrollListener(new RecyclerView.OnScrollListener() {
@Override
public void onScrollStateChanged(RecyclerView recyclerView, int newState) {
super.onScrollStateChanged(recyclerView, newState);
Log.e("RecyclerView", "onScrollStateChanged");
}

@Override
public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
super.onScrolled(recyclerView, dx, dy);
}
});
}


// load initial data
public void loadData() {

for (int i = 0; i <= 20; i++) {
mDataSet.add(new Student("Student " + i, "androidstudent" + i + "@gmail.com"));

}


}

}

Step: 5
======

Student.java


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
package com.pratap.swipe;

import java.io.Serializable;

public class Student implements Serializable {

private static final long serialVersionUID = 1L;

private String name;

private String emailId;

public Student() {

}
public Student(String name, String emailId) {
this.name = name;
this.emailId = emailId;
}

public String getName() {
return name;
}

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

public String getEmailId() {
return emailId;
}

public void setEmailId(String emailId) {
this.emailId = emailId;
}


}


DividerItemDecoration.java

To give divider for each row in RecyclerView


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
package com.pratap.swipe;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.Rect;
import android.graphics.drawable.Drawable;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.AttributeSet;
import android.view.View;

public class DividerItemDecoration extends RecyclerView.ItemDecoration {

private Drawable mDivider;
private boolean mShowFirstDivider = false;
private boolean mShowLastDivider = false;


public DividerItemDecoration(Context context, AttributeSet attrs) {
final TypedArray a = context
.obtainStyledAttributes(attrs, new int[]{android.R.attr.listDivider});
mDivider = a.getDrawable(0);
a.recycle();
}

public DividerItemDecoration(Context context, AttributeSet attrs, boolean showFirstDivider,
boolean showLastDivider) {
this(context, attrs);
mShowFirstDivider = showFirstDivider;
mShowLastDivider = showLastDivider;
}

public DividerItemDecoration(Drawable divider) {
mDivider = divider;
}

public DividerItemDecoration(Drawable divider, boolean showFirstDivider,
boolean showLastDivider) {
this(divider);
mShowFirstDivider = showFirstDivider;
mShowLastDivider = showLastDivider;
}

@Override
public void getItemOffsets(Rect outRect, View view, RecyclerView parent,
RecyclerView.State state) {
super.getItemOffsets(outRect, view, parent, state);
if (mDivider == null) {
return;
}
if (parent.getChildPosition(view) < 1) {
return;
}

if (getOrientation(parent) == LinearLayoutManager.VERTICAL) {
outRect.top = mDivider.getIntrinsicHeight();
} else {
outRect.left = mDivider.getIntrinsicWidth();
}
}

@Override
public void onDrawOver(Canvas c, RecyclerView parent, RecyclerView.State state) {
if (mDivider == null) {
super.onDrawOver(c, parent, state);
return;
}

// Initialization needed to avoid compiler warning
int left = 0, right = 0, top = 0, bottom = 0, size;
int orientation = getOrientation(parent);
int childCount = parent.getChildCount();

if (orientation == LinearLayoutManager.VERTICAL) {
size = mDivider.getIntrinsicHeight();
left = parent.getPaddingLeft();
right = parent.getWidth() - parent.getPaddingRight();
} else { //horizontal
size = mDivider.getIntrinsicWidth();
top = parent.getPaddingTop();
bottom = parent.getHeight() - parent.getPaddingBottom();
}

for (int i = mShowFirstDivider ? 0 : 1; i < childCount; i++) {
View child = parent.getChildAt(i);
RecyclerView.LayoutParams params = (RecyclerView.LayoutParams) child.getLayoutParams();

if (orientation == LinearLayoutManager.VERTICAL) {
top = child.getTop() - params.topMargin;
bottom = top + size;
} else { //horizontal
left = child.getLeft() - params.leftMargin;
right = left + size;
}
mDivider.setBounds(left, top, right, bottom);
mDivider.draw(c);
}

// show last divider
if (mShowLastDivider && childCount > 0) {
View child = parent.getChildAt(childCount - 1);
RecyclerView.LayoutParams params = (RecyclerView.LayoutParams) child.getLayoutParams();
if (orientation == LinearLayoutManager.VERTICAL) {
top = child.getBottom() + params.bottomMargin;
bottom = top + size;
} else { // horizontal
left = child.getRight() + params.rightMargin;
right = left + size;
}
mDivider.setBounds(left, top, right, bottom);
mDivider.draw(c);
}
}

private int getOrientation(RecyclerView parent) {
if (parent.getLayoutManager() instanceof LinearLayoutManager) {
LinearLayoutManager layoutManager = (LinearLayoutManager) parent.getLayoutManager();
return layoutManager.getOrientation();
} else {
throw new IllegalStateException(
"DividerItemDecoration can only be used with a LinearLayoutManager.");
}
}
}


Step: 6
======

ScreenShots
 

 


Step: 7
======

Source Code

DownloadLink


Sample Apk

Sample Apk File






UPTET news