android实现界面左右滑动(GridView动态设置item,支持每个item按某个属性排序来显示在不同的界面)
效果图 :
分别是第一页、第二页、第三页,随手截的图,不整齐,勿见怪。开始走了弯路,废了不少时间。
思路如下:
1、用ViewPager实现左右分页滑动,布局最下面远点标记页码是根据有多个item动态添加的。
2、每个页面的GridView的item的个数根据屏幕的宽度高度来动态设置。
3、每个界面的GridView的adapter也是根据当前页面的itemCount来动态设置的。
主要代码:
- package com.example.testview;
- import java.util.ArrayList;
- import java.util.List;
- import android.content.Context;
- import android.view.View;
- import android.view.ViewGroup;
- import android.widget.BaseAdapter;
- import android.widget.ImageView;
- import android.widget.TextView;
- public class ViewPager_GV_ItemAdapter extends BaseAdapter {
- private List<ChannelInfoBean> list_info;
- private Context context;
- /** ViewPager页码 */
- private int index;
- /** 根据屏幕大小计算得到的每页item个数 */
- private int pageItemCount;
- /** 传进来的List的总长度 */
- private int totalSize;
- /** 当前页item的实际个数 */
- // private int itemRealNum;
- @SuppressWarnings("unchecked")
- public ViewPager_GV_ItemAdapter(Context context, List<?> list) {
- this.context = context;
- this.list_info = (List<ChannelInfoBean>) list;
- }
- public ViewPager_GV_ItemAdapter(Context context, List<?> list, int index, int pageItemCount) {
- this.context = context;
- this.index = index;
- this.pageItemCount = pageItemCount;
- list_info = new ArrayList<ChannelInfoBean>();
- totalSize = list.size();
- // itemRealNum=list.size()-index*pageItemCount;
- // 当前页的item对应的实体在List<?>中的其实下标
- int list_index = index * pageItemCount;
- for (int i = list_index; i < list.size(); i++) {
- list_info.add((ChannelInfoBean) list.get(i));
- }
- }
- @Override
- public int getCount() {
- int size = totalSize / pageItemCount;
- if (index == size)
- return totalSize - pageItemCount * index;
- else
- return pageItemCount;
- // return itemRealNum;
- }
- @Override
- public Object getItem(int arg0) {
- return null;
- }
- @Override
- public long getItemId(int position) {
- return position;
- }
- @Override
- public View getView(int position, View convertView, ViewGroup parent) {
- ViewHolder iv;
- if (convertView == null)
- iv = new ViewHolder(context, R.layout.channel_gridview_item);
- else
- iv = (ViewHolder) convertView.getTag();
- iv.updateViews(position, null);
- return iv.mView;
- }
- class ViewHolder extends ChannelViewHolder {
- ImageView iv_icon;
- TextView tv_name;
- public ViewHolder(Context context, int resource) {
- super(context, resource);
- }
- @Override
- protected void findViews() {
- iv_icon = (ImageView) mView.findViewById(R.id.iv_gv_item_icon);
- tv_name = (TextView) mView.findViewById(R.id.tv_gv_item_Name);
- }
- @Override
- protected void updateViews(int position, Object inst) {
- // 不管用
- // iv_icon.setBackgroundResource(list_info.get(position).getIconID());
- iv_icon.setImageResource(list_info.get(position).getIconID());
- tv_name.setText(list_info.get(position).getName());
- }
- }
- }
GridViewGallery:
- package com.example.testview;
- import java.util.ArrayList;
- import java.util.List;
- import android.content.Context;
- import android.support.v4.view.ViewPager;
- import android.util.AttributeSet;
- import android.view.LayoutInflater;
- import android.view.View;
- import android.widget.AdapterView;
- import android.widget.AdapterView.OnItemClickListener;
- import android.widget.GridView;
- import android.widget.ImageView;
- import android.widget.LinearLayout;
- /**
- *
- * GridView滑动
- *
- * @author chenwansong
- *
- */
- public class GridViewGallery extends LinearLayout {
- private Context context;
- /** 保存实体对象链表 */
- private List<ChannelInfoBean> list;
- private ViewPager viewPager;
- private LinearLayout ll_dot;
- private ImageView[] dots;
- /** ViewPager当前页 */
- private int currentIndex;
- /** ViewPager页数 */
- private int viewPager_size;
- /** 默认一页12个item */
- private int pageItemCount = 12;
- /** 保存每个页面的GridView视图 */
- private List<View> list_Views;
- public GridViewGallery(Context context, AttributeSet attrs) {
- super(context, attrs);
- this.context = context;
- this.list = null;
- initView();
- }
- @SuppressWarnings("unchecked")
- public GridViewGallery(Context context, List<?> list) {
- super(context);
- this.context = context;
- this.list = (List<ChannelInfoBean>) list;
- initView();
- initDots();
- setAdapter();
- }
- private void setAdapter() {
- list_Views = new ArrayList<View>();
- for (int i = 0; i < viewPager_size; i++) {
- list_Views.add(getViewPagerItem(i));
- }
- viewPager.setAdapter(new ViewPager_GridView_Adapter(list_Views));
- }
- private void initView() {
- View view = LayoutInflater.from(context).inflate(R.layout.channel_activity, null);
- viewPager = (ViewPager) view.findViewById(R.id.vPager);
- ll_dot = (LinearLayout) view.findViewById(R.id.ll_channel_dots);
- addView(view);
- }
- // 初始化底部小圆点
- private void initDots() {
- // 根据屏幕宽度高度计算pageItemCount
- int width = Util.getWindowWidth(context);
- int high = Util.getWindowHeight(context);
- int col = (width / 160) > 2 ? (width / 160) : 3;
- int row = (high / 200) > 4 ? (high / 200) : 4;
- pageItemCount = col * row;
- viewPager_size = list.size() / pageItemCount + 1;
- if (0 < viewPager_size) {
- ll_dot.removeAllViews();
- if (1 == viewPager_size) {
- ll_dot.setVisibility(View.GONE);
- } else if (1 < viewPager_size) {
- ll_dot.setVisibility(View.VISIBLE);
- for (int j = 0; j < viewPager_size; j++) {
- ImageView image = new ImageView(context);
- LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(10, 10);
- params.setMargins(3, 0, 3, 0);
- image.setBackgroundResource(R.drawable.channel_dot);
- ll_dot.addView(image, params);
- }
- }
- }
- if (viewPager_size != 1) {
- dots = new ImageView[viewPager_size];
- for (int i = 0; i < viewPager_size; i++) {
- dots[i] = (ImageView) ll_dot.getChildAt(i);
- dots[i].setEnabled(true);
- dots[i].setTag(i);
- }
- currentIndex = 0;
- dots[currentIndex].setEnabled(false);
- viewPager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
- @Override
- public void onPageSelected(int arg0) {
- setCurDot(arg0);
- }
- @Override
- public void onPageScrolled(int arg0, float arg1, int arg2) {
- // TODO Auto-generated method stub
- }
- @Override
- public void onPageScrollStateChanged(int arg0) {
- // TODO Auto-generated method stub
- }
- });
- }
- }
- /** 当前底部小圆点 */
- private void setCurDot(int positon) {
- if (positon < 0 || positon > viewPager_size - 1 || currentIndex == positon) {
- return;
- }
- dots[positon].setEnabled(false);
- dots[currentIndex].setEnabled(true);
- currentIndex = positon;
- }
- private View getViewPagerItem(int index) {
- LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
- View layout = inflater.inflate(R.layout.channel_viewpage_gridview, null);
- GridView gridView = (GridView) layout.findViewById(R.id.vp_gv);
- int width = Util.getWindowWidth(context);
- int col = (width / 160) > 2 ? (width / 160) : 3;
- gridView.setNumColumns(col);
- ViewPager_GV_ItemAdapter adapter = new ViewPager_GV_ItemAdapter(context, list, index, pageItemCount);
- gridView.setAdapter(adapter);
- gridView.setOnItemClickListener(new OnItemClickListener() {
- @Override
- public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
- if (null != list.get(position + currentIndex * pageItemCount).getOnClickListener())
- list.get(position + currentIndex * pageItemCount).getOnClickListener().ongvItemClickListener(view);
- }
- });
- return gridView;
- }
- }
实体类(贴上来没啥用,主要是处理不同item的点击事件的时候,这么处理的:定义了一个点击的事件接口,实例化的时候实现这个item需要做的事情):
- package com.example.testview;
- import java.util.Collections;
- import java.util.List;
- import android.graphics.drawable.Drawable;
- import android.view.View;
- public class ChannelInfoBean implements Comparable<ChannelInfoBean> {
- private int id;
- private String name;
- private Drawable icon;
- private String iconUrl;
- private int iconID;
- private String describtion;
- private int type;
- // 排序标记
- private int order;
- private onGridViewItemClickListener onClickListener;
- public ChannelInfoBean(String name, int iconID, int order) {
- super();
- this.name = name;
- this.iconID = iconID;
- this.order = order;
- }
- public ChannelInfoBean(String name, int iconID, int order,onGridViewItemClickListener onClickListener) {
- super();
- this.name = name;
- this.iconID = iconID;
- this.order = order;
- this.onClickListener=onClickListener;
- }
- public ChannelInfoBean(int id, String name, Drawable icon, String iconUrl, int iconID, int type, int order, String describtion) {
- super();
- this.id = id;
- this.name = name;
- this.icon = icon;
- this.iconUrl = iconUrl;
- this.iconID = iconID;
- this.type = type;
- this.order = order;
- this.describtion = describtion;
- }
- public onGridViewItemClickListener getOnClickListener() {
- return onClickListener;
- }
- public void setOnClickListener(onGridViewItemClickListener onClickListener) {
- this.onClickListener = onClickListener;
- }
- public String getIconUrl() {
- return iconUrl;
- }
- public void setIconUrl(String iconUrl) {
- this.iconUrl = iconUrl;
- }
- public int getIconID() {
- return iconID;
- }
- public void setIconID(int iconID) {
- this.iconID = iconID;
- }
- public String getIconurl() {
- return iconUrl;
- }
- public void setIconurl(String iconurl) {
- this.iconUrl = iconurl;
- }
- public int getId() {
- return id;
- }
- public void setId(int id) {
- this.id = id;
- }
- public String getName() {
- return name;
- }
- public void setName(String name) {
- this.name = name;
- }
- public Drawable getIcon() {
- return icon;
- }
- public void setIcon(Drawable icon) {
- this.icon = icon;
- }
- public int getType() {
- return type;
- }
- public void setType(int type) {
- this.type = type;
- }
- public int getOrder() {
- return order;
- }
- public void setOrder(int order) {
- this.order = order;
- }
- public String getDescribtion() {
- return describtion;
- }
- public void setDescribtion(String describtion) {
- this.describtion = describtion;
- }
- @Override
- public int compareTo(ChannelInfoBean info) {
- if (info != null) {
- if (this.getOrder() > info.getOrder()) {
- return 1;
- } else {
- return -1;
- }
- } else {
- return 0;
- }
- }
- //得到排序的List
- public static List<ChannelInfoBean> getOrderList(List<ChannelInfoBean> list) {
- Collections.sort(list);
- return list;
- }
- public interface onGridViewItemClickListener
- {
- public abstract void ongvItemClickListener(View v);
- }
- }
把这几个自我感觉比较麻烦的类贴上来,有需要的可以看看。
android实现界面左右滑动(GridView动态设置item,支持每个item按某个属性排序来显示在不同的界面),,5-wow.com
郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。