在做项目的时候,遇到了scrollView与listView结合的使用,导致了滑动的混乱,但是有一个办法可以解决掉这个问题,就是手写listView的高度,还有另外一种方法,传送门:《Android -- 在ScrollView中嵌套ListView》。

但是在项目中,我们的scrollview中嵌套这两个ListView,这就更麻烦了,为了不去用两个上述方法,我们将另外一个ListView改写为动态加载布局的方法来实现,在布局等操作上感觉还是跟listview差不多,但是没有Adapter。

子布局

<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:layout_width="match_parent"    android:layout_height="match_parent"    android:orientation="horizontal" >    <ImageView        android:id="@+id/img"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:contentDescription="@string/action_settings"        android:src="@drawable/ic_launcher" />    <TextView        android:id="@+id/txt"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:text="@string/app_name" /></LinearLayout>

显示布局

<LinearLayout 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"    android:orientation="vertical"    tools:context=".MainActivity" >    <ScrollView        android:layout_width="fill_parent"        android:layout_height="wrap_content" >        <LinearLayout            android:id="@+id/lay"            android:layout_width="fill_parent"            android:layout_height="wrap_content"            android:orientation="vertical" >        </LinearLayout>    </ScrollView>    <Button        android:id="@+id/btn_add"        android:layout_width="fill_parent"        android:layout_height="wrap_content"        android:onClick="click_add"        android:text="@string/app_name" /></LinearLayout>

代码实现

public class MainActivity extends Activity {    private LinearLayout lay;    private LinearLayout item;    private ImageView img;    private TextView txt;    private Button btn_add;    private int[] pic = { R.drawable.ic_launcher, R.drawable.maps,            R.drawable.appstore, R.drawable.calculator, R.drawable.camera };    private String[] str_pic = { "ic_launcher", "maps", "appstore",            "calculator", "camera" };    private String[] str = { "1", "2", "3", "4", "5" };    private int time = 0;    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_main);        lay = (LinearLayout) findViewById(R.id.lay);        btn_add = (Button) findViewById(R.id.btn_add);        btn_add.setOnClickListener(new View.OnClickListener() {            @Override            public void onClick(View v) {                try {                    inflateAndFind();                } catch (Exception e) {                    // TODO 自动生成的 catch 块                    e.printStackTrace();                }            }        });    }    private void inflateAndFind() throws Exception {        item = (LinearLayout) View.inflate(getBaseContext(), R.layout.item,                null);        img = (ImageView) item.findViewById(R.id.img);        txt = (TextView) item.findViewById(R.id.txt);                if (time < 5) {            Class<com.yydcdut.layout.R.drawable> cls = R.drawable.class;            int value = cls.getDeclaredField(str_pic[time]).getInt(null);            // img.setImageResource(pic[time]);            img.setImageResource(value);            txt.setText(str[time]);            lay.addView(item);        } else            time = 0;        time++;    }}

代码解析

其实运用的方法就是通过inflate方法将新添加的布局一个个添加上去,inflate在Android里面叫打气筒哈,就是将布局一个个打上去。

后面还有个Class<com.yydcdut.layout.R.drawable>,这个是通过名字去获取ID的int值,应该就是Java的反射机制吧~

我是天王盖地虎的分割线

源代码:http://pan.baidu.com/s/1dD1Qx01

layout.zip

转载请注明出处:http://www.cnblogs.com/yydcdut

更多相关文章

  1. android上改变listView的选中颜色
  2. Android+Jquery Mobile学习系列(5)-SQLite数据库
  3. 【Mark 】AndroidStudio_移动应用开发
  4. android监听键盘
  5. Android(安卓)Activity 详解
  6. Android开发艺术探索知识回顾——第2章 IPC机制:1、IPC 的基础知
  7. Android(安卓)Dagger2 初学笔记
  8. Android菜单Menu的创建
  9. Android(安卓)ViewPager多页面滑动切换以及动画效果

随机推荐

  1. Android(安卓)UI设计——ListView控件和A
  2. Android周学习Step By Step(6)--Android的
  3. Android(安卓)Drawable Resource学习(一)、
  4. Android(安卓)AsyncTask
  5. 创建android Notification
  6. Android(安卓)imageView图片按比例缩放
  7. Android之提高Service优先级总结及androi
  8. Android(安卓)anr介绍
  9. android studio无法更新之解决方案
  10. android布局及常见布局属性 二