FlabbyListView——顯示ListView的Android類庫

FlabbyListView是Android類庫,用於顯示不規則單元格的ListView,單元格會根據ListView的滾動出現波浪效果。
首先來看listView的佈局文件

    <com.ys.flabbylistviewdemo.FlabbyListView
    android:id="@+id/flabbyListView"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:divider="#00ff00"
    android:dividerHeight="1px" />

再來看FlabbyListView的子佈局文件

<com.ys.flabbylistviewdemo.FlabbyLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/apk/tools"
    android:layout_width="match_parent"
    android:layout_height="100dp"
    android:orientation="vertical">

    <TextView
        android:id="@+id/text"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center_vertical"
        android:padding="8dp"
        android:tag="Text"/>

</com.ys.flabbylistviewdemo.FlabbyLayout>

佈局就這麼簡單!
接下來來看MainActivity.java

public class MainActivity extends Activity implements OnRefreshListener{
    private static final int NUM_LIST_ITEM = 50;
    private MyAdapter mAdapter;
    private FlabbyListView mListView;
    private SwipeRefreshLayout swipeRefreshLayout;
    private List<String> items ;

    private Handler handler = new Handler(){
        public void handleMessage(android.os.Message msg) {
            super.handleMessage(msg);
            if(msg.what==1){
                addData();
                swipeRefreshLayout.setRefreshing(false);
            }
        }
    };
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        items = getListItems();
        mListView = (FlabbyListView) findViewById(R.id.flabbyListView);
        swipeRefreshLayout =  (SwipeRefreshLayout) findViewById(R.id.swipeRefreshLayout);
        swipeRefreshLayout.setColorSchemeColors(Color.parseColor("#ff0000ff"));
        mAdapter = new MyAdapter(this);
        mListView.setAdapter(mAdapter);
        mAdapter.setData(items);
        swipeRefreshLayout.setOnRefreshListener(this);
    }

    private ArrayList<String> getListItems() {
        ArrayList<String> list = new ArrayList<String>();
        for(int i=0;i<NUM_LIST_ITEM;i++){
            list.add("Item"+i);
        }
        return list;
    }

    private void addData(){
        for(int i=1;i<=10;i++){
            items.add("你好"+i);
        }
    }

    @Override
    public void onRefresh() {
        handler.sendEmptyMessageDelayed(1, 5000);
    }

}

就是這麼easy,實現Item佈局顏色變化也很容易

public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder;
        if(convertView==null){
            holder = new ViewHolder();
            convertView = LayoutInflater.from(mContext).inflate(R.layout.item_list, parent,false);
            holder.text = (TextView) convertView.findViewById(R.id.text);
            convertView.setTag(holder);
        }else{
            holder = (ViewHolder) convertView.getTag();
        }
        int color = Color.argb(255, mRandom.nextInt(256), mRandom.nextInt(256), mRandom.nextInt(256));
        ((FlabbyLayout)convertView).setFlabbyColor(color);
        holder.text.setText(mData.get(position));
        return convertView;
    }

    static class ViewHolder{
        private TextView text;
    }

是不是很炫酷啊

是不是看起來特別炫酷啊!
Github託管地址:https://github.com/jpardogo/FlabbyListView

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章