记录关于LinearLayout 水平动态加载item item重复问题

遇到一个需求,老板不要静态布局了,而是要调用后台提供的接口动态加载布局。首先整体是一个RecyclerView,用的是Brvah开源框架。这里用到的是RecyclerView通过ItemType来加载不同的布局。其中下面展示的只是一个Item的部分代码。

Item的布局很简单,只有一个LinearLayout:item_product.layout

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/item_content"
    android:layout_width="match_parent"
    android:layout_height="@dimen/dp_87"
    android:layout_marginLeft="@dimen/dp_10"
    android:layout_marginRight="@dimen/dp_10"
    android:layout_centerHorizontal="true"
    android:orientation="horizontal">
</LinearLayout>

LinearLayout要动态加载的Item布局文件 item_product_line.layout

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/rl"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="?android:attr/selectableItemBackground"
    android:clickable="true">
        <ImageView
            android:id="@+id/iv"
            android:layout_width="@dimen/dp_40"
            android:layout_height="@dimen/dp_40"
            android:layout_centerHorizontal="true"
            android:layout_marginLeft="@dimen/dp_23"
            android:layout_marginTop="@dimen/dp_15"
            android:scaleType="fitXY"
            android:src="@mipmap/hezuowoshou"
            />
        <TextView
            android:id="@+id/tv"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_below="@+id/iv"
            android:layout_centerHorizontal="true"
            android:layout_marginTop="@dimen/dp_5"
            android:text="xxxx"
            android:textColor="@color/black"
            android:textSize="@dimen/dp_14" />
</RelativeLayout>


LinearLayout linearLayout = helper.getView(R.id.item_content);
linearLayout.removeAllViews();
                if (EmptyUtils.isNotEmpty(ProductManager.getInstance().mProductLineModel)) {
                    for (int i = 0; i < ProductManager.getInstance().mProductLineModel.data.size(); i++) {
                        final ProductLineModel.DataBean item = ProductManager.getInstance().mProductLineModel.data.get(i);
                        View view = View.inflate(getContext(), R.layout.item_product_line, null);
                        RelativeLayout mL=view.findViewById(R.id.rl);
                        //设置权重
                        mL.setLayoutParams(new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,ViewGroup.LayoutParams.WRAP_CONTENT,1.0f));
                        TextView textName = view.findViewById(R.id.tv);
                        textName.setText(item.title + "");
                        ImageView logo = view.findViewById(R.id.iv);
                        RoundedCorners roundedCorners = new RoundedCorners(10);
                        RequestOptions options = RequestOptions.bitmapTransform(roundedCorners);
                        GlideApp.with(getContext()).load(item.url + item.img_url).apply(options).into(logo);
                        view.setOnClickListener(new View.OnClickListener() {
                            @Override
                            public void onClick(View v) {
                                switch (item.click_type){
                                    case TYPE:
      
                                        break;
                                }
                           }
                        });
                        linearLayout.addView(view);

一开始没有添加

linearLayout.removeAllViews();

每次刷新都会重复同样的条目。

所以,
在item中,linearLayout根据后台提供的接口动态addView(),解决此类的问题关键是
要在添加之前linearLayout.removeAllViews();

【完】

你可能感兴趣的:(Android)