前言
聽聞Android5.0有推出CardView,不過我這個例子還是自定義的,可以做其他改變(改成橢圓?)。在研究期間還遇上了ListView item高度問題,
所以裡面還有放自定義listView修正滾軸感應方法、大item點擊事件以及內部元件Button點擊事件!
關鍵句我認為是在CardActivity.java(相當於MainActivity)內的setAdapter後呼叫方法:
| listView.setAdapter(cardArrayAdapter); | |
| setListViewHeightBasedOnChildren(listView); | |
方法定義如下:
| public int Dp2Px(Context context, float dp) { |
| final float scale = context.getResources().getDisplayMetrics().density; |
| return (int) (dp * scale + 0.5f); |
| } |
| // 建立函數動態設定ListView的高度 |
| public void setListViewHeightBasedOnChildren(ListView listView) { |
| ListAdapter listAdapter = listView.getAdapter(); |
| if (listAdapter == null) { |
| return; |
| } |
| int totalHeight = 0; |
| for (int i = 0; i < listAdapter.getCount(); i++) { |
| View listItem = listAdapter.getView(i, null, listView); |
| listItem.measure(0, 0); |
| totalHeight += listItem.getMeasuredHeight(); |
| } |
| ViewGroup.LayoutParams params = listView.getLayoutParams(); |
| params.height = totalHeight + (listView.getDividerHeight() * (listAdapter.getCount()- 1)); |
| ((MarginLayoutParams)params).setMargins(10, 10, 10, 10); |
| listView.setLayoutParams(params); |
| } |
效果圖
View post on imgur.com後記
用Android studio實作

import android.content.Context;
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.ListAdapter;
import android.widget.ListView;
import android.widget.Toast;
public class CardActivity extends AppCompatActivity {
private static final String TAG = "CardListActivity";
private CardArrayAdapter cardArrayAdapter;
private ListView listView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_card);
listView = (ListView) findViewById(R.id.card_listView);
listView.addHeaderView(new View(this));
listView.addFooterView(new View(this));
cardArrayAdapter = new CardArrayAdapter(getApplicationContext(),
R.layout.list_item);
for (int i = 0; i < 2; i++) {
Card card = new Card("name", "person", "time");
cardArrayAdapter.add(card);
}
listView.setAdapter(cardArrayAdapter);
setListViewHeightBasedOnChildren(listView);
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
Toast.makeText(CardActivity.this, "Bigitem", Toast.LENGTH_LONG)
.show();
}
});
}
public int Dp2Px(Context context, float dp) {
final float scale = context.getResources().getDisplayMetrics().density;
return (int) (dp * scale + 0.5f);
}
public void setListViewHeightBasedOnChildren(ListView listView) {
ListAdapter listAdapter = listView.getAdapter();
if (listAdapter == null) {
return;
}
int totalHeight = 0;
for (int i = 0; i < listAdapter.getCount(); i++) {
View listItem = listAdapter.getView(i, null, listView);
listItem.measure(0, 0);
totalHeight += listItem.getMeasuredHeight();
}
ViewGroup.LayoutParams params = listView.getLayoutParams();
params.height = totalHeight + (listView.getDividerHeight() * (listAdapter.getCount()- 1));
((ViewGroup.MarginLayoutParams)params).setMargins(10, 10, 10, 10);
listView.setLayoutParams(params);
}=========================================
public class Card {
private String line1;
private String line2;
private String line3;
public Card(String line1, String line2, String line3) {
this.line1 = line1;
this.line2 = line2;
this.line3 = line3;
}
public String getLine1() {
return line1;
}
public String getLine2() {
return line2;
}
public String getLine3() {
return line3;
}
}=========================================
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;
import java.util.ArrayList;
import java.util.List;
public class CardArrayAdapter extends ArrayAdapter<Card> {
private static final String TAG = "CardArrayAdapter";
private List<Card> cardList = new ArrayList<Card>();
Context context;
static class CardViewHolder {
TextView line1;
TextView line2;
TextView line3;
Button btn1;
}
public CardArrayAdapter(Context context, int textViewResourceId) {
super(context, textViewResourceId);
this.context=context;
}
@Override
public void add(Card object) {
cardList.add(object);
super.add(object);
}
@Override
public int getCount() {
return this.cardList.size();
}
@Override
public Card getItem(int index) {
return this.cardList.get(index);
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View row = convertView;
CardViewHolder viewHolder;
if (row == null) {
LayoutInflater inflater = (LayoutInflater) this.getContext()
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
row = inflater.inflate(R.layout.list_item, parent, false);
viewHolder = new CardViewHolder();
viewHolder.line1 = (TextView) row.findViewById(R.id.line1);
viewHolder.line2 = (TextView) row.findViewById(R.id.line2);
viewHolder.line3 = (TextView) row.findViewById(R.id.line3);
viewHolder.btn1= (Button) row.findViewById(R.id.button1);
row.setTag(viewHolder);
} else {
viewHolder = (CardViewHolder) row.getTag();
}
Card card = getItem(position);
viewHolder.line1.setText(card.getLine1());
viewHolder.line2.setText(card.getLine2());
viewHolder.line3.setText(card.getLine3());
viewHolder.btn1.setOnClickListener(new Button.OnClickListener(){
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
Toast.makeText(context, "item內的Button", Toast.LENGTH_LONG)
.show();
}
});
return row;
}
public Bitmap decodeToBitmap(byte[] decodedByte) {
return BitmapFactory
.decodeByteArray(decodedByte, 0, decodedByte.length);
}
}==============activity_card.xml==============================
<?xml version="1.0" encoding="utf-8"?>
<ScrollView
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:fadingEdge = "none"
android:background="#e2e4fe"
xmlns:android="http://schemas.android.com/apk/res/android"
>
<LinearLayout
android:gravity="center_horizontal"
android:orientation="vertical"
android:background="#e2e4fe"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<ListView
android:id="@+id/card_listView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:listSelector="@android:color/transparent"
android:cacheColorHint="@android:color/transparent"
android:divider="@null"
android:dividerHeight="10dp"
/>
</LinearLayout>
</ScrollView>
====================list_item.xml===========================
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:descendantFocusability="blocksDescendants"
android:paddingLeft="15dp"
android:paddingRight="15dp"
>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/card_background_selector"
android:descendantFocusability="afterDescendants"
android:gravity="right"
android:paddingBottom="20dp"
android:paddingLeft="15dp"
android:paddingRight="15dp"
android:paddingTop="20dp" >
<ImageView
android:id="@+id/imageView1"
android:layout_width="60dp"
android:layout_height="60dp"
android:src="@mipmap/ic_launcher" />
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
android:layout_weight="1" >
<TextView
android:id="@+id/line1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="1px"
android:gravity="top"
android:textColor="@color/card_text" />
<TextView
android:id="@+id/line2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop = "1px"
android:textColor="@color/card_text" />
<TextView
android:id="@+id/line3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop = "1px"
android:background="@color/card_bg"
android:textColor="@color/card_text" />
</LinearLayout>
<Button
android:id="@+id/button1"
android:layout_width="60dp"
android:layout_height="60dp"
android:text="+" />
</LinearLayout>
</FrameLayout>==================vaules下的colors========================================
<color name="card_text">#000000</color> <color name="card_bg">#32CD32</color>
=====================card_background.xml===================
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item>
<shape android:shape="rectangle">
<solid android:color="#CABBBBBB" />
<corners android:radius="2dp" />
</shape>
</item>
<item
android:bottom="2dp"
android:left="0dp"
android:right="0dp"
android:top="0dp">
<shape android:shape="rectangle">
<solid android:color="@android:color/white" />
<corners android:radius="2dp" />
</shape>
</item>
</layer-list>==============card_background_selector.xml(關於button點下去與梅點的樣式)=======
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="@drawable/card_state_pressed" android:state_pressed="true" />
<item android:drawable="@drawable/card_background" />
</selector>==============card_state_pressed.xml=======
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item>
<shape android:shape="rectangle">
<solid android:color="#ca39883d" />
<corners android:radius="2dp" />
</shape>
</item>
<item
android:bottom="2dp"
android:left="0dp"
android:right="0dp"
android:top="0dp">
<shape android:shape="rectangle">
<solid android:color="#87CEFA" />
<corners android:radius="2dp" />
</shape>
</item>
</layer-list>下載-Eclipse
參考資料:
留言列表

