通过代码动态添加列表item实现列表(不使用listview列表)

来源:互联网 发布:js不等于怎么写 编辑:程序博客网 时间:2024/06/07 04:00

先上效果图,通过接口获取银行卡列表数据,通过数据来动态添加item。

不是使用listview,通过动态添加列表item实现列表

首先,我们需要写好一个item的xml。

代码:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
     android:background="@color/white"
    android:orientation="vertical" >
 
    <RelativeLayout
        android:id="@+id/ll_xinyongka"
        android:layout_width="match_parent"
        android:layout_height="65dp" 
        android:orientation="vertical"
        android:background="@color/white"
        >
      <ImageView 
          android:id="@+id/iv_bankico"
          android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:background="@drawable/bank_gonghang"
            android:layout_marginLeft="20dp"
            android:layout_centerVertical="true"
          />
 
        <TextView
              android:id="@+id/tv_bankname"
            android:layout_width="wrap_content"
            android:layout_toRightOf="@id/iv_bankico"
            android:layout_height="35dp"
            android:layout_marginLeft="10dp"
            android:text="招商银行(0000)"
            android:textSize="16sp"
            android:gravity="center_vertical"
            android:layout_centerVertical="true"
             />
         
     <ImageView 
          android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:background="@drawable/skiparrow"
            android:layout_alignParentRight="true"
            android:layout_marginRight="20dp"
            android:layout_centerVertical="true"
          />
      
     
        
    </RelativeLayout>
 
     
   <View
       android:id="@+id/view_line"
            android:layout_width="match_parent"
            android:layout_height="0.5dp"
            android:layout_marginLeft="20dp"
            android:background="@color/grey_mybankcard"
         />
     
     
</LinearLayout>


item效果如下图:

不是使用listview,通过动态添加列表item实现列表


接下来,实现解析json数据,拿到数据,拿到实体类之后,按照个数动态添加item。

安卓代码:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
view_line1.setVisibility(View.VISIBLE);
            view_line2.setVisibility(View.VISIBLE);
             
            for (int i = 0; i < myBankCardInfoy.getCredits().size(); i++) {
                LayoutInflater inflater=LayoutInflater.from(getActivity());
                itemView=inflater.inflate(R.layout.item_card, null);
                itemView.setOnClickListener(this);
                myBankCardInfoy.getDebits().get(i).setCardType("信用卡");
                itemView.setTag(myBankCardInfoy.getDebits().get(i));
                iv_bankico=(ImageView) itemView.findViewById(R.id.iv_bankico);
                tv_bankname=(TextView) itemView.findViewById(R.id.tv_bankname);
 
                tv_bankname.setText(myBankCardInfoy.getCredits().get(i).getCardName()+
                        myBankCardInfoy.getCredits().get(i).getCardNo());
                 
                //添加银行卡item
                ll_mycardslist1.addView(itemView);
 
                 
                if(i==myBankCardInfoy.getCredits().size()){
                    //最后一个item设置隐藏横线
                    view_line= itemView.findViewById(R.id.view_line);
                    view_line.setVisibility(View.GONE);
                }
                 
                 
            }          

希望大家有用。

0 0