Skip to content

Instantly share code, notes, and snippets.

@AhmadVatani
Last active July 15, 2016 06:49
Show Gist options
  • Save AhmadVatani/15f04644d3c1521025cd6eb60247a785 to your computer and use it in GitHub Desktop.
Save AhmadVatani/15f04644d3c1521025cd6eb60247a785 to your computer and use it in GitHub Desktop.
Android Horizontal Listview using RecycleView
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<android.support.v7.widget.RecyclerView
android:id="@+id/recyclerView"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</RelativeLayout>
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
import java.util.ArrayList;
public class AdapterHorizontalList extends RecyclerView.Adapter<AdapterHorizontalList.ViewHolder> {
private ArrayList<String> items;
public AdapterHorizontalList(ArrayList<String> items) {
this.items = items;
}
public static class ViewHolder extends RecyclerView.ViewHolder {
ImageView img;
TextView txt;
private ViewHolder(View view) {
super(view);
img = (ImageView) view.findViewById(R.id.img);
txt = (TextView) view.findViewById(R.id.txtName);
}
}
@Override
public ViewHolder onCreateViewHolder(ViewGroup viewGroup, int i) {
View v = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.row_list, null);
ViewHolder viewHolder = new ViewHolder(v);
return viewHolder;
}
@Override
public void onBindViewHolder(ViewHolder holder, int position) {
String item = items.get(position);
holder.txt.setText(item);
}
@Override
public int getItemCount() {
return items.size();
}
}
add below dependency to your app gradle:
compile 'com.android.support:design:23.4.0'
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import java.util.ArrayList;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
RecyclerView recyclerView = (RecyclerView) findViewById(R.id.recyclerView);
//Make Listview Horizontal
LinearLayoutManager layoutManager= new LinearLayoutManager(this, LinearLayoutManager.HORIZONTAL, true);
recyclerView.setLayoutManager(layoutManager);
//end
ArrayList<String> items = new ArrayList<>();
items.add("Book 1");
items.add("Book 2");
items.add("Book 3");
items.add("Book 4");
items.add("Book 5");
items.add("Book 6");
items.add("Book 7");
AdapterHorizontalList adapter = new AdapterHorizontalList(items);
recyclerView.setAdapter(adapter);
}
}
<?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"
android:padding="16dp">
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<ImageView
android:id="@+id/img"
android:layout_width="100dp"
android:layout_height="100dp"
android:src="@mipmap/ic_launcher"/>
<TextView
android:id="@+id/txtName"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/img"
android:layout_centerHorizontal="true"
android:text="Hello World"/>
</RelativeLayout>
</LinearLayout>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment