Android 在ListView中逐个显示项目
我有一个列表视图Android 在ListView中逐个显示项目,android,listview,Android,Listview,我有一个列表视图 <ListView android:layout_width="match_parent" android:divider="@null" android:dividerHeight="0dp" android:layout_height="match_parent" android:id="@+id/listView1"/> 在我的自定义适配器中,我还使用了自定义布局,其中只有一个T
<ListView
android:layout_width="match_parent"
android:divider="@null"
android:dividerHeight="0dp"
android:layout_height="match_parent"
android:id="@+id/listView1"/>
在我的自定义适配器中,我还使用了自定义布局,其中只有一个TextVew:
<?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>
一切都很好。工作正常。文本视图逐行显示文本。
例如:
等等
但是,我不想逐行显示文本。我想一个接一个地显示文本
比如:
但当它到达其父屏幕或设备屏幕的末尾时,它将使用一个新行
提前谢谢 我认为您需要水平列表视图而不是垂直列表视图 有以下步骤: 在布局(XML)“your_activity.XML”文件中实现Recyclerview。
<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>
<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>
实现列表适配器项目布局(XML)“Item_List.XML”文件。
<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>
<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>
实现自定义列表适配器“CustomListAdapter.java”
公共类CustomListAdapter扩展了RecyclerView.Adapterlist=new ArrayList
()
公共CustomListAdapter(活动上下文,ArrayList列表){
this.context=上下文;
this.list=列表;
}
@凌驾
public ViewHolder onCreateViewHolder(视图组父级,int-viewType){
视图=LayoutFlater.from(上下文)。充气(R.layout.item_列表,父项,false);
返回新的ViewHolder(视图);
}
@凌驾
公共无效onBindViewHolder(最终视图持有人,最终整型位置){
试一试{
holder.getTxtMessage().setText(list[position]);
}捕获(例外e){
e、 printStackTrace();
}
}
@凌驾
public int getItemCount(){
返回list.size();
}
公共类ViewHolder扩展了RecyclerView.ViewHolder{
私有文本视图txtMessage;
公共视图持有者(视图){
超级(视图);
txtMessage=(TextView)view.findViewById(R.id.txtMessage);
}
公共文本视图getTxtMessage(){
返回txtMessage;
}
}
在OnCreate方法的活动“YourActivity.java”中设置适配器。
<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>
<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>
@重写public void onCreate(Bundle savedInstanceState){super.onCreate(savedInstanceState);setContentView(R.layout.your_活动)
RecyclerView horizontalRecyclerView=(RecyclerView)findViewById(R.id.horizontalRecyclerView)
LinearLayoutManager LinearLayoutManager=新的LinearLayoutManager(this,LinearLayoutManager.HORIZONTAL,false);horizontalRecyclerView.setLayoutManager(LinearLayoutManager)
ArrayList list=new ArrayList();
添加(“第一个文本视图”);
添加(“第二个文本视图”);
添加(“第三个文本视图”);
添加(“四个文本视图”);
添加(“第五文本视图”);
private CustomListAdapter=new CustomListAdapter(YourActivity.this,list);horizontalRecyclerView.setAdapter(topMemberAdapter)
}试试RecyclerView。它比listview更好…你可以垂直、水平或网格显示listitems。我认为你正在寻找类似FlowLayout的东西。请看:此外,1.你可以摆脱线性布局。如果你真的需要该布局,那么将布局宽度更改为“包裹内容”.2.将文本视图的布局宽度更改为“包装内容”,否则它将始终采用全宽度。@RafiKamal stackoverflow问题链接未打开。@KishorNR请给我教程链接。。查看此链接
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 ");