public class custom_adapter extends BaseAdapter{ public Context mContext; public LayoutInflater mInflater; public int[] resId; public custom_adapter(Context context, int[] resId){ mContext = context; mInflater = LayoutInflater.from(mContext); this.resId = resId; } @Override public int getCount() { return resId.length; } @Override public Object getItem(int position) { return null; } @Override public long getItemId(int position) { return 0; } @Override public View getView(int position, View convertView, ViewGroup parent) { ViewHolder holder = null; if (convertView == null){ // load layout convertView = mInflater.inflate(R.layout.listview_adap, null); holder = new ViewHolder(); holder.txt1 = (TextView) convertView.findViewById(R.id.text1); convertView.setTag(holder); } else { holder = (ViewHolder) convertView.getTag(); } holder.image = (ImageView) convertView.findViewById(R.id.imgView); holder.image.setImageResource(resId[position]); holder.txt1.setText(String.valueOf(position) + " : First"); return convertView; } } public class ViewHolder{ TextView txt1; ImageView image; }
public class MainActivity extends AppCompatActivity { private ListView listview; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); int[] resId = {R.drawable.batman, R.drawable.ironman, R.drawable.spiderman, R.drawable.superman}; listview = (ListView) findViewById(R.id.listview_main); listview.setAdapter(new custom_adapter(getApplicationContext(), resId)); } }
<?xml version="1.0" encoding="utf-8"?> <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" android:paddingBottom="@dimen/activity_vertical_margin" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" tools:context="com.example.first_home.myapplication.MainActivity"> <ImageView android:id="@+id/header" android:layout_width="match_parent" android:layout_height="300px" android:src="@mipmap/ic_launcher"/> <ListView android:id="@+id/listview_main" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_below="@+id/header" > </ListView> </RelativeLayout>
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="match_parent" android:layout_height="match_parent"> <RelativeLayout android:id="@+id/layout" android:layout_width="match_parent" android:layout_height="wrap_content"> <TextView android:id="@+id/text1" android:text="hello" android:textSize="20dp" android:textColor="#000000" android:layout_width="match_parent" android:layout_height="wrap_content" /> <TextView android:id="@+id/text2" android:layout_width="match_parent" android:layout_height="wrap_content" android:textSize="30sp" android:textColor="#000000" android:text="Description..." android:layout_below="@+id/text1"/> <ImageView android:id="@+id/imgView" android:layout_width="400px" android:layout_height="400px" android:src="@drawable/menu01" android:layout_below="@+id/text2"/> </RelativeLayout> </LinearLayout>
เพื่อความปลอดภัยของเว็บบอร์ด ไม่อนุญาติให้แทรก แท็ก [img]....[/img] โดยการอัพโหลดไฟล์รูปจากที่อื่น เช่นเว็บไซต์ ฟรีอัพโหลดต่าง ๆ อัพโหลดแทรกรูปภาพ ให้ใช้บริการอัพโหลดไฟล์ของไทยครีเอท และตัดรูปภาพให้พอดีกับสกรีน เพื่อความโหลดเร็วและไฟล์ไม่ถูกลบทิ้ง