StackView is used to display views in StackView. StackView is added from API level 11.
Steps to implement StackView
1. add StackView in layout .
<StackView
android:id="@+id/stackView"
android:layout_width="match_parent"
android:layout_height="match_parent" />
2. Find StackView in Java class file.
StackView stackView = (StackView)findViewById(R.id.stackView);
3. Create Base Adapter class for StackView.
public class StackAdapter extends BaseAdapter {
private final int[] mImages = {R.drawable.eggs, R.drawable.gurudwara, R.drawable.heart, R.drawable.stars, R.drawable.universe};
private final Context mContext;
public StackAdapter(Context context) {
mContext = context;
}
@Override
public int getCount() {
return mImages.length;
}
@Override
public Object getItem(int position) {
return mImages[position];
}
@Override
public long getItemId(int position) {
return 0;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View view = convertView;
if (view == null) {
LayoutInflater vi = (LayoutInflater)
mContext.getSystemService(
Context.LAYOUT_INFLATER_SERVICE);
view = vi.inflate(R.layout.item_view, null, false);
}
ImageView imageView = (ImageView) view.findViewById(
R.id.imageview);
imageView.setImageResource(mImages[position]);
return view;
}
}
4. Create Adapter Instance and set adapter to StackView.
StackAdapter adapter = new StackAdapter(this);
stackView.setAdapter(adapter);
Happy Coding :)
0 Comment(s)