ListView 嵌套 GridView 大法

起因

项目需求要用表格形式实现布局
但是!!!需要下拉和上拉加载数据。
因为项目里只有github里的XListView改造的ListView,又因为本人太笨太懒,不想写上拉下来gridview。。

废话不多说

ListView 嵌套 GridView 大法_第1张图片

1、定义一个主视图 listview.xml

<ListView xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/listview" android:layout_width="match_parent" android:layout_height="match_parent" android:clipToPadding="false" android:divider="@null" android:dividerHeight="10dp" android:padding="10dp"></ListView>

就这样,因为本例讲的是listview嵌套gridview,不涉及上拉下拉,所以就用普通的listview咯

2、自定义GridView—NoScrollGridView

其实不自定义也行,当时为了防止gridview显示补全blabla的,还是写一个保证万无一失。

public class NoScrollGridView extends GridView {
    public NoScrollGridView(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

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

    public NoScrollGridView(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
    }


    public void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        //核心在此
        int expandSpec = MeasureSpec.makeMeasureSpec(Integer.MAX_VALUE >> 2,
                MeasureSpec.AT_MOST);
        super.onMeasure(widthMeasureSpec, expandSpec+50);
    }
}

3、定义ListView单项布局 listview_item.xml

<?xml version="1.0" encoding="utf-8"?>
<com.example.shen.sample.NoScrollGridView xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/gridview" android:layout_width="match_parent" android:layout_height="wrap_content" android:numColumns="2" android:stretchMode="columnWidth" android:horizontalSpacing="10dp" android:verticalSpacing="10dp" />
<!-- 注意 android:numColumns="2" android:stretchMode="columnWidth" -->

4、定义GridView单项布局 gridview_item.xml

<?xml version="1.0" encoding="utf-8"?>
<ImageView  xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:id="@+id/imageview" android:background="#000" android:layout_height="80dp" />

5、定义GridView的数据适配器 GridViewAdapter.java

/** * Created by Shen on 2015/10/29. * 普通的适配器 */
public class GridViewAdapter extends BaseAdapter {

    //数据源
    private List<MainActivity.Bean> mList = new ArrayList<>();

    private Context mContext;

    public GridViewAdapter(Context context) {
        super();
        this.mContext = context;
    }

    public List<MainActivity.Bean> getmList() {
        return mList;
    }

    public void setmList(List<MainActivity.Bean> mList) {
        this.mList = mList;
    }

    @Override
    public int getCount() {
        return mList.size();
    }

    @Override
    public MainActivity.Bean getItem(int position) {
        return mList.get(position);
    }

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

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder;
        if (convertView == null) {
            convertView = LayoutInflater.from(mContext).inflate(R.layout.gridview_item, parent, false);
            holder = new ViewHolder(convertView);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }
        //设置图片内容,本例木有图片
// holder.iv.setImageResource(getItem(position));
        return convertView;
    }

    class ViewHolder {

        ImageView iv;

        public ViewHolder(View view) {
            iv = (ImageView) view.findViewById(R.id.imageview);
            view.setTag(this);
        }
    }
}

6、定义ListView的数据适配器 ListViewAdapter.java

/** * Created by Shen on 2015/10/29. * ListView的数据源 * 在这里给gridview设置数据 * 重要 */
public class ListViewAdapter extends BaseAdapter {

    //数据源
    private List<MainActivity.Bean> mList = new ArrayList<>();

    //列数
    private int mColumn = 2;

    private Context mContext;

    public ListViewAdapter(Context context, List<MainActivity.Bean> list) {
        super();
        this.mContext = context;
        this.mList = list;
    }

    /** * 这部很重要 *(核心) * @return listview的行数 */
    @Override
    public int getCount() {
        int count = mList.size() / mColumn;
        if (mList.size() % mColumn > 0) {
            count++;
        }
        return count;
    }

    @Override
    public MainActivity.Bean getItem(int position) {
        return mList.get(position);
    }

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

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder;
        if (convertView == null) {
            convertView = LayoutInflater.from(mContext).inflate(R.layout.listview_item, parent, false);
            holder = new ViewHolder(convertView);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }

        //定义gridView的数据源
        List<MainActivity.Bean> list = new ArrayList<>();

        //根据列数,将数据塞进去(核心)
        for (int i = 0; i < mColumn; i++) {
            int n = position * mColumn + i;
            if (n < mList.size()) {
                list.add(getItem(n));
            }
        }

        //更新数据源(核心)
        holder.adapter.setmList(list);
        holder.adapter.notifyDataSetChanged();
        return convertView;
    }

    class ViewHolder {

        GridView gridView;
        GridViewAdapter adapter;

        public ViewHolder(View view) {

            gridView = (GridView) view.findViewById(R.id.gridview);
            //将adapter定义在此,优化滑动效果(核心)
            adapter = new GridViewAdapter(mContext);
            //在此设置适配器,数据源在getView中添加(核心)
            gridView.setAdapter(adapter);

            view.setTag(this);
        }
    }
}

最后MainActivity.java

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.listview);

        ListView listview = (ListView) findViewById(R.id.listview);
        List<Bean> list = new ArrayList();
        for (int i = 0; i < 19; i++) {
            list.add(new Bean());
        }
        ListViewAdapter adapter = new ListViewAdapter(this, list);
        listview.setAdapter(adapter);
    }

    public static class Bean {

    }
}

大功告成!

你可能感兴趣的:(android,ListView,GridView)