開源專案MergeAdapter,實現ListView複雜佈局

鋸齒流沙發表於2017-12-26

推薦一個超級好用的adapter,可以實現ListView的複雜佈局。該專案在GitHub上已經具有五百多顆星了。 專案的地址是:cwac-merge

這個MergeAdapter可以通過addView和addAdapter的方式加入多個view或者adapter,實現一些比較複雜的佈局。大家可以到github上,下載下來使用。

下面帶大家走一波

mergeadapter.png

這是我簡單實現的一個效果圖,當然大家可以通過addView或者addAdapter實現你想要的佈局。

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.lwj.merge.ListMergeActivity">

    <ListView
        android:id="@+id/meger_lsit"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="#ffffff"
        android:divider="@null"
        android:dividerHeight="0dp" />

</android.support.constraint.ConstraintLayout>
複製程式碼

佈局就一個ListView就可以了

public class ListMergeActivity extends AppCompatActivity {

    private ListView mListView;
    private List<String> mList01 = new ArrayList<>();
    private List<String> mList02 = new ArrayList<>();
    private MergeAdapter mMergeAdapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_list_meger);
        getData();
        mListView = (ListView)this.findViewById(R.id.meger_lsit);
        mMergeAdapter = new MergeAdapter();
        //addAdapter
        ListAdapter adapter = new ListAdapter(ListMergeActivity.this,mList01);
        mMergeAdapter.addAdapter(adapter);
        //addView
        ImageView mImageView = new ImageView(ListMergeActivity.this);
        mImageView.setImageResource(R.drawable.timg);
        mMergeAdapter.addView(mImageView);
        //addAdapter
        ListAdapter adapter02 = new ListAdapter(ListMergeActivity.this,mList02);
        mMergeAdapter.addAdapter(adapter02);
        //listview設定adapter
        mListView.setAdapter(mMergeAdapter);


    }

    public void getData() {
        for (int i = 0; i < 5; i++) {
            String data ="mergeadapter01...." + i;
            mList01.add(data);
        }
        for (int i = 5; i < 10; i++) {
            String data ="mergeadapter02...." + i;
            mList02.add(data);
        }
    }
}

複製程式碼

這裡按順序依次addAdapter、addView、addAdapter,然後佈局在ListView上依次顯示出來了。

public class ListAdapter extends BaseAdapter {
    private List<String> datas;
    private Context mContext;

    public ListAdapter(Context mContext,List<String> datas) {
        this.datas = datas;
        this.mContext = mContext;
    }

    @Override
    public int getCount() {
        return datas == null ? 0 : datas.size();
    }

    @Override
    public Object getItem(int i) {
        return datas == null ? null : datas.get(i);
    }

    @Override
    public long getItemId(int i) {
        return i;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup viewGroup) {
        if (convertView == null && !(convertView instanceof ItemLayout)){
            convertView = new ItemLayout(mContext);
        }
        ((ItemLayout)convertView).setData(datas.get(position));
        return convertView;
    }

    public static class ItemLayout extends LinearLayout{
        public String tagStr;
        private Button mButton;

        public ItemLayout(Context context) {
            super(context);
            initView(context);
        }

        private void initView(Context context) {
            int MP = LinearLayout.LayoutParams.MATCH_PARENT;
            int WC = LinearLayout.LayoutParams.WRAP_CONTENT;
            LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(MP, WC);
            LinearLayout layout = (LinearLayout) LayoutInflater.from(context).inflate(R.layout.list_item_layout,null);
            addView(layout,params);
            mButton = (Button)layout.findViewById(R.id.text);
        }
        public void setData(String str){
            if (str == null || str == tagStr){
                return;
            }else{
                tagStr = str;
                mButton.setText(str);
            }
        }

    }
}
複製程式碼

這是我add進去的adapter。

大家如果需要的話不妨下載下來,我覺得是一個非常棒的adapter。

相關文章