kin*_*iva 15 java android android-recyclerview
我想将我的android RecyclerView项目与基于日期的标题分组,如下所示:
1 week ago
- item
- item
- item
- item
2 weeks ago
- item
- item
- item
Run Code Online (Sandbox Code Playgroud)
基于某个元素的那种东西或分组.
kin*_*iva 28
这是一个解决方案,我借助于对网络和博客链接以及Kartikey Kuswhaha的大量研究,所以它不是我所有的功劳,但我只想更清楚地说明它.下面是代码:创建以下文件:PojoOfJsonArray,MainActivity,ListItem,GeneralItem,DateItem,Adapter
PojoOfJsonArray .java-这个文件将代表你的POJO课程或你在应用程序中进行的任何pojo:
public class PojoOfJsonArray {
public PojoOfJsonArray(String name, String date) {
this.name = name;
this.date = date;
}
private String name,date;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getDate() {
return date;
}
public void setDate(String date) {
this.date = date;
}
}
Run Code Online (Sandbox Code Playgroud)
MainActivity .java这是您将用于实现recyclerview的活动:
public class MainActivity extends AppCompatActivity {
private List<PojoOfJsonArray> myOptions = new ArrayList<>();
List<ListItem> consolidatedList = new ArrayList<>();
private RecyclerView mRecyclerView;
private Adapter adapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mRecyclerView = (RecyclerView) findViewById(R.id.recyclerview);
mRecyclerView.setHasFixedSize(true);
myOptions.add(new PojoOfJsonArray("name 1", "2016-06-21"));
myOptions.add(new PojoOfJsonArray("name 2", "2016-06-05"));
myOptions.add(new PojoOfJsonArray("name 2", "2016-06-05"));
myOptions.add(new PojoOfJsonArray("name 3", "2016-05-17"));
myOptions.add(new PojoOfJsonArray("name 3", "2016-05-17"));
myOptions.add(new PojoOfJsonArray("name 3", "2016-05-17"));
myOptions.add(new PojoOfJsonArray("name 3", "2016-05-17"));
myOptions.add(new PojoOfJsonArray("name 2", "2016-06-05"));
myOptions.add(new PojoOfJsonArray("name 3", "2016-05-17"));
HashMap<String, List<PojoOfJsonArray>> groupedHashMap = groupDataIntoHashMap(myOptions);
for (String date : groupedHashMap.keySet()) {
DateItem dateItem = new DateItem();
dateItem.setDate(date);
consolidatedList.add(dateItem);
for (PojoOfJsonArray pojoOfJsonArray : groupedHashMap.get(date)) {
GeneralItem generalItem = new GeneralItem();
generalItem.setPojoOfJsonArray(pojoOfJsonArray);//setBookingDataTabs(bookingDataTabs);
consolidatedList.add(generalItem);
}
}
adapter = new Adapter(this, consolidatedList);
LinearLayoutManager layoutManager = new LinearLayoutManager(this);
layoutManager.setOrientation(LinearLayoutManager.VERTICAL);
mRecyclerView.setLayoutManager(layoutManager);
mRecyclerView.setAdapter(adapter);
}
private HashMap<String, List<PojoOfJsonArray>> groupDataIntoHashMap(List<PojoOfJsonArray> listOfPojosOfJsonArray) {
HashMap<String, List<PojoOfJsonArray>> groupedHashMap = new HashMap<>();
for (PojoOfJsonArray pojoOfJsonArray : listOfPojosOfJsonArray) {
String hashMapKey = pojoOfJsonArray.getDate();
if (groupedHashMap.containsKey(hashMapKey)) {
// The key is already in the HashMap; add the pojo object
// against the existing key.
groupedHashMap.get(hashMapKey).add(pojoOfJsonArray);
} else {
// The key is not there in the HashMap; create a new key-value pair
List<PojoOfJsonArray> list = new ArrayList<>();
list.add(pojoOfJsonArray);
groupedHashMap.put(hashMapKey, list);
}
}
return groupedHashMap;
}
}
Run Code Online (Sandbox Code Playgroud)
myOptions是用于将数据输入的地方. ListItem .java
public abstract class ListItem {
public static final int TYPE_DATE = 0;
public static final int TYPE_GENERAL = 1;
abstract public int getType();
}
Run Code Online (Sandbox Code Playgroud)
GeneralItem .java
public class GeneralItem extends ListItem {
private PojoOfJsonArray pojoOfJsonArray;
public PojoOfJsonArray getPojoOfJsonArray() {
return pojoOfJsonArray;
}
public void setPojoOfJsonArray(PojoOfJsonArray pojoOfJsonArray) {
this.pojoOfJsonArray = pojoOfJsonArray;
}
@Override
public int getType() {
return TYPE_GENERAL;
}
}
Run Code Online (Sandbox Code Playgroud)
DateItem .java
public class DateItem extends ListItem {
private String date;
public String getDate() {
return date;
}
public void setDate(String date) {
this.date = date;
}
@Override
public int getType() {
return TYPE_DATE;
}
}
Run Code Online (Sandbox Code Playgroud)
适配器 .java这个适配器用于recyclerview如果你没有充分了解如何制作简单的循环再循环视图,那么我建议你阅读那些并且在这方面做得很好,因为这样会变得更加棘手:
public class Adapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
private Context mContext;
List<ListItem> consolidatedList = new ArrayList<>();
public Adapter(Context context, List<ListItem> consolidatedList) {
this.consolidatedList = consolidatedList;
this.mContext = context;
}
@Override
public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
RecyclerView.ViewHolder viewHolder = null;
LayoutInflater inflater = LayoutInflater.from(parent.getContext());
switch (viewType) {
case ListItem.TYPE_GENERAL:
View v1 = inflater.inflate(R.layout.items, parent,
false);
viewHolder = new GeneralViewHolder(v1);
break;
case ListItem.TYPE_DATE:
View v2 = inflater.inflate(R.layout.itemsh, parent, false);
viewHolder = new DateViewHolder(v2);
break;
}
return viewHolder;
}
@Override
public void onBindViewHolder(RecyclerView.ViewHolder viewHolder, int position) {
switch (viewHolder.getItemViewType()) {
case ListItem.TYPE_GENERAL:
GeneralItem generalItem = (GeneralItem) consolidatedList.get(position);
GeneralViewHolder generalViewHolder= (GeneralViewHolder) viewHolder;
generalViewHolder.txtTitle.setText(generalItem.getPojoOfJsonArray().getName());
break;
case ListItem.TYPE_DATE:
DateItem dateItem = (DateItem) consolidatedList.get(position);
DateViewHolder dateViewHolder = (DateViewHolder) viewHolder;
dateViewHolder.txtTitle.setText(dateItem.getDate());
// Populate date item data here
break;
}
}
// ViewHolder for date row item
class DateViewHolder extends RecyclerView.ViewHolder {
protected TextView txtTitle;
public DateViewHolder(View v) {
super(v);
this.txtTitle = (TextView) v.findViewById(R.id.txt);
}
}
// View holder for general row item
class GeneralViewHolder extends RecyclerView.ViewHolder {
protected TextView txtTitle;
public GeneralViewHolder(View v) {
super(v);
this.txtTitle = (TextView) v.findViewById(R.id.txt);
}
}
@Override
public int getItemViewType(int position) {
return consolidatedList.get(position).getType();
}
@Override
public int getItemCount() {
return consolidatedList != null ? consolidatedList.size() : 0;
}
}
Run Code Online (Sandbox Code Playgroud)
这有两种布局.所以
我只是在这里报告kinsley kajiva响应的Kotlin版本。
注意:此版本使用数据竞价,因此请记住在应用程序 build.gradle 上启用它:
android {
...
buildFeatures {
dataBinding true
}
}
Run Code Online (Sandbox Code Playgroud)
PojoOfJsonArray.kt
data class PojoOfJsonArray(
val name: String,
val date: String
)
Run Code Online (Sandbox Code Playgroud)
MainActivity.kt
class MainActivity : AppCompatActivity() {
private lateinit var binding: ActivityMainBinding
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
binding = DataBindingUtil.setContentView(this, R.layout.activity_main)
binding.list.setHasFixedSize(true)
val myOptions = listOf(
PojoOfJsonArray("name 1", "2016-06-21"),
PojoOfJsonArray("name 2", "2016-06-05"),
PojoOfJsonArray("name 2", "2016-06-05"),
PojoOfJsonArray("name 3", "2016-05-17"),
PojoOfJsonArray("name 3", "2016-05-17"),
PojoOfJsonArray("name 3", "2016-05-17"),
PojoOfJsonArray("name 3", "2016-05-17"),
PojoOfJsonArray("name 2", "2016-06-05"),
PojoOfJsonArray("name 3", "2016-05-17")
)
val groupedMapMap: Map<String, List<PojoOfJsonArray>> = myOptions.groupBy {
it.date
}
val consolidatedList = mutableListOf<ListItem>()
for (date:String in groupedMapMap.keys){
consolidatedList.add(DateItem(date))
val groupItems: List<PojoOfJsonArray>? = groupedMapMap[date]
groupItems?.forEach {
consolidatedList.add(GeneralItem(it.name))
}
}
val adapter = Adapter(consolidatedList)
val layoutManager = LinearLayoutManager(this)
layoutManager.orientation = LinearLayoutManager.VERTICAL
binding.list.layoutManager = layoutManager
binding.list.adapter = adapter
}
}
Run Code Online (Sandbox Code Playgroud)
活动主文件
<?xml version="1.0" encoding="utf-8"?>
<layout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools">
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/list"
android:layout_width="0dp"
android:layout_height="0dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent"
tools:listitem="@layout/general_item"/>
</androidx.constraintlayout.widget.ConstraintLayout>
</layout>
Run Code Online (Sandbox Code Playgroud)
列表项.kt
open class ListItem(
val type: Int
) {
companion object {
const val TYPE_DATE = 0
const val TYPE_GENERAL = 1
}
}
Run Code Online (Sandbox Code Playgroud)
通用项目.kt
class GeneralItem(
var name: String,
) : ListItem(TYPE_GENERAL)
Run Code Online (Sandbox Code Playgroud)
日期项.kt
class DateItem(
val date: String
) : ListItem(TYPE_DATE)
Run Code Online (Sandbox Code Playgroud)
适配器.kt
class Adapter(
private val items: List<ListItem>,
) : RecyclerView.Adapter<RecyclerView.ViewHolder>() {
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): RecyclerView.ViewHolder {
val layoutInflater = LayoutInflater.from(parent.context)
return when (viewType) {
ListItem.TYPE_DATE ->
DateViewHolder(DateItemBinding.inflate(layoutInflater))
else ->
GeneralViewHolder(GeneralItemBinding.inflate(layoutInflater))
}
}
override fun onBindViewHolder(holder: RecyclerView.ViewHolder, position: Int) {
when (holder.itemViewType) {
ListItem.TYPE_DATE -> (holder as DateViewHolder).bind(
item = items[position] as DateItem,
)
ListItem.TYPE_GENERAL -> (holder as GeneralViewHolder).bind(
item = items[position] as GeneralItem
)
}
}
override fun getItemViewType(position: Int): Int {
return items[position].type
}
override fun getItemCount(): Int {
return items.size
}
inner class DateViewHolder(val binding: DateItemBinding) : RecyclerView.ViewHolder(binding.root) {
fun bind(item: DateItem) {
binding.txtDate.text = item.date
}
}
inner class GeneralViewHolder(val binding: GeneralItemBinding) : RecyclerView.ViewHolder(binding.root) {
fun bind(item: GeneralItem) {
binding.txtTitle.text = item.name
}
}
}
Run Code Online (Sandbox Code Playgroud)
日期项目.xml
<?xml version="1.0" encoding="utf-8"?>
<layout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools">
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:id="@+id/txt_date"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="8dp"
android:textStyle="bold"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
tools:text="01/01/2021"/>
</androidx.constraintlayout.widget.ConstraintLayout>
</layout>
Run Code Online (Sandbox Code Playgroud)
通用项目.xml
<?xml version="1.0" encoding="utf-8"?>
<layout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools">
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:id="@+id/txt_title"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="8dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
tools:text="Name"/>
</androidx.constraintlayout.widget.ConstraintLayout>
</layout>
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
11041 次 |
| 最近记录: |