版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/kururunga/article/details/86570374
思路:第一个recycleview的adapter里将所有用到的数据传进去,在第二个里面的convert方法里设置第二个adapter的数据,第二个adapter的recyclew写在第一个recycleview的item中,设置一个boolean值,convert方法中根据点击第一个recycleview的时候设为true显示第二个可见,再次点击设置false为不可见,通过给外部的点击回调控制boolean值,如在adapter这样写:
public void setOpenList(int position, boolean flag) {
this.openPosition = position;
isOpen = flag;
notifyDataSetChanged();
}
具体代码:
1.Adapter:
public class CoachEndAdapter extends BaseQuickAdapter<CoachEndItem, BaseViewHolder> {
private Context mContext;
private OnItemChildItemClickListener listener;
private List<CoachEndItem> data;
private int openPosition = -1;
private boolean isOpen = false;
public CoachEndAdapter(Context mContext, @Nullable List<CoachEndItem> data) {
super(R.layout.item_coach_end, data);
this.mContext = mContext;
this.data = data;
}
@Override
protected void convert(BaseViewHolder helper, CoachEndItem item) {
helper.setVisible(R.id.tv_coach_end, false);
int position = helper.getAdapterPosition();
helper.setText(R.id.rv_coach_end_city, item.getCity() + " (" + item.getStations().size() + ")");
if (position == openPosition) {
//点击同一个会发送一个false过来
if (!isOpen) {
helper.setVisible(R.id.rv_coach_end_station_list, false)
.setBackgroundColor(R.id.rv_coach_end_city,
mContext.getResources().getColor(R.color.white));
return;
}
helper.setVisible(R.id.rv_coach_end_station_list, true)
.setBackgroundColor(R.id.rv_coach_end_city,
mContext.getResources().getColor(R.color.grey_200));
RecyclerView stationList = helper.getView(R.id.rv_coach_end_station_list);
stationList.setNestedScrollingEnabled(false);
stationList.setHasFixedSize(true);
stationList.setLayoutManager(new LinearLayoutManager(mContext));
CoachEndStationAdapter mAdapter = new CoachEndStationAdapter(item.getStations());
stationList.setAdapter(mAdapter);
mAdapter.setOnItemChildClickListener(new OnItemChildClickListener() {
@Override
public void onItemChildClick(BaseQuickAdapter adapter, View view, int position1) {
CoachEndItem.StationsBean stationsBean = data.get(position).getStations().get(position1);
if (listener != null) {
listener.onItemChildItemClick(stationsBean);
}
}
});
} else {
helper.setVisible(R.id.rv_coach_end_station_list, false)
.setBackgroundColor(R.id.rv_coach_end_city,
mContext.getResources().getColor(R.color.white));
}
}
public void setOpenList(int position, boolean flag) {
this.openPosition = position;
isOpen = flag;
notifyDataSetChanged();
}
public class CoachEndStationAdapter extends BaseQuickAdapter<CoachEndItem.StationsBean, BaseViewHolder> {
public CoachEndStationAdapter(@Nullable List<CoachEndItem.StationsBean> data) {
super(R.layout.item_coach_end_station, data);
}
@Override
protected void convert(BaseViewHolder helper, CoachEndItem.StationsBean item) {
helper.setText(R.id.tv_station, item.getName())
.addOnClickListener(R.id.tv_station);
}
}
public void setOnItemChildItemClickListener(OnItemChildItemClickListener listener) {
this.listener = listener;
}
public interface OnItemChildItemClickListener {
//列表的子列表的点击事件
void onItemChildItemClick(CoachEndItem.StationsBean stationBean);
}
}
2.第一个adapter的布局:
<?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="wrap_content"
android:background="@color/white"
android:orientation="vertical">
<TextView
android:id="@+id/tv_coach_end"
android:layout_width="match_parent"
android:layout_height="24dp"
android:background="@color/grey_300"
android:gravity="center_vertical"
android:paddingLeft="12dp"
android:text="字母" />
<View
android:layout_width="match_parent"
android:layout_height="4dp"
android:background="@color/grey_300" />
<TextView
android:id="@+id/rv_coach_end_city"
android:layout_width="match_parent"
android:layout_height="40dp"
android:gravity="center_vertical"
android:paddingLeft="12dp"
android:text="城市"
android:textColor="@color/black"
android:textSize="@dimen/sp_16"
android:textStyle="bold" />
<View
android:layout_width="match_parent"
android:layout_height="1dp"
android:background="@color/grey_300"
android:visibility="gone" />
<android.support.v7.widget.RecyclerView
android:id="@+id/rv_coach_end_station_list"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</LinearLayout>
3.第二个adapter的布局:
<?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="wrap_content"
android:background="@color/white"
android:orientation="vertical">
<TextView
android:id="@+id/tv_station"
android:layout_width="match_parent"
android:layout_height="34dp"
android:layout_marginLeft="24dp"
android:gravity="center_vertical"
android:textColor="@color/black"
android:textSize="@dimen/sp_16" />
<View
android:layout_width="match_parent"
android:layout_height="1dp"
android:background="@color/grey_300" />
</LinearLayout>
4.activity使用
public void initRecyclerView() {
endList.setNestedScrollingEnabled(false);
endList.setHasFixedSize(true);
endList.setLayoutManager(new LinearLayoutManager(this));
mAdapter = new CoachEndAdapter(this, datas);
endList.setAdapter(mAdapter);
mAdapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
@Override
public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
//第二次点击同一个则关闭
if (currentOpenPosition == position && isOpen) {
isOpen = false;
mAdapter.setOpenList(currentOpenPosition, false);
} else {
isOpen = true;
mAdapter.setOpenList(position, true);
mAdapter.notifyDataSetChanged();
}
currentOpenPosition = position;
}
});
mAdapter.setOnItemChildItemClickListener(new CoachEndAdapter.OnItemChildItemClickListener() {
@Override
public void onItemChildItemClick(CoachEndItem.StationsBean stationBean) {
Intent intent = new Intent(CoachEndActivity.this, CoachActivity.class);
intent.putExtra("end", stationBean);
setResult(RESULT_OK, intent);
finish();
}
});
}