美文网首页
CardView的使用

CardView的使用

作者: Summer_27d1 | 来源:发表于2018-07-25 10:33 被阅读0次

    一。 cardView使用需要导入v7包

    image.png
    image.png
    首先 我们 在主布局中定义一个listView控件 让内容逐条显示
    效果图:
    image.png
    activity_main.xml
    ···
    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.example.wangye.androidxmlc_class5.MainActivity">
    <ListView
        android:id="@+id/listview"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />
    

    </LinearLayout>

    ···
    然后定义一个adapter.xml

    ···
    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <android.support.v7.widget.CardView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_margin="10dp"
        android:clickable="true"
        android:foreground="?attr/selectableItemBackground"
        android:stateListAnimator="@drawable/lift_on_touch"
        app:cardBackgroundColor="@color/colorAccent"
        app:cardCornerRadius="10dp"
        app:cardElevation="12dp">
    
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:orientation="vertical">
    
            <TextView
                android:id="@+id/textView"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:text="TextView"
                android:textSize="20sp" />
    
            <TextView
                android:id="@+id/textView2"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:text="TextView"
                android:textSize="20sp" />
    
        </LinearLayout>
    
    </android.support.v7.widget.CardView>
    

    </LinearLayout>
    ···


    image.png

    定义的这句话
    我们在drawable 文件夹下
    定义了一个xml文件
    ···
    <?xml version="1.0" encoding="utf-8"?>
    <selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:state_enabled="true" android:state_pressed="true">
    <set>
    <objectAnimator
    android:duration="@android:integer/config_shortAnimTime"
    android:propertyName="translationZ"
    android:valueTo="6dp"
    android:valueType="floatType" />
    </set>
    </item>
    <item>
    <set>
    <objectAnimator
    android:duration="@android:integer/config_shortAnimTime"
    android:propertyName="translationZ"
    android:valueTo="0"
    android:valueType="floatType" />
    </set>
    </item>
    </selector>
    ···
    MainActivity
    适配器写的是内部类实现的
    ···
    package com.example.wangye.androidxmlc_class5;

    import android.support.v7.app.AppCompatActivity;
    import android.os.Bundle;
    import android.view.View;
    import android.view.ViewGroup;
    import android.widget.AdapterView;
    import android.widget.BaseAdapter;
    import android.widget.ListView;
    import android.widget.TextView;
    import android.widget.Toast;

    import java.util.ArrayList;
    import java.util.HashMap;

    public class MainActivity extends AppCompatActivity {
    ListView listView;
    ArrayList<HashMap<String,String>> arrayList = new ArrayList<>();
    @Override
    protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    init();
    }
    public void init(){
    listView = (ListView) findViewById(R.id.listview);
    for (int i = 0; i < 10; i++) {
    HashMap<String,String> map = new HashMap<>();
    map.put("name","张三"+i);
    map.put("age",(18+i)+"");
    arrayList.add(map);
    }

        listView.setAdapter(new MyAdapter());
        //使用cardView 时 ListView 的点击事件没效果
    

    // listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
    // @Override
    // public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
    // Toast.makeText(MainActivity.this, "1111", Toast.LENGTH_SHORT).show();
    // }
    // });
    }

    class MyAdapter extends BaseAdapter{
    
        @Override
        public int getCount() {
            return arrayList.size();
        }
    
        @Override
        public Object getItem(int i) {
            return arrayList.get(i);
        }
    
        @Override
        public long getItemId(int i) {
            return 0;
        }
    
        @Override
        public View getView(int i, View view, ViewGroup viewGroup) {
            view = View.inflate(MainActivity.this,R.layout.adapter,null);
            //如果这里给view添加监听也是不可以的 没有效果 ,如果不使用cardView 是可以给视图添加监听的
            //所以我们就单独的给TextView 添加监听
            TextView txName = view.findViewById(R.id.textView);
            txName.setText(arrayList.get(i).get("name"));
            TextView txAge = view.findViewById(R.id.textView2);
            txAge.setText(arrayList.get(i).get("age"));
            txName.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    Toast.makeText(MainActivity.this, "2222", Toast.LENGTH_SHORT).show();
                }
            });
            return view;
        }
    }
    

    }

    ···

    相关文章

      网友评论

          本文标题:CardView的使用

          本文链接:https://www.haomeiwen.com/subject/cbzumftx.html