Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲
 Android教程網 >> Android技術 >> 關於Android編程 >> 仿餓了嗎點餐界面兩個ListView聯動效果

仿餓了嗎點餐界面兩個ListView聯動效果

編輯:關於Android編程

如圖是效果圖

是仿餓了的點餐界面

1.點擊左側的ListView,通過在在適配器中設置Item來改變顏色,再通過notifyDataSetInvalidated來刷新並用lv_home.setSelection(showTitle.get(arg2));來關聯右側的

2.右側的主要是重寫下onScroll的方法;來改變左側ListView的顏色及背景

不過程序中還有個問題,望大神解答就是我右側的ListView下拉時,上面的TextView能改變;但是上拉時,TextView的不能及時改變應為滑動時我只拿了firstVisibleItem來判斷的

Demo的連接:http://download.csdn.net/detail/qq_29774291/9634011

如下是主程序代碼

package com.item.jiejie;
import java.util.ArrayList;
import java.util.List;
import com.item.jiejie.adapter.HomeAdapter;
import com.item.jiejie.adapter.MenuAdapter;
import com.item.jiejie.entity.FoodData;
import android.widget.AbsListView.OnScrollListener;
import android.os.Bundle;
import android.app.Activity;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.view.Window;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.AbsListView;
import android.widget.ListView;
import android.widget.TextView;
/**
* 防餓了的ListView聯動的Demo
* 有BUG
* @author Administrator
*
*/
public class MainActivity extends Activity {
/**左側菜單*/
private ListView lv_menu;
/**右側主菜*/
private ListView lv_home;
private TextView tv_title;
private MenuAdapter menuAdapter;
private HomeAdapter homeAdapter;
private int currentItem;
/**
* 數據源
*/
private List<FoodData> foodDatas;
private String data[] = {"熱銷榜","新品套餐","便當套餐","單點菜品","飲料類","水果罐頭","米飯"};
/**
* 裡面存放右邊ListView需要顯示標題的條目position
*/
private ArrayList<Integer> showTitle;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.activity_main);
setView();
setData();
}
private void setView() {
// TODO Auto-generated method stub
lv_menu = (ListView)findViewById(R.id.lv_menu);
tv_title = (TextView)findViewById(R.id.tv_titile);
lv_home = (ListView)findViewById(R.id.lv_home);
foodDatas = new ArrayList<FoodData>();
for(int i =0;i < data.length; i++){
foodDatas.add(new FoodData(i, data[0] + i, data[0]));
}
for(int i =0;i < data.length -1; i++){
foodDatas.add(new FoodData(i, data[1] + i, data[1]));
}
for(int i =0;i < data.length-2; i++){
foodDatas.add(new FoodData(i, data[2] + i, data[2]));
}
for(int i =0;i < data.length-3; i++){
foodDatas.add(new FoodData(i, data[3] + i, data[3]));
}
for(int i =0;i < data.length-4; i++){
foodDatas.add(new FoodData(i, data[4] + i, data[4]));
}
for(int i =0;i < data.length-3; i++){
foodDatas.add(new FoodData(i, data[5] + i, data[5]));
}
for(int i =0;i < 6; i++){
foodDatas.add(new FoodData(i, data[6] + i, data[6]));
}
showTitle = new ArrayList<Integer>();
for(int i = 0; i < foodDatas.size(); i++){
if( i ==0){
showTitle.add(i );
System.out.println(i + "dd");
}else if (!TextUtils.equals(foodDatas.get(i).getTitle(), foodDatas.get(i - 1).getTitle())) {
showTitle.add(i );
System.out.println(i + "dd");
}
}
}
private void setData() {
// TODO Auto-generated method stub
tv_title.setText(foodDatas.get(0).getTitle());
menuAdapter = new MenuAdapter(this);
homeAdapter = new HomeAdapter(this, foodDatas);
lv_menu.setAdapter(menuAdapter);
lv_home.setAdapter(homeAdapter);
lv_menu.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
long arg3) {
// TODO Auto-generated method stub
menuAdapter.setSelectItem(arg2);
menuAdapter.notifyDataSetInvalidated();
lv_home.setSelection(showTitle.get(arg2));
tv_title.setText(data[arg2]);
}
});
lv_home.setOnScrollListener(new OnScrollListener() {
private int scrollState;
@Override
public void onScrollStateChanged(AbsListView view, int scrollState) {
// TODO Auto-generated method stub
//System.out.println("onScrollStateChanged" + " scrollState" + scrollState);
this.scrollState = scrollState;
}
@Override
public void onScroll(AbsListView view, int firstVisibleItem,
int visibleItemCount, int totalItemCount) {
// TODO Auto-generated method stub
if (scrollState == AbsListView.OnScrollListener.SCROLL_STATE_IDLE) { 
return; 
} 
Log.d("jiejie", "onScroll" + " firstVisibleItem" + firstVisibleItem
+" visibleItemCount" + visibleItemCount + " totalItemCount" + totalItemCount);
int current =showTitle.indexOf(firstVisibleItem );
System.out.println(current + "dd" + firstVisibleItem);
// lv_home.setSelection(current);
if(currentItem != current && current >=0){
currentItem = current;
tv_title.setText(data[current]);
menuAdapter.setSelectItem(currentItem);
menuAdapter.notifyDataSetInvalidated();
}
}
});
}
}

左側ListView的適配器代碼

package com.item.jiejie.adapter;
import com.item.jiejie.R;
import android.content.Context;
import android.graphics.Color;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;
/**
* 左側菜單ListView的適配器
* @author Administrator
*
*/
public class MenuAdapter extends BaseAdapter{
private Context context;
private int selectItem = 0;
private String data[] = {"熱銷榜","新品套餐","便當套餐","單點菜品","飲料類","水果罐頭","米飯"};
public MenuAdapter(Context context) {
this.context = context;
}
public int getSelectItem() {
return selectItem;
}
public void setSelectItem(int selectItem) {
this.selectItem = selectItem;
}
@Override
public int getCount() {
// TODO Auto-generated method stub
return data.length;
}
@Override
public Object getItem(int arg0) {
// TODO Auto-generated method stub
return null;
}
@Override
public long getItemId(int arg0) {
// TODO Auto-generated method stub
return 0;
}
@Override
public View getView(int arg0, View arg1, ViewGroup arg2) {
// TODO Auto-generated method stub
ViewHolder holder = null;
if(arg1 == null) {
holder = new ViewHolder();
arg1 = View.inflate(context, R.layout.item_menu, null);
holder.tv_name = (TextView)arg1.findViewById(R.id.item_name);
arg1.setTag(holder);
}else {
holder = (ViewHolder)arg1.getTag();
}
if(arg0 == selectItem){
holder.tv_name.setBackgroundColor(Color.WHITE);
holder.tv_name.setTextColor(context.getResources().getColor(R.color.text_green));
}else {
holder.tv_name.setBackgroundColor(context.getResources().getColor(R.color.ll_coachback));
holder.tv_name.setTextColor(context.getResources().getColor(R.color.text_deep));
}
holder.tv_name.setText(data[arg0]);
return arg1;
}
static class ViewHolder{
private TextView tv_name;
}
}

右側的適配器代碼

package com.item.jiejie.adapter;
import java.util.List;
import com.item.jiejie.R;
import com.item.jiejie.entity.FoodData;
import android.content.Context;
import android.text.TextUtils;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;
/**
* 右側主界面ListView的適配器
* 
* @author Administrator
* 
*/
public class HomeAdapter extends BaseAdapter {
private Context context;
private List<FoodData> foodDatas;
public HomeAdapter(Context context, List<FoodData> foodDatas) {
this.context = context;
this.foodDatas = foodDatas;
}
@Override
public int getCount() {
// TODO Auto-generated method stub
if(foodDatas!=null){
return foodDatas.size();
}else { 
return 10;
} 
}
@Override
public Object getItem(int arg0) {
// TODO Auto-generated method stub
return null;
}
@Override
public long getItemId(int arg0) {
// TODO Auto-generated method stub
return 0;
}
@Override
public View getView(int arg0, View arg1, ViewGroup arg2) {
// TODO Auto-generated method stub
ViewHold holder = null;
if(arg1 == null){
arg1 = View.inflate(context, R.layout.item_home, null);
holder = new ViewHold();
holder.tv_title = (TextView)arg1.findViewById(R.id.item_home_title);
holder.tv_name = (TextView)arg1.findViewById(R.id.item_home_name);
arg1.setTag(holder);
}else {
holder = (ViewHold)arg1.getTag();
}
holder.tv_name.setText(foodDatas.get(arg0).getName());
holder.tv_title.setText(foodDatas.get(arg0).getTitle());
if(arg0 == 0){
holder.tv_title.setVisibility(View.VISIBLE);
}else if (!TextUtils.equals(foodDatas.get(arg0).getTitle(), foodDatas.get(arg0 -1).getTitle())) {
holder.tv_title.setVisibility(View.VISIBLE);
}else {
holder.tv_title.setVisibility(View.GONE);
}
return arg1;
}
private static class ViewHold{
private TextView tv_title;
private TextView tv_name;
}
}

以上所述是小編給大家介紹的仿餓了點餐界面2個ListView聯動效果,希望對大家有所幫助,如果大家有任何疑問請給我留言,小編會及時回復大家的。在此也非常感謝大家對本站網站的支持!

  1. 上一頁:
  2. 下一頁:
熱門文章
閱讀排行版
Copyright © Android教程網 All Rights Reserved