Reputation: 1
I have a ListView
<ListView
android:layout_width="match_parent"
android:divider="@null"
android:dividerHeight="0dp"
android:layout_height="match_parent"
android:id="@+id/listView1"/>
Here i'm adding some item using my custom adapter.
CustomAdapter adapter = new CustomAdapter(this, myArrayList);
ListView listView = (ListView) findViewById(R.id. listView1);
listView.setAdapter(adapter);
In my custom Adapter, i used also a custom layout, where only one textVeiw:
<?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">
<TextView
android:paddingLeft="16dp"
android:paddingRight="16dp"
android:paddingBottom="8dp"
android:paddingTop="8dp"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/custom_textView"/>
</LinearLayout>
Everything is good. Working properly. The textView displaying texts line by line. Example:
<TextView>
<TextView>
<TextView>
and so on..
But, i don't want to display text line by line. i want to display text one immediately after another.
Like:
<TextView><TextView><TextView><TextView><TextView><TextView>
but it'll take a new line when it reaches the end of its parent or device screen.
Thanks in advance.
Upvotes: 0
Views: 394
Reputation: 6857
I think you want horizontal list view instead of vertical list view.
Following Step are there :
Implement Recyclerview in your layout (XML) “your_activity.xml” file.
<android.support.v7.widget.RecyclerView
android:id="@+id/horizontalRecyclerView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/colorWhite">
</android.support.v7.widget.RecyclerView>
Implement List Adapter Item Layout (XML) “item_list.xml” file.
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:orientation="vertical"
android:id="@+id/lnrItem"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:id="@+id/txtMessage"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:text="Name"
android:textColor="@color/colorWhite"
android:textSize="14sp" />
</LinearLayout>
Implement Custome List Adapter “CustomListAdapter.java”
public class CustomListAdapter extends RecyclerView.Adapter< CustomListAdapter.ViewHolder> { private Activity context; private ArrayList<String> list = new ArrayList<String>
();
public CustomListAdapter(Activity context, ArrayList<String> list) {
this.context = context;
this.list = list;
}
@Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = LayoutInflater.from(context).inflate(R.layout.item_list, parent, false);
return new ViewHolder(view);
}
@Override
public void onBindViewHolder(final ViewHolder holder, final int position) {
try {
holder.getTxtMessage().setText(list[position]);
} catch (Exception e) {
e.printStackTrace();
}
}
@Override
public int getItemCount() {
return list.size();
}
public class ViewHolder extends RecyclerView.ViewHolder {
private TextView txtMessage;
public ViewHolder(View view) {
super(view);
txtMessage = (TextView) view.findViewById(R.id.txtMessage);
}
public TextView getTxtMessage() {
return txtMessage;
}
}
Set Adapter in Activity “YourActivity.java” in OnCreate Method.
@Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.your_activity);
RecyclerView horizontalRecyclerView = (RecyclerView) findViewById(R.id.horizontalRecyclerView);
LinearLayoutManager linearLayoutManager = new LinearLayoutManager(this, LinearLayoutManager.HORIZONTAL, false); horizontalRecyclerView.setLayoutManager(linearLayoutManager);
ArrayList<String> list = new ArrayList<>();
list.add("First TextView ");
list.add("Second TextView ");
list.add("Third TextView ");
list.add("Four TextView ");
list.add("Fifth TextView ");
private CustomListAdapter adapter = new CustomListAdapter(YourActivity.this, list); horizontalRecyclerView.setAdapter(topMemberAdapter);
}
Upvotes: 0