horizontalscrollview嵌套Gridview(换种思路recycleView轻松实现)

最近有用到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是个大舞台,要学的东西还很多,要走的路还很长。 

 

慢慢来不一定是要求自己放松,更多的是持之以恒的努力。

写下这些一来是记录自己走的一些弯路,也是留待以后自己能经常回过头来看看自己经历的那些,走过的那些路

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

material_无机化学

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值