• ListView加载特效


    (1)首先是透明界面的实现http://zhaohaiyang.blog.51cto.com/2056753/517044
     透明界面的实现可以在XML里面修改属性。
     首先在Androidmanifest.xml中加上
     <activity android:name=".WordSearch"
               android:label="@string/app_name"
              android:theme="@style/transparent">
     </activity>
      
         再在res/values/colors.xml中加上
     <resources>
              <drawable name="translucent_background">#7F000000</drawable>
     </resources>
         最后在styles.xml中添加以下代码
     <resources>
          <style name="transparent">    
          <item name="android:windowBackground">@drawable/translucent_background</item>
          <item name="android:windowIsTranslucent">true</item>
          </style>
     </resources>
      
        这样该Activity就为透明的了。
     貌似还可在代码里面修改,具体怎么实现我还没试过

     
    注:#7F000000表示颜色,其中前两位表示透明度(00—FF),后面六位表示颜色(000000纯黑 ,FFFFFF纯白)。
     
     
    (2)ListView随EditView的改变动态刷新
     
     EditView不能监听Text的改变,要想监听Text的改变则需使用TextWatcher类。下面是主要代码
     
         ArrayList<String> strs=new ArrayList<String>();
       ListView view=null;
         EditText et=null;
     ArrayAdapter<String> adapter;
     TextWatcher watcher;
     
     protected void onCreate(Bundle savedInstanceState) {
             super.onCreate(savedInstanceState);
             requestWindowFeature(Window.FEATURE_NO_TITLE);
             setContentView(R.layout.wordsearch);
         
             et=(EditText)this.findViewById(R.id.result);
             view=(ListView)this.findViewById(R.id.ListView01);
          
             adapter=new ArrayAdapter<String>(this,R.layout.words_item,strs);
             view.setAdapter(adapter);
     
      et.addTextChangedListener(watcher);      
     }
     
     TextWatcher watcher=new TextWatcher(){
     
      @Override
          public void afterTextChanged(Editable s) {
              // TODO Auto-generated method stub
          }
     
          @Override
          public void beforeTextChanged(CharSequence s, int start, int count,
                 int after) {
              // TODO Auto-generated method stub
          }
     
          @Override
          public void onTextChanged(CharSequence s, int start, int before, int count) {
              String string=et.getText().toString();
              strs.add(string);
              adapter.notifyDataSetChanged();
        /*
       这里可以调用adapter.notifyDataSetChanged();
       或是直接改变adapter  然后调用view.setAdapter(adapter);即可
       */
          }
     }
    ------------------------------------------------------------------------------------------
    ListView实现分页加载和滚动加载,发现可以用listView的OnScroll方法来实现,直接上代码
    ListViewScroll.java
    view plaincopy to clipboardprint?
    package zy.lucifer.ListViewScroll;  
    import android.app.Activity;  
    import android.os.Bundle;  
    import android.util.Log;  
    import android.view.Gravity;  
    import android.view.View;  
    import android.view.ViewGroup;  
    import android.widget.AbsListView;  
    import android.widget.BaseAdapter;  
    import android.widget.LinearLayout;  
    import android.widget.ListView;  
    import android.widget.ProgressBar;  
    import android.widget.TextView;  
    import android.widget.Toast;  
    import android.widget.AbsListView.OnScrollListener;  
    import android.widget.LinearLayout.LayoutParams;  
    public class ListViewScroll extends Activity {  
        /** Called when the activity is first created. */  
        private LayoutParams mLayoutParams = new LinearLayout.LayoutParams(  
                LinearLayout.LayoutParams.WRAP_CONTENT,  
                LinearLayout.LayoutParams.WRAP_CONTENT);  
        /** 
         * 设置布局显示目标最大化属性 
         */  
        private LayoutParams FFlayoutParams = new LinearLayout.LayoutParams(  
                LinearLayout.LayoutParams.FILL_PARENT,  
                LinearLayout.LayoutParams.FILL_PARENT);  
        ListView listView ;  
        private int lastItem = 0;  
        LinearLayout loadingLayout;  
        private listViewAdapter adapter;  
        @Override  
        public void onCreate(Bundle savedInstanceState) {  
            super.onCreate(savedInstanceState);  
            setContentView(R.layout.main);  
            listView = (ListView) findViewById(R.id.myList);  
              
            Log.i("test", "onCreate(Bundle savedInstanceState)>>>>>>>>>>>>>>>");  
            // 线性布局  
            LinearLayout layout = new LinearLayout(this);  
            // 设置布局 水平方向  
            layout.setOrientation(LinearLayout.HORIZONTAL);  
            // 进度条  
            ProgressBar progressBar = new ProgressBar(this);  
            // 进度条显示位置  
            progressBar.setPadding(0, 0, 15, 0);  
            // 把进度条加入到layout中  
            layout.addView(progressBar, mLayoutParams);  
            // 文本内容  
            TextView textView = new TextView(this);  
            textView.setText("加载中...");  
            textView.setGravity(Gravity.CENTER_VERTICAL);  
            // 把文本加入到layout中  
            layout.addView(textView, FFlayoutParams);  
            // 设置layout的重力方向,即对齐方式是  
            layout.setGravity(Gravity.CENTER);  
            // 设置ListView的页脚layout  
            loadingLayout = new LinearLayout(this);  
            loadingLayout.addView(layout, mLayoutParams);  
            loadingLayout.setGravity(Gravity.CENTER);  
            listView.addFooterView(loadingLayout);  
            adapter = new listViewAdapter();  
            listView.setAdapter(adapter);  
            listView.setOnScrollListener(new OnScrollListener() {  
       
                @Override  
                public void onScroll(AbsListView view, int firstVisibleItem,  
                        int visibleItemCount, int totalItemCount) {  
                    // TODO Auto-generated method stub  
                    Log.i("test" , "Scroll>>>first: " + firstVisibleItem + ", visible: " + visibleItemCount + ", total: " + totalItemCount);  
                    lastItem = firstVisibleItem + visibleItemCount - 1;  
                    Log.i("test" , "Scroll>>>lastItem:" + lastItem);  
                    //显示50条ListItem,即0-49,因为onScroll是在“滑动”执行过之后才触发,所以用adapter.count<=41作条件  
                    int scrolllength=101;  
                    if (adapter.count<=scrolllength) {  
                        if (firstVisibleItem+visibleItemCount==totalItemCount) {  
                            adapter.count += 10;  
                            adapter.notifyDataSetChanged();  
                            listView.setSelection(lastItem);  
                            int currentPage=adapter.count/10;  
                            Toast.makeText(getApplicationContext(), "第"+currentPage+"页", Toast.LENGTH_LONG).show();  
                        }  
                    }  
                    else {  
                         listView.removeFooterView(loadingLayout);   
                    }  
                }  
                @Override  
                public void onScrollStateChanged(AbsListView view, int scrollState) {  
                    // TODO Auto-generated method stub  
                }  
            });  
        }  
        class listViewAdapter extends BaseAdapter {  
            int count = 10; /* starting amount */  
            public int getCount() {  
                return count;  
            }  
            public Object getItem(int pos) {  
                return pos;  
            }  
            public long getItemId(int pos) {  
                return pos;  
            }  
            public View getView(int pos, View v, ViewGroup p) {  
                Log.i("test", "getView>>>pos:" + pos);  
                TextView view;  
                if (v == null) {  
                    view = new TextView(ListViewScroll.this);  
                } else {  
                    view = (TextView) v;  
                }  
                view.setText("ListItem " + pos);  
                view.setTextSize(20f);  
                view.setGravity(Gravity.CENTER);  
                view.setHeight(60);  
                return view;  
                  
            }  
        }  
    }  
    main.xml
    view plaincopy to clipboardprint?
    <?xml version="1.0" encoding="utf-8"?>  
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
        android:orientation="vertical"  
        android:layout_width="fill_parent"  
        android:layout_height="fill_parent"  
        >  
    <ListView android:cacheColorHint="#00000000" android:id="@+id/myList"  
    android:layout_width="fill_parent" android:layout_height="fill_parent"  
            >  
        </ListView>  
    </LinearLayout>
    ---------------------------------------------
    如果你对Android系统自带的UI控件感觉不够满意,可以尝试下自定义控件,我们就以Button为例,很早以前Android123就写到过Android Button按钮控件美化方法里面提到了xml的selector构造。当然除了使用drawable这样的图片外今天Android开发网谈下自定义图形shape的方法,对于Button控件Android上支持以下几种属性shape、gradient、stroke、corners等。
      我们就以目前系统的Button的selector为例说下:
              <shape>
                <gradient
                    android:startColor="#ff8c00"
                    android:endColor="#FFFFFF"
                    android:angle="270" />
                <stroke
                    android:width="2dp"
                    android:color="#dcdcdc" />
                <corners
                    android:radius="2dp" />
                <padding
                    android:left="10dp"
                    android:top="10dp"
                    android:right="10dp"
                    android:bottom="10dp" />
            </shape>
        对于上面,这条shape的定义,分别为渐变,在gradient中startColor属性为开始的颜色,endColor为渐变结束的颜色,下面的angle是角度。接下来是stroke可以理解为边缘,corners为拐角这里radius属性为半径,最后是相对位置属性padding。
     对于一个Button完整的定义可以为
      <?xml version="1.0" encoding="utf-8"?>
    <selector
        xmlns:android="http://schemas.android.com/apk/res/android">
        <item android:state_pressed="true" >
            <shape>
                <gradient
                    android:startColor="#ff8c00"
                    android:endColor="#FFFFFF"
                    android:angle="270" />
                <stroke
                    android:width="2dp"
                    android:color="#dcdcdc" />
                <corners
                    android:radius="2dp" />
                <padding
                    android:left="10dp"
                    android:top="10dp"
                    android:right="10dp"
                    android:bottom="10dp" />
            </shape>
        </item>
        <item android:state_focused="true" >
            <shape>
                <gradient
                    android:startColor="#ffc2b7"
                    android:endColor="#ffc2b7"
                    android:angle="270" />
                <stroke
                    android:width="2dp"
                    android:color="#dcdcdc" />
                <corners
                    android:radius="2dp" />
                <padding
                    android:left="10dp"
                    android:top="10dp"
                    android:right="10dp"
                    android:bottom="10dp" />
            </shape>
        </item>
        <item>        
            <shape>
                <gradient
                    android:startColor="#ff9d77"
                    android:endColor="#ff9d77"
                    android:angle="270" />
                <stroke
                    android:width="2dp"
                    android:color="#fad3cf" />
                <corners
                    android:radius="2dp" />
                <padding
                    android:left="10dp"
                    android:top="10dp"
                    android:right="10dp"
                    android:bottom="10dp" />
            </shape>
        </item>
    </selector>
    注意Android123提示大家,以上几个item的区别主要是体现在state_pressed按下或state_focused获得焦点时,当当来判断显示什么类型,而没有state_xxx属性的item可以看作是常规状态下。
  • 相关阅读:
    从尾到头打印链表(基于js)
    替换空格(基于js)
    二维数组的查找(基于js)
    关于document对象
    js之DOM操作总结
    将博客搬至CSDN
    关于js中的数组
    干货集中营
    vim编辑器学习记录
    python3 多线程爆破ftp、mysql、ssh
  • 原文地址:https://www.cnblogs.com/sardine/p/2141661.html
Copyright © 2020-2023  润新知