• 安卓天天练练(十)ListView


    ListView不能和ScrollView同时使用,因为它已经包含了滚动支持。

    还有个Gallery

    http://blog.csdn.net/dazlly/article/details/7863923

    居然过期了。。。用HorizontalScrollView代替

    package com.narumi.android_12_1;
    
    import android.app.Activity;
    import android.os.Bundle;
    import android.view.View;
    import android.view.ViewGroup;
    import android.widget.AdapterView;
    import android.widget.AdapterView.OnItemClickListener;
    import android.widget.AdapterView.OnItemSelectedListener;
    import android.widget.BaseAdapter;
    import android.widget.HorizontalScrollView;
    import android.widget.ImageView;
    import android.widget.LinearLayout;
    import android.widget.ListView;
    import android.widget.TextView;
    
    public class MainActivity extends Activity {
        //所有图片
        int [] drawableIds = {
                R.drawable.andy,R.drawable.bill,R.drawable.edgar,R.drawable.linus,R.drawable.turing
        };
        
        int [] msgIds= {
                R.string.andy,R.string.bill,R.string.edgar,R.string.linus,R.string.turing
        };
        
        
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.main);
            
            ListView lv = (ListView)findViewById(R.id.ListView01);
            
            BaseAdapter ba = new BaseAdapter() {
                
                @Override
                public View getView(int arg0, View arg1, ViewGroup arg2) {
                    // TODO Auto-generated method stub
                    LinearLayout ll=new LinearLayout(MainActivity.this);
                    ll.setOrientation(LinearLayout.HORIZONTAL);
                    ll.setPadding(5, 5, 5, 5);
                    ImageView ii = new ImageView(MainActivity.this);
                    ii.setImageDrawable(getResources().getDrawable(drawableIds[arg0], getTheme()));
                    ii.setScaleType(ImageView.ScaleType.FIT_XY);
                    ii.setLayoutParams(new HorizontalScrollView.LayoutParams(100,98)); 
                    ll.addView(ii);
                    TextView tv=new TextView(MainActivity.this);
                    tv.setText(getResources().getText(msgIds[arg0]));
                    tv.setTextSize(24);
                    tv.setTextColor(MainActivity.this.getResources().getColor(R.color.white));
                    tv.setPadding(5, 5, 5, 5);
                    tv.setGravity(HorizontalScrollView.FOCUS_LEFT);
                    ll.addView(tv);
                    return ll;
                }
                
                @Override
                public long getItemId(int arg0) {
                    // TODO Auto-generated method stub
                    return 0;
                }
                
                @Override
                public Object getItem(int arg0) {
                    // TODO Auto-generated method stub
                    return null;
                }
                
                @Override
                public int getCount() {
                    // TODO Auto-generated method stub
                    return 5;
                }
            };
            
            lv.setAdapter(ba);
            
            lv.setOnItemSelectedListener(
                    new OnItemSelectedListener(){
    
                        @Override
                        public void onItemSelected(AdapterView<?> arg0, View arg1,
                                int arg2, long arg3) {
                            // TODO Auto-generated method stub
                            TextView tView=(TextView)findViewById(R.id.TextView01);
                            LinearLayout llLayout=(LinearLayout)arg1;
                            TextView tvn=(TextView)llLayout.getChildAt(1);
                            StringBuilder sb=new StringBuilder();
                            sb.append(getResources().getText(R.string.ys));
                            sb.append(".");
                            sb.append(tvn.getText());
                            String stemp=sb.toString();
                            tView.setText(stemp.split("\n")[0]);
                        }
    
                        @Override
                        public void onNothingSelected(AdapterView<?> parent) {
                            // TODO Auto-generated method stub
                            
                        }
                    }
                    );
            lv.setOnItemClickListener(
                    new OnItemClickListener(){
    
                        @Override
                        public void onItemClick(AdapterView<?> arg0, View arg1,
                                int arg2, long arg3) {
                            // TODO Auto-generated method stub
                            TextView tView=(TextView)findViewById(R.id.TextView01);
                            LinearLayout llLayout=(LinearLayout)arg1;
                            TextView tvn=(TextView)llLayout.getChildAt(1);
                            StringBuilder sb=new StringBuilder();
                            sb.append(getResources().getText(R.string.ys));
                            sb.append(".");
                            sb.append(tvn.getText());
                            String stemp=sb.toString();
                            tView.setText(stemp.split("\n")[0]);
                        }
                    }
                    );
        }
    
    }

    自己P了五张图

      

               图灵小朋友真萌 O(∩_∩)O

    由于使用了安卓自带控件,布局文件很简单

    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:background="@color/black"
        android:orientation="vertical"
        tools:context="com.narumi.android_12_1.MainActivity" >
    
        <TextView
            android:id="@+id/TextView01"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:textSize="24sp" 
            android:textColor="@color/white"
            android:text="@string/hello"
             />
    
        <ListView
            android:id="@+id/ListView01"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:background="@color/black"
            android:choiceMode="singleChoice" />
    
    </LinearLayout>

    建立color.xml的时候要注意类型是Color List,下一步的时候可以填生成路径,直接把color改成 values

       <color name="red">#fd8d8d</color>
        <color name="green">#9cfda3</color>
        <color name="blue">#8d9dfd</color>
        <color name="white">#ffffff</color>
        <color name="black">#000000</color>
        <color name="gray">#050505</color>

    strings一般原来就有

        <string name="hello">您选择了</string>
        <string name="app_name">ListViewExample</string>
        <string name="andy">Andy Rubin 
    Android的创造者</string>
        <string name="bill">Bill Joy 
    Java创造者之一</string>
        <string name="edgar">Edgr F. Codd 
    关系数据库之父</string>
        <string name="linus">Linus Torvalds 
    Linux之父</string>
        <string name="turing">Turing Alan 
    IT的祖师爷</string>
        <string name="ys">您选择了</string>

    此例的效果也就是点一行会再顶部显示与这行同样的信息,java代码有点多有点乱,貌似已经使得HorizontalScrollView与ListView冲突了,待修改(Gallery有毛线用啊)。

  • 相关阅读:
    Luogu P4727 [HNOI2009]图的同构记数
    ARC 101 E
    JSOI2019 Round2 游记
    JSOI2019 Round1(十二省联考)游记
    Technocup 2019
    Codeforces Round #533 (Div. 2)比赛总结
    学习链接
    2018.12.29-2018.1.9安师大附中集训
    关于考试
    NOIP2018提高组 游记
  • 原文地址:https://www.cnblogs.com/haimingpro/p/4754196.html
Copyright © 2020-2023  润新知