最近有用到horizantalsrcollview嵌套gridview,说实话,效果能实现,但bug挺多。
而且布局的宽度必须计算好,搞不好还会因为适配原因,各种不匹配真心纠结。
所以后来果断换了种方式。recycleview可以轻松实现这种功能。
布局文件xml
<?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="match_parent"
android:background="#ffffff"
android:orientation="vertical">
<View
android:layout_width="match_parent"
android:layout_height="1px"
android:background="#E3E3E3" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="2dp">
<ImageView
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:paddingBottom="8dp"
android:paddingTop="8dp"
android:src="@mipmap/red_rectangle" />
<TextView
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="15"
android:paddingBottom="8dp"
android:paddingTop="8dp"
android:text="为您推荐" />
</LinearLayout>
<View
android:layout_width="match_parent"
android:layout_height="1px"
android:background="#E3E3E3" />
<android.support.v7.widget.RecyclerView
android:id="@+id/rv_show"
android:layout_width="match_parent"
android:layout_height="match_parent"
></android.support.v7.widget.RecyclerView>
</LinearLayout>
Mainactivity中:
protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_home);
//1.获取recycleview
recyclerView = (RecyclerView) findViewById(R.id.rv_show);
//2.载入数据
initRvBeans();
//3.设置适配器
myRvAdapter = new MyRvAdapter(getContext(), gridBeans);
recyclerView.setAdapter(myRvAdapter);
recyclerView.setLayoutManager(new LinearLayoutManager(getActivity(),
LinearLayoutManager.HORIZONTAL, false));
public void initRvBeans() {
DataRequest.getjsonData(getActivity(), s_recommondProduct, params, new DataRequest.CallBackData() {
@Override
public void success(String result) {
try {
JSONObject dataObject = new JSONObject(result);
JSONArray dataArray = dataObject.getJSONArray("Goods");
for (int i = 0; i < dataArray.length(); i++) {
JSONObject object = dataArray.getJSONObject(i);
int goodsBuyNumber = object.getInt("goodsBuyNumber");
String goodsImagePath = object.getString("goodsImagePath");
int goodsLimitNumber = object.getInt("goodsLimitNumber");
String goodsName = object.getString("goodsName");
int special = object.getInt("special");
db_Bean = new Db_Bean(goodsBuyNumber, goodsImagePath, goodsLimitNumber, goodsName, special);
gridBeans.add(db_Bean);
myRvAdapter.notifyDataSetChanged(); }
} catch (Exception e) {
e.printStackTrace();
}
}
@Override
public void fail(String result) {
}
});
IT是个大舞台,要学的东西还很多,要走的路还很长。
慢慢来不一定是要求自己放松,更多的是持之以恒的努力。
写下这些一来是记录自己走的一些弯路,也是留待以后自己能经常回过头来看看自己经历的那些,走过的那些路