大家想想listView的实现方式 就是一个item一个item 添加到一个布局中,
那么LinearLayout可不可以像ListView 那样 往里面添加item 答案是可以的
我们先模拟listView 的LinearLayout类
public class LinearLayoutForListView extends LinearLayout {
private ListAdapter adapter;
private OnClickListener onClickListener = null;
private OnTouchListener onTouchListener = null;
/**
* 绑定布局
*/
public void bindLinearLayout() {
int count = adapter.getCount();
for (int i = 0; i < count; i++) {
View v = adapter.getView(i, null, null);
v.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));
v.setOnTouchListener(this.onTouchListener);
v.setOnClickListener(this.onClickListener);
v.setId(i);
addView(v, i);
}
Log.v("countTAG", "" + count);
}
public LinearLayoutForListView(Context context) {
super(context);
}
public LinearLayoutForListView(Context context, AttributeSet attrs) {
super(context, attrs);
// TODO Auto-generated constructor stub
}
/**
* 获取Adapter
*
* @return adapter
*/
public ListAdapter getAdpater() {
return adapter;
}
/**
* 设置数据
*
* @param adpater
*/
public void setAdapter(ListAdapter adpater) {
this.adapter = adpater;
bindLinearLayout();
}
/**
* 获取点击事件
*
* @return
*/
public OnClickListener getOnclickListner() {
return onClickListener;
}
/**
* 设置点击事件
*
* @param onClickListener
*/
public void setOnclickLinstener(OnClickListener onClickListener) {
this.onClickListener = onClickListener;
}
public OnTouchListener getOnTouchListener() {
return onTouchListener;
}
public void setOnTouchListener(OnTouchListener onTouchListener) {
this.onTouchListener = onTouchListener;
}
}
这就好比一个listView
使用的时候
<com.groupbuy.view.LinearLayoutForListView
android:id="@+id/linlistview"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:scrollbars="vertical"
android:orientation="vertical"
android:paddingLeft="5dp"
android:paddingRight="5dp"
android:paddingTop="3dp"
android:paddingBottom="30dp"
android:background="#ffFFFF">
</com.groupbuy.view.LinearLayoutForListView>
adapter 和平常listView 一样
设置adapter
LinearLayoutForListView mSetupList = (LinearLayoutForListView) findViewById(R.id.linlistview);
mSetupList.setOnclickLinstener(SetupListClickEvent);
mSetupList.setOnTouchListener(SetupListTouchEvent);
mSetupList.setAdapter(new MainListAdpter(this));
//触摸事件处理
View.OnTouchListener SetupListTouchEvent = new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
// TODO Auto-generated method stub
return false;
}
};
//点击事件处理
View.OnClickListener SetupListClickEvent = new View.OnClickListener()
{
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
int position = v.getId();
Intent intent=new Intent(HomeActivity.this,ShopDetail.class);
startActivity(intent);
}
};
ok 这样就可以避免冲突啦
<ScrollView
android:layout_width="fill_parent"
android:layout_height="wrap_content">
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
>
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="200dp"
>
<android.support.v4.view.ViewPager
android:id="@+id/viewPager"
android:layout_width="fill_parent"
android:layout_height="match_parent" />
<LinearLayout
android:id="@+id/viewGroup"
android:layout_width="fill_parent"
android:layout_height="10dip"
android:layout_alignParentBottom="true"
android:layout_marginBottom="5dip"
android:layout_marginRight="10dip"
android:gravity="right"
android:orientation="horizontal" >
</LinearLayout>
</RelativeLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="1dp"
android:background="@drawable/more_item_press"
>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@drawable/account_tab_bg"
android:orientation="horizontal" >
<TextView
android:id="@+id/My_checkin"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_margin="8dp"
android:layout_weight="1"
android:gravity="center"
android:padding="10dp"
android:text="洗车"
android:textColor="#666666"
android:textSize="17sp" />
<TextView
android:id="@+id/My_comment"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_margin="8dp"
android:layout_weight="1"
android:gravity="center"
android:padding="10dp"
android:text="补胎"
android:textColor="#666666"
android:textSize="17sp" />
<TextView
android:id="@+id/My_photo"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_margin="8dp"
android:layout_weight="1"
android:gravity="center"
android:padding="10dp"
android:text="流动补胎"
android:textColor="#666666"
android:textSize="17sp" />
</LinearLayout>
</LinearLayout>
<com.groupbuy.view.LinearLayoutForListView
android:id="@+id/linlistview"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:scrollbars="vertical"
android:orientation="vertical"
android:paddingLeft="5dp"
android:paddingRight="5dp"
android:paddingTop="3dp"
android:paddingBottom="30dp"
android:background="#ffFFFF">
</com.groupbuy.view.LinearLayoutForListView>
</LinearLayout>
</ScrollView>
版权声明:本文为博主原创文章,未经博主允许不得转载。