在 ExpandableListview 的 child 下面创建 Recyclerview 时,它的位置被弄乱了
On Creating Recyclerview on below the child of ExpandableListview its position is messed up
嘿伙计们,在搜索了这么多时间后终于帮帮我,我发布了这个我创建了 expandableListView,它工作正常,但我想创建它三级所以我尝试了一种方法 我在 child 可扩展视图中定义的 recyclerview 像这样会设置 recyclerview 可见性消失或可见 以前在回收站上尝试过但在这个 中没有效果或者伙计们告诉我创建 3 的最佳方法前两个用于导航的级别可扩展列表视图或回收器是静态的,最后一个是动态的,前两个-:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:android="http://schemas.android.com/apk/res/android">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="40dp"
app:srcCompat="@drawable/alarm_clock_24dp"
android:id="@+id/child_icon"
/>
<TextView
android:id="@+id/childItem"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:hint="gfchgc"
android:layout_toEndOf="@+id/child_icon"
android:textAppearance="?android:attr/textAppearanceMedium" />
<ImageView
android:id="@+id/drp_icon"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentEnd="true"
android:layout_marginEnd="40dp"
app:srcCompat="@drawable/chevron_right_black_24dp" />
<android.support.v7.widget.RecyclerView
android:layout_height="300dp"
android:layout_marginStart="40dp"
android:layout_width="match_parent"
android:layout_below="@id/drp_icon"
android:id="@+id/rec_childsubcat"/>
</RelativeLayout>
但在获取价值时,我的回收商在第二位显示数据 child
这是我的适配器-:
public class CustomAdapter extends BaseExpandableListAdapter {
private Context context;
AppCompatActivity activity;
private ArrayList<GroupInfo> deptList;
private String API = "***";
//chidsub category items defined
public static List<ChildSubCatModel> childSubCatModelList;
private ChildSubCatAdapter childSubCatAdapter;
public CustomAdapter(Context context, ArrayList<GroupInfo> deptList) {
this.context = context;
this.deptList = deptList;
}
@Override
public Object getChild(int groupPosition, int childPosition) {
ArrayList<ChildInfo> productList = deptList.get(groupPosition).getProductList();
return productList.get(childPosition);
}
@Override
public int getChildType(int groupPosition, int childPosition) {
return super.getChildType(groupPosition, childPosition);
}
@Override
public long getChildId(int groupPosition, int childPosition) {
return childPosition;
}
@Override
public View getChildView(int groupPosition, final int childPosition, boolean isLastChild,
View view, ViewGroup parent) {
/*final int tempgroupPos = groupPosition;
final int tempchildPos = childPosition;
final ChildInfo detailInfo = (ChildInfo) getChild(groupPosition, childPosition);
View row = view;
if (row == null)
{LayoutInflater infalInflater = (LayoutInflater) this.context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
row = infalInflater.inflate(R.layout.child_items, parent, false);
TextView tvb = (TextView) row.findViewById(R.id.childItem);
RecyclerView mRecyclerChildSubCat = (RecyclerView) row.findViewById(R.id.rec_childsubcat);
ChildViewHolder cholder = new ChildViewHolder();
cholder.addView(tvb);
cholder.addView(mRecyclerChildSubCat);
row.setTag(cholder);
}
ChildViewHolder cholder = (ChildViewHolder) row.getTag();
TextView tvb = (TextView) cholder.getView(R.id.childItem);
RecyclerView mRecyclerChildSubCat = (RecyclerView) cholder.getView(R.id.rec_childsubcat);
mRecyclerChildSubCat.setLayoutManager(new LinearLayoutManager(context));
// mRecyclerChildSubCat.addItemDecoration(new GridSpacingItemDecoration(2, 1, true));
mRecyclerChildSubCat.setNestedScrollingEnabled(false);
childSubCatModelList = new ArrayList<>();
childSubCatAdapter = new ChildSubCatAdapter(context, childSubCatModelList);
mRecyclerChildSubCat.setAdapter(childSubCatAdapter);
*//*tvb.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
fetchNavigation(detailInfo.getHeadername(),detailInfo.getId());
Toast.makeText(context,childPosition+"",Toast.LENGTH_SHORT).show();
}
});*//*
tvb.setText(detailInfo.getName());*/
// return row;
final ChildInfo detailInfo = (ChildInfo) getChild(groupPosition, childPosition);
if (view == null) {
LayoutInflater infalInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
view = infalInflater.inflate(R.layout.child_items, null);
}
RecyclerView mRecyclerChildSubCat = (RecyclerView) view.findViewById(R.id.rec_childsubcat);
mRecyclerChildSubCat.setLayoutManager(new LinearLayoutManager(context));
// mRecyclerChildSubCat.addItemDecoration(new GridSpacingItemDecoration(2, 1, true));
mRecyclerChildSubCat.setNestedScrollingEnabled(false);
childSubCatModelList = new ArrayList<>();
childSubCatAdapter = new ChildSubCatAdapter(context, childSubCatModelList);
mRecyclerChildSubCat.setAdapter(childSubCatAdapter);
TextView childItem = (TextView) view.findViewById(R.id.childItem);
childItem.setText(detailInfo.getName().trim());
ImageView iv_childicon = (ImageView) view.findViewById(R.id.child_icon);
iv_childicon.setImageResource(detailInfo.getDrawable());
//title.setText(getChild(groupPosition, childPosition).getVenue_item_name());
childItem.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
fetchNavigation(detailInfo.getHeadername(),detailInfo.getId());
}
});
return view;
}
@Override
public int getChildrenCount(int groupPosition) {
ArrayList<ChildInfo> productList = deptList.get(groupPosition).getProductList();
return productList.size();
}
@Override
public Object getGroup(int groupPosition) {
return deptList.get(groupPosition);
}
@Override
public int getGroupCount() {
return deptList.size();
}
@Override
public long getGroupId(int groupPosition) {
return groupPosition;
}
@Override
public View getGroupView(int groupPosition, boolean isLastChild, View view,
ViewGroup parent) {
GroupInfo headerInfo = (GroupInfo) getGroup(groupPosition);
if (view == null) {
LayoutInflater inf = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
view = inf.inflate(R.layout.group_items, null);
}
TextView heading = (TextView) view.findViewById(R.id.heading);
heading.setText(headerInfo.getName().trim());
ImageView headerIcon = (ImageView) view.findViewById(R.id.header_icon);
headerIcon.setImageResource(headerInfo.getDrawable());
ImageView imageView = (ImageView) view.findViewById(R.id.expand_icon);
if(isLastChild){
imageView.setImageResource(R.drawable.expand_more_black_24dp);
}else {
imageView.setImageResource(R.drawable.chevron_right_black_24dp);
}
return view;
}
@Override
public boolean hasStableIds() {
return true;
}
@Override
public boolean isChildSelectable(int groupPosition, int childPosition) {
return true;
}
public void fetchNavigation(final String headerName, final String categoryid) {
StringRequest stringRequest = new StringRequest(Request.Method.GET, API,
new Response.Listener<String>() {
@Override
public void onResponse(String response) {
//Toast.makeText(getApplicationContext(),response,Toast.LENGTH_SHORT).show();
try {
childSubCatModelList.clear();
Log.d("TEST", "onResponse: " + response);
// Toast.makeText(getActivity(),response,Toast.LENGTH_SHORT).show();
JSONObject jsonRootObject = new JSONObject(response);
String error = jsonRootObject.getString("error");
//Toast.makeText(getApplicationContext(), error, Toast.LENGTH_SHORT).show();
JSONArray jsonArray = jsonRootObject.getJSONArray("navs");
if (error.trim().equalsIgnoreCase("0")) {
for (int i = 0; i < jsonArray.length(); i++) {
JSONObject jsonObject = jsonArray.getJSONObject(i);
String category = jsonObject.optString("category");
//Toast.makeText(getApplicationContext(),category,Toast.LENGTH_LONG).show();
if (category.equalsIgnoreCase(headerName)) {
JSONObject jsonnew = jsonArray.getJSONObject(i);
JSONArray jsonarrayROW = jsonnew.getJSONArray("child");
for (int j = 0; j < jsonarrayROW.length(); j++) {
JSONObject jsonObjectRow = jsonarrayROW.getJSONObject(j);
String category_id = jsonObjectRow.optString("category_id");
//Toast.makeText(getApplicationContext(),category_id,Toast.LENGTH_SHORT).show();
if (category_id.equalsIgnoreCase(categoryid)) {
JSONObject jsonnew1 = jsonarrayROW.getJSONObject(j);
JSONArray jsonarrayChild = jsonnew1.getJSONArray("child");
for (int k = 0; k < jsonarrayChild.length(); k++) {
JSONObject jsonObjectRow1 = jsonarrayChild.getJSONObject(k);
String category_id1 = jsonObjectRow1.optString("category_id");
String category_name = jsonObjectRow1.optString("category");
childSubCatModelList.add(new ChildSubCatModel(category_name,category_id));
//Toast.makeText(context,category_name,Toast.LENGTH_SHORT).show();
}
}
}
}
}
}
childSubCatAdapter.notifyDataSetChanged();
} catch (JSONException e) {
e.printStackTrace();
}
}
},
new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
Log.e("Volley", "Error");
}
}
);
RetryPolicy retryPolicy = new DefaultRetryPolicy(30000, 0, DefaultRetryPolicy.DEFAULT_BACKOFF_MULT);
stringRequest.setRetryPolicy(retryPolicy);
VolleySingleton.getInstance(context).addToRequestQueue(stringRequest);
}
/* static class ChildViewHolder {
private HashMap<Integer, View> storedViews = new HashMap<Integer, View>();
public ChildViewHolder addView(View view)
{
int id = view.getId();
storedViews.put(id, view);
return this;
}
public View getView(int id)
{
return storedViews.get(id);
}
}*/
}
包装内容后,recyclerview 只有一个 recyclershows 链接到笔记本电脑配件和值更新,但它在适配器中声明,因此它应该显示在每个 child 下方
得到答案我没有以系统的方式定义回收器做了一些改变并让它工作..创建了 2 级可扩展到 3 级
这是我最新的列表项 xml
<android.support.v7.widget.CardView
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:card_view="http://schemas.android.com/apk/res-auto"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="wrap_content"
card_view:cardUseCompatPadding="true">
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<LinearLayout
android:id="@+id/ll_resource"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center_vertical"
android:orientation="horizontal"
android:padding="20dp"
android:weightSum="10">
<ImageView
android:id="@+id/iv_forward"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="0.3"
app:srcCompat="@drawable/fiber_manual_record_black_24dp" />
<TextView
android:id="@+id/name_resources"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginStart="20dp"
android:layout_weight="9"
android:hint=" Here is Resources heading. "
android:textColor="@color/black"
android:textColorHint="@color/black"
android:textSize="17sp"
android:typeface="monospace" />
<ImageView
android:id="@+id/iv_dropdown"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="0.7" />
</LinearLayout>
<android.support.v7.widget.RecyclerView
android:id="@+id/rec_reschild"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/ll_resource"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:visibility="gone" />
</RelativeLayout>
</android.support.v7.widget.CardView>
并将适配器字符串请求更改为查看持有者,而不是在方法中调用:
像这样-:
public class CustomAdapter extends BaseExpandableListAdapter {
private Context context;
AppCompatActivity activity;
private ArrayList<GroupInfo> deptList;
private String API = "your Api";
//chidsub category items defined
public CustomAdapter(Context context, ArrayList<GroupInfo> deptList) {
this.context = context;
this.deptList = deptList;
}
@Override
public Object getChild(int groupPosition, int childPosition) {
ArrayList<ChildInfo> productList = deptList.get(groupPosition).getProductList();
return productList.get(childPosition);
}
@Override
public int getChildType(int groupPosition, int childPosition) {
return super.getChildType(groupPosition, childPosition);
}
@Override
public long getChildId(int groupPosition, int childPosition) {
return childPosition;
}
@Override
public View getChildView(int groupPosition, final int childPosition, boolean isLastChild,
View view, ViewGroup parent) {
/*final int tempgroupPos = groupPosition;
final int tempchildPos = childPosition;
final ChildInfo detailInfo = (ChildInfo) getChild(groupPosition, childPosition);
View row = view;
if (row == null)
{LayoutInflater infalInflater = (LayoutInflater) this.context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
row = infalInflater.inflate(R.layout.child_items, parent, false);
TextView tvb = (TextView) row.findViewById(R.id.childItem);
RecyclerView mRecyclerChildSubCat = (RecyclerView) row.findViewById(R.id.rec_childsubcat);
ChildViewHolder cholder = new ChildViewHolder();
cholder.addView(tvb);
cholder.addView(mRecyclerChildSubCat);
row.setTag(cholder);
}
ChildViewHolder cholder = (ChildViewHolder) row.getTag();
TextView tvb = (TextView) cholder.getView(R.id.childItem);
RecyclerView mRecyclerChildSubCat = (RecyclerView) cholder.getView(R.id.rec_childsubcat);
mRecyclerChildSubCat.setLayoutManager(new LinearLayoutManager(context));
// mRecyclerChildSubCat.addItemDecoration(new GridSpacingItemDecoration(2, 1, true));
mRecyclerChildSubCat.setNestedScrollingEnabled(false);
childSubCatModelList = new ArrayList<>();
childSubCatAdapter = new ChildSubCatAdapter(context, childSubCatModelList);
mRecyclerChildSubCat.setAdapter(childSubCatAdapter);
*//*tvb.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
fetchNavigation(detailInfo.getHeadername(),detailInfo.getId());
Toast.makeText(context,childPosition+"",Toast.LENGTH_SHORT).show();
}
});*//*
tvb.setText(detailInfo.getName());*/
// return row;
final ChildInfo detailInfo = (ChildInfo) getChild(groupPosition, childPosition);
final List<ChildSubCatModel> childSubCatModelList;
final ChildSubCatAdapter childSubCatAdapter;
if (view == null) {
LayoutInflater infalInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
view = infalInflater.inflate(R.layout.cardview_resources, null);
}
final RecyclerView mRecyclerChildSubCat = (RecyclerView) view.findViewById(R.id.rec_reschild);
mRecyclerChildSubCat.setLayoutManager(new LinearLayoutManager(context));
// mRecyclerChildSubCat.addItemDecoration(new GridSpacingItemDecoration(2, 1, true));
mRecyclerChildSubCat.setNestedScrollingEnabled(false);
childSubCatModelList = new ArrayList<>();
childSubCatAdapter = new ChildSubCatAdapter(context, childSubCatModelList);
mRecyclerChildSubCat.setAdapter(childSubCatAdapter);
TextView childItem = (TextView) view.findViewById(R.id.name_resources);
childItem.setText(detailInfo.getName().trim());
//ImageView iv_childicon = (ImageView) view.findViewById(R.id.child_icon);
// iv_childicon.setImageResource(detailInfo.getDrawable());
//title.setText(getChild(groupPosition, childPosition).getVenue_item_name());
childItem.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
StringRequest stringRequest = new StringRequest(Request.Method.GET, API,
new Response.Listener<String>() {
@Override
public void onResponse(String response) {
mRecyclerChildSubCat.setVisibility(View.VISIBLE);
//Toast.makeText(getApplicationContext(),response,Toast.LENGTH_SHORT).show();
try {
childSubCatModelList.clear();
Log.d("TEST", "onResponse: " + response);
// Toast.makeText(getActivity(),response,Toast.LENGTH_SHORT).show();
JSONObject jsonRootObject = new JSONObject(response);
String error = jsonRootObject.getString("error");
//Toast.makeText(getApplicationContext(), error, Toast.LENGTH_SHORT).show();
JSONArray jsonArray = jsonRootObject.getJSONArray("navs");
if (error.trim().equalsIgnoreCase("0")) {
for (int i = 0; i < jsonArray.length(); i++) {
JSONObject jsonObject = jsonArray.getJSONObject(i);
String category = jsonObject.optString("category");
//Toast.makeText(getApplicationContext(),category,Toast.LENGTH_LONG).show();
if (category.equalsIgnoreCase(detailInfo.getHeadername())) {
JSONObject jsonnew = jsonArray.getJSONObject(i);
JSONArray jsonarrayROW = jsonnew.getJSONArray("child");
for (int j = 0; j < jsonarrayROW.length(); j++) {
JSONObject jsonObjectRow = jsonarrayROW.getJSONObject(j);
String category_id = jsonObjectRow.optString("category_id");
//Toast.makeText(getApplicationContext(),category_id,Toast.LENGTH_SHORT).show();
if (category_id.equalsIgnoreCase(detailInfo.getId())) {
JSONObject jsonnew1 = jsonarrayROW.getJSONObject(j);
JSONArray jsonarrayChild = jsonnew1.getJSONArray("child");
for (int k = 0; k < jsonarrayChild.length(); k++) {
JSONObject jsonObjectRow1 = jsonarrayChild.getJSONObject(k);
String category_id1 = jsonObjectRow1.optString("category_id");
String category_name = jsonObjectRow1.optString("category");
childSubCatModelList.add(new ChildSubCatModel(category_name,category_id));
//Toast.makeText(context,category_name,Toast.LENGTH_SHORT).show();
}
}
}
}
}
}
childSubCatAdapter.notifyDataSetChanged();
} catch (JSONException e) {
e.printStackTrace();
}
}
},
new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
Log.e("Volley", "Error");
}
}
);
RetryPolicy retryPolicy = new DefaultRetryPolicy(30000, 0, DefaultRetryPolicy.DEFAULT_BACKOFF_MULT);
stringRequest.setRetryPolicy(retryPolicy);
VolleySingleton.getInstance(context).addToRequestQueue(stringRequest);
}
});
return view;
}
@Override
public int getChildrenCount(int groupPosition) {
ArrayList<ChildInfo> productList = deptList.get(groupPosition).getProductList();
return productList.size();
}
@Override
public Object getGroup(int groupPosition) {
return deptList.get(groupPosition);
}
@Override
public int getGroupCount() {
return deptList.size();
}
@Override
public long getGroupId(int groupPosition) {
return groupPosition;
}
@Override
public View getGroupView(int groupPosition, boolean isLastChild, View view,
ViewGroup parent) {
GroupInfo headerInfo = (GroupInfo) getGroup(groupPosition);
if (view == null) {
LayoutInflater inf = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
view = inf.inflate(R.layout.group_items, null);
}
TextView heading = (TextView) view.findViewById(R.id.heading);
heading.setText(headerInfo.getName().trim());
ImageView headerIcon = (ImageView) view.findViewById(R.id.header_icon);
headerIcon.setImageResource(headerInfo.getDrawable());
ImageView imageView = (ImageView) view.findViewById(R.id.expand_icon);
if(isLastChild){
imageView.setImageResource(R.drawable.expand_more_black_24dp);
}else {
imageView.setImageResource(R.drawable.chevron_right_black_24dp);
}
return view;
}
@Override
public boolean hasStableIds() {
return true;
}
@Override
public boolean isChildSelectable(int groupPosition, int childPosition) {
return true;
}
public void fetchNavigation(final String headerName, final String categoryid) {
}
/* static class ChildViewHolder {
private HashMap<Integer, View> storedViews = new HashMap<Integer, View>();
public ChildViewHolder addView(View view)
{
int id = view.getId();
storedViews.put(id, view);
return this;
}
public View getView(int id)
{
return storedViews.get(id);
}
}*/
}
得到我需要的输出-:
嘿伙计们,在搜索了这么多时间后终于帮帮我,我发布了这个我创建了 expandableListView,它工作正常,但我想创建它三级所以我尝试了一种方法 我在 child 可扩展视图中定义的 recyclerview 像这样会设置 recyclerview 可见性消失或可见 以前在回收站上尝试过但在这个 中没有效果或者伙计们告诉我创建 3 的最佳方法前两个用于导航的级别可扩展列表视图或回收器是静态的,最后一个是动态的,前两个-:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:android="http://schemas.android.com/apk/res/android">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="40dp"
app:srcCompat="@drawable/alarm_clock_24dp"
android:id="@+id/child_icon"
/>
<TextView
android:id="@+id/childItem"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:hint="gfchgc"
android:layout_toEndOf="@+id/child_icon"
android:textAppearance="?android:attr/textAppearanceMedium" />
<ImageView
android:id="@+id/drp_icon"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentEnd="true"
android:layout_marginEnd="40dp"
app:srcCompat="@drawable/chevron_right_black_24dp" />
<android.support.v7.widget.RecyclerView
android:layout_height="300dp"
android:layout_marginStart="40dp"
android:layout_width="match_parent"
android:layout_below="@id/drp_icon"
android:id="@+id/rec_childsubcat"/>
</RelativeLayout>
但在获取价值时,我的回收商在第二位显示数据 child
这是我的适配器-:
public class CustomAdapter extends BaseExpandableListAdapter {
private Context context;
AppCompatActivity activity;
private ArrayList<GroupInfo> deptList;
private String API = "***";
//chidsub category items defined
public static List<ChildSubCatModel> childSubCatModelList;
private ChildSubCatAdapter childSubCatAdapter;
public CustomAdapter(Context context, ArrayList<GroupInfo> deptList) {
this.context = context;
this.deptList = deptList;
}
@Override
public Object getChild(int groupPosition, int childPosition) {
ArrayList<ChildInfo> productList = deptList.get(groupPosition).getProductList();
return productList.get(childPosition);
}
@Override
public int getChildType(int groupPosition, int childPosition) {
return super.getChildType(groupPosition, childPosition);
}
@Override
public long getChildId(int groupPosition, int childPosition) {
return childPosition;
}
@Override
public View getChildView(int groupPosition, final int childPosition, boolean isLastChild,
View view, ViewGroup parent) {
/*final int tempgroupPos = groupPosition;
final int tempchildPos = childPosition;
final ChildInfo detailInfo = (ChildInfo) getChild(groupPosition, childPosition);
View row = view;
if (row == null)
{LayoutInflater infalInflater = (LayoutInflater) this.context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
row = infalInflater.inflate(R.layout.child_items, parent, false);
TextView tvb = (TextView) row.findViewById(R.id.childItem);
RecyclerView mRecyclerChildSubCat = (RecyclerView) row.findViewById(R.id.rec_childsubcat);
ChildViewHolder cholder = new ChildViewHolder();
cholder.addView(tvb);
cholder.addView(mRecyclerChildSubCat);
row.setTag(cholder);
}
ChildViewHolder cholder = (ChildViewHolder) row.getTag();
TextView tvb = (TextView) cholder.getView(R.id.childItem);
RecyclerView mRecyclerChildSubCat = (RecyclerView) cholder.getView(R.id.rec_childsubcat);
mRecyclerChildSubCat.setLayoutManager(new LinearLayoutManager(context));
// mRecyclerChildSubCat.addItemDecoration(new GridSpacingItemDecoration(2, 1, true));
mRecyclerChildSubCat.setNestedScrollingEnabled(false);
childSubCatModelList = new ArrayList<>();
childSubCatAdapter = new ChildSubCatAdapter(context, childSubCatModelList);
mRecyclerChildSubCat.setAdapter(childSubCatAdapter);
*//*tvb.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
fetchNavigation(detailInfo.getHeadername(),detailInfo.getId());
Toast.makeText(context,childPosition+"",Toast.LENGTH_SHORT).show();
}
});*//*
tvb.setText(detailInfo.getName());*/
// return row;
final ChildInfo detailInfo = (ChildInfo) getChild(groupPosition, childPosition);
if (view == null) {
LayoutInflater infalInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
view = infalInflater.inflate(R.layout.child_items, null);
}
RecyclerView mRecyclerChildSubCat = (RecyclerView) view.findViewById(R.id.rec_childsubcat);
mRecyclerChildSubCat.setLayoutManager(new LinearLayoutManager(context));
// mRecyclerChildSubCat.addItemDecoration(new GridSpacingItemDecoration(2, 1, true));
mRecyclerChildSubCat.setNestedScrollingEnabled(false);
childSubCatModelList = new ArrayList<>();
childSubCatAdapter = new ChildSubCatAdapter(context, childSubCatModelList);
mRecyclerChildSubCat.setAdapter(childSubCatAdapter);
TextView childItem = (TextView) view.findViewById(R.id.childItem);
childItem.setText(detailInfo.getName().trim());
ImageView iv_childicon = (ImageView) view.findViewById(R.id.child_icon);
iv_childicon.setImageResource(detailInfo.getDrawable());
//title.setText(getChild(groupPosition, childPosition).getVenue_item_name());
childItem.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
fetchNavigation(detailInfo.getHeadername(),detailInfo.getId());
}
});
return view;
}
@Override
public int getChildrenCount(int groupPosition) {
ArrayList<ChildInfo> productList = deptList.get(groupPosition).getProductList();
return productList.size();
}
@Override
public Object getGroup(int groupPosition) {
return deptList.get(groupPosition);
}
@Override
public int getGroupCount() {
return deptList.size();
}
@Override
public long getGroupId(int groupPosition) {
return groupPosition;
}
@Override
public View getGroupView(int groupPosition, boolean isLastChild, View view,
ViewGroup parent) {
GroupInfo headerInfo = (GroupInfo) getGroup(groupPosition);
if (view == null) {
LayoutInflater inf = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
view = inf.inflate(R.layout.group_items, null);
}
TextView heading = (TextView) view.findViewById(R.id.heading);
heading.setText(headerInfo.getName().trim());
ImageView headerIcon = (ImageView) view.findViewById(R.id.header_icon);
headerIcon.setImageResource(headerInfo.getDrawable());
ImageView imageView = (ImageView) view.findViewById(R.id.expand_icon);
if(isLastChild){
imageView.setImageResource(R.drawable.expand_more_black_24dp);
}else {
imageView.setImageResource(R.drawable.chevron_right_black_24dp);
}
return view;
}
@Override
public boolean hasStableIds() {
return true;
}
@Override
public boolean isChildSelectable(int groupPosition, int childPosition) {
return true;
}
public void fetchNavigation(final String headerName, final String categoryid) {
StringRequest stringRequest = new StringRequest(Request.Method.GET, API,
new Response.Listener<String>() {
@Override
public void onResponse(String response) {
//Toast.makeText(getApplicationContext(),response,Toast.LENGTH_SHORT).show();
try {
childSubCatModelList.clear();
Log.d("TEST", "onResponse: " + response);
// Toast.makeText(getActivity(),response,Toast.LENGTH_SHORT).show();
JSONObject jsonRootObject = new JSONObject(response);
String error = jsonRootObject.getString("error");
//Toast.makeText(getApplicationContext(), error, Toast.LENGTH_SHORT).show();
JSONArray jsonArray = jsonRootObject.getJSONArray("navs");
if (error.trim().equalsIgnoreCase("0")) {
for (int i = 0; i < jsonArray.length(); i++) {
JSONObject jsonObject = jsonArray.getJSONObject(i);
String category = jsonObject.optString("category");
//Toast.makeText(getApplicationContext(),category,Toast.LENGTH_LONG).show();
if (category.equalsIgnoreCase(headerName)) {
JSONObject jsonnew = jsonArray.getJSONObject(i);
JSONArray jsonarrayROW = jsonnew.getJSONArray("child");
for (int j = 0; j < jsonarrayROW.length(); j++) {
JSONObject jsonObjectRow = jsonarrayROW.getJSONObject(j);
String category_id = jsonObjectRow.optString("category_id");
//Toast.makeText(getApplicationContext(),category_id,Toast.LENGTH_SHORT).show();
if (category_id.equalsIgnoreCase(categoryid)) {
JSONObject jsonnew1 = jsonarrayROW.getJSONObject(j);
JSONArray jsonarrayChild = jsonnew1.getJSONArray("child");
for (int k = 0; k < jsonarrayChild.length(); k++) {
JSONObject jsonObjectRow1 = jsonarrayChild.getJSONObject(k);
String category_id1 = jsonObjectRow1.optString("category_id");
String category_name = jsonObjectRow1.optString("category");
childSubCatModelList.add(new ChildSubCatModel(category_name,category_id));
//Toast.makeText(context,category_name,Toast.LENGTH_SHORT).show();
}
}
}
}
}
}
childSubCatAdapter.notifyDataSetChanged();
} catch (JSONException e) {
e.printStackTrace();
}
}
},
new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
Log.e("Volley", "Error");
}
}
);
RetryPolicy retryPolicy = new DefaultRetryPolicy(30000, 0, DefaultRetryPolicy.DEFAULT_BACKOFF_MULT);
stringRequest.setRetryPolicy(retryPolicy);
VolleySingleton.getInstance(context).addToRequestQueue(stringRequest);
}
/* static class ChildViewHolder {
private HashMap<Integer, View> storedViews = new HashMap<Integer, View>();
public ChildViewHolder addView(View view)
{
int id = view.getId();
storedViews.put(id, view);
return this;
}
public View getView(int id)
{
return storedViews.get(id);
}
}*/
}
包装内容后,recyclerview 只有一个 recyclershows 链接到笔记本电脑配件和值更新,但它在适配器中声明,因此它应该显示在每个 child 下方
得到答案我没有以系统的方式定义回收器做了一些改变并让它工作..创建了 2 级可扩展到 3 级 这是我最新的列表项 xml
<android.support.v7.widget.CardView
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:card_view="http://schemas.android.com/apk/res-auto"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="wrap_content"
card_view:cardUseCompatPadding="true">
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<LinearLayout
android:id="@+id/ll_resource"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center_vertical"
android:orientation="horizontal"
android:padding="20dp"
android:weightSum="10">
<ImageView
android:id="@+id/iv_forward"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="0.3"
app:srcCompat="@drawable/fiber_manual_record_black_24dp" />
<TextView
android:id="@+id/name_resources"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginStart="20dp"
android:layout_weight="9"
android:hint=" Here is Resources heading. "
android:textColor="@color/black"
android:textColorHint="@color/black"
android:textSize="17sp"
android:typeface="monospace" />
<ImageView
android:id="@+id/iv_dropdown"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="0.7" />
</LinearLayout>
<android.support.v7.widget.RecyclerView
android:id="@+id/rec_reschild"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/ll_resource"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:visibility="gone" />
</RelativeLayout>
</android.support.v7.widget.CardView>
并将适配器字符串请求更改为查看持有者,而不是在方法中调用: 像这样-:
public class CustomAdapter extends BaseExpandableListAdapter {
private Context context;
AppCompatActivity activity;
private ArrayList<GroupInfo> deptList;
private String API = "your Api";
//chidsub category items defined
public CustomAdapter(Context context, ArrayList<GroupInfo> deptList) {
this.context = context;
this.deptList = deptList;
}
@Override
public Object getChild(int groupPosition, int childPosition) {
ArrayList<ChildInfo> productList = deptList.get(groupPosition).getProductList();
return productList.get(childPosition);
}
@Override
public int getChildType(int groupPosition, int childPosition) {
return super.getChildType(groupPosition, childPosition);
}
@Override
public long getChildId(int groupPosition, int childPosition) {
return childPosition;
}
@Override
public View getChildView(int groupPosition, final int childPosition, boolean isLastChild,
View view, ViewGroup parent) {
/*final int tempgroupPos = groupPosition;
final int tempchildPos = childPosition;
final ChildInfo detailInfo = (ChildInfo) getChild(groupPosition, childPosition);
View row = view;
if (row == null)
{LayoutInflater infalInflater = (LayoutInflater) this.context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
row = infalInflater.inflate(R.layout.child_items, parent, false);
TextView tvb = (TextView) row.findViewById(R.id.childItem);
RecyclerView mRecyclerChildSubCat = (RecyclerView) row.findViewById(R.id.rec_childsubcat);
ChildViewHolder cholder = new ChildViewHolder();
cholder.addView(tvb);
cholder.addView(mRecyclerChildSubCat);
row.setTag(cholder);
}
ChildViewHolder cholder = (ChildViewHolder) row.getTag();
TextView tvb = (TextView) cholder.getView(R.id.childItem);
RecyclerView mRecyclerChildSubCat = (RecyclerView) cholder.getView(R.id.rec_childsubcat);
mRecyclerChildSubCat.setLayoutManager(new LinearLayoutManager(context));
// mRecyclerChildSubCat.addItemDecoration(new GridSpacingItemDecoration(2, 1, true));
mRecyclerChildSubCat.setNestedScrollingEnabled(false);
childSubCatModelList = new ArrayList<>();
childSubCatAdapter = new ChildSubCatAdapter(context, childSubCatModelList);
mRecyclerChildSubCat.setAdapter(childSubCatAdapter);
*//*tvb.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
fetchNavigation(detailInfo.getHeadername(),detailInfo.getId());
Toast.makeText(context,childPosition+"",Toast.LENGTH_SHORT).show();
}
});*//*
tvb.setText(detailInfo.getName());*/
// return row;
final ChildInfo detailInfo = (ChildInfo) getChild(groupPosition, childPosition);
final List<ChildSubCatModel> childSubCatModelList;
final ChildSubCatAdapter childSubCatAdapter;
if (view == null) {
LayoutInflater infalInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
view = infalInflater.inflate(R.layout.cardview_resources, null);
}
final RecyclerView mRecyclerChildSubCat = (RecyclerView) view.findViewById(R.id.rec_reschild);
mRecyclerChildSubCat.setLayoutManager(new LinearLayoutManager(context));
// mRecyclerChildSubCat.addItemDecoration(new GridSpacingItemDecoration(2, 1, true));
mRecyclerChildSubCat.setNestedScrollingEnabled(false);
childSubCatModelList = new ArrayList<>();
childSubCatAdapter = new ChildSubCatAdapter(context, childSubCatModelList);
mRecyclerChildSubCat.setAdapter(childSubCatAdapter);
TextView childItem = (TextView) view.findViewById(R.id.name_resources);
childItem.setText(detailInfo.getName().trim());
//ImageView iv_childicon = (ImageView) view.findViewById(R.id.child_icon);
// iv_childicon.setImageResource(detailInfo.getDrawable());
//title.setText(getChild(groupPosition, childPosition).getVenue_item_name());
childItem.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
StringRequest stringRequest = new StringRequest(Request.Method.GET, API,
new Response.Listener<String>() {
@Override
public void onResponse(String response) {
mRecyclerChildSubCat.setVisibility(View.VISIBLE);
//Toast.makeText(getApplicationContext(),response,Toast.LENGTH_SHORT).show();
try {
childSubCatModelList.clear();
Log.d("TEST", "onResponse: " + response);
// Toast.makeText(getActivity(),response,Toast.LENGTH_SHORT).show();
JSONObject jsonRootObject = new JSONObject(response);
String error = jsonRootObject.getString("error");
//Toast.makeText(getApplicationContext(), error, Toast.LENGTH_SHORT).show();
JSONArray jsonArray = jsonRootObject.getJSONArray("navs");
if (error.trim().equalsIgnoreCase("0")) {
for (int i = 0; i < jsonArray.length(); i++) {
JSONObject jsonObject = jsonArray.getJSONObject(i);
String category = jsonObject.optString("category");
//Toast.makeText(getApplicationContext(),category,Toast.LENGTH_LONG).show();
if (category.equalsIgnoreCase(detailInfo.getHeadername())) {
JSONObject jsonnew = jsonArray.getJSONObject(i);
JSONArray jsonarrayROW = jsonnew.getJSONArray("child");
for (int j = 0; j < jsonarrayROW.length(); j++) {
JSONObject jsonObjectRow = jsonarrayROW.getJSONObject(j);
String category_id = jsonObjectRow.optString("category_id");
//Toast.makeText(getApplicationContext(),category_id,Toast.LENGTH_SHORT).show();
if (category_id.equalsIgnoreCase(detailInfo.getId())) {
JSONObject jsonnew1 = jsonarrayROW.getJSONObject(j);
JSONArray jsonarrayChild = jsonnew1.getJSONArray("child");
for (int k = 0; k < jsonarrayChild.length(); k++) {
JSONObject jsonObjectRow1 = jsonarrayChild.getJSONObject(k);
String category_id1 = jsonObjectRow1.optString("category_id");
String category_name = jsonObjectRow1.optString("category");
childSubCatModelList.add(new ChildSubCatModel(category_name,category_id));
//Toast.makeText(context,category_name,Toast.LENGTH_SHORT).show();
}
}
}
}
}
}
childSubCatAdapter.notifyDataSetChanged();
} catch (JSONException e) {
e.printStackTrace();
}
}
},
new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
Log.e("Volley", "Error");
}
}
);
RetryPolicy retryPolicy = new DefaultRetryPolicy(30000, 0, DefaultRetryPolicy.DEFAULT_BACKOFF_MULT);
stringRequest.setRetryPolicy(retryPolicy);
VolleySingleton.getInstance(context).addToRequestQueue(stringRequest);
}
});
return view;
}
@Override
public int getChildrenCount(int groupPosition) {
ArrayList<ChildInfo> productList = deptList.get(groupPosition).getProductList();
return productList.size();
}
@Override
public Object getGroup(int groupPosition) {
return deptList.get(groupPosition);
}
@Override
public int getGroupCount() {
return deptList.size();
}
@Override
public long getGroupId(int groupPosition) {
return groupPosition;
}
@Override
public View getGroupView(int groupPosition, boolean isLastChild, View view,
ViewGroup parent) {
GroupInfo headerInfo = (GroupInfo) getGroup(groupPosition);
if (view == null) {
LayoutInflater inf = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
view = inf.inflate(R.layout.group_items, null);
}
TextView heading = (TextView) view.findViewById(R.id.heading);
heading.setText(headerInfo.getName().trim());
ImageView headerIcon = (ImageView) view.findViewById(R.id.header_icon);
headerIcon.setImageResource(headerInfo.getDrawable());
ImageView imageView = (ImageView) view.findViewById(R.id.expand_icon);
if(isLastChild){
imageView.setImageResource(R.drawable.expand_more_black_24dp);
}else {
imageView.setImageResource(R.drawable.chevron_right_black_24dp);
}
return view;
}
@Override
public boolean hasStableIds() {
return true;
}
@Override
public boolean isChildSelectable(int groupPosition, int childPosition) {
return true;
}
public void fetchNavigation(final String headerName, final String categoryid) {
}
/* static class ChildViewHolder {
private HashMap<Integer, View> storedViews = new HashMap<Integer, View>();
public ChildViewHolder addView(View view)
{
int id = view.getId();
storedViews.put(id, view);
return this;
}
public View getView(int id)
{
return storedViews.get(id);
}
}*/
}
得到我需要的输出-: