android LinearLayoutForListView

由于 scrollview 套 listview 会有很多问题,网上很多人用 LinearLayout 模拟 listview, 也可以设置 adapter。

很多人直接继承 BaseAdapter, 可能是偷懒吧, 其实没有必要, BaseAdapter 里面有很多其它无用的方法被继承,

其次 getView 等方法的签名可能不太符合, 直接定义一个接口或抽象类更好。

这种方式在界面中有很多动态 list 布局时,抽离出来可能会灵活一些。 不用在代码中每次都 for 循环,动态创建 view 再添加。

 

1 可以简单的定义一个接口供子 adapter 实现

android LinearLayoutForListView
public interface MyLinearLayoutInterface {
    int getCount();

    Object getItem(int position);

    View getView(int position);
}
android LinearLayoutForListView

2 如果子类代码比较重复的话,也可以抽出一个基类

android LinearLayoutForListView
public abstract class LinearLayoutBaseAdapter {
    private List<? extends Object> list;
    private Context context;

    public LinearLayoutBaseAdapter(Context context, List<? extends Object> list) {
        this.context = context;
        this.list = list;
    }

    public LayoutInflater getLayoutInflater() {
        if (context != null) {
            return LayoutInflater.from(context);
        }

        return null;
    }

    public int getCount() {
        if (list != null) {
            return list.size();
        }

        return 0;
    };

    public Object getItem(int position) {
        if (list != null) {
            return list.get(position);
        }

        return null;
    };

    /**
     * 供子类复写
     * 
     * @param position
     * @return
     */
    abstract View getView(int position);
}
android LinearLayoutForListView

3 主类将 adapter 里面的 view 全部加入 LinearLayout 并设置回调监听

android LinearLayoutForListView
public class MyLinearLayoutForListView extends LinearLayout {
    private LinearLayoutBaseAdapter adapter;
    private OnItemClickListener onItemClickListener;

    public MyLinearLayoutForListView(Context context) {
        super(context);
    }

    public MyLinearLayoutForListView(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public void setAdapter(LinearLayoutBaseAdapter adapter) {
        this.adapter = adapter;
        // setAdapter 时添加 view
        bindView();
    }

    public void setOnItemClickListener(OnItemClickListener onItemClickListener) {
        this.onItemClickListener = onItemClickListener;

    }

    /**
     * 绑定 adapter 中所有的 view
     */
    private void bindView() {
        if (adapter == null) {
            return;
        }

        for (int i = 0; i < adapter.getCount(); i++) {
            final View v = adapter.getView(i);
            final int tmp = i;
            final Object obj = adapter.getItem(i);

            // view 点击事件触发时回调我们自己的接口
            v.setOnClickListener(new View.OnClickListener() {

                @Override
                public void onClick(View v) {
                    if (onItemClickListener != null) {
                        onItemClickListener.onItemClicked(v, obj, tmp);
                    }
                }
            });

            addView(v);
        }
    }

    /**
     * 
     * 回调接口
     */
    interface OnItemClickListener {
        /**
         * 
         * @param v
         *            点击的 view
         * @param obj
         *            点击的 view 所绑定的对象
         * @param position
         *            点击位置的 index
         */
        public void onItemClicked(View v, Object obj, int position);
    }
}
android LinearLayoutForListView

4 前台代码

android LinearLayoutForListView
List<Info> list = new ArrayList<Info>();
MyLinearLayoutForListView layout = (MyLinearLayoutForListView) findViewById(R.id.mylayout);

MyAdapter adapter = new MyAdapter(this, list);
layout.setAdapter(adapter);

layout.setOnItemClickListener(new MyLinearLayoutForListView.OnItemClickListener() {

    @Override
    public void onItemClicked(View v, Object obj, int position) {
        Info info = (Info) obj;
        if (info != null) {
            // do something
        }
    }
});
android LinearLayoutForListView

 

 

 

 

  

android LinearLayoutForListView,布布扣,bubuko.com

android LinearLayoutForListView

上一篇:Android Activity的加载模式和onActivityResult方法之间的冲突


下一篇:android4.3 截屏功能的尝试与失败分析