一 介绍
1, 该文章并没有实现gridview 的 android:numColumns的功能 ,只能实现gridview 的多item效果
2 , 目的是为了借用 listview 添加头部的功能 网上有很多gridview添加头部的实现 但是我有点看不懂,并且我个 人感觉gridview添加头部没有listview添加头部的功能强大。
二 思路
1 我们可以在listview的item布局中写入多个item布局水平排列,每个item用layout包裹。 在适配器中按顺序添 加数据。
2 listview 的item点击事件------- 在适配器中给每个layout添加点击事件 当用户点击layout时利用回调将点击事件得到的数据传递到外面。
(item.xml 代码)
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="250dp"
android:orientation="horizontal">
<LinearLayout
android:id="@+id/layout_left"
android:layout_width="0dp"
android:layout_height="250dp"
android:layout_weight="1"
android:padding="5dp"
android:orientation="vertical">
<ImageView
android:id="@+id/image_legt"
android:layout_width="match_parent"
android:layout_height="200dp"
android:background="#fff"
/>
<TextView
android:id="@+id/text_left"
android:layout_width="match_parent"
android:layout_height="50dp"
android:padding="16dp"
android:background="#fff"
android:text=" " />
</LinearLayout>
<TextView
android:layout_width="4dp"
android:layout_height="match_parent"
android:background="#ccc"/>
<LinearLayout
android:id="@+id/layout_right"
android:layout_width="0dp"
android:layout_height="250dp"
android:layout_weight="1"
android:padding="5dp"
android:orientation="vertical">
<ImageView
android:id="@+id/image_right"
android:layout_width="match_parent"
android:layout_height="200dp"
android:background="#fff"
/>
<TextView
android:id="@+id/text_right"
android:layout_width="match_parent"
android:layout_height="50dp"
android:background="#fff"
android:padding="16dp"
android:text=" " />
</LinearLayout>
</LinearLayout>
adapter 代码
package xwh.qianfeng.com.myapplication;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import java.util.ArrayList;
import java.util.List;
/**
* Created by ~~~~~~ on 2016/11/12.
*/
public class MyListAdapter extends BaseAdapter {
List<Goods.GoodsListBean> datas;
CustomOnItemClickListent clickListent;
protected ViewGroup viewGroup;
protected int count;
protected Goods.GoodsListBean data;
protected View v;
final int TYPE1 = 0;
final int TYPE2 = 1;
int datacount = -1;
public void setdatas(List<Goods.GoodsListBean> datas) {
// datas.remove(0);
if (datas.size() % 2 == 0) {
datacount = (datas.size() / 2);
} else {
datacount = (datas.size() / 2 + 1);
}
this.datas = datas;
}
@Override
public int getCount() {
if (datas == null) {
return 0;
} else {
int num = (datas.size() % 2);
int count = datas.size() / 2;
if (num != 0) {
datacount = count + 1;
return (count + 1);
}
datacount = count;
return (count);
}
}
@Override
public int getItemViewType(int position) {
if (position + 1 < datacount) {
return TYPE1;
} else {
if (datas.size() % 2 != 0) {
return TYPE2;
} else {
return TYPE1;
}
}
}
@Override
public ArrayList<Goods.GoodsListBean> getItem(int position) {
return null;
}
@Override
public long getItemId(int position) {
return 0;
}
@Override
public View getView(final int position, final View convertView, final ViewGroup parent) {
int type = getItemViewType(position);
View view=null;
// view=convertView;
switch (type) {
case TYPE1:
view=convertView;
ViewHolder viewHolder = null;
if (view == null || !(view.getTag() instanceof ViewHolder)) {
view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item, null);
viewHolder = new ViewHolder(view);
view.setTag(viewHolder);
} else {
viewHolder = (ViewHolder) view.getTag();
}
viewHolder.textLeft.setText(datas.get(2 * position).getName());
viewHolder.textRight.setText(datas.get(2 * position + 1).getName());
XUser.imageSent(viewHolder.imageLegt, datas.get(2 * position).getImage());
XUser.imageSent(viewHolder.imageRight, datas.get(2 * position + 1).getImage());
viewHolder.layoutLeft.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
clickListent.onItemClick(parent, convertView, (position * 2), datas.get((position * 2)));
}
});
viewHolder.layoutRight.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
clickListent.onItemClick(parent, convertView, (position * 2 + 1), datas.get((position * 2 + 1)));
}
});
break;
case TYPE2:
view=new View(parent.getContext());
ViewHolderType2 holderType2 = null;
if (view == null || !(view.getTag() instanceof ViewHolder)) {
view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_type2, null);
holderType2 = new ViewHolderType2(view);
view.setTag(holderType2);
} else {
holderType2 = (ViewHolderType2) view.getTag();
}
holderType2.textLeftType2.setText(datas.get(2 * position).getName());
XUser.imageSent(holderType2.imageLegtType2, datas.get(2 * position).getImage());
holderType2.layoutLeftType2.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
clickListent.onItemClick(parent, convertView, (position * 2), datas.get((position * 2)));
}
});
}
return view;
}
static class ViewHolder {
protected ImageView imageLegt;
protected TextView textLeft;
protected LinearLayout layoutLeft;
protected ImageView imageRight;
protected TextView textRight;
protected LinearLayout layoutRight;
ViewHolder(View rootView) {
initView(rootView);
}
private void initView(View rootView) {
imageLegt = (ImageView) rootView.findViewById(R.id.image_legt);
textLeft = (TextView) rootView.findViewById(R.id.text_left);
layoutLeft = (LinearLayout) rootView.findViewById(R.id.layout_left);
imageRight = (ImageView) rootView.findViewById(R.id.image_right);
textRight = (TextView) rootView.findViewById(R.id.text_right);
layoutRight = (LinearLayout) rootView.findViewById(R.id.layout_right);
}
}
static class ViewHolderType2 {
protected ImageView imageLegtType2;
protected TextView textLeftType2;
protected LinearLayout layoutLeftType2;
ViewHolderType2(View rootView) {
initView(rootView);
}
private void initView(View rootView) {
imageLegtType2 = (ImageView) rootView.findViewById(R.id.image_legt_type2);
textLeftType2 = (TextView) rootView.findViewById(R.id.text_left_type2);
layoutLeftType2 = (LinearLayout) rootView.findViewById(R.id.layout_left_type2);
}
}
//外部回调接口
public void setOnItemClickListent(CustomOnItemClickListent customOnItemClickListent) {
clickListent = customOnItemClickListent;
}
//内部接口
public interface CustomOnItemClickListent {
public void onItemClick(ViewGroup parent, View view, int position, Goods.GoodsListBean data);
}
}
调用方法
list = (ListView) findViewById(R.id.list);
list.setAdapter(adapter);
adapter.setOnItemClickListent(new MyListAdapter.CustomOnItemClickListent() {
@Override
public void onItemClick(ViewGroup parent, View view, int position, Goods.GoodsListBean data) {
Log.e("点击事件", " " + position + " " + data.getName());
}
});
main 布局
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent" android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="xwh.qianfeng.com.myapplication.MainActivity">
<ListView
android:id="@+id/list"
android:dividerHeight="4dp"
android:divider="#ccc"
android:clickable="false"
android:layout_width="match_parent"
android:layout_height="wrap_content">
</ListView>
</RelativeLayout>
效果图