1,activity_main.xml
<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"
android:orientation="vertical"
tools:context=".Main2Activity">
<android.support.v7.widget.Toolbar
android:layout_width="match_parent"
android:layout_height="30dp"
android:background="#ccc"
>
<ImageView
android:id="@+id/menu"
android:layout_width="30dp"
android:layout_height="30dp"
android:src="@drawable/menu"
android:focusable="true"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="200dp"
android:text="我的电商"/>
</android.support.v7.widget.Toolbar>
<android.support.v4.widget.DrawerLayout
android:id="@+id/drawer"
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<FrameLayout
android:id="@+id/frame"
android:layout_width="match_parent"
android:layout_height="match_parent">
</FrameLayout>
</LinearLayout>
<LinearLayout
android:layout_width="200dp"
android:layout_height="match_parent"
android:background="@color/colorPrimary"
android:orientation="vertical"
android:layout_gravity="start">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@mipmap/ic_launcher_round"
android:layout_marginLeft="55dp"
android:layout_marginTop="50dp"/>
<TextView
android:id="@+id/home"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="首页"
android:layout_marginTop="100dp"
android:layout_marginLeft="55dp"
/>
<TextView
android:id="@+id/myshopcart"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="我的购物车"
android:layout_marginTop="200dp"
android:layout_marginLeft="50dp"
/>
</LinearLayout>
</android.support.v4.widget.DrawerLayout>
</LinearLayout>
2,MainActivity
public class Main2Activity extends AppCompatActivity {
@BindView(R.id.menu)
ImageView menu;
@BindView(R.id.myshopcart)
TextView myshopcart;
@BindView(R.id.drawer)
DrawerLayout drawer;
@BindView(R.id.frame)
FrameLayout frame;
@BindView(R.id.home)
TextView home;
private home_frame home_frame=new home_frame();
private shop_frame shop_frame=new shop_frame();
private FragmentManager supportFragmentManager;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main2);
ButterKnife.bind(this);
supportFragmentManager = getSupportFragmentManager();
FragmentTransaction fragmentTransaction = supportFragmentManager.beginTransaction();
fragmentTransaction.add(R.id.frame,home_frame);
fragmentTransaction.add(R.id.frame,shop_frame);
fragmentTransaction.show(home_frame).hide(shop_frame);
fragmentTransaction.commit();
}
@OnClick({R.id.menu, R.id.home, R.id.myshopcart})
public void onViewClicked(View view) {
FragmentTransaction fragmentTransaction = supportFragmentManager.beginTransaction();
switch (view.getId()) {
case R.id.menu:
drawer.openDrawer(Gravity.LEFT);
break;
case R.id.home:
drawer.closeDrawer(Gravity.LEFT);
fragmentTransaction.show(home_frame).hide(shop_frame).commit();
break;
case R.id.myshopcart:
drawer.closeDrawer(Gravity.LEFT);
fragmentTransaction.show(shop_frame).hide(home_frame).commit();
break;
}
}
}
3,在shopcart_frame中json解析本地数据
public class shop_frame extends Fragment {
@BindView(R.id.recy_shop)
RecyclerView recyShop;
Unbinder unbinder;
private List<shopcartBean.DataBean> data=new ArrayList<>();
@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View inflate = inflater.inflate(R.layout.shop_frame, null);
unbinder = ButterKnife.bind(this, inflate);
init();
return inflate;
}
private void init() {
InputStreamReader inputStreamReader;
try {
inputStreamReader = new InputStreamReader(getResources().getAssets().open("cart" +
".json"), "UTF-8");
BufferedReader bufferedReader = new BufferedReader(
inputStreamReader);
String line;
StringBuilder stringBuilder = new StringBuilder();
while ((line = bufferedReader.readLine()) != null) {
stringBuilder.append(line);
}
inputStreamReader.close();
bufferedReader.close();
String resultString = stringBuilder.toString();
Log.d("hhhhh", stringBuilder.toString());
Gson gson = new Gson();
shopcartBean shopcartBean = gson.fromJson(resultString, shopcartBean.class);
List<com.example.moni.Bean.shopcartBean.DataBean> data = shopcartBean.getData();
Log.d("qqqqq", shopcartBean+"");
/*data.addAll(shopcartBean.getData());*/
LinearLayoutManager linearLayoutManager = new LinearLayoutManager(getActivity(), LinearLayoutManager.VERTICAL, false);
recyShop.setLayoutManager(linearLayoutManager);
MyshopcartRecyclerAdapter adapter=new MyshopcartRecyclerAdapter(getActivity(), data);
recyShop.setAdapter(adapter);
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
@Override
public void onDestroyView() {
super.onDestroyView();
unbinder.unbind();
}
}