源码片段
public class MainActivity extends FragmentActivity implements OnCardClickListener{
List<string> list;
private TestFragment frag;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initUI();
}
private void initUI() {
CardView cardView = (CardView) findViewById(R.id.cardView1);
cardView.setOnCardClickListener(this);
cardView.setItemSpace(Utils.convertDpToPixelInt(this, 20));
MyCardAdapter adapter = new MyCardAdapter(this);
adapter.addAll(initData());
cardView.setAdapter(adapter);
FragmentManager manager = getSupportFragmentManager();
frag = new TestFragment();
manager.beginTransaction().add(R.id.contentView, frag).commit();
}
@Override
public void onCardClick(final View view, final int position) {
Toast.makeText(MainActivity.this, position "", Toast.LENGTH_SHORT).show();
Bundle bundle = new Bundle();
bundle.putString("text", list.get(position%list.size()));
frag.show(view,bundle);
}
private List<string> initData() {
list = new ArrayList<string>();
list.add("a");
list.add("b");
list.add("c");
list.add("d");
list.add("e");
list.add("f");
list.add("g");
return list;
}
public class MyCardAdapter extends CardAdapter<string>{
public MyCardAdapter(Context context) {
super(context);
}
@Override
public int getCount() {
return Integer.MAX_VALUE;
}
@Override
protected View getCardView(int position,
View convertView, ViewGroup parent) {
if(convertView == null) {
LayoutInflater inflater = LayoutInflater.from(MainActivity.this);
convertView = inflater.inflate(R.layout.item_layout, parent, false);
}
TextView tv = (TextView) convertView.findViewById(R.id.textView1);
String text = getItem(position%list.size());
tv.setText(text);
return convertView;
}
}
}</string></string></string></string>