Friday, 8 July 2016

Horizontal List using recycler view in android

activity_main.xml

<?xml version="1.0" encoding="utf-8"?><android.support.v7.widget.RecyclerView
android:id="@+id/recycler_view"
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@android:color/white"
android:layout_gravity="center"
/>

item_view.xml 
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#efefef"
android:layout_margin="5dp">
<ImageView
android:id="@+id/item_icon"
android:layout_width="40dp"
android:layout_height="40dp"
android:layout_centerVertical="true"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_alignParentTop="true"
android:layout_marginRight="6dip"
android:src="@android:drawable/ic_notification_overlay"/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_toRightOf="@id/item_icon"
android:layout_centerVertical="true"
android:layout_margin="5dp"
android:padding="5dp"
android:background="@android:color/white"
android:orientation="vertical">
<TextView
android:id="@+id/item_text"
android:layout_width="match_parent"
android:layout_height="26dp"
android:ellipsize="marquee"
android:singleLine="true"
android:textColor="@android:color/black"
android:textSize="16sp"/>
<TextView
android:id="@+id/item_text2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text=":)"
android:textColor="@android:color/holo_blue_bright"
android:textSize="12sp"/>
</LinearLayout>
</RelativeLayout>
  

MyRecyclerHolder.java

 package com.rajeshvijayakumar.recycler;

import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

public class MyRecyclerHolder extends RecyclerView.ViewHolder {
 
public TextView textView;
public ImageView imageView;
public TextView textView2;
 
    public MyRecyclerHolder(View view) {
      super(view);
      this.imageView = (ImageView) view.findViewById(R.id.item_icon);
      this.textView = (TextView) view.findViewById(R.id.item_text);
      this.textView2 =(TextView) view.findViewById(R.id.item_text2);
    }
}

MyRecyclerAdapter.java

package com.rajeshvijayakumar.recycler;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

public class MyRecyclerAdapter extends RecyclerView.Adapter<MyRecyclerHolder> {

String[] menus;
Context adapContext;
public MyRecyclerAdapter(Context context, String[] menu) { adapContext = context; menus = menu;
}
@Override
public MyRecyclerHolder onCreateViewHolder(ViewGroup viewGroup, int i) {
View view = LayoutInflater.from(viewGroup.getContext()).inflate(
R.layout.item, null);
MyRecyclerHolder viewHolder = new MyRecyclerHolder(view);
return viewHolder;
}
@Override
public void onBindViewHolder(MyRecyclerHolder viewHolder, int i) {
// int length = (menus != null && menus.length > 0) ? menus.length : 0; if (i < length) {
viewHolder.textView.setText(menus[i]);
}
}
@Override
public int getItemCount() {
return (null != menus ? menus.length : 0);
}
//
}

RecyclerDemoActivity.java

package com.rajeshvijayakumar.recycler;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;

public class RecyclerDemoActivity extends AppCompatActivity {

private RecyclerView mRecyclerView;
private MyRecyclerAdapter adapter;
private String[] menus = { "item1", "item2", "item3", "item4", "item5", "item6", "item7", "item8", "item9", "item10",
"item11", "item12", "item13", "item14", "item15", "item16", "item17", "item18", "item19", "item20"};

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

// Initialize recycler view mRecyclerView = (RecyclerView) findViewById(R.id.recycler_view);
LinearLayoutManager layoutManager = new LinearLayoutManager(this);
layoutManager.setOrientation(LinearLayoutManager.HORIZONTAL);
mRecyclerView.setLayoutManager(layoutManager);
adapter = new MyRecyclerAdapter(this, menus);
mRecyclerView.setAdapter(adapter);
}
}



Output :





No comments:

Post a Comment