package com.example.a1_.adapter;import android.content.Context;import android.graphics.Color;import android.view.View;import android.view.ViewGroup;import android.widget.BaseAdapter;import android.widget.ListView;import android.widget.TextView;import com.example.a1_.Bean.LeftBean;import com.example.a1_.Bean.RightBean;import com.example.a1_.R;import java.util.List;import java.util.Random;import se.emilsjolander.stickylistheaders.StickyListHeadersAdapter;/** * Created by Administrator on 2017.05.27.0027. */public class RightAdapter extends BaseAdapter implements StickyListHeadersAdapter {private List<LeftBean> mLeft;private List<RightBean> mRight;private Context context;public RightAdapter(List<LeftBean> mLeft, List<RightBean> mRight, Context context) { this.mLeft = mLeft; this.mRight = mRight; this.context = context;}@Overridepublic View getHeaderView(int position, View convertView, ViewGroup parent) { TextView tv = new TextView(context); tv.setTextColor(Color.RED); tv.setTextSize(18); tv.setText(mRight.get(position).type); return tv;}@Overridepublic long getHeaderId(int position) { return mRight.get(position).typeId;}@Overridepublic int getCount() { return mRight.size();}@Overridepublic RightBean getItem(int position) { return mRight.get(position);}@Overridepublic long getItemId(int position) { return position;}@Overridepublic View getView(int position, View convertView, ViewGroup parent) { ViewHolder viewHolder = null; if (convertView==null){ convertView = View.inflate(context, R.layout.right_item,null); viewHolder = new ViewHolder(); viewHolder.title1 = (TextView) convertView.findViewById(R.id.tv_right_title1); viewHolder.title2 = (TextView) convertView.findViewById(R.id.tv_right_title2); viewHolder.count = (TextView) convertView.findViewById(R.id.tv_right_count); convertView.setTag(viewHolder); }else { viewHolder = (ViewHolder) convertView.getTag(); } RightBean rightBean = mRight.get(position); viewHolder.title1.setText(rightBean.biaoti); viewHolder.title2.setText(rightBean.biaoti); //使用Random获取随机数 Random random = new Random(); int i = random.nextInt(100); viewHolder.count.setText("月销量"+i+"份"); return convertView;}static class ViewHolder{ TextView title1; TextView title2; TextView count;}}
package com.example.a1_.data;import com.example.a1_.Bean.LeftBean;import com.example.a1_.Bean.RightBean;import java.util.ArrayList;import java.util.List;/** * Created by wangcaisheng on 2017/5/27. */public class Data {private static String[] leftData = new String[]{"13.9特价套餐","粗粮主食","佐餐小吃","用心营养套餐(不含主食)","三杯鸡双拼尊享套餐","带鱼双拼尊享套餐","红烧肉双拼尊享套餐"};private static String[] rightData0 = new String[]{"洋芋粉炒腊肉","土鸡炖香菇","新疆大盘辣子土鸡","清炖土鸡块","农家蒸碗 ","香辣野猪肉","香辣薯条大虾","麻辣猪血"};private static String[] rightData1 = new String[]{"商芝扣肉","羊肉萝卜","干烧鱼 ","干煸野猪肉 ","排骨火锅","土鸡火锅","牛肉火锅","狗肉火锅 "};private static String[] rightData2 = new String[]{"虎皮辣子炒咸肉","重庆飘香水煮鱼","红烧土鸡块","干煸辣子土鸡","清炖全鸡 "};private static String[] rightData3 = new String[]{"洋芋粉炒腊肉","土鸡炖香菇","新疆大盘辣子土鸡","清炖土鸡块","农家蒸碗 ","香辣野猪肉","香辣薯条大虾","麻辣猪血"};private static String[] rightData4 = new String[]{"洋芋粉炒腊肉","土鸡炖香菇","新疆大盘辣子土鸡","清炖土鸡块","农家蒸碗 ","香辣野猪肉","香辣薯条大虾","麻辣猪血"};private static String[] rightData5 = new String[]{"洋芋粉炒腊肉","土鸡炖香菇","新疆大盘辣子土鸡","清炖土鸡块","农家蒸碗 ","香辣野猪肉","香辣薯条大虾","麻辣猪血"};private static String[] rightData6 = new String[]{"洋芋粉炒腊肉","土鸡炖香菇","新疆大盘辣子土鸡","清炖土鸡块","农家蒸碗 ","香辣野猪肉","香辣薯条大虾","麻辣猪血"};public static List<LeftBean> getLeftData(){ List<LeftBean> list = new ArrayList<LeftBean>(); for (int i = 0; i < leftData.length; i++) { LeftBean bean = new LeftBean(); bean.title = leftData[i]; bean.type = i; list.add(bean); } return list;}public static List<RightBean> getRightData(List<LeftBean> list){ List<RightBean> rightList = new ArrayList<RightBean>(); for (int i = 0; i < list.size(); i++) { LeftBean leftBean = list.get(i); int mType = leftBean.type; switch (mType) { case 0: rightList = getRightList(rightData0, leftBean, mType, rightList); break; case 1: rightList = getRightList(rightData1, leftBean, mType, rightList); break; case 2: rightList = getRightList(rightData2, leftBean, mType, rightList); break; case 3: rightList = getRightList(rightData3, leftBean, mType, rightList); break; case 4: rightList = getRightList(rightData4, leftBean, mType, rightList); break; case 5: rightList = getRightList(rightData5, leftBean, mType, rightList); break; case 6: rightList = getRightList(rightData6, leftBean, mType, rightList); break; } } return rightList;}private static List<RightBean> getRightList(String[] arr, LeftBean leftBean, int mType, List<RightBean> rightList){ for (int j = 0; j < arr.length; j++) { RightBean bean = new RightBean(); bean.type = leftBean.title; bean.biaoti = arr[j]; bean.typeId = mType; rightList.add(bean); } return rightList;}}
package com.example.a1_.activity;import android.os.Bundle;import android.support.v7.app.AppCompatActivity;import android.view.View;import android.widget.AdapterView;import android.widget.ListView;import com.example.a1_.Bean.LeftBean;import com.example.a1_.Bean.RightBean;import com.example.a1_.R;import com.example.a1_.adapter.LeftAdapter;import com.example.a1_.adapter.RightAdapter;import com.example.a1_.data.Data;import java.util.List;import se.emilsjolander.stickylistheaders.StickyListHeadersListView;public class MainActivity extends AppCompatActivity {private ListView lv_left;private StickyListHeadersListView lv_right;private int currentLeftItem;@Overrideprotected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); initView(); initData();}//初始化控件private void initView() { //初始化控件 lv_left = (ListView) findViewById(R.id.lv_left); lv_right = (StickyListHeadersListView) findViewById(R.id.lv_right);}//设置适配器private void initData() { //创建适配器 final LeftAdapter leftAdapter = new LeftAdapter(Data.getLeftData()); //获取左侧数据 final List<LeftBean> leftData = Data.getLeftData(); //获取右侧数据 final List<RightBean> rightData = Data.getRightData(leftData); RightAdapter rightAdapter = new RightAdapter(leftData, rightData, this); //为左侧布局设置适配器 lv_left.setAdapter(leftAdapter); lv_right.setAdapter(rightAdapter); //为左侧条目设置点击事件 lv_left.setOnItemClickListener(new AdapterView.OnItemClickListener() { @Override public void onItemClick(AdapterView<?> parent, View view, int position, long id) { //当左侧条目被点击时,记录下被点击条目的type int type = leftData.get(position).type; //遍历右侧条目,并获取右侧条目的typeId,与刚刚获取的type对比,是否一致 for (int i = 0; i < rightData.size(); i++) { if (type == rightData.get(i).typeId) { //如果找到对应的条目,那就将右侧条目滚动至对应条目,并跳出循环 lv_right.smoothScrollToPosition(i); currentLeftItem = i; //设置当前被选中的左侧条目 leftAdapter.setCurrentSelect(currentLeftItem); //刷新数据适配器 leftAdapter.notifyDataSetChanged(); break; } }// Toast.makeText(MainActivity.this, "您选中了"+leftData.get(position).title, Toast.LENGTH_SHORT).show(); } }); //为右侧条目设置点击事件 lv_right.setOnItemClickListener(new AdapterView.OnItemClickListener() { @Override public void onItemClick(AdapterView<?> parent, View view, int position, long id) {// Toast.makeText(MainActivity.this, "右侧条目被点击了"+position, Toast.LENGTH_SHORT).show(); //当右侧条目被点击时,获取被点击条目的typeId int typeId = rightData.get(position).typeId; //遍历左侧条目 for (int i = 0; i < leftData.size(); i++) { //获取左侧条目的type,与右侧条目的typeId对比是否一致 if (typeId == leftData.get(i).type) { //说明找到了对应条目,跳出循环,设置当前被选中的条目 currentLeftItem = i; //设置当前被选中的左侧条目 leftAdapter.setCurrentSelect(currentLeftItem); //刷新数据适配器 leftAdapter.notifyDataSetChanged(); break; } } } });}}