android中双向滑动listview

因项目中需要实现表格类的显示方式,显示数量不确定,同时产品对一些体验,以及设计的要求,参考网上资料,自己写的一个双向滑动listview,废话不多说直接上代码和效果图

1.效果图



2代码:

首先,界面说明(其他东西可忽略项目中需要使用),竖向是一个listview,横向是一个自定义HorizontalScrollView,(此处没有使用自定义横向listview,因为时间紧,并且事件触摸分发繁琐,就索性直接一个自定义HorizontalScrollView)大笑大笑大笑代码如下:

CustomScrollView 类:

package view;

import android.content.Context;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.widget.HorizontalScrollView;

import fragment.PlanScheduleFragment;


public class CustomScrollView extends HorizontalScrollView{

public CustomScrollView(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
 //       activity = (MainActivity) context;
    }




    public CustomScrollView(Context context, AttributeSet attrs) {
        super(context, attrs);
//        activity = (MainActivity) context;
    }


    public CustomScrollView(Context context) {
        super(context);
//        activity = (MainActivity) context;
    }

    @Override
    public boolean onTouchEvent(MotionEvent ev) {
        //PlanScheduleFragment的实例,当前的mTouchView 是CustomScrollView的实例,滑动进行触摸赋值
    PlanScheduleFragment.planScheduleFragment.mTouchView = this;
        return super.onTouchEvent(ev);
    }


    @Override
    protected void onScrollChanged(int l, int t, int oldl, int oldt) {
        //当前的CHSCrollView被触摸时,滑动其它
        if (PlanScheduleFragment.planScheduleFragment.mTouchView  == this) {
        PlanScheduleFragment.planScheduleFragment.onScrollChanged(l, t, oldl, oldt);
        } else {
            super.onScrollChanged(l, t, oldl, oldt);
        }
    }


}


PlanScheduleFragment类:

package fragment;


import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


import utils.ScheduleManagerUtil;
import view.CustomScrollView;
import android.content.Context;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.HorizontalScrollView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;
import bean.ProjectBean;


import com.schedule.schedulemanager.R;


import fragment.PlanScheduleFragment.ScrollAdapter;
import fragment.PlanScheduleFragment.ScrollAdapter.ViewHolder;


public class PlanScheduleFragment extends Fragment {


private View view;
private TextView header_back;
private TextView header_button_right;
public static HorizontalScrollView mTouchView;
public static PlanScheduleFragment planScheduleFragment = null;


private List<CustomScrollView> mHScrollViews = new ArrayList<CustomScrollView>();


private ListView mListView;


private ScrollAdapter mAdapter;


public static String [] floorName = new String[]{ "9F", "8F", "7F", "6F", "5F", "4F", 
"3F","2F","1F", "B1"};
@Override
public void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
planScheduleFragment = this;
}


@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
view = view = inflater.inflate(R.layout.planschedulefragment, null);
header_back = (TextView) view.findViewById(R.id.header_back);
header_button_right = (TextView) view
.findViewById(R.id.header_button_right);
CustomScrollView headerScroll = (CustomScrollView) view
.findViewById(R.id.item_scroll_title);
// 添加头滑动事件
mHScrollViews.add(headerScroll);
mListView = (ListView) view.findViewById(R.id.hlistview_scroll_list);
initView();
return view;
}
    //添加模拟数据,并设置adapter显示
private void initView() {
List<Map<String, ProjectBean>> datas = new ArrayList<Map<String, ProjectBean>>();
for (int i = 0; i < floorName.length; i++) {
Map<String, ProjectBean> data = new HashMap<String, ProjectBean>();
//定义的一个bean对象,作为item中CustomScrollView中的每一项linearLayout的数据模型
ProjectBean projectBean = new ProjectBean();
projectBean.setName(ScheduleManagerUtil.floorName[i]);
data.put("title" + i, projectBean);
for (int j = 0; j < 7; j++) {
ProjectBean projectbeanTemp = new ProjectBean();
projectbeanTemp.setName("12" + j);
projectbeanTemp.setDate("日期");
data.put("A_" + j, projectbeanTemp);
}
datas.add(data);
}


mAdapter = new ScrollAdapter(getActivity(), datas);
mListView.setAdapter(mAdapter);
}
 

//因为是刚刚写完所以,没怎么按照一定格式整理,对应类分开的,同时放在一起感觉方便看代码
class ScrollAdapter extends BaseAdapter {


private List<? extends Map<String, ?>> datas;
private int[] to;
private Context context;


public ScrollAdapter(Context context,
List<? extends Map<String, ?>> data) {
this.context = context;
this.datas = data;
}


@Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder viewHolder = null;
if (convertView == null) {
viewHolder = new ViewHolder();
convertView = LayoutInflater.from(context).inflate(
R.layout.hilstview_item, null);
// 第一次初始化的时候添加进头部
addHViews((CustomScrollView) convertView
.findViewById(R.id.item_chscroll_scroll));
viewHolder.item_titlev = (TextView) convertView
.findViewById(R.id.item_titlev);
viewHolder.item_datav1 = (TextView) convertView
.findViewById(R.id.item_datav1);
viewHolder.item_datav2 = (TextView) convertView
.findViewById(R.id.item_datav2);
viewHolder.item_datav3 = (TextView) convertView
.findViewById(R.id.item_datav3);
viewHolder.item_datav4 = (TextView) convertView
.findViewById(R.id.item_datav4);
viewHolder.item_datav5 = (TextView) convertView
.findViewById(R.id.item_datav5);
viewHolder.item_datav6 = (TextView) convertView
.findViewById(R.id.item_datav6);
viewHolder.item_datav7 = (TextView) convertView
.findViewById(R.id.item_datav7);
viewHolder.item_datav8 = (TextView) convertView
.findViewById(R.id.item_datav8);
viewHolder.item_datav9 = (TextView) convertView
.findViewById(R.id.item_datav9);
viewHolder.item_datav10 = (TextView) convertView
.findViewById(R.id.item_datav10);
viewHolder.item_datav11 = (TextView) convertView
.findViewById(R.id.item_datav11);
viewHolder.item_datav12 = (TextView) convertView
.findViewById(R.id.item_datav12);
viewHolder.item_datav13 = (TextView) convertView
.findViewById(R.id.item_datav13);
viewHolder.item_datav14 = (TextView) convertView
.findViewById(R.id.item_datav14);


viewHolder.layout1 = (LinearLayout) convertView
.findViewById(R.id.item1_layout);
viewHolder.layout2 = (LinearLayout) convertView
.findViewById(R.id.item2_layout);
viewHolder.layout3 = (LinearLayout) convertView
.findViewById(R.id.item3_layout);
viewHolder.layout4 = (LinearLayout) convertView
.findViewById(R.id.item4_layout);
viewHolder.layout5 = (LinearLayout) convertView
.findViewById(R.id.item5_layout);
viewHolder.layout6 = (LinearLayout) convertView
.findViewById(R.id.item6_layout);
viewHolder.layout7 = (LinearLayout) convertView
.findViewById(R.id.item7_layout);
convertView.setTag(viewHolder);
} else {
viewHolder = (ViewHolder) convertView.getTag();
}
final Map<String, ProjectBean> map = (Map<String, ProjectBean>) datas
.get(position);
viewHolder.item_titlev.setText(map.get("title" + position)
.getName());
//item每一项我这边暂时是定义为7个,固定值(这也是使用这个自定义的一个不足之处,如果使用横向的listview,就可以自动适配数据模型的数量)
for (int j = 0; j < 7; j++) {
switch (j) {
case 0:
viewHolder.item_datav1.setText(map.get("A_" + j).getName());
viewHolder.item_datav2.setText(map.get("A_" + j).getDate());
viewHolder.layout1.setId(j);
//这里是每一项的点击事件,暂时这样处理,(可以定义一个接口,传值出去,然后统一处理这些),一下都一样的
viewHolder.layout1
.setOnClickListener(new View.OnClickListener() {


@Override
public void onClick(View arg0) {
Toast.makeText(
context,
map.get("A_" + arg0.getId())
.getName(),
Toast.LENGTH_LONG).show();
}
});
break;
case 1:
viewHolder.item_datav3.setText(map.get("A_" + j).getName());
viewHolder.item_datav4.setText(map.get("A_" + j).getDate());
viewHolder.layout2.setId(j);
viewHolder.layout2
.setOnClickListener(new View.OnClickListener() {


@Override
public void onClick(View arg0) {
Toast.makeText(
context,
map.get("A_" + arg0.getId())
.getName(),
Toast.LENGTH_LONG).show();
}
});
break;
case 2:
viewHolder.item_datav5.setText(map.get("A_" + j).getName());
viewHolder.item_datav6.setText(map.get("A_" + j).getDate());
viewHolder.layout3.setId(j);
viewHolder.layout3
.setOnClickListener(new View.OnClickListener() {


@Override
public void onClick(View arg0) {
Toast.makeText(
context,
map.get("A_" + arg0.getId())
.getName(),
Toast.LENGTH_LONG).show();
}
});
break;
case 3:
viewHolder.item_datav7.setText(map.get("A_" + j).getName());
viewHolder.item_datav8.setText(map.get("A_" + j).getDate());
viewHolder.layout4.setId(j);
viewHolder.layout4
.setOnClickListener(new View.OnClickListener() {


@Override
public void onClick(View arg0) {
Toast.makeText(
context,
map.get("A_" + arg0.getId())
.getName(),
Toast.LENGTH_LONG).show();
}
});
break;
case 4:
viewHolder.item_datav9.setText(map.get("A_" + j).getName());
viewHolder.item_datav10
.setText(map.get("A_" + j).getDate());
viewHolder.layout5.setId(j);
viewHolder.layout5
.setOnClickListener(new View.OnClickListener() {


@Override
public void onClick(View arg0) {
Toast.makeText(
context,
map.get("A_" + arg0.getId())
.getName(),
Toast.LENGTH_LONG).show();
}
});
break;
case 5:
viewHolder.item_datav11
.setText(map.get("A_" + j).getName());
viewHolder.item_datav12
.setText(map.get("A_" + j).getDate());
viewHolder.layout6.setId(j);
viewHolder.layout6
.setOnClickListener(new View.OnClickListener() {


@Override
public void onClick(View arg0) {
Toast.makeText(
context,
map.get("A_" + arg0.getId())
.getName(),
Toast.LENGTH_LONG).show();
}
});
break;
case 6:
viewHolder.item_datav13
.setText(map.get("A_" + j).getName());
viewHolder.item_datav14
.setText(map.get("A_" + j).getDate());
viewHolder.layout7.setId(j);
viewHolder.layout7
.setOnClickListener(new View.OnClickListener() {


@Override
public void onClick(View arg0) {
Toast.makeText(
context,
map.get("A_" + arg0.getId())
.getName(),
Toast.LENGTH_LONG).show();
}
});
break;
}
}
return convertView;
}


@Override
public int getCount() {
// TODO Auto-generated method stub
return datas.size();
}


@Override
public Object getItem(int arg0) {
// TODO Auto-generated method stub
return datas.get(arg0);
}


@Override
public long getItemId(int arg0) {
// TODO Auto-generated method stub
return arg0;
}


class ViewHolder {
private TextView item_titlev;
private TextView item_datav1;
private TextView item_datav2;
private TextView item_datav3;
private TextView item_datav4;
private TextView item_datav5;


private TextView item_datav6;
private TextView item_datav7;
private TextView item_datav8;
private TextView item_datav9;
private TextView item_datav10;


private TextView item_datav11;
private TextView item_datav12;
private TextView item_datav13;
private TextView item_datav14;


private LinearLayout layout1;
private LinearLayout layout2;
private LinearLayout layout3;
private LinearLayout layout4;
private LinearLayout layout5;
private LinearLayout layout6;
private LinearLayout layout7;


}


}



//添加自定义的CustomScrollView到listview中,作为listview的item项
public void addHViews(final CustomScrollView hScrollView) {
if (!mHScrollViews.isEmpty()) {
int size = mHScrollViews.size();
CustomScrollView 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);
}


//滑动位置的改变,供自定义CustomScrollView里面的调用(也就是为什么要当前这个fragment的实例)
public void onScrollChanged(int l, int t, int oldl, int oldt) {
for (CustomScrollView scrollView : mHScrollViews) {
// 防止重复滑动
if (mTouchView != scrollView)
scrollView.smoothScrollTo(l, t);
}
}


@Override
public void onResume() {
// TODO Auto-generated method stub
super.onResume();
}


@Override
public void onStop() {
// TODO Auto-generated method stub
super.onStop();
}


@Override
public void onDestroyView() {
// TODO Auto-generated method stub
super.onDestroyView();
}


@Override
public void onDestroy() {
// TODO Auto-generated method stub
super.onDestroy();
}

}



最后就是简单的bean:(里面属性字段可随便扩展,在item中可以随时判断,做不同显示和处理以及数据暂存)

package bean;


import java.io.Serializable;


public class ProjectBean implements Serializable {


private String name;
private String date;
private int color;


public String getName() {
return name;
}


public void setName(String name) {
this.name = name;
}


public String getDate() {
return date;
}


public void setDate(String date) {
this.date = date;
}


public int getColor() {
return color;
}


public void setColor(int color) {
this.color = color;
}


}



  • 1
    点赞
  • 3
    评论
  • 0
    收藏
  • 一键三连
    一键三连
  • 扫一扫,分享海报

©️2021 CSDN 皮肤主题: 大白 设计师:CSDN官方博客 返回首页
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、C币套餐、付费专栏及课程。

余额充值