码迷,mamicode.com
首页 > 移动开发 > 详细

Android例子源码非第三方实现根据字母排序的城市列表

时间:2016-05-02 21:19:07      阅读:259      评论:0      收藏:0      [点我收藏+]

标签:

values 下dimens.xml

技术分享
<resources>

    <!-- Default screen margins, per the Android Design guidelines. -->
    <dimen name="activity_horizontal_margin">16dp</dimen>
    <dimen name="activity_vertical_margin">16dp</dimen>

</resources>
技术分享

布局:

activity_main.xml

技术分享
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity" >

    <ListView
        android:id="@+id/listView1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:scrollbars="none" >
    </ListView>

    <ListView
        android:id="@+id/listView2"
        android:layout_width="15dp"
        android:layout_height="match_parent"
        android:layout_alignParentRight="true"
        android:layout_alignParentTop="true"
        android:cacheColorHint="#00000000"
        android:divider="@null"
        android:scrollbars="none" >
    </ListView>

</RelativeLayout>
技术分享

letter_list.xml

技术分享
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="wrap_content"
    android:layout_height="match_parent"
    tools:context=".MainActivity" >

        <TextView
            android:id="@+id/letterListTextView"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_centerHorizontal="true"
            android:textColor="#3aa2cf" />
</RelativeLayout>
技术分享

letter.xml

技术分享
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity" >

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true"
        android:orientation="vertical"
        android:background="#eeeeee"
        android:gravity="center_vertical" >

        <TextView
            android:id="@+id/letterTextView"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginLeft="5dp"
            android:layout_marginTop="5dp"
            android:layout_marginBottom="5dp"
            android:text="TextView" />

    </LinearLayout>

</RelativeLayout>
技术分享

city.xml

技术分享
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity" >

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true"
        android:orientation="vertical"
        android:background="#ffffff"
        android:gravity="center_vertical" >

        <TextView
            android:id="@+id/cityTextView"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginLeft="10dp"
            android:layout_marginTop="10dp"
            android:layout_marginBottom="10dp"
            android:text="TextView" />

    </LinearLayout>

</RelativeLayout>
技术分享

letter_city.xml

技术分享
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity" >

    <LinearLayout
        android:id="@+id/ll"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true"
        android:orientation="vertical"
        android:gravity="center_vertical" >

        <TextView
            android:id="@+id/letterTextView"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginLeft="10dp"
            android:text="TextView" />

    </LinearLayout>

</RelativeLayout>
技术分享

主页

MainActivity.java

技术分享
package com.example.zimusortlistview;

import java.util.ArrayList;
import java.util.List;
import android.os.Bundle;
import android.app.Activity;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.BaseAdapter;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

/**
 * @author 彭向明
 * 在listview中加载多个布局
 * 以城市列表为例,保证cityLetter和cityName中没有相同的元素,且letter中没有相同的元素
 */
public class MainActivity extends Activity implements OnItemClickListener{

    String[] letter={"A","B","C","D","E","F","G","H","I","J","K","L","M","N","O","P","Q","R","S","T","U","V","W","X","Y","Z"};
    String[] cityLetter={"C","B","H","G","W","C","S","S","C","W","H","Z","C","D","N","L","Q","S","S"
            ,"L","N","H","X","H","S","Z","G","N","J","T","S","X","A"};
    String[] cityName={"长沙","北京","杭州","广州","武汉","重庆","上海","深圳","长春","乌鲁木齐","哈尔滨","郑州","成都","大连","南昌","兰州","齐齐哈尔","汕头","苏州"
            ,"拉萨","南京","呼和浩特","厦门","合肥","沈阳","张家界","贵州","宁夏","济南","天津","石家庄","西安","澳门"};
    List<String> letterToCity=new ArrayList<String>();
    int count;
    ListView lv;
    
    ListView lv1;
    
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        
        String str="";
        for(int i=0;i<letter.length;i++){
            str=letter[i];
            boolean isAddLetter=false;
            for(int j=0;j<cityLetter.length;j++){
                if(str.equals(cityLetter[j])){
                    if(!isAddLetter){
                        letterToCity.add(str);
                        isAddLetter=true;
                    }
                    letterToCity.add(cityName[j]);
                }
            }
        }
        
        lv=(ListView)findViewById(R.id.listView1);
        lv.setAdapter(new MyAdapter());
        lv.setOnItemClickListener(this);
        
        lv1=(ListView)findViewById(R.id.listView2);
        lv1.setAdapter(new MyAdapter1());
        lv1.setOnItemClickListener(this);
    }
    
    class MyAdapter1 extends BaseAdapter{
        
        @Override
        public int getCount() {
            // TODO Auto-generated method stub
            return letter.length;
        }

        @Override
        public Object getItem(int position) {
            // TODO Auto-generated method stub
            return letter[position];
        }

        @Override
        public long getItemId(int position) {
            // TODO Auto-generated method stub
            return position;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            View view=LayoutInflater.from(MainActivity.this).inflate(R.layout.letter_list, null);
            TextView tv=(TextView)view.findViewById(R.id.letterListTextView);
            tv.setText(letter[position]);
            return view;
        }
        
    }
    
    class MyAdapter extends BaseAdapter{

        final static int TYPE_1=1;
        final static int TYPE_2=2;
        
        @Override
        public int getCount() {
            // TODO Auto-generated method stub
            return letterToCity.size();
        }

        @Override
        public Object getItem(int position) {
            // TODO Auto-generated method stub
            return letterToCity.get(position);
        }

        @Override
        public long getItemId(int position) {
            // TODO Auto-generated method stub
            return position;
        }
        
        @Override
        public int getItemViewType(int position) {
            for(int i=0;i<letter.length;i++){
                if(letterToCity.get(position).equals(letter[i])){
                    return TYPE_1;
                }
            }
            return TYPE_2;
        }
        
        @Override
        public int getViewTypeCount() {
            return 3;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            /**
             * 即使convertView缓存了一些布局,但是在重构时,根本不知道怎么样去让convertView返回你所需要的布局,这时你需
             * 要让adapter知道我当前有哪些布局,我重构Item时的布局选取规则,好让convertView能返回你需要的布局。
             * 需要重写以下两个函数
             * @Override
             * public int getItemViewType(int position) {}这个函数获取在getView中创建的视图的类型
             * @Override
             * public int getViewTypeCount() {}返回在getView中创建视图类型的数量
             * 至于这两个方法的详细用处,自己看api即可
             */
            ViewHolder1 vh1=null;
            ViewHolder2 vh2=null;
            int type=getItemViewType(position);
            if(convertView==null){
                switch (type) {
                case TYPE_1:
                    convertView=LayoutInflater.from(MainActivity.this).inflate(R.layout.letter, null);
                    vh1=new ViewHolder1();
                    vh1.tv=(TextView)convertView.findViewById(R.id.letterTextView);
                    convertView.setTag(vh1);
                    break;
                case TYPE_2:
                    convertView=LayoutInflater.from(MainActivity.this).inflate(R.layout.city, null);
                    vh2=new ViewHolder2();
                    vh2.tv=(TextView)convertView.findViewById(R.id.cityTextView);
                    convertView.setTag(vh2);
                    break;
                default:
                    break;
                }
            }else{
                switch (type) {
                case TYPE_1:
                    vh1=(ViewHolder1)convertView.getTag();
                    break;
                case TYPE_2:
                    vh2=(ViewHolder2)convertView.getTag();
                    break;
                default:
                    break;
                }
            }
            switch (type) {
            case TYPE_1:
                vh1.tv.setText(letterToCity.get(position));
                break;
            case TYPE_2:
                vh2.tv.setText(letterToCity.get(position));
                break;
            default:
                break;
            }
            return convertView;
            
            //此方法不可取,刷新时view会刷新
//            boolean isLetter=false;
//            for(int i=0;i<letter.length;i++){
//                if(letterToCity.get(position).equals(letter[i])){
//                    isLetter=true;
//                    break;
//                }
//            }
//            ViewHolder vh=null;
//            if(convertView==null){
//                vh=new ViewHolder();
//                if(isLetter){
//                    convertView=LayoutInflater.from(MainActivity.this).inflate(R.layout.letter, null);
//                    vh.tv=(TextView)convertView.findViewById(R.id.letterTextView);
//                }else{
//                    convertView=LayoutInflater.from(MainActivity.this).inflate(R.layout.city, null);
//                    vh.tv=(TextView)convertView.findViewById(R.id.cityTextView);
//                }
//                convertView.setTag(vh);
//            }else{
//                vh = (ViewHolder) convertView.getTag();
//            }
//            vh.tv.setText(letterToCity.get(position));
//            return convertView;
            
            //城市列表,修改布局
//            boolean isLetter=false;
//            for(int i=0;i<letter.length;i++){
//                if(letterToCity.get(position).equals(letter[i])){
//                    isLetter=true;
//                    break;
//                }
//            }
//            View view;
//            ViewHolder vh;
//            if(convertView==null){
//                view=LayoutInflater.from(MainActivity.this).inflate(R.layout.letter_city, null);
//                vh=new ViewHolder();
//                vh.tv=(TextView)view.findViewById(R.id.letterTextView);
//                vh.ll=(LinearLayout)view.findViewById(R.id.ll);
//                convertView=view;
//                convertView.setTag(vh);
//            }else{
//                view=(View)convertView;
//                vh=(ViewHolder)convertView.getTag();
//            }
//            vh.tv.setText(letterToCity.get(position));
//            if(isLetter){
//                vh.ll.setBackgroundColor(Color.LTGRAY);
//                vh.tv.setPadding(5, 5, vh.tv.getPaddingRight(), 5);
//            }else{
//                vh.ll.setBackgroundColor(Color.WHITE);
//                vh.tv.setPadding(10, 10, vh.tv.getPaddingRight(), 10);
//            }
//             return view;
        }
    }

    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position,
            long id) {
        switch (parent.getId()) {
        case R.id.listView1:
            boolean  isLetter=false;
            for(int i=0;i<letter.length;i++){
                if(letter[i].equals(letterToCity.get(position))){
                    isLetter=true;
                    break;
                }
            }
            if(!isLetter){
                Toast.makeText(this, letterToCity.get(position), Toast.LENGTH_SHORT).show();
            }
            break;
        case R.id.listView2:
            for(int i=0;i<letterToCity.size();i++){
                if(letter[position].equals(letterToCity.get(i))){
                    lv.setSelection(i);
                    break;
                }
            }
            break;
        default:
            break;
        }
    }
}

class ViewHolder{
    TextView tv;
    LinearLayout ll;
}
class ViewHolder1{
    TextView tv;
}
class ViewHolder2{
    TextView tv;
}
技术分享

Android例子源码非第三方实现根据字母排序的城市列表

标签:

原文地址:http://www.cnblogs.com/wbp0818/p/5453027.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!