|
|
|
Android จะทำ Indicatorให้แสดงจำนวนและตำแหน่งรูปภาพใน Galleryได้อย่างไรครับ |
|
|
|
|
|
|
|
นี่เป็นgalleryที่ผมสร้างไว้ครับ อาศัยตัวอย่างจากเว็บต่างๆ อยากได้ indicaor กลมๆที่แสดงจำนวนรูปภาพและตำแหน่งของรูปด้วยครับ
MainAcivity
public class MainActivity extends ActionBarActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
FragmentManager fragmentManager = getFragmentManager();
fragmentManager.beginTransaction().replace(R.id.frame_container, new MainFragment()).commit();
}
}
Code (Android-Java)
public class MainFragment extends Fragment {
private ListView listView;
private Gallery gallery;
private List<Item> generateData() {
List<Item> list = new ArrayList<Item>();
list.add(new Item(R.mipmap.ic_launcher, "A1", "Detail A1", "12.1256756", "10.68846"));
list.add(new Item(R.mipmap.ic_launcher, "A2", "Detail A2", "13.1256756", "15.68846"));
list.add(new Item(R.mipmap.ic_launcher, "A3", "Detail A3", "14.1256756", "13.68846"));
list.add(new Item(R.mipmap.ic_launcher, "A4", "Detail A4", "15.1256756", "11.68846"));
list.add(new Item(R.mipmap.ic_launcher, "A5", "Detail A5", "16.1256756", "17.68846"));
list.add(new Item(R.mipmap.ic_launcher, "A6", "Detail A6", "11.1256756", "16.68846"));
list.add(new Item(R.mipmap.ic_launcher, "A7", "Detail A7", "17.1256756", "17.68846"));
list.add(new Item(R.mipmap.ic_launcher, "A8", "Detail A8", "19.1256756", "48.68846"));
list.add(new Item(R.mipmap.ic_launcher, "A9", "Detail A9", "15.1256756", "58.68846"));
list.add(new Item(R.mipmap.ic_launcher, "A10", "Detail A10", "15.1256756", "63.68846"));
list.add(new Item(R.mipmap.ic_launcher, "A11", "Detail A11", "16.1256756", "87.68846"));
list.add(new Item(R.mipmap.ic_launcher, "A12", "Detail A12", "17.1256756", "42.68846"));
list.add(new Item(R.mipmap.ic_launcher, "A13", "Detail A13", "22.1256756", "15.68846"));
list.add(new Item(R.mipmap.ic_launcher, "A14", "Detail A14", "23.1256756", "36.68846"));
list.add(new Item(R.mipmap.ic_launcher, "A15", "Detail A15", "15.1256756", "17.68846"));
list.add(new Item(R.mipmap.ic_launcher, "A16", "Detail A16", "58.1256756", "23.68846"));
list.add(new Item(R.mipmap.ic_launcher, "A17", "Detail A17", "95.1256756", "33.68846"));
list.add(new Item(R.mipmap.ic_launcher, "A18", "Detail A18", "36.1256756", "22.68846"));
return list;
}
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_main, container, false);
gallery = (Gallery) rootView.findViewById(android.R.id.list);
gallery.setAdapter(new ItemAdapter(generateData(), getActivity()));
return rootView;
}
}
Code (Android-Java)
public class ItemAdapter extends BaseAdapter {
private List<Item> list;
private Context mContext;
public ItemAdapter(List<Item> list, Context mContext) { Context จาก Activity ที่เรียกใช้
this.list = list;
this.mContext = mContext;
}
@Override
public int getCount() {
return list.size();
}
@Override
public Item getItem(int position) {
return list.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder viewHolder = null;
if(convertView == null){
viewHolder = new ViewHolder();
convertView = LayoutInflater.from(mContext).inflate(R.layout.list_item, null);
viewHolder.icon = (ImageView) convertView.findViewById(android.R.id.icon); layout ไปให้ attribute icon ของ class ViewHolde
viewHolder.background = (RelativeLayout) convertView.findViewById(android.R.id.background);
convertView.setTag(viewHolder);
}else{
viewHolder = (ViewHolder) convertView.getTag();
}
Item item = getItem(position);
viewHolder.icon.setImageResource(item.getImageResId());
return convertView;
}
private class ViewHolder{
/
ImageView icon;
RelativeLayout background;
}
ITEM
<RelativeLayout
android:id="@android:id/background"
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<ImageView
android:id="@android:id/icon"
android:layout_width="@dimen/list_icon_size"
android:layout_height="@dimen/list_icon_size"
android:layout_margin="@dimen/list_icon_margin"
android:src="@mipmap/ic_launcher"/>
</RelativeLayout>
Fragment
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<Gallery
android:id="@android:id/list"
android:layout_width="match_parent"
android:layout_height="400dp"></Gallery>
</RelativeLayout>
activity_main
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<FrameLayout
android:id="@+id/frame_container"
android:layout_width="match_parent"
android:layout_height="match_parent">
</FrameLayout>
</LinearLayout>
Tag : Mobile, Android
|
|
|
|
|
|
Date :
2015-07-25 20:39:00 |
By :
lekkungza1 |
View :
1408 |
Reply :
1 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Load balance : Server 05
|