RecyclerView and CardView

android.support.v7.widget.RecyclerView

android.support.v7.widget.CardView

RecyclerView使用時需要 adapter 和 layout manager

adapter extends RecyclerView.Adapter 

layout manager extends RecyclerView.LayoutManager

animations extends RecyclerView.ItemAnimator() 使用setItemAnimator()

public class Myadapter extends RecyclerView.Adapter<MyAdapter.ViewHolder>{
	private String[] mDataset;

	public static class viewHolder extends RecyclerView.ViewHolder{
		public TextView mTextView;
		public ViewHolder(TextView v){
			super(v);
			mTextView = v;
		}
	}

	public MyAdapter(String[] myDataset){
		mDataset = myDataset;
	}

	public MyAdapter.ViewHolder onCreateViewHolder(ViewGroup parent,int viewType){
		View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.my_text_view,parent,false);
		//...
		ViewHolder vh = new ViewHolder(v);
		return vh;
	}

	public void onBindViewHolder(ViewHolder holder,int position){
		holder.mTextView.setText(mDataset[positioni]);
	}

	public int getItemCount(){
		return mDataset.length;
	}
}


dependencies {
    ...
    compile 'com.android.support:cardview-v7:+'
    compile 'com.android.support:recyclerview-v7:+'
}


CardView extends FrameLayout
card_view:cardCornerRadius  <--> CardView.setRadius

card_view:cardBackgroundColor 

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    xmlns:card_view="http://schemas.android.com/apk/res-auto"
    ... >
    <!-- A CardView that contains a TextView -->
    <android.support.v7.widget.CardView
        xmlns:card_view="http://schemas.android.com/apk/res-auto"
        android:id="@+id/card_view"
        android:layout_gravity="center"
        android:layout_width="200dp"
        android:layout_height="200dp"
        card_view:cardCornerRadius="4dp">

        <TextView
            android:id="@+id/info_text"
            android:layout_width="match_parent"
            android:layout_height="match_parent" />
    </android.support.v7.widget.CardView>
</LinearLayout>








android.support.v7.widget.CardView
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章