お腹が空きましたか?注文インターフェース二つのListView連動効果


図のように効果図です

お腹が空いたような注文画面です。
1.左側のListViewをクリックし、アダプターにItemを設定することで色を変え、notifyData SetInvalidatedを通じてリフレッシュし、lv_を使う。home.set Selection(showTitle.get(arg 2));を選択して、右側の
2.右側は主にオンスクリーンを書き換える方法です。左のListViewの色と背景を変えます。
でも、プログラムの中にもう一つの問題があります。大神解答は私の右側のListViewを引く時、上のTextViewは変えられます。しかし、アップデートする時、TextViewのは直ちにスライドに変えられない時、私はfirst Visible Itemだけを持って判断しました。
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连动効果について、皆さんに助けてほしいです。もし何か疑问があれば、メッセージをください。小编はすぐに返事します。ここでも私たちのサイトを応援してくれてありがとうございます。