工作小记~~~使用LinearLayout实现仿ListView(解决嵌套ListView显示不全)

使用LinearLayout实现仿ListView(解决嵌套ListView显示不全)

滑动控件嵌套ListView时就会出现ListView中的数据显示不全的现象,这时我们就会想到嵌套NoScrollListView去实现。功能是可以实现但是在列表中使用NoScrollListView的话会出现滑动卡顿的现象。 因为NoScrollListView是在显示前计算好高度显示的,所以如果在显示前隐藏了控件显示后再去显示该控件就会造成内容显示不全。我们可以自定义的LinearLayout做一个仿ListView的控件。既然是仿ListView那我们除了自定义控件之外,我们还需要自定义一个Adapter。

  • 自定义LinearLayout代码
public class CustomNoScrollListView extends LinearLayout {

    private CustomAdapter mAdapter;


    public CustomNoScrollListView(Context context) {
        super(context);
        setOrientation(LinearLayout.VERTICAL);
    }

    public CustomNoScrollListView(Context context, AttributeSet attrs) {
        super(context, attrs);
        setOrientation(LinearLayout.VERTICAL);
    }

    public CustomNoScrollListView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        setOrientation(LinearLayout.VERTICAL);
    }


    public void setAdapter(CustomAdapter adapter) {
        if (adapter == null) {
            throw new NullPointerException("CustomAdapter is null, please check setAdapter(CustomAdapter adapter)...");
        }
        mAdapter = adapter;
        adapter.setOnNotifyDataSetChangedListener(new CustomAdapter.OnNotifyDataSetChangedListener() {
            @Override
            public void OnNotifyDataSetChanged() {
                notifyDataSetChanged();
            }
        });
        adapter.notifyDataSetChanged();
    }

    public CustomAdapter getAdapter() {
        return mAdapter;
    }


    private void notifyDataSetChanged() {

        removeAllViews();
        if (mAdapter == null || mAdapter.getCount() == 0) {
            return;
        }
        LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
        for (int i = 0; i < mAdapter.getCount(); i++) {
            final int index = i;
            View view = mAdapter.getView(index);
            if (view == null) {
                throw new NullPointerException("item layout is null, please check getView()...");
            }
            addView(view, index, layoutParams);
        }

    }

}
  • 自定义Adapter代码(像普通的listView一样,继承下面的adapter添加自己的布局及逻辑即可)
public abstract class CustomAdapter {

    public abstract int getCount();

    public abstract Object getItem(int i);

    public abstract long getItemId(int i);

    public abstract View getView(int i);

    public void notifyDataSetChanged(){
        if(null != mOnNotifyDataSetChangedListener){
            mOnNotifyDataSetChangedListener.OnNotifyDataSetChanged();
        }
    }


    /**
     *  释放一个接口 串联adapter与view中间的数据刷新
     */
    public interface OnNotifyDataSetChangedListener{
        void OnNotifyDataSetChanged();
    }
    private OnNotifyDataSetChangedListener mOnNotifyDataSetChangedListener;
    public void setOnNotifyDataSetChangedListener(OnNotifyDataSetChangedListener listener){
        mOnNotifyDataSetChangedListener = listener;
    }
}

其实可以把Adapter的代码定义到View中,分开定义是为了替换ListView或NoScrollListView时方便。

使用方法与ListView一样直接调用setAdapter()设置自定义的适配器,刷新数据使用adapter的notifyDataSetChanged()方法

你可能感兴趣的:(工作随笔)