我想将水平RecyclerView( InnerRecyclerView )放在另一个垂直RecyclerView( OuterRecyclerView )中,并在两次单独的凌空调用之后将数据解析到其中。在第一个Web服务调用之后,我能够成功地将数据解析到垂直RecyclerView中。现在,我试图将第二个Web服务中的数据放入水平RecyclerView中。
为此,我实现了两个单独的RecyclerView适配器类。我不明白的是如何像在第一个Web服务调用中那样传递第二个Web服务调用中的数据,从而在OuterRecyclerView中定义InnerRecyclerView并使整个事情协同工作。我是一个初学者,感谢您的帮助。
MainActivity.java
public class MainActivity extends AppCompatActivity {
RequestQueue requestQueue ;
RequestQueue requestQueue2 ;
RecyclerView recyclerView;
RecyclerView.Adapter recyclerViewadapter;
List<OuterDataModel> DataAdapterClassList;
List<InnerOuterDataModel> InnerAdapterClassList;
LinearLayoutManager recyclerViewlayoutManager;
public String callIdForParse;
OuterDataModel GetOuterDataModel = new OuterDataModel();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
DataAdapterClassList = new ArrayList<>();
recyclerView = (RecyclerView) findViewById(R.id.recyclerView1);
recyclerViewlayoutManager = new LinearLayoutManager(this);
recyclerView.setLayoutManager(recyclerViewlayoutManager);
myurl = "http://pastebin.com/START";
OuterWebCall();
//Some actions then I call the second webservice
InnerWebCall();
}
public void OuterWebCall(){
String HTTP_SERVER_URL= String.format("http://pastebin.com/mySampleApi/student/%1$s",mStudentRoll);
JsonArrayRequest jsArrRequest = new JsonArrayRequest
(Request.Method.POST, HTTP_SERVER_URL, null, new Response.Listener<JSONArray>() {
@Override
public void onResponse(JSONArray response) {
//mTxtDisplay.setText("Response: " + response.toString());
AfterOuterWebCall(response);
Log.i(TAG, "Hello");
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
// TODO Auto-generated method stub
}
}){
};
requestQueue = Volley.newRequestQueue(this);
requestQueue.add(jsArrRequest);
}
public void AfterOuterWebCall(JSONArray array){
for(int i = 0; i<array.length(); i++) {
OuterDataModel GetOuterDataModel = new OuterDataModel();
JSONObject json = null;
try {
json = array.getJSONObject(i);
GetOuterDataModel.setId(json.getString("CLASS"));
GetOuterDataModel.setPlateNo(json.getString("HOUSE"));
GetOuterDataModel.setPlateCode(json.getString("IMAGEURL"));
} catch (JSONException e) {
e.printStackTrace();
}
DataAdapterClassList.add(GetOuterDataModel);
mSwipeRefreshLayout.setRefreshing(false);
}
if (array.length() != 0) {
recyclerViewadapter = new NewRecyclerViewAdapter(DataAdapterClassList, this);
recyclerView.setAdapter(recyclerViewadapter);
}
}
public void InnerWebCall(){
String HTTP_SERVER_URL= String.format("http://192.1.2.2/test");
JsonArrayRequest jsArrRequest = new JsonArrayRequest
(Request.Method.POST, HTTP_SERVER_URL, null, new Response.Listener<JSONArray>() {
@Override
public void onResponse(JSONArray response) {
//mTxtDisplay.setText("Response: " + response.toString());
AfterInnerWebCall(response);
Log.i(TAG, "Hello");
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
// TODO Auto-generated method stub
}
}){
};
requestQueue2 = Volley.newRequestQueue(this);
requestQueue2.add(jsArrRequest);
}
public void AfterInnerWebCall (JSONArray array){
for(int i = 0; i<array.length(); i++) {
InnerOuterDataModel GetInnerOuterDataModel = new InnerOuterDataModel();
JSONObject json = null;
try {
json = array.getJSONObject(i);
GetInnerOuterDataModel.setName(json.getString("Name"));
GetInnerOuterDataModel.setRank(json.getString("Rank"));
GetInnerOuterDataModel.setPerId(json.getString("ID"));
} catch (JSONException e) {
e.printStackTrace();
}
InnerAdapterClassList.add(GetInnerOuterDataModel);
}
if (array.length() != 0) {
// How to pass the data to the InnerRecyclerViewAdapter?
}
}
}
OuterRecyclerViewAdapter.java
public class OuterRecyclerViewAdapter extends RecyclerView.Adapter<OuterRecyclerViewAdapter.ViewHolder> {
Context context;
public List<DataModel> dataModels;
public List<PersonnelOnDutyDataModel> dataModels2;
public int mExpandedPosition=-1;
public OuterRecyclerViewAdapter(List<DataModel> getDataAdapter, Context context){
super();
this.dataModels = getDataAdapter;
this.context = context;
}
@Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.card_layout, parent, false);
ViewHolder viewHolder = new ViewHolder(view);
return viewHolder;
}
@Override
public void onBindViewHolder(final ViewHolder viewHolder, final int position) {
DataModel dataAdapter = dataModels.get(position);
RecyclerView.LayoutManager manager = new LinearLayoutManager(context);
viewHolder.InnerRecyclerView.setLayoutManager(manager);
viewHolder.class.setText(dataAdapter.getClass());
viewHolder.house.setText(dataAdapter.getHouse());
//Glide.with(context).load(dataAdapter.getImgUrl()).into(viewHolder.imageView)
final boolean isExpanded = position==mExpandedPosition;
// viewHolder.linearLayout.setVisibility(isExpanded?View.VISIBLE:View.GONE);
viewHolder.itemView.setActivated(isExpanded);
viewHolder.itemView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
mExpandedPosition = isExpanded ? -1:position;
//TransitionManager.beginDelayedTransition();
notifyDataSetChanged();
}
});
}
@Override
public int getItemCount() {
return dataModels.size();
}
class ViewHolder extends RecyclerView.ViewHolder{
public TextView class;
public TextView house;
public ImageView imageView;
private RecyclerView InnerRecyclerView;
ConstraintLayout constraintLayout;
public ViewHolder(View itemView) {
super(itemView);
InnerRecyclerView= (RecyclerView) itemView.findViewById(R.id.innerRecyclerView);
imageView = (ImageView) itemView.findViewById(R.id.imageView);
class=(TextView)itemView.findViewById(R.id.textViewClass);
house=(TextView)itemView.findViewById(R.id.textViewHouse);
constraintLayout=(ConstraintLayout)itemView.findViewById(R.id.constrainLayout);
}
}
}
InnerRecyclerView.java
public class InnerRecyclerViewAdapter extends RecyclerView.Adapter<InnerRecyclerViewAdapter.ViewHolder> {
Context context;
public List<InnerDataModel> dataModels;
public InnerRecyclerViewAdapter(List<InnerDataModel> getDataAdapter, Context context){
super();
this.dataModels = getDataAdapter;
this.context = context;
}
public int mExpandedPosition=-1;
@Override
public InnerRecyclerViewAdapter.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.personnel_on_duty_card, parent, false);
InnerRecyclerViewAdapter.ViewHolder viewHolder = new InnerRecyclerViewAdapter.ViewHolder(view);
return viewHolder;
}
@Override
public void onBindViewHolder(final InnerRecyclerViewAdapter.ViewHolder viewHolder, final int position) {
InnerDataModel dataAdapter = dataModels.get(position);
viewHolder.NameStudentCard.setText(dataAdapter.getName());
viewHolder.RankCard.setText(dataAdapter.getRank());
viewHolder.IDCard.setText(dataAdapter.getId());
final boolean isExpanded = position==mExpandedPosition;
viewHolder.itemView.setActivated(isExpanded);
viewHolder.itemView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
mExpandedPosition = isExpanded ? -1:position;
//TransitionManager.beginDelayedTransition();
notifyDataSetChanged();
}
});
}
@Override
public int getItemCount() {
return dataModels.size();
}
class ViewHolder extends RecyclerView.ViewHolder{
public TextView NameStudentCard;
public TextView RankCard;
public TextView IDCard;
ConstraintLayout constraintLayout;
public ViewHolder(View itemView) {
super(itemView);
NameStudentCard=(TextView)itemView.findViewById(R.id.textViewNameInCard);
RankCard=(TextView)itemView.findViewById(R.id.textViewrankInCard);
IDCard =(TextView)itemView.findViewById(R.id.IDInCard);
constraintLayout=(ConstraintLayout)itemView.findViewById(R.id.constrainLayout);
}
}
}
MainActivity.xml
<?xml version="1.0" encoding="utf-8"?>
<com.sothree.slidinguppanel.SlidingUpPanelLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:sothree="http://schemas.android.com/apk/res-auto"
android:id="@+id/sliding_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="bottom"
sothree:umanoPanelHeight="68dp"
sothree:umanoShadowHeight="4dp"
sothree:umanoParallaxOffset="100dp"
sothree:umanoOverlay="true"
sothree:umanoFadeColor="@android:color/transparent">
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center|top"
android:text=" Sliding Up Panel"
android:textSize="16sp" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#ffffff"
android:orientation="vertical"
android:clickable="true"
android:focusable="false"
android:id="@+id/dragView">
<android.support.v7.widget.RecyclerView
android:id="@+id/recyclerView1"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
/>
</LinearLayout>
</com.sothree.slidinguppanel.SlidingUpPanelLayout>
OuterRecyclerView.xml
<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.CardView 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/CardViewOuter"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginBottom="4dp"
android:layout_marginLeft="8dp"
android:layout_marginRight="8dp"
android:layout_marginTop="4dp"
card_view:cardCornerRadius="4dp"
android:foreground="?attr/selectableItemBackground"
app:cardElevation="2dp"
android:transitionName="weatherCardTransition"
>
<android.support.constraint.ConstraintLayout
android:id="@+id/constrainLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_constraintBottom_toBottomOf="@+id/textViewStatus"
app:layout_constraintTop_toBottomOf="@+id/textViewStatus">
<TextView
android:id="@+id/textViewClass"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginStart="8dp"
android:layout_marginTop="16dp"
android:text="Class"
android:textColor="@color/card_text_primary"
android:textSize="36sp"
android:textStyle="bold"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<TextView
android:id="@+id/textViewHose"
android:layout_width="wrap_content"
android:layout_height="28dp"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
android:text="@string/card_header_status"
android:textSize="18sp"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/textViewCalass" />
<android.support.v7.widget.RecyclerView
android:layout_width="352dp"
android:layout_height="134dp"
android:layout_marginTop="8dp"
android:layout_marginEnd="8dp"
android:id="@+id/innerRecyclerView"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toBottomOf="@+id/textViewCalass" />
</android.support.constraint.ConstraintLayout>
</android.support.v7.widget.CardView>
InnerCard.xml
<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.CardView 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/cardViewInnerCard"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="4dp"
android:layout_marginLeft="8dp"
android:layout_marginRight="8dp"
android:layout_marginTop="4dp"
card_view:cardCornerRadius="4dp"
android:foreground="?attr/selectableItemBackground"
app:cardElevation="2dp"
android:transitionName="weatherCardTransition"
>
<android.support.constraint.ConstraintLayout
android:layout_width="200dp"
android:layout_height="100dp">
<TextView
android:id="@+id/textViewNameInCard"
android:layout_width="wrap_content"
android:layout_height="19dp"
android:layout_marginTop="8dp"
android:layout_marginEnd="8dp"
android:textStyle="bold"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<TextView
android:id="@+id/textViewrankInCard"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="8dp"
android:layout_marginEnd="8dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toBottomOf="@+id/textViewNameInCard" />
<TextView
android:id="@+id/IDInCard"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="8dp"
android:layout_marginEnd="8dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toBottomOf="@+id/textViewrankInCard" />
<ImageButton
android:id="@+id/imageButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="8dp"
android:background="@null"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:srcCompat="@drawable/ic_more_vert_black_24dp" />
</android.support.constraint.ConstraintLayout>