仿百度外卖、美团外卖、淘点点等左右联动ListView菜单展示

    


!!之前代码有问题,左边Listview项滚出屏幕后位置不对,没有联动,重新改了下,https://github.com/wpq2014/MyDemo


相关文章:http://www.jianshu.com/p/afb8ff16701e


最近做项目时想把店铺内菜单展示做成百度外卖、美团外卖和淘点点的样子,网上没找到完美的Demo,所以只能自己瞎搞了;从Libraries for developers上找了个PinnedHeaderListView作为菜单展示页(即右半部分),左边店内分类用普通ListView就行(左半部分),主要方法就两个:

1. 左边ListView的setOnItemClickListener,点击左边分类时右边对应分类滚动到顶部(到底则停止滚动)

left_listView.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> arg0, View view, int position,
					long arg3) {
				isScroll = false;

				for (int i = 0; i < left_listView.getChildCount(); i++)
				{
					if (i == position)
					{
						left_listView.getChildAt(i).setBackgroundColor(Color.rgb(255, 255, 255));
					} else
					{
						left_listView.getChildAt(i).setBackgroundColor(Color.TRANSPARENT);
					}
				}

				int rightSection = 0;
				for(int i=0;i<position;i++){
					rightSection += sectionedAdapter.getCountForSection(i)+1;
				}
				right_listview.setSelection(rightSection);
				
			}

		});


2. 右边PinnedHeaderListView的setOnScrollListener,右边菜单上下滚动时左边对应分类滚动到对应位置

right_listview.setOnScrollListener(new OnScrollListener() {
			
			@Override
			public void onScrollStateChanged(AbsListView arg0, int arg1) {
				
			}
			
			@Override
			public void onScroll(AbsListView view, int firstVisibleItem,
					int visibleItemCount, int totalItemCount) {
				if(isScroll){
					for (int i = 0; i < left_listView.getChildCount(); i++)
					{
						
						if (i == sectionedAdapter.getSectionForPosition(firstVisibleItem))
						{
							left_listView.getChildAt(i).setBackgroundColor(
									Color.rgb(255, 255, 255));
						} else
						{
							left_listView.getChildAt(i).setBackgroundColor(Color.TRANSPARENT);

						}
					}
						
				}else{
					isScroll = true;
				}
			}
		});


还有一个比较重要的是右边PinnedHeaderListView的Adapter,具体内容可以下载源码自己研究改动

public class TestSectionedAdapter extends SectionedBaseAdapter {
	
	private Context mContext;
	private String[] leftStr;
	private String[][] rightStr;
	
	public TestSectionedAdapter(Context context, String[] leftStr, String[][] rightStr){
		this.mContext = context;
		this.leftStr = leftStr;
		this.rightStr = rightStr;
	}

    @Override
    public Object getItem(int section, int position) {
        return rightStr[section][position];
    }

    @Override
    public long getItemId(int section, int position) {
        return position;
    }

    @Override
    public int getSectionCount() {
        return leftStr.length;
    }

    @Override
    public int getCountForSection(int section) {
        return rightStr[section].length;
    }

    @Override
    public View getItemView(final int section, final int position, View convertView, ViewGroup parent) {
        LinearLayout layout = null;
        if (convertView == null) {
            LayoutInflater inflator = (LayoutInflater) parent.getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            layout = (LinearLayout) inflator.inflate(R.layout.list_item, null);
        } else {
            layout = (LinearLayout) convertView;
        }
        ((TextView) layout.findViewById(R.id.textItem)).setText(rightStr[section][position]);
        layout.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View arg0) {
				Toast.makeText(mContext, rightStr[section][position], Toast.LENGTH_SHORT).show();
			}
		});
        return layout;
    }

    @Override
    public View getSectionHeaderView(int section, View convertView, ViewGroup parent) {
        LinearLayout layout = null;
        if (convertView == null) {
            LayoutInflater inflator = (LayoutInflater) parent.getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            layout = (LinearLayout) inflator.inflate(R.layout.header_item, null);
        } else {
            layout = (LinearLayout) convertView;
        }
        layout.setClickable(false);
        ((TextView) layout.findViewById(R.id.textItem)).setText(leftStr[section]);
        return layout;
    }

}

  我的效果图:

                     奉上源码



  • 5
    点赞
  • 26
    收藏
    觉得还不错? 一键收藏
  • 12
    评论
以下是Android Studio仿美团外卖页面的代码示例: 1.布局文件 总布局 ```xml <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="horizontal"> <!-- 左侧切换 --> <FrameLayout android:id="@+id/fl_left" android:layout_width="0dp" android:layout_height="match_parent" android:layout_weight="1"> <!-- 左侧切换布局 --> <LinearLayout android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical"> <!-- 左侧切换标题 --> <TextView android:layout_width="match_parent" android:layout_height="wrap_content" android:background="@color/colorPrimary" android:gravity="center" android:padding="10dp" android:text="左侧切换" android:textColor="@android:color/white" android:textSize="18sp" /> <!-- 左侧切换内容 --> <ListView android:id="@+id/lv_left" android:layout_width="match_parent" android:layout_height="match_parent" android:background="@android:color/white" /> </LinearLayout> </FrameLayout> <!-- 右侧切换 --> <FrameLayout android:id="@+id/fl_right" android:layout_width="0dp" android:layout_height="match_parent" android:layout_weight="3"> <!-- 右侧切换布局 --> <LinearLayout android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical"> <!-- 顶部切换 --> <LinearLayout android:id="@+id/ll_top" android:layout_width="match_parent" android:layout_height="wrap_content" android:background="@color/colorPrimary" android:orientation="horizontal"> <!-- 顶部切换按钮1 --> <TextView android:id="@+id/tv_top1" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" android:gravity="center" android:padding="10dp" android:text="顶部切换1" android:textColor="@android:color/white" android:textSize="18sp" /> <!-- 顶部切换按钮2 --> <TextView android:id="@+id/tv_top2" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" android:gravity="center" android:padding="10dp" android:text="顶部切换2" android:textColor="@android:color/white" android:textSize="18sp" /> <!-- 顶部切换按钮3 --> <TextView android:id="@+id/tv_top3" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" android:gravity="center" android:padding="10dp" android:text="顶部切换3" android:textColor="@android:color/white" android:textSize="18sp" /> </LinearLayout> <!-- 右侧切换内容 --> <ListView android:id="@+id/lv_right" android:layout_width="match_parent" android:layout_height="match_parent" android:background="@android:color/white" /> </LinearLayout> </FrameLayout> </LinearLayout> ``` 2.相关问题:
评论 12
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值