动态填充LinearLayout ,LinearLayoutForListView

在开发的过程中,有时不愿意使用NOScrollListview,或者因为其影响性能问题替换NoScrollListview,如果是这样的话,分享的代码将对你说很有帮助,因为它就是利用Adaper的中getview 方法,将数据一条条添加到LinearLayout 中,对原有代码改动很小。

public class LinearLayoutForListView extends LinearLayout {

    private BaseAdapter adapter;

    public interface OnListItemClickListener {
    	
    	void onListItemClick(BaseAdapter adapter, View view, int position); 
    	
    }

	/**
     * 绑定布局
     */
    public void bindLinearLayout(int startIndex) {
        int count = adapter.getCount();
        for (int i = startIndex; i < count; i++) {
            View v = adapter.getView(i, null, null);
            addView(v, i);
        }
        invalidate();
    }
    
    public void removeItem(int index) {
    	removeViewAt(index);
    	invalidate();
    }
    
    public void clean() {
    	removeAllViewsInLayout();
    	invalidate();
    }

    public LinearLayoutForListView(Context context) {
        super(context);

    }

    public LinearLayoutForListView(Context context, AttributeSet attrs) {
        super(context, attrs);
        // TODO Auto-generated constructor stub

    }

    /**
     * 获取Adapter
     * 
     * @return adapter
     */
    public BaseAdapter getAdpater() {
        return adapter;
    }
    
    public void setAdapter(BaseAdapter adpater) {
    	 this.adapter = adpater;
    }

    /**
     * 设置数据
     * 
     * @param adpater
     */
    public void setAdapter(BaseAdapter adpater, OnListItemClickListener loadMoreClickListener) {
        this.setAdapter(adpater);
        if (adapter instanceof PresentGetterAdapter) {
        	((PresentGetterAdapter) adpater).setListItemClickListener(loadMoreClickListener);
        } else if (adapter instanceof SnapShotDiscussAdapter) {
        	((SnapShotDiscussAdapter) adpater).setListItemClickListener(loadMoreClickListener);
        }
    }

}


你可能感兴趣的:(动态填充LinearLayout ,LinearLayoutForListView)