1. GridView
<GridView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:numColumns="auto_fit" 列数设置为自动
android:columnWidth="90dp" 每列的宽度,也就是Item的宽度
android:stretchMode="columnWidth" 缩放与列宽大小同步
android:verticalSpacing="10dp" 两行之间的边距
android:horizontalSpacing="10dp"/> 两列之间的边距
2.重新计算Listview高度
public class NoScrollListView extends ListView{
public NoScrollListView(Context context, AttributeSet attrs){
super(context, attrs);
}
public void onMeasure(int widthMeasureSpec, int heightMeasureSpec){
int mExpandSpec = MeasureSpec.makeMeasureSpec(Integer.MAX_VALUE >> 2, MeasureSpec.AT_MOST);
super.onMeasure(widthMeasureSpec, mExpandSpec);
}
}
3.ListView左滑删除效果
convertView.setOnTouchListener(new OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
final int eA = event.getAction();
switch (eA) {
case MotionEvent.ACTION_DOWN:
if (vview != null) {
ViewHolder viewHolder1 = (ViewHolder) vview.getTag();
viewHolder1.HSView.smoothScrollTo(0, 0);
}
case MotionEvent.ACTION_UP:
ViewHolder viewHolder2 = (ViewHolder) v.getTag();
vview = v;
int scrollX = viewHolder2.HSView.getScrollX();
int actionW = viewHolder2.b1.getWidth();
if (scrollX < actionW / 3) {
viewHolder2.HSView.smoothScrollTo(0, 0);
} else {
viewHolder2.HSView.smoothScrollTo(actionW, 0);
}
return true;
case MotionEvent.ACTION_MOVE:
}
return false;
}
});
private void removeItemView(View v, final int position) {
Animation animation = AnimationUtils.loadAnimation(v.getContext(),
android.R.anim.slide_out_right);
animation.setAnimationListener(new AnimationListener() {
@Override
public void onAnimationStart(Animation animation) {
}
public void onAnimationRepeat(Animation animation) {
}
public void onAnimationEnd(Animation animation) {
itemListener.onDeleted(position);
}
});
v.startAnimation(animation);
}
private class ViewHolder {
private HorizontalScrollView HSView;
}
4. GetView方法里内容
ViewHolder holder;
if(convertView==null){
convertView = LayoutInflater.from(context).inflate(R.layout.frg, null);
holder = new ViewHolder();
holder.iName = (TextView) convertView.findViewById(R.id.gutan);
convertView.setTag(holder);
}else{
holder = (ViewHolder) convertView.getTag();
}
HashMap<String, Object> map = maps.get(position);
return convertView;
public final class ViewHolder{
TextView iName;
}
5. ListView加单选按钮
1、Adapter
private HashMap<String, Boolean> states = new HashMap<String, Boolean>();
final RadioButton radio = (RadioButton) view.findViewById(R.id.radio_btn);
vh.rdBtn = radio;
vh.rdBtn.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
onTenderRedAdapListener.onTenderRedAdap(TenderRedAdapter.this, bonus_record_id,amount);
for (String key : states.keySet()) {
states.put(key, false);
}
states.put(String.valueOf(i), radio.isChecked());
TenderRedAdapter.this.notifyDataSetChanged();
}
});
boolean res = false;
if (states.get(String.valueOf(i)) == null || states.get(String.valueOf(i)) == false) {
res = false;
states.put(String.valueOf(i), false);
} else
res = true;
vh.rdBtn.setChecked(res);
2、XML
<RadioButton
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginLeft="50dp"
style="@style/CustomCheckboxTheme"
android:gravity="center"
android:checked="false"
android:layout_weight="3"/>
3、styles
<style name="CustomCheckboxTheme" parent="@android:style/Widget.CompoundButton.CheckBox">
<item name="android:button">@drawable/spa</item>
</style>
4、drawable
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<!-- 勾选的样式图放到res目录下的drawable中,然后在这里使用 -->
<item android:drawable="@mipmap/tender_check_1" android:state_checked="true" />
<!-- 未勾选的样式图放到res目录下的drawable中,然后在这里使用 -->
<item android:drawable="@mipmap/tender_check_2" android:state_checked="false" />
<item android:drawable="@mipmap/tender_check_2" />
</selector>
6.单击ListView等控件背景颜色变换Select
单击效果1
<item android:state_focused="true" android:state_pressed="true" android:drawable="@color/orange"></item>
<item android:state_focused="false" android:state_pressed="true" android:drawable="@color/white"></item>
单击效果2
<item android:drawable="@drawable/gushen_gushen2" android:state_enabled="true"
android:state_focused="true" android:state_pressed="false"/>
<item android:drawable="@drawable/gushen_gushen2"
android:state_checked="true" android:state_enabled="true"/>
7.ListView ItemClick和Button单击事件冲突的解决
在包含button的Layout中加入属性 android:descendantFocusability= "blocksDescendants"
在buttion的属性加入android:focusable="false"
android:descendantFocusability该属性是当一个为view获取焦点时,定义viewGroup和其子控件两者之间的关系。属性的值有三种:
beforeDescendants:viewgroup会优先其子类控件而获取到焦点
afterDescendants:viewgroup只有当其子类控件不需要获取焦点时才获取焦点
blocksDescendants:viewgroup会覆盖子类控件而直接获得焦点
通常我们用到的是第三种,即在Item布局的根布局加上android:descendantFocusability=”blocksDescendants”的属性就好了
8.v.getTag()和v.setTag的理解
ListView滑动时数据混乱,滑动过程中图片显示重复错位闪烁问题,出现错乱的原因是异步加载及对象被复用造成的,
如果每次getView能给对象一个标识,在异步加载完成时比较标识与当前行item的标识是否一致,一致则显示,否则不做处理即可