您的位置:首页 > 其它

仿大众点评在布局文件中控制listview的显示和隐藏

2016-01-14 11:43 381 查看
仿大众点评中选择条目所做的一个效果图。其本质就是在控制两个listview的显示和隐藏。

下面附上主要的代码:

(1)显示的mainActivity的逻辑代码

package com.example.mylistviewdemo;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

import android.app.Activity;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;

public class MainActivity extends Activity   {
private ListView mListView, mShoplist_toplist, mShoplist_threelist,
mShoplist_onelist2, mShoplist_twolist2, mShoplist_onelist1,
mShoplist_twolist1;
private TextView mShoplist_title_textbtn1, mShoplist_title_textbtn2,
mShoplist_title_textbtn3;
private LinearLayout mShoplist_shanghuleixing, mShoplist_mainlist2,
mShoplist_mainlist1;
private boolean threelistview = false;
private boolean mainlistview2 = false;
private boolean mainlistview1 = false;
private ArrayList<Map<String, Object>> mainList1;
private ArrayList<Map<String, Object>> mainList2;
private SearchMainAdapter oneadapter1;
private SearchMoreAdapter twoadapter1;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initView();
}
/**
* 初始化id
*/
private void initView() {
// TODO Auto-generated method stub
mShoplist_title_textbtn1 = (TextView) findViewById(R.id.Shoplist_title_textbtn1);
mShoplist_title_textbtn2 = (TextView) findViewById(R.id.Shoplist_title_textbtn2);
mShoplist_title_textbtn3 = (TextView) findViewById(R.id.Shoplist_title_textbtn3);
mShoplist_toplist = (ListView) findViewById(R.id.Shoplist_toplist);
mShoplist_mainlist1 = (LinearLayout) findViewById(R.id.Shoplist_mainlist1);
mShoplist_onelist1 = (ListView) findViewById(R.id.Shoplist_onelist1);
mShoplist_twolist1 = (ListView) findViewById(R.id.Shoplist_twolist1);
mShoplist_mainlist2 = (LinearLayout) findViewById(R.id.Shoplist_mainlist2);
mShoplist_onelist2 = (ListView) findViewById(R.id.Shoplist_onelist2);
mShoplist_twolist2 = (ListView) findViewById(R.id.Shoplist_twolist2);
mShoplist_threelist = (ListView) findViewById(R.id.Shoplist_threelist);
MyOnclickListener mOnclickListener = new MyOnclickListener();
//按钮的点击事件
mShoplist_title_textbtn1.setOnClickListener(mOnclickListener);
mShoplist_title_textbtn2.setOnClickListener(mOnclickListener);
mShoplist_title_textbtn3.setOnClickListener(mOnclickListener);
//初始化数据
initModel1();
initModel2();
oneadapter1 = new SearchMainAdapter(MainActivity.this, mainList1,R.layout.shop_list1_item,false);
oneadapter1.setSelectItem(0);
mShoplist_onelist1.setAdapter(oneadapter1);
initAdapter1(Model.SHOPLIST_PLACESTREET[0]);
Onelistclick1 onelistclick1 = new Onelistclick1();
mShoplist_onelist1.setOnItemClickListener(onelistclick1);

}
/**
* 初始化数据
* @param array
*/
private void initAdapter1(String[] array) {
twoadapter1 = new SearchMoreAdapter(MainActivity.this, array,R.layout.shop_list2_item);
mShoplist_twolist1.setAdapter(twoadapter1);
twoadapter1.notifyDataSetChanged();
}
/**
* 点击对应的条目
*/
//点击对应的条目
private class Onelistclick1 implements OnItemClickListener {
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
long arg3) {
initAdapter1(Model.SHOPLIST_PLACESTREET[arg2]);
oneadapter1.setSelectItem(arg2);
oneadapter1.notifyDataSetChanged();
}
}
//解析数据(这个从服务器中进行获取)
private void initModel1() {
mainList1 = new ArrayList<Map<String, Object>>();
for (int i = 0; i < Model.SHOPLIST_PLACE.length; i++) {
Map<String, Object> map = new HashMap<String, Object>();
map.put("txt", Model.SHOPLIST_PLACE[i]);
mainList1.add(map);
}
}
//解析数据
private void initModel2() {
mainList2 = new ArrayList<Map<String, Object>>();
for (int i = 0; i < Model.LISTVIEWTXT.length; i++) {
Map<String, Object> map = new HashMap<String, Object>();
map.put("img", Model.LISTVIEWIMG[i]);
map.put("txt", Model.LISTVIEWTXT[i]);
mainList2.add(map);
}
}
/**
* 按钮的点击事件
*/
private class MyOnclickListener implements View.OnClickListener {
public void onClick(View v) {
int mID = v.getId();

//默认排序
//          if (mID == R.id.Shoplist_title_textbtn3) {
//              Drawable drawable = null;
//              if (!threelistview) {
//                  drawable = getResources().getDrawable(
//                          R.drawable.ic_arrow_up_black);
//                  mShoplist_threelist.setVisibility(View.VISIBLE);
//                  threeadapter.notifyDataSetChanged();
//                  threelistview = true;
//              } else {
//                  drawable = getResources().getDrawable(
//                          R.drawable.ic_arrow_down_black);
//                  mShoplist_threelist.setVisibility(View.GONE);
//                  threelistview = false;
//              }
//              // 这一步必须要做,否则不会显示.
//              drawable.setBounds(0, 0, drawable.getMinimumWidth(),
//                      drawable.getMinimumHeight());
//              mShoplist_title_textbtn3.setCompoundDrawables(null, null,
//                      drawable, null);
//          } else {
//              Drawable drawable = getResources().getDrawable(
//                      R.drawable.ic_arrow_down_black);
//              drawable.setBounds(0, 0, drawable.getMinimumWidth(),
//                      drawable.getMinimumHeight());
//              mShoplist_title_textbtn3.setCompoundDrawables(null, null,
//                      drawable, null);
//              mShoplist_threelist.setVisibility(View.GONE);
//              threelistview = false;
//          }
//美食
//          if (mID == R.id.Shoplist_title_textbtn2) {
//              Drawable drawable = null;
//              if (!mainlistview2) {
//                  drawable = getResources().getDrawable(
//                          R.drawable.ic_arrow_up_black);
//                  mShoplist_mainlist2.setVisibility(View.VISIBLE);
//                  twoadapter2.notifyDataSetChanged();
//                  mainlistview2 = true;
//              } else {
//                  drawable = getResources().getDrawable(
//                          R.drawable.ic_arrow_down_black);
//                  mShoplist_mainlist2.setVisibility(View.GONE);
//                  mainlistview2 = false;
//              }
//              // 这一步必须要做,否则不会显示.
//              drawable.setBounds(0, 0, drawable.getMinimumWidth(),
//                      drawable.getMinimumHeight());
//              mShoplist_title_textbtn2.setCompoundDrawables(null, null,
//                      drawable, null);
//          } else {
//              Drawable drawable = getResources().getDrawable(
//                      R.drawable.ic_arrow_down_black);
//              drawable.setBounds(0, 0, drawable.getMinimumWidth(),
//                      drawable.getMinimumHeight());
//              mShoplist_title_textbtn2.setCompoundDrawables(null, null,
//                      drawable, null);
//              mShoplist_mainlist2.setVisibility(View.GONE);
//              mainlistview2 = false;
//          }
//全部地区
if (mID == R.id.Shoplist_title_textbtn1) {
Drawable drawable = null;
if (!mainlistview1) {
drawable = getResources().getDrawable(
R.drawable.ic_arrow_up_black);
mShoplist_mainlist1.setVisibility(View.VISIBLE);
twoadapter1.notifyDataSetChanged();
mainlistview1 = true;
} else {
drawable = getResources().getDrawable(
R.drawable.ic_arrow_down_black);
mShoplist_mainlist1.setVisibility(View.GONE);
mainlistview1 = false;
}
// 这一步必须要做,否则不会显示.
drawable.setBounds(0, 0, drawable.getMinimumWidth(),
drawable.getMinimumHeight());
mShoplist_title_textbtn1.setCompoundDrawables(null, null,
drawable, null);
} else {
//如果不是点击的显示的默认的状态
Drawable drawable = getResources().getDrawable(
R.drawable.ic_arrow_down_black);
drawable.setBounds(0, 0, drawable.getMinimumWidth(),
drawable.getMinimumHeight());
mShoplist_title_textbtn1.setCompoundDrawables(null, null,
drawable, null);
mShoplist_mainlist1.setVisibility(View.GONE);
mainlistview1 = false;
}
}
}

}


(2)显示listview的适配器

package com.example.mylistviewdemo;

import java.util.List;
import java.util.Map;

import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

/**
* 查找中的更多的界面中左边listview的适配器
* @author 苦涩
*</BR> </BR> By:苦涩 </BR> 联系作者:QQ 534429149
*/

public class SearchMainAdapter extends BaseAdapter {

private Context ctx;
private List<Map<String, Object>> list;
private int position = 0;
private boolean islodingimg = true;
private int layout = R.layout.search_more_mainlist_item;

public SearchMainAdapter(Context ctx, List<Map<String, Object>> list) {
this.ctx = ctx;
this.list = list;
}

public SearchMainAdapter(Context ctx, List<Map<String, Object>> list,
int layout, boolean islodingimg) {
this.ctx = ctx;
this.list = list;
this.layout = layout;
this.islodingimg = islodingimg;
}

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

public Object getItem(int arg0) {
return list.get(arg0);
}

public long getItemId(int arg0) {
return arg0;
}

public View getView(int arg0, View arg1, ViewGroup arg2) {
Holder hold;
if (arg1 == null) {
hold = new Holder();
arg1 = View.inflate(ctx, layout, null);
hold.txt = (TextView) arg1
.findViewById(R.id.Search_more_mainitem_txt);
hold.img = (ImageView) arg1
.findViewById(R.id.Search_more_mainitem_img);
hold.layout = (LinearLayout) arg1
.findViewById(R.id.Search_more_mainitem_layout);
arg1.setTag(hold);
} else {
hold = (Holder) arg1.getTag();
}
if(islodingimg == true){
hold.img.setImageResource(Integer.parseInt(list.get(arg0).get("img")
.toString()));
}
hold.txt.setText(list.get(arg0).get("txt").toString());
hold.layout
.setBackgroundResource(R.drawable.search_more_mainlistselect);
if (arg0 == position) {
hold.layout.setBackgroundResource(R.drawable.list_bkg_line_u);
}
return arg1;
}

public void setSelectItem(int i) {
position = i;
}

public int getSelectItem() {
return position;
}

private static class Holder {
LinearLayout layout;
ImageView img;
TextView txt;
}

}


(3)布局文件

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >

<RelativeLayout
android:id="@+id/titlerelativelayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@drawable/title_background"
android:gravity="center_vertical" >

<ImageView
android:id="@+id/Shoplist_back"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:paddingLeft="10dp"
android:src="@drawable/ic_back" />

<LinearLayout
android:id="@+id/Shoplist_shanghuleixing"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:gravity="center_vertical"
android:orientation="horizontal"
android:padding="10dp" >

<TextView
android:id="@+id/Shoplist_title_txt"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center_vertical"
android:text="全部商户"
android:textColor="#FF8C00"
android:textSize="18sp" />

<ImageView
android:id="@+id/Search_city_img"
android:layout_width="22dp"
android:layout_height="22dp"
android:src="@drawable/search_city" />
</LinearLayout>
</RelativeLayout>

<LinearLayout
android:id="@+id/middlelinearlayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/titlerelativelayout" >

<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:background="@drawable/tuan_tab_linear_background"
android:paddingBottom="0.5dp"
android:paddingLeft="0dp"
android:paddingRight="0.5dp"
android:paddingTop="0dp" >

<TextView
android:id="@+id/Shoplist_title_textbtn1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@drawable/my_tab_background"
android:drawableRight="@drawable/ic_arrow_down_black"
android:gravity="center_horizontal"
android:padding="10dp"
android:singleLine="true"
android:text="全部地区"
android:textColor="#666666" />
</LinearLayout>

<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:background="@drawable/tuan_tab_linear_background"
android:paddingBottom="0.5dp"
android:paddingLeft="0dp"
android:paddingRight="0.5dp"
android:paddingTop="0dp" >

<TextView
android:id="@+id/Shoplist_title_textbtn2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@drawable/my_tab_background"
android:drawableRight="@drawable/ic_arrow_down_black"
android:gravity="center_horizontal"
android:padding="10dp"
android:singleLine="true"
android:text="美食"
android:textColor="#666666" />
</LinearLayout>

<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:background="@drawable/tuan_tab_linear_background"
android:paddingBottom="0.5dp"
android:paddingLeft="0dp"
android:paddingRight="0.5dp"
android:paddingTop="0dp" >

<TextView
android:id="@+id/Shoplist_title_textbtn3"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@drawable/my_tab_background"
android:drawableRight="@drawable/ic_arrow_down_black"
android:gravity="center_horizontal"
android:padding="10dp"
android:singleLine="true"
android:text="默认排序"
android:textColor="#666666" />
</LinearLayout>
</LinearLayout>

<ListView
android:id="@+id/ShopListView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="@+id/middlelinearlayout" />

<ListView
android:id="@+id/Shoplist_toplist"
android:layout_width="match_parent"
android:layout_height="350dp"
android:layout_below="@+id/titlerelativelayout"
android:background="#f4f4f4"
android:divider="@null"
android:listSelector="@drawable/search_more_morelistselect"
android:padding="10dp"
android:visibility="gone" />

<ListView
android:id="@+id/Shoplist_threelist"
android:layout_width="match_parent"
android:layout_height="350dp"
android:layout_below="@+id/middlelinearlayout"
android:background="#ffffff"
android:divider="@null"
android:listSelector="@drawable/search_more_morelistselect"
android:padding="3dp"
android:visibility="gone" />

<LinearLayout
android:id="@+id/Shoplist_mainlist2"
android:layout_width="match_parent"
android:layout_height="350dp"
android:layout_below="@+id/middlelinearlayout"
android:orientation="horizontal"
android:visibility="gone" >

<ListView
android:id="@+id/Shoplist_onelist2"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="20"
android:background="#F4F4F4"
android:divider="@null"
android:listSelector="@drawable/search_more_morelistselect"
android:scrollbars="none" />

<ListView
android:id="@+id/Shoplist_twolist2"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="17"
android:background="#FFFFFF"
android:divider="@null"
android:listSelector="@drawable/search_more_morelistselect" />
</LinearLayout>

<LinearLayout
android:id="@+id/Shoplist_mainlist1"
android:layout_width="match_parent"
android:layout_height="350dp"
android:layout_below="@+id/middlelinearlayout"
android:orientation="horizontal"
android:visibility="gone" >

<ListView
android:id="@+id/Shoplist_onelist1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="20"
android:background="#F4F4F4"
android:divider="@null"
android:listSelector="@drawable/search_more_morelistselect"
android:scrollbars="none" />

<ListView
android:id="@+id/Shoplist_twolist1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="17"
android:background="#FFFFFF"
android:divider="@null"
android:listSelector="@drawable/search_more_morelistselect" />
</LinearLayout>

</RelativeLayout>


这里写链接内容
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: