• Listview多种布局的使用


    ListView中有两个可以用来让ListView可以在视图中显示多种布局的方法,分别是getItemType和getViewTypeCount

    其中

        getItemViewType返回的是有参数position所决定的的view的id     getViewTypeCount,顾名思义,就是返回不同布局的数目

    接下来我们要做的就是在getView方法中根据不同的itemType来创建正确的convertView

    话不多说,先上图

          

    主要代码:

    Activity

    复制代码
    public class MainActivity extends Activity {
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
            initView();
        }
        
        private void initView() {
            ListView listView = (ListView) findViewById(R.id.listview);
            listView.setAdapter(new MySeparatorListAdapter(initData()));
        }
        
        private List<ListData> initData() {
            List<ListData> data = new ArrayList<MainActivity.ListData>();
            for(int i = 0; i < 30; i ++) 
                data.add(new ListData("标题:" + i, "内容:" + i, "作者:" + i));
            return data;
        }
    }
    复制代码

    Adapter类

    复制代码
    class MySeparatorListAdapter extends BaseAdapter {
            private static final int TYPE_MAIN = 0;
            private static final int TYPE_SEPARATOR = 1;
            private static final int TYPE_COUNT = 2;
            
            private List<ListData> mData;
            private LayoutInflater mInflater;
      
            private TreeSet mSeparatorsSet = new TreeSet();
            
            public MySeparatorListAdapter(List<ListData> data) {
                mInflater = (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);
                mData = data;
                setSeparator();
            }
            
            @SuppressWarnings("unchecked")
            private void setSeparator() {
                int index = 0;
                for(int i = 0, n = mData.size(); i < n; i ++) {
                    if(i % 3 == 0) {
                        //保存分界位置信息
                        mSeparatorsSet.add(i + index);
                        mData.add(i + index, new ListData("分界" + index, null, null));
                        index ++;
                    }
                }
                notifyDataSetChanged();
            }
    
            @Override
            public int getItemViewType(int position) {
                return mSeparatorsSet.contains(position) ? TYPE_SEPARATOR : TYPE_MAIN;
            }
      
            @Override
            public int getViewTypeCount() {
                return TYPE_COUNT;
            }
      
            @Override
            public int getCount() {
                return mData.size();
            }
      
            @Override
            public long getItemId(int position) {
                return position;
            }
            
            @Override
            public ListData getItem(int position) {
                return mData.get(position);
            }
    
            @Override
            public View getView(int position, View convertView, ViewGroup parent) {
                ViewHolder holder = null;
                int type = getItemViewType(position);
                if (convertView == null) {
                    holder = new ViewHolder();
                    switch (type) {
                        case TYPE_MAIN:
                            convertView = mInflater.inflate(R.layout.music_hot_review_item, null);
                            holder.titleView = (TextView)convertView.findViewById(R.id.music_hot_review_musicname);
                            holder.contentView = (TextView)convertView.findViewById(R.id.music_hot_review_review);
                            holder.authorView = (TextView)convertView.findViewById(R.id.music_hot_review_username);
                            break;
                        case TYPE_SEPARATOR:
                            convertView = mInflater.inflate(R.layout.separator_item, null);
                            holder.titleView = (TextView)convertView.findViewById(R.id.separetor_title);
                            break;
                    }
                    convertView.setTag(holder);
                } else {
                    holder = (ViewHolder)convertView.getTag();
                }
                switch (type) {
                case TYPE_MAIN:
                    holder.titleView.setText(mData.get(position).getTitle().toString());
                    holder.contentView.setText(mData.get(position).getContent().toString());
                    holder.authorView.setText(mData.get(position).getAuthor().toString());
                    break;
                case TYPE_SEPARATOR:
                    holder.titleView.setText(mData.get(position).getTitle().toString());
                    break;
                }
                return convertView;
            }
        }
    复制代码

    xml代码:

    主要布局:music_hot_review_item.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="@drawable/list_rect_selector"
        android:orientation="vertical" >
    
        <RelativeLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent" >
    
            <ImageView
                android:id="@+id/music_hot_review_imageview"
                android:layout_width="70dp"
                android:layout_height="70dp"
                android:layout_marginLeft="5dp"
                android:layout_marginTop="5dp"
                android:scaleType="centerCrop"
                android:src="@drawable/music_image_default" />
    
            <TextView
                android:id="@+id/music_hot_review_musicname"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginLeft="10dp"
                android:layout_marginTop="5dp"
                android:layout_toRightOf="@id/music_hot_review_imageview"
                android:ellipsize="marquee"
                android:marqueeRepeatLimit="marquee_forever"
                android:singleLine="true"
                android:text="我好想你"
                android:textColor="@color/font_color"
                android:textSize="18sp" />
    
            <TextView
                android:id="@+id/music_hot_review_username"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_below="@id/music_hot_review_musicname"
                android:layout_marginLeft="13dp"
                android:layout_marginTop="2dp"
                android:layout_toRightOf="@id/music_hot_review_imageview"
                android:text="居来提"
                android:textColor="@color/font_color"
                android:textSize="16sp" />
    
            <TextView
                android:id="@+id/music_hot_review_review"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_below="@id/music_hot_review_username"
                android:layout_marginLeft="13dp"
                android:layout_marginRight="5dp"
                android:layout_marginTop="2dp"
                android:layout_toRightOf="@id/music_hot_review_imageview"
                android:paddingBottom="10dp"
                android:text="dsahdkasdh"
                android:textColor="@color/font_color"
                android:textSize="15sp" />
        </RelativeLayout>
    
    </LinearLayout>
    复制代码

    分界布局: separator_item.xml

    复制代码
    <?xml version="1.0" encoding="utf-8"?>
    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:padding="5dp" >
        
        <TextView 
            android:id="@+id/separetor_title"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_centerHorizontal="true"
            android:textSize="20sp"
            android:textColor="@color/my_green"
            android:text="分界"
            />
    
    </RelativeLayout>
    复制代码
  • 相关阅读:
    VS Code安装以及工作区的创建
    var let const的使用和区别
    springboot 配置mysql日期返回格式
    vue安装Node和NPM配置,路由安装。
    分组查询语句(group by函数)
    ORA-00918:未明确定义列
    内连接(inner join)
    右外连接(right join)
    左外连接(left join)
    比较oracle两表中date类型数据是否一致语句查询
  • 原文地址:https://www.cnblogs.com/hbxblogs/p/3412959.html
Copyright © 2020-2023  润新知