activity layout
<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"> <android.support.v7.widget.RecyclerView
android:id="@+id/recycler_view"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:clipToPadding="false"
android:paddingBottom="80dp"
android:scrollbars="vertical" /> </RelativeLayout>
content_main layout
<?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="wrap_content"
android:orientation="vertical"> <RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"> <ImageView
android:id="@+id/country_photo"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:contentDescription="@string/action_settings"
android:scaleType="centerCrop" /> <TextView
android:id="@+id/country_name"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_below="@+id/country_photo"
android:gravity="center"
android:paddingBottom="8dp"
android:paddingTop="8dp"
android:textSize="13sp" /> </RelativeLayout> </LinearLayout>
ItemObject class
public class ItemObject { String name; int photo; public ItemObject(String name, int photo) { this.name = name; this.photo = photo; } public String getName() { return name; } public void setName(String name) { this.name = name; } public int getPhoto() { return photo; } public void setPhoto(int photo) { this.photo = photo; } }
RecyclerViewHolders class
public class RecyclerViewHolders extends RecyclerView.ViewHolder implements View.OnClickListener { public TextView countryName; public ImageView countryPhoto; // add event click
public RecyclerViewHolders(View itemView) { super(itemView); itemView.setOnClickListener(this); countryName = (TextView) itemView.findViewById(R.id.country_name); countryPhoto = (ImageView) itemView.findViewById(R.id.country_photo); } @Override
public void onClick(View v) { Toast.makeText(v.getContext(), "pos:" + getPosition(), Toast.LENGTH_SHORT).show(); } }
RecyclerViewAdapter
public class RecyclerViewAdapter extends RecyclerView.Adapter<RecyclerViewHolders> { private List<ItemObject> itemList; private Context context; public RecyclerViewAdapter(Context context, List<ItemObject> itemList) { this.itemList = itemList; this.context = context; } @Override
public RecyclerViewHolders onCreateViewHolder(ViewGroup parent, int viewType) { View layoutView = LayoutInflater.from(parent.getContext()).inflate(R.layout.content_main, null); RecyclerViewHolders rcv = new RecyclerViewHolders(layoutView); return rcv; } @Override
public void onBindViewHolder(RecyclerViewHolders holder, int position) { holder.countryName.setText(itemList.get(position).getName()); holder.countryPhoto.setImageResource(itemList.get(position).getPhoto()); } @Override
public int getItemCount() { return this.itemList.size(); } }
MainActivity class
public class MainActivity extends AppCompatActivity { private GridLayoutManager lLayout; @Override
protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); List<ItemObject> rowListItem = getAllItemList(); lLayout = new GridLayoutManager(MainActivity.this, 4);// 4->columns
RecyclerView rView = (RecyclerView) findViewById(R.id.recycler_view); rView.setHasFixedSize(true); rView.setLayoutManager(lLayout); RecyclerViewAdapter rcAdapter = new RecyclerViewAdapter(this, rowListItem); rView.setAdapter(rcAdapter); } private List<ItemObject> getAllItemList() { List<ItemObject> allItems = new ArrayList<ItemObject>(); allItems.add(new ItemObject("United States", R.drawable.ic_launcher)); allItems.add(new ItemObject("Canada", R.drawable.ic_launcher)); allItems.add(new ItemObject("United Kingdom", R.drawable.ic_launcher)); allItems.add(new ItemObject("Germany", R.drawable.ic_launcher)); allItems.add(new ItemObject("Sweden", R.drawable.ic_launcher)); allItems.add(new ItemObject("United Kingdom", R.drawable.ic_launcher)); allItems.add(new ItemObject("Germany", R.drawable.ic_launcher)); allItems.add(new ItemObject("Sweden", R.drawable.ic_launcher)); allItems.add(new ItemObject("United States", R.drawable.ic_launcher)); allItems.add(new ItemObject("Canada", R.drawable.ic_launcher)); allItems.add(new ItemObject("United Kingdom", R.drawable.ic_launcher)); allItems.add(new ItemObject("Germany", R.drawable.ic_launcher)); allItems.add(new ItemObject("Sweden", R.drawable.ic_launcher)); allItems.add(new ItemObject("United Kingdom", R.drawable.ic_launcher)); allItems.add(new ItemObject("Germany", R.drawable.ic_launcher)); allItems.add(new ItemObject("Sweden", R.drawable.ic_launcher)); return allItems; } }
No comments:
Post a Comment