第一步:导入依赖

	//recyclerview依赖
    implementation 'com.android.support:recyclerview-v7:28.0.0'
    //Brvah:RecyclerView--Adapter快速开发框架
    implementation 'com.github.CymChad:BaseRecyclerViewAdapterHelper:2.9.30'

第二步:在布局中写控件

    //RecyclerView--数据展示
    <android.support.v7.widget.RecyclerView
        android:id="@+id/rv"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_gravity="center_horizontal">

    </android.support.v7.widget.RecyclerView>

第三步:在.java中查找控件并进行代码编写
public class Frag_home extends Fragment implements Contract_rxxp.Rxxp_View_Interface {


    private RecyclerView rv;

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate( R.layout.frag_home, container, false );
        //查找控件
        rv = view.findViewById( R.id.rv );

        return view;
    }

    @Override
    //数据刷新--热销新品
    public void showRxxpData(final String message) {
        //线程
        getActivity().runOnUiThread( new Runnable() {
            @Override
            public void run() {
                //解析数据
                Gson gson = new Gson();
                ShangPinBean shangPinBean = gson.fromJson( message, ShangPinBean.class );
                List<ShangPinBean.ResultBean.RxxpBean.CommodityListBean> rxxplist = shangPinBean.getResult().getRxxp().get( 0 ).getCommodityList();

                //创建布局管理器
                LinearLayoutManager layoutManager = new LinearLayoutManager( getActivity(),LinearLayoutManager.HORIZONTAL,false );
                //设置布局管理器
                rv.setLayoutManager( layoutManager );

                //设置适配器
                MyAdapter adapter = new MyAdapter( R.layout.item_rxxp, rxxplist );
                rv.setAdapter( adapter );
            }
        } );
    }

}

我们再来看一下适配器的代码
public class MyAdapter extends BaseQuickAdapter<ShangPinBean.ResultBean.RxxpBean.CommodityListBean,BaseViewHolder> {

public MyAdapter(int layoutResId, @Nullable List<ShangPinBean.ResultBean.RxxpBean.CommodityListBean> data) {
        super( layoutResId, data );
    }

    @Override
    protected void convert(BaseViewHolder helper, ShangPinBean.ResultBean.RxxpBean.CommodityListBean item) {
        //给名称赋值
        helper.setText( R.id.tv_rxxp,item.getCommodityName() );
        //给价格赋值
        helper.setText( R.id.tv_rxxp_price,"¥"+item.getPrice() );
        //查找图片并赋值
        ImageView img_rxxp = helper.getView( R.id.img_rxxp );
        Glide.with( mContext ).load( item.getMasterPic() ).into( img_rxxp );
    }

}

item_rxxp.xml(Recyclerview子条目的布局)

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:gravity="center_horizontal"
    android:orientation="vertical">

    //卡片布局
    <android.support.v7.widget.CardView
        android:layout_width="105dp"
        android:layout_height="166dp"
        app:cardCornerRadius="8dp">
        //商品图片
        <ImageView
            android:id="@+id/img_rxxp"
            android:layout_width="85dp"
            android:layout_height="100dp"
            android:layout_marginTop="5dp"
            android:layout_gravity="center_horizontal"
            android:src="@mipmap/fangdajing"/>
        //商品名称
        <TextView
            android:id="@+id/tv_rxxp"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginTop="115dp"
            android:gravity="center_horizontal"
            android:maxEms="8"
            android:singleLine="true"
            android:ellipsize="end"
            android:text="商品名称"/>
        //商品价格
        <TextView
            android:id="@+id/tv_rxxp_price"
            android:layout_width="match_parent"
            android:gravity="center_horizontal"
            android:layout_height="wrap_content"
            android:layout_marginTop="140dp"
            android:textSize="14sp"
            android:textColor="#f00"
            android:text="商品价格"/>
    </android.support.v7.widget.CardView>

</LinearLayout>

Logo

瓜分20万奖金 获得内推名额 丰厚实物奖励 易参与易上手

更多推荐