How to bind multiple RecyclerViews in a single | Android RecyclerView Example – Multiple ViewTypes

Jyotishgher Astrology
By -
0

 

Android Recycler View Example – Multiple View Types | how to use two recycler view in android



What do you mean by combing RecyclerView's? Put a RecyclerView inside item of another RecyclerView? It is possible, many do this. Or you want to combine different data sources inside single RecyclerView? This is possible either, you have to create composite adapter and feed it with other adapters in case you have no access to data classes code, otherwise you can use delegation and create single adapter, feed it with ViewHolder’s you need and provide external view type selector for it, and you have to put marker interface on your data classes, or put your data objects inside containers to be able to store your data inside single collection, because you need stable positioning of items on screen. So yes, you can combine RecyclerView's and their data.
=================================================================================
=================================================================================
EXAMPLE :

<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:card_view="http://schemas.android.com/tools"
android:id="@+id/scrollView"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="@color/color3">





<
TextView

android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Data of Traceability:"
android:textColor="@android:color/black"
android:textSize="12sp"
android:textStyle="bold" />


<
LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_margin="10dp"
android:layout_marginTop="1dp"
android:orientation="horizontal">

<androidx.recyclerview.widget.RecyclerView
android:id="@+id/recyclerView"
android:layout_width="match_parent"
android:layout_height="match_parent">

</androidx.recyclerview.widget.RecyclerView
>

</
LinearLayout>

<
TextView

android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Data of Observation:"
android:textColor="@android:color/black"
android:textSize="12sp"
android:textStyle="bold" />
<
LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_margin="10dp"
android:layout_marginTop="1dp"
android:orientation="horizontal">

<androidx.recyclerview.widget.RecyclerView
android:id="@+id/recyclerViewObservation"
android:layout_width="match_parent"
android:layout_height="match_parent">

</androidx.recyclerview.widget.RecyclerView>

</
LinearLayout>
</
LinearLayout>
</
ScrollView>
=================================================================================
=================================================================================



=================================================================================
=================================================================================

RecyclerViewAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder>

import android.annotation.SuppressLint;
import android.content.Context;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CheckBox;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.rcf.coach.stage.sip.GetDataAdapter;
import com.rcf.coach.stage.sip.R;

import java.util.List;

import static android.graphics.Color.RED;


public class RecyclerViewAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder>{

private static int TYPE_TRACE = 1;
private static int TYPE_OBSER = 2;

Context context;
public List<GetDataAdapter> getDataAdapter;
public RecyclerViewAdapter(List<GetDataAdapter> getDataAdapter, Context context) {
super();

this.getDataAdapter = getDataAdapter;
this.context = context;
}
@NonNull
@Override
public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int viewType) {
View view;
if (viewType == TYPE_TRACE) { // for call layout
view = LayoutInflater.from(context).inflate(R.layout.display_layout, viewGroup, false);
return new CallViewHolder(view);

} else { // for email layout
view = LayoutInflater.from(context).inflate(R.layout.display_layout, viewGroup, false);
return new EmailViewHolder(view);
}
}


@Override
public int getItemViewType(int position) {
if (getDataAdapter.get(position).getTYPELAYOUT().equalsIgnoreCase("TRACE")) {
return TYPE_TRACE;
} else {
return TYPE_OBSER;
}
}


@SuppressLint("SetTextI18n")
@Override
public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, final int position) {

if (getItemViewType(position) == TYPE_TRACE) {
((
CallViewHolder) holder).setCallDetails(getDataAdapter.get(position));
}
else {
((
EmailViewHolder) holder).setEmailDetails(getDataAdapter.get(position));
}




}


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

static class CallViewHolder extends RecyclerView.ViewHolder {

TextView f, s, t;

CallViewHolder(@NonNull View itemView) {
super(itemView);
f = (TextView) itemView.findViewById(R.id.f);
s = (TextView) itemView.findViewById(R.id.s);
t = (TextView) itemView.findViewById(R.id.t);

}

void setCallDetails(GetDataAdapter getDataAdapter1) {
f.setText(getDataAdapter1.getCODE());
s.setText(getDataAdapter1.getCODE());
}
}






static class EmailViewHolder extends RecyclerView.ViewHolder {

TextView f, s, t;

EmailViewHolder(@NonNull View itemView) {
super(itemView);
f = (TextView) itemView.findViewById(R.id.f);
s = (TextView) itemView.findViewById(R.id.s);
t = (TextView) itemView.findViewById(R.id.t);

}

void setEmailDetails(GetDataAdapter getDataAdapter1) {
f.setText(getDataAdapter1.getCODE());
s.setText(getDataAdapter1.getDESC_ENG());
}
}
}

=================================================================================
=================================================================================
private void JSON_DATA_WEB_CALL_TRACE(String FORM_NUMBER) {

final ProgressDialog loading = ProgressDialog.show(input_form_by_form_no.this, "Fetching Traceability", "Please wait...", false, true);
// Toast.makeText(this, EMP_NO, Toast.LENGTH_SHORT).show();
StringRequest stringRequest = new StringRequest(Request.Method.POST, traceability_lov,
new Response.Listener<String>() {
@Override
public void onResponse(String response) {

// Toast.makeText(hospital.this, response, Toast.LENGTH_SHORT).show();
Log.d("traceability_lov:", "onResponse: " + response);
JSONObject j = null;
try {
//Parsing the fetched Json String to JSON Object
j = new JSONObject(response);

//Storing the Array of JSON String to our JSON Array
result = j.getJSONArray(JSON_ARRAY);
if (result.length() > 0) {

loading.dismiss();
JSON_PARSE_DATA_AFTER_WEB_TRACE(result);


} else {

AlertDialog.Builder alertDialog = new AlertDialog.Builder(input_form_by_form_no.this);

// Setting Dialog Title
alertDialog.setTitle("No record Found");

// Setting Dialog Message
alertDialog.setMessage("Try Again!!");

// Setting Icon to Dialog
alertDialog.setIcon(R.drawable.certificate);

// Setting Positive "Yes" Button
alertDialog.setPositiveButton("Got!!!", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
finish();
}
});

// Showing Alert Message
alertDialog.show();

}

} catch (JSONException e) {
e.printStackTrace();
}


}
},

new Response.ErrorListener() {

@Override
public void onErrorResponse(VolleyError error) {
loading.dismiss();
// stopping swipe refresh
// Toast.makeText(vendor_name.this, "Error", Toast.LENGTH_SHORT).show();

}
}) {
@Override
protected Map<String, String> getParams() {

Log.d("FORM_NUMBER-", "getParams: " + FORM_NUMBER);
//PASSING STRING VALUE TO THE TAGGING PARAMETER WITH TENDS TO GO FOR TBALE NAME
Map<String, String> params = new HashMap<String, String>();

params.put("FORM_NO", FORM_NUMBER);//ORACLEUSER
params.put("NUMBER", "2");//ORACLEUSER


return params;
}

};

RequestQueue requestQueue = Volley.newRequestQueue(input_form_by_form_no.this);
requestQueue.add(stringRequest);
}


RECYCLEVIEW 1 JSON VALUES FROM DATABASE
private void JSON_PARSE_DATA_AFTER_WEB_TRACE(JSONArray resultTRACE)
{

for (int i = 0; i < resultTRACE.length(); i++) {

GetDataAdapter GetDataAdapter2 = new GetDataAdapter();
JSONObject json = null;
try {
json = resultTRACE.getJSONObject(i);


GetDataAdapter2.setCODE(json.getString("CODE"));
GetDataAdapter2.setDESC_ENG(json.getString("DESC_ENG"));
GetDataAdapter2.setDESC_HINDI(json.getString("DESC_HINDI"));
GetDataAdapter2.setTYPELAYOUT("TRACE");


}
catch (JSONException e) {

e.printStackTrace();
}
GetDataAdapter1.add(GetDataAdapter2);
}

recyclerViewadaAdapter = new RecyclerViewAdapter(GetDataAdapter1, input_form_by_form_no.this);

recyclerView.setAdapter(recyclerViewadaAdapter);
}
=================================================================================
private void JSON_DATA_WEB_CALL_OBSER(String FORM_NUMBER) {

final ProgressDialog loading = ProgressDialog.show(input_form_by_form_no.this, "Fetching Traceability", "Please wait...", false, true);
// Toast.makeText(this, EMP_NO, Toast.LENGTH_SHORT).show();
StringRequest stringRequest = new StringRequest(Request.Method.POST, traceability_lov,
new Response.Listener<String>() {
@Override
public void onResponse(String response) {

// Toast.makeText(hospital.this, response, Toast.LENGTH_SHORT).show();
Log.d("OBSER_lov:", "onResponse: " + response);
JSONObject j = null;
try {
//Parsing the fetched Json String to JSON Object
j = new JSONObject(response);

//Storing the Array of JSON String to our JSON Array
result = j.getJSONArray(JSON_ARRAY);
if (result.length() > 0) {

loading.dismiss();
JSON_PARSE_DATA_AFTER_WEB_OBSER(result);


} else {

AlertDialog.Builder alertDialog = new AlertDialog.Builder(input_form_by_form_no.this);

// Setting Dialog Title
alertDialog.setTitle("No record Found");

// Setting Dialog Message
alertDialog.setMessage("Try Again!!");

// Setting Icon to Dialog
alertDialog.setIcon(R.drawable.certificate);

// Setting Positive "Yes" Button
alertDialog.setPositiveButton("Got!!!", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
finish();
}
});

// Showing Alert Message
alertDialog.show();

}

} catch (JSONException e) {
e.printStackTrace();
}


}
},

new Response.ErrorListener() {

@Override
public void onErrorResponse(VolleyError error) {
loading.dismiss();
// stopping swipe refresh
// Toast.makeText(vendor_name.this, "Error", Toast.LENGTH_SHORT).show();

}
}) {
@Override
protected Map<String, String> getParams() {

Log.d("FORM_NUMBER-", "getParams: " + FORM_NUMBER);
//PASSING STRING VALUE TO THE TAGGING PARAMETER WITH TENDS TO GO FOR TBALE NAME
Map<String, String> params = new HashMap<String, String>();

params.put("FORM_NO", FORM_NUMBER);//ORACLEUSER
params.put("NUMBER", "3");//ORACLEUSER


return params;
}

};

RequestQueue requestQueue = Volley.newRequestQueue(input_form_by_form_no.this);
requestQueue.add(stringRequest);
}

RECYCLEVIEW 2 JSON VALUES FROM DATABASE

private void JSON_PARSE_DATA_AFTER_WEB_OBSER(JSONArray resultOBSER)
{
for (int i = 0; i < resultOBSER.length(); i++) {

GetDataAdapter GetDataAdapter2 = new GetDataAdapter();
JSONObject json = null;
try {
json = resultOBSER.getJSONObject(i);


GetDataAdapter2.setCODE(json.getString("CODE"));
GetDataAdapter2.setDESC_ENG(json.getString("DESC_ENG"));
GetDataAdapter2.setDESC_HINDI(json.getString("DESC_HINDI"));
GetDataAdapter2.setTYPELAYOUT("OBSER");


}
catch (JSONException e) {

e.printStackTrace();
}
GetDataAdapter3.add(GetDataAdapter2);
}

recyclerViewadaAdapter = new RecyclerViewAdapter(GetDataAdapter3, input_form_by_form_no.this);

recyclerViewObservation.setAdapter(recyclerViewadaAdapter);

}

Post a Comment

0Comments

Post a Comment (0)