两listview联动

package com.mttz;

 

import java.util.ArrayList;
import java.util.List;

import com.mttz.adapter.CaiDanADP;
import com.mttz.bean.ShopDTO;

import android.graphics.Color;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v4.view.ViewPager.OnPageChangeListener;
import android.util.Log;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AbsListView;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.ScrollView;
import android.widget.TextView;

public class CaiPinFragment extends Fragment{

//开始
ListView listView, listView2;
/**
* 本来想做标题停留在最上方的,不想想了
*/
// TextView textView;
/**
* 左边listview的要使用的数组
*/
String[] arr = new String[] {"快餐","超市","水果","生鲜","药店","更多" };


String[] arr2 = new String[] { "快餐", "水饺", "面", "米粉", "辣椒", "" };
String[] arr3 = new String[] { "超市", "急", "用", "钱", "找", "我", "们" };
String[] arr4 = new String[] { "水果", "放", "款", "快", "额" };
String[] arr5 = new String[] { "生鲜", "度", "高", "无", "需", "低", "压",
"food" };
String[] arr6 = new String[] { "药店", "联", "系", "电" };
String[] arr7 = new String[] { "更多", "话", "x", "x", "x", "x", "x",
"x", "x", "x", "美", "通", "投", "资" };

String[][] arr8 = new String[][] { arr2, arr3, arr4, arr5, arr6, arr7 };

/**
* 用来存放 food数组
*/
List<String> list;

/**
* 用来记录每一个 1 2 3 4 5 6 在右边listview的位置;
*/
List<Integer> nums = new ArrayList<Integer>();
//结束


// private ListView cp_caidan_lv,cp_caipin_lv;



@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_caipin,container, false);
// initWithView(rootView);
initView(rootView);

return rootView;
}


// private void initWithView(View view){
// cp_caidan_lv = (ListView) view.findViewById(R.id.cp_caidan_lv);//左侧菜单栏
// cp_caipin_lv = (ListView) view.findViewById(R.id.cp_caidan_lv);//右侧菜品栏
//
//
// }
//



private void initView(View view)
{
// textView = (TextView) findViewById(R.id.textView1);
listView = (ListView) view.findViewById(R.id.cp_caidan_lv);

List<String> CaiPinLV= new ArrayList<String>();
// list = new ArrayList<String>();

for (int j = 0; j < arr.length; j++)
{

CaiPinLV.add(arr[j]);

}

CaiDanADP adp = new CaiDanADP(CaiPinLV, getActivity());
listView.setAdapter(adp);


list = new ArrayList<String>();

for (int j = 0; j < arr8.length; j++)
{
for (int j2 = 0; j2 < arr8[j].length; j2++)
{
list.add(arr8[j][j2]);
}
}

for (int i = 0; i < arr8.length; i++)
{
if (i == 0)
{
nums.add(0);
} else if (i > 0 && i < arr8.length)
{
int num = 0;
for (int j = 0; j < i; j++)
{
num = num + arr8[j].length;

}
nums.add(num);
}
}


listView2 =(ListView) view.findViewById(R.id.cp_caipin_lv);
listView2.setAdapter(new MyAdapter());

/**
*
* 判断listview滑动
* */
listView2.setOnScrollListener(new AbsListView.OnScrollListener()
{

@Override
public void onScrollStateChanged(AbsListView view, int scrollState)
{

}

@Override
public void onScroll(AbsListView view, int firstVisibleItem,
int visibleItemCount, int totalItemCount)
{
if (nums.contains(firstVisibleItem) && listView.getChildCount() > 0)
{

for (int i = 0; i < listView.getChildCount(); i++)
{
if (i == nums.indexOf(firstVisibleItem))
{
listView.getChildAt(i).setBackgroundColor(
Color.rgb(200, 200, 200));
} else
{
listView.getChildAt(i).setBackgroundColor(Color.TRANSPARENT);

}


}



}
}
});

listView.setOnItemClickListener(new AdapterView.OnItemClickListener()
{
@Override
public void onItemClick(AdapterView<?> parent, View view, int position,
long id)
{
for (int i = 0; i < listView.getChildCount(); i++)
{
if (i == position)
{
view.setBackgroundColor(Color.rgb(255, 250, 250));
} else
{
view.setBackgroundColor(Color.rgb(200, 200, 200));
}
}

listView2.setSelection(nums.get(position));

}
});

}

class MyAdapter extends BaseAdapter
{
private LayoutInflater inflater;

@Override
public int getCount()
{
return list.size();
}

@Override
public Object getItem(int position)
{
return list.get(position);
}

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

@Override
public View getView(int position, View convertView, ViewGroup parent)
{
// TextView textView = new TextView(getActivity());
// textView.setText(list.get(position));
// textView.setGravity(Gravity.CENTER_VERTICAL);
// textView.setMinHeight(200);
//
//
// if (nums.contains(position))
// {
// textView.setBackgroundColor(Color.argb(80,80, 80, 80));
// }


// if (convertView == null) {
inflater = LayoutInflater.from(getActivity());
convertView = inflater.inflate(R.layout.shop_item, null);


TextView shop_name = (TextView) convertView.findViewById(R.id.shop_name);
ImageView shoplogo = (ImageView) convertView.findViewById(R.id.shop_logo);
// convertView.setTag(holder);
shop_name.setText(list.get(position));
shoplogo.setBackgroundResource(R.drawable.eleme_sjm);
if (nums.contains(position))
{
convertView = inflater.inflate(R.layout.goods_details, null);
// convertView.setg
TextView cp = (TextView) convertView.findViewById(R.id.cp_root);
cp.setText(list.get(position));
cp.setTextColor(Color.rgb(255,250,250));
convertView.setBackgroundColor(Color.rgb(200, 200, 200));
return convertView;
}else {
return convertView;
}
// }
//
//
// if (mInfo!=null) {
// String shopname= mInfo.getShopname();
// String imageUrl = mInfo.getShoplogo();




}

}
}

转载于:https://www.cnblogs.com/sunzan/p/4773549.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值