标签:android 文档 android应用 listview 界面
listview首例不动其他可以横向滑动
下载地址:http://download.csdn.net/detail/menglele1314/8640023
自定义HorizontalScrollView
public class CHScrollView extends HorizontalScrollView {
MainActivity activity;
public CHScrollView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
activity = (MainActivity) context;
}
public CHScrollView(Context context, AttributeSet attrs) {
super(context, attrs);
activity = (MainActivity) context;
}
public CHScrollView(Context context) {
super(context);
activity = (MainActivity) context;
}
@Override
public boolean onTouchEvent(MotionEvent ev) {
//进行触摸赋值
activity.mTouchView = this;
return super.onTouchEvent(ev);
}
@Override
protected void onScrollChanged(int l, int t, int oldl, int oldt) {
//当当前的CHSCrollView被触摸时,滑动其它
if(activity.mTouchView == this) {
activity.onScrollChanged(l, t, oldl, oldt);
}else{
super.onScrollChanged(l, t, oldl, oldt);
}
}
}
/**
*
* 带滑动表头与固定列的ListView
*/
public class MainActivity extends Activity {
private ListView mListView;
// 方便测试,直接写的public
public HorizontalScrollView mTouchView;
// 装入所有的HScrollView
private List<CHScrollView> mHScrollViews = new ArrayList<CHScrollView>();
private ImageView leftOk,rightOk,leftNo,rightNo;
private GridView gridView;
private List<Book> datas = new ArrayList<Book>();
private List<String> name;
public HorizontalScrollView gTouchView;
// 装入所有的HScrollView
private List<GHScrollView> gHScrollViews = new ArrayList<GHScrollView>();
private ImageView leftView;
private ImageView rightView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
initGridView();
initListView();
}
private void initGridView() {
setData();
leftView = (ImageView) findViewById(R.id.iv_left_ok_i);
rightView = (ImageView) findViewById(R.id.iv_right_ok_i);
gridView = (GridView) findViewById(R.id.grid);
GHScrollView gscroll = (GHScrollView) findViewById(R.id.grid_title);
// 添加头滑动事件
gHScrollViews.add(gscroll);
int size = name.size();
DisplayMetrics dm = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(dm);
float density = dm.density;
int gridviewWidth = (int) ((size * 80) * density);
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(
gridviewWidth, LinearLayout.LayoutParams.WRAP_CONTENT);
gridView.setLayoutParams(params); // 宽度
gridView.setColumnWidth(80);
gridView.setNumColumns(size); // 列数数量
GridViewAdapter adapter = new GridViewAdapter(this, name);
gridView.setAdapter(adapter);
}
private void initListView() {
leftOk = (ImageView) findViewById(R.id.iv_left_ok);
leftNo = (ImageView) findViewById(R.id.iv_left_no);
rightOk = (ImageView) findViewById(R.id.iv_right_ok);
rightNo = (ImageView) findViewById(R.id.iv_right_no);
CHScrollView headerScroll = (CHScrollView) findViewById(R.id.item_scroll_title);
// 添加头滑动事件
mHScrollViews.add(headerScroll);
mListView = (ListView) findViewById(R.id.scroll_list);
for (int i = 0; i < 100; i++) {
datas.add(new Book("book" + i, "ccc" + i, "ddd" + i, "aaa" + i,
"bbb" + i, "eee" + i, "fff" + i));
}
ListAdapter adapter = new ListAdapter(this, mHScrollViews, mListView,
datas);
mListView.setAdapter(adapter);
}
public void onGcrollChanged(int l, int t, int oldl, int oldt) {
DisplayMetrics dm = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(dm);
int width = dm.widthPixels;
Log.i("asfdsfas", l+"=="+t+"=="+oldl+"=="+oldt+"=="+width);
if (l == 0) {
leftView.setVisibility(View.GONE);
} else {
leftView.setVisibility(View.VISIBLE);
}
if (l > width) {
rightView.setVisibility(View.GONE);
} else {
rightView.setVisibility(View.VISIBLE);
}
for (GHScrollView scrollView : gHScrollViews) {
// 防止重复滑动
if (gTouchView != scrollView)
scrollView.smoothScrollTo(l, t);
}
}
public void onScrollChanged(int l, int t, int oldl, int oldt) {
DisplayMetrics dm = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(dm);
int width = dm.widthPixels;
if (l == 0) {
leftOk.setVisibility(View.GONE);
leftNo.setVisibility(View.VISIBLE);
} else {
leftOk.setVisibility(View.VISIBLE);
leftNo.setVisibility(View.GONE);
}
if (l == width) {
rightOk.setVisibility(View.GONE);
rightNo.setVisibility(View.VISIBLE);
} else {
rightOk.setVisibility(View.VISIBLE);
rightNo.setVisibility(View.GONE);
}
for (CHScrollView scrollView : mHScrollViews) {
// 防止重复滑动
if (mTouchView != scrollView)
scrollView.smoothScrollTo(l, t);
}
}
private void setData() {
name = new ArrayList<String>();
name.add("深圳");
name.add("上海");
name.add("广州");
name.add("北京");
name.add("武汉");
name.add("济南");
name.add("南京");
name.add("天津");
name.add("重庆");
}
}
适配器
public class ListAdapter extends BaseAdapter {
private List<Book> book;
private LayoutInflater inflater;
private List<CHScrollView> mHScrollViews;
private ListView mListView;
private Context context;
public ListAdapter(Context context, List<CHScrollView> mHScrollViews,
ListView mListView, List<Book> book) {
this.context = context;
this.inflater = LayoutInflater.from(context);
this.mHScrollViews = mHScrollViews;
this.mListView = mListView;
this.book = book;
}
@Override
public int getCount() {
// TODO Auto-generated method stub
return book.size();
}
@Override
public Book getItem(int position) {
return book.get(position);
}
@Override
public long getItemId(int position) {
// TODO Auto-generated method stub
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder = null;
if (convertView == null) {
convertView = inflater.inflate(R.layout.list_item, null);
// 第一次初始化的时候装进来
addHViews((CHScrollView) convertView.findViewById(R.id.item_scroll));
holder = new ViewHolder();
holder.item_title = (TextView) convertView
.findViewById(R.id.item_title);
holder.item_data1 = (TextView) convertView
.findViewById(R.id.item_data1);
holder.item_data2 = (TextView) convertView
.findViewById(R.id.item_data2);
holder.item_data3 = (TextView) convertView
.findViewById(R.id.item_data3);
holder.item_data4 = (TextView) convertView
.findViewById(R.id.item_data4);
holder.item_data5 = (TextView) convertView
.findViewById(R.id.item_data5);
holder.item_data6 = (TextView) convertView
.findViewById(R.id.item_data6);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
final Book book = getItem(position);
holder.item_title.setText(book.getName());
holder.item_data1.setText(book.getA());
holder.item_data2.setText(book.getB());
holder.item_data3.setText(book.getC());
holder.item_data4.setText(book.getD());
holder.item_data5.setText(book.getE());
holder.item_data6.setText(book.getF());
holder.item_title.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
Toast.makeText(context, "xxx" + book.getName(), 1).show();
}
});
return convertView;
}
private class ViewHolder {
private TextView item_title;
private TextView item_data1;
private TextView item_data2;
private TextView item_data3;
private TextView item_data4;
private TextView item_data5;
private TextView item_data6;
}
public void addHViews(final CHScrollView hScrollView) {
if (!mHScrollViews.isEmpty()) {
int size = mHScrollViews.size();
CHScrollView scrollView = mHScrollViews.get(size - 1);
final int scrollX = scrollView.getScrollX();
// 第一次满屏后,向下滑动,有一条数据在开始时未加入
if (scrollX != 0) {
mListView.post(new Runnable() {
@Override
public void run() {
// 当listView刷新完成之后,把该条移动到最终位置
hScrollView.scrollTo(scrollX, 0);
}
});
}
}
mHScrollViews.add(hScrollView);
}
}
......
下载地址:http://download.csdn.net/detail/menglele1314/8640023
标签:android 文档 android应用 listview 界面
原文地址:http://blog.csdn.net/menglele1314/article/details/45331749