防京東分類
阿新 • • 發佈:2018-11-24
效果
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