ScrollView+LinearLayout 仿Listview 效果

布局代码
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/activity_main"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context="com.wsd.sun.myapplication.MainActivity">

    <ScrollView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="#ebebeb"
        android:orientation="vertical"
        android:scrollbars="none">

        <LinearLayout
            android:id="@+id/get_lottery"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginTop="15dp"
            android:orientation="vertical" />

    </ScrollView>
</LinearLayout>

  代码
public class MainActivity extends AppCompatActivity {

    private LinearLayout mLayout;
    private Context context;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        context=this;
        mLayout= (LinearLayout) findViewById(R.id.get_lottery);
        initLottery();
    }

    private void initLottery() {
        mLayout.removeAllViews();

        final String[] tags = new String[]{"去测试0","去测试1","去测试2","去测试3","去测试4","去测试5","去测试6"};
        if (tags != null && tags.length > 0) {
            mLayout.setVisibility(View.VISIBLE);
            for ( int i = 0; i < tags.length ; i++) {
                    final String name_str = tags[i];
                    View view = LayoutInflater.from(context).inflate(R.layout.list_item_select_lottery, null);
                    TextView nameView = (TextView) view.findViewById(R.id.list_item_lottery);
                    LinearLayout layoutView = (LinearLayout) view.findViewById(R.id.list_item_layout);
                    nameView.setText(name_str);
                    nameView.setTextColor(0xff4d4d4d);
                    layoutView.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        Toast.makeText(context,"点击了:"+name_str,Toast.LENGTH_SHORT).show();
                    }
                });
                    mLayout.addView(view);
            }
        } else {
            mLayout.setVisibility(View.GONE);
        }
    }
}




  demo地址:http://download.csdn.net/detail/wang1897b7/9714057

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值