- Add following line in build.gradle
compile 'com.android.support:recyclerview-v7:26.+'
- Add following line in main.xml
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content">
<android.support.v7.widget.RecyclerView
android:id="@+id/recycle_home_work"
android:layout_width="fill_parent"
android:layout_height="wrap_content">
</android.support.v7.widget.RecyclerView>
</LinearLayout>
- Add following code in MainActivity.class
RecyclerView recycle_test = (RecyclerView) findViewById(R.id.recycle_test);
//For Linear Layout Manager for only Listview(Item in List)
TestAdapter testAdapter = new TestAdapter(TestActivity.this, list_array);
LinearLayoutManager llm = new LinearLayoutManager(TestActivity.this);
llm.setOrientation(LinearLayoutManager.VERTICAL);
recycle_test.setLayoutManager(llm);
recycle_test.setAdapter(testAdapter);
testAdapter.notifyDataSetChanged();
//For Layout Manager for only Gridview(Item in Grid)
TestAdapter testAdapter = new TestAdapter(TestActivity.this, list_array);
RecyclerView.LayoutManager mLayoutManager = new GridLayoutManager (TestActivity.this, 2);
recycle_test.setLayoutManager(mLayoutManager);
recycle_test.setAdapter(testAdapter);
testAdapter.notifyDataSetChanged();
//Adapter
public class TestAdapter extends RecyclerView.Adapter<TestAdapter.MyViewHolder> {
public Context context;
List<PhotoResponseData> array_list;
public TestAdapter(Context context, List<PhotoResponseData> array_list) {
this.context = context;
this.array_list = array_list;
}
@Override
public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View itemView = LayoutInflater.from(parent.getContext())
.inflate(R.layout.text_photo, parent, false);
final MyViewHolder holder = new MyViewHolder(itemView);
return new MyViewHolder(itemView);
}
@Override
public void onBindViewHolder(final MyViewHolder holder, final int position) {
String image = array_list.get(position).getImagename();
if (image != null && !image.isEmpty()) {
Picasso.with(context).load(AppConstant.TAG_Image_Photo + image).placeholder(R.mipmap.ic_launcher).into(holder.image_photo);
} else {
holder.image_photo.setImageResource(R.mipmap.ic_launcher);
}
}
@Override
public int getItemCount() {
return array_list.size();
}
@Override
public int getItemViewType(int position) {
return position;
}
public class MyViewHolder extends RecyclerView.ViewHolder {
ImageView image_photo;
public MyViewHolder(View convertView) {
super(convertView);
image_photo = (ImageView) convertView.findViewById(R.id.image_photo);
}
}
}
compile 'com.android.support:recyclerview-v7:26.+'
- Add following line in main.xml
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content">
<android.support.v7.widget.RecyclerView
android:id="@+id/recycle_home_work"
android:layout_width="fill_parent"
android:layout_height="wrap_content">
</android.support.v7.widget.RecyclerView>
</LinearLayout>
- Add following code in MainActivity.class
RecyclerView recycle_test = (RecyclerView) findViewById(R.id.recycle_test);
//For Linear Layout Manager for only Listview(Item in List)
TestAdapter testAdapter = new TestAdapter(TestActivity.this, list_array);
LinearLayoutManager llm = new LinearLayoutManager(TestActivity.this);
llm.setOrientation(LinearLayoutManager.VERTICAL);
recycle_test.setLayoutManager(llm);
recycle_test.setAdapter(testAdapter);
testAdapter.notifyDataSetChanged();
//For Layout Manager for only Gridview(Item in Grid)
TestAdapter testAdapter = new TestAdapter(TestActivity.this, list_array);
RecyclerView.LayoutManager mLayoutManager = new GridLayoutManager (TestActivity.this, 2);
recycle_test.setLayoutManager(mLayoutManager);
recycle_test.setAdapter(testAdapter);
testAdapter.notifyDataSetChanged();
//Adapter
public class TestAdapter extends RecyclerView.Adapter<TestAdapter.MyViewHolder> {
public Context context;
List<PhotoResponseData> array_list;
public TestAdapter(Context context, List<PhotoResponseData> array_list) {
this.context = context;
this.array_list = array_list;
}
@Override
public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View itemView = LayoutInflater.from(parent.getContext())
.inflate(R.layout.text_photo, parent, false);
final MyViewHolder holder = new MyViewHolder(itemView);
return new MyViewHolder(itemView);
}
@Override
public void onBindViewHolder(final MyViewHolder holder, final int position) {
String image = array_list.get(position).getImagename();
if (image != null && !image.isEmpty()) {
Picasso.with(context).load(AppConstant.TAG_Image_Photo + image).placeholder(R.mipmap.ic_launcher).into(holder.image_photo);
} else {
holder.image_photo.setImageResource(R.mipmap.ic_launcher);
}
}
@Override
public int getItemCount() {
return array_list.size();
}
@Override
public int getItemViewType(int position) {
return position;
}
public class MyViewHolder extends RecyclerView.ViewHolder {
ImageView image_photo;
public MyViewHolder(View convertView) {
super(convertView);
image_photo = (ImageView) convertView.findViewById(R.id.image_photo);
}
}
}
No comments:
Post a Comment