效果
MainActivity布局
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".LogActivity"
android:orientation="horizontal">
<android.support.v7.widget.RecyclerView
android:id="@+id/rv_left"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
</android.support.v7.widget.RecyclerView>
<android.support.v7.widget.RecyclerView
android:id="@+id/rv_right"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1" />
</LinearLayout>
左边适配器:
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="wrap_content"
android:layout_height="match_parent"
android:orientation="vertical">
<TextView
android:id="@+id/name_left"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="6660"
android:textSize="20sp"
android:textColor="#000"
android:padding="15dp"/>
</LinearLayout>
右边适配器布局
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<ImageView
android:id="@+id/image_right"
android:layout_width="150dp"
android:layout_height="150dp"
android:src="@mipmap/ic_launcher"/>
<TextView
android:id="@+id/name_right"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="666"/>
</LinearLayout>
MainActivity代码:
public class LogActivity extends AppCompatActivity {
private RecyclerView mRv_left;
private RecyclerView mRv_right;
private String strLeftUrl = "http://www.zhaoapi.cn/product/getCatagory";
private String strRightUrl = "http://www.zhaoapi.cn/product/getProductCatagory";
private MyLeftAdapter mMyLeftAdapter;
private List<LeftResponBean.DataBean> leftData;
private MyRightAdapter mMyRightAdapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_log);
//初始化控件
initView();
//加载数据
leftLoadData();
//左边适配器
leftAdapter();
//加载数据
rightLoadData();
//右边适配器
rightAdapter();
}
//右边适配器
private void rightAdapter() {
mMyRightAdapter = new MyRightAdapter(this);
mRv_right.setAdapter(mMyRightAdapter);
mRv_right.setLayoutManager(new GridLayoutManager(this,2,GridLayoutManager.VERTICAL,false));
}
//加载数据
private void rightLoadData() {
//接口回调获取位置
mMyLeftAdapter.setCidLinstener(new MyLeftAdapter.LeftInterfaceCidLinstener() {
@Override
public void setCid(String cid) {
HashMap<String, String> map = new HashMap<>();
map.put("cid",cid);
//调用工具类
OkHttpUril instance = OkHttpUril.getInstance();
//调用方法
instance.doPost(strRightUrl, map, new OkHttpUril.OkHttpInterface() {
@Override
public void failure(Exception e) {
}
@Override
public void success(String json) {
//解析数据
RightResponBean rightResponBean = new Gson().fromJson(json, RightResponBean.class);
List<RightResponBean.DataBean> data = rightResponBean.getData();
//设置数据
mMyRightAdapter.setData(data);
}
});
}
});
}
//左边适配器
private void leftAdapter() {
mMyLeftAdapter = new MyLeftAdapter(LogActivity.this);
mRv_left.setAdapter(mMyLeftAdapter);
mRv_left.setLayoutManager(new LinearLayoutManager(this,LinearLayoutManager.VERTICAL,false));
}
//左边加载数据
private void leftLoadData() {
OkHttpUril instance = OkHttpUril.getInstance();
instance.doGet(strLeftUrl, new OkHttpUril.OkHttpInterface() {
@Override
public void failure(Exception e) {
}
@Override
public void success(String json) {
LeftResponBean leftResponBean = new Gson().fromJson(json, LeftResponBean.class);
leftData = leftResponBean.getData();
//更新适配器
mMyLeftAdapter.setData(leftData);
}
});
}
//初始化布局
private void initView() {
mRv_left = findViewById(R.id.rv_left);
mRv_right = findViewById(R.id.rv_right);
}
}
左边适配器代码
public class MyLeftAdapter extends RecyclerView.Adapter<MyLeftAdapter.ViewHolder> {
private List<LeftResponBean.DataBean> mData;
private Context mContext;
public MyLeftAdapter(Context context) {
mContext = context;
mData = new ArrayList<>();
}
//设置数据的方法
public void setData(List<LeftResponBean.DataBean> data){
mData.clear();
if(data != null){
mData.addAll(data);
}
notifyDataSetChanged();
}
@NonNull
@Override
public ViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
View view = View.inflate(mContext, R.layout.left_adapter, null);
ViewHolder viewItem = new ViewHolder(view);
return viewItem;
}
@Override
public void onBindViewHolder(@NonNull ViewHolder viewHolder, final int i) {
final LeftResponBean.DataBean dataBean = mData.get(i);
viewHolder.name.setText(dataBean.getName());
//设置点击事件传位置
viewHolder.itemView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
mLeftInterfaceCidLinstener.setCid(dataBean.getCid()+"");
}
});
}
@Override
public int getItemCount() {
return mData.size();
}
public class ViewHolder extends RecyclerView.ViewHolder {
private final TextView name;
public ViewHolder(@NonNull View itemView) {
super(itemView);
name = itemView.findViewById(R.id.name_left);
}
}
//创建接口传入cid
public interface LeftInterfaceCidLinstener{
void setCid(String cid);
}
private LeftInterfaceCidLinstener mLeftInterfaceCidLinstener;
public void setCidLinstener(LeftInterfaceCidLinstener leftInterfaceCidLinstener){
mLeftInterfaceCidLinstener = leftInterfaceCidLinstener;
}
}
右边适配器没有点击事件其他都照着自己的控件写就ok