首先是重写了一个HorizontalScrollView 作为主要滑动控件,
然后是在MyBaseAdapter中 将这个自定义的ScrollView设置为根控件
在MyBaseAdapter 和 MyBaseHolder 中封装好菜单的动态控制方法
代码如下:
package com.test_scroll_to_do;
import android.content.Context;
import android.graphics.Color;
import android.support.v4.app.Fragment;
import android.util.AttributeSet;
import android.view.Gravity;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.FrameLayout;
import android.widget.HorizontalScrollView;
import android.widget.LinearLayout;
import android.widget.ScrollView;
import android.widget.TextView;
/**
* Created by Administrator on 2016/8/5.
*/
public class MyScrollView extends HorizontalScrollView {
private int nowLeft = 0;
private int menuWidth = 0;
private int height;
private LinearLayout linearLayout;
private int width;
public static MyScrollView selectMyScrollView ;
public MyScrollView(Context context) {
this(context, null);
}
public MyScrollView(Context context, AttributeSet attrs) {
this(context, attrs, 0);
}
public MyScrollView(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
init();
}
private void init(){
linearLayout = new LinearLayout(getContext());
FrameLayout.LayoutParams params = new FrameLayout.LayoutParams
(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT);
addView(linearLayout, 0, params);
}
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
width = MeasureSpec.getSize(widthMeasureSpec);
layoutChild();
}
private void layoutChild(){
if(linearLayout.getChildCount() > 0){
View content = linearLayout.getChildAt(0);
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams
(width,height);
content.setLayoutParams(params);
}
}
public void setItemPosition(int position){
linearLayout.setTag(position);
}
public void addContent(View v){
if(linearLayout.getChildCount() != 0)
throw new RuntimeException("viewgroup had a contentView now");
v.measure(0, 0);
height = v.getMeasuredHeight();
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams
(width,height);
linearLayout.addView(v, 0, params);
}
public void setMenuVisible(String menuTag,int isVisible){
View v = linearLayout.findViewWithTag(menuTag);
if(v != null){
v.setVisibility(isVisible);
}
}
public void addMenu(String text, Object tag,OnClickListener onClickListener){
addMenu(text, tag, Color.BLACK,Color.WHITE,onClickListener);
}
public void addMenu(String text, Object tag,int tvColor,int bgColor,OnClickListener onClickListener){
TextView t = new TextView(getContext());
t.setGravity(Gravity.CENTER);
t.setText(text);
t.setTextColor(tvColor);
t.setBackgroundColor(bgColor);
t.setTag(tag);
t.setOnClickListener(onClickListener);
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams
((int) (height * 1.5),height);
linearLayout.addView(t, linearLayout.getChildCount(), params);
menuWidth = (int) ((height * 1.5)) * (linearLayout.getChildCount() - 1);
}
@Override
protected void onScrollChanged(int l, int t, int oldl, int oldt) {
nowLeft = l;
}
@Override
public boolean onTouchEvent(MotionEvent ev) {
if(ev.getAction() == MotionEvent.ACTION_UP){
if(nowLeft >= menuWidth/2){
scrollTo(menuWidth,0);
}
else{
scrollTo(0,0);
}
return true;
}
else{
if(selectMyScrollView != this){
if(selectMyScrollView != null){
selectMyScrollView.scrollTo(0,0);
}
}
selectMyScrollView = this;
return super.onTouchEvent(ev);
}
}
}
MyBaseAdapter
package com.test_scroll_to_do;
import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ScrollView;
import android.widget.Toast;
/**
* Created by Administrator on 2016/8/5.
*/
public abstract class MyBaseAdapter extends BaseAdapter{
@Override
public View getView(int i, View view, ViewGroup viewGroup) {
MyBaseHolder myBaseHolder = null;
if(view == null){
MyScrollView scrollView = new MyScrollView(viewGroup.getContext());
scrollView.addContent(getContentView(viewGroup.getContext()));
myBaseHolder = createHolder(scrollView);
scrollView.setTag(myBaseHolder);
view = scrollView;
}
else{
myBaseHolder = (MyBaseHolder) view.getTag();
}
myBaseHolder.bindView(i);
return view;
}
abstract MyBaseHolder createHolder(MyScrollView myScrollView);
abstract View getContentView(Context context);
class MyBaseHolder {
MyScrollView myScrollView;
public MyBaseHolder(MyScrollView myScrollView){
this.myScrollView = myScrollView;
}
public void addView(String text,Object tag,View.OnClickListener onClickListener){
myScrollView.addMenu(text,tag,onClickListener);
}
public void addView(String text,Object tag,int tvColor,int bgColor,View.OnClickListener onClickListener){
myScrollView.addMenu(text,tag,tvColor,bgColor,onClickListener);
}
public void setMenuVisibble(String tag , int visible){
myScrollView.setMenuVisible(tag,visible);
}
public void bindView(int position){
}
public void setPosition(int position){
myScrollView.setItemPosition(position);
}
}
}