1、实现ListView
package com.xingin.recyclerviewtest;
import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
import java.util.ArrayList;
import java.util.List;
public class MainActivity extends AppCompatActivity {
public RecyclerView recycler;
private List<ItemBean> list;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
recycler = findViewById(R.id.recycler);
initData();
initView(list, true, false);
}
private class ItemBean {
int icon;
String title;
}
private class ListViewAdapt extends RecyclerView.Adapter<ListViewAdapt.InnerHolder> {
private final List<ItemBean> mData;
public ListViewAdapt(List<ItemBean> mData) {
this.mData = mData;
}
@Override
public InnerHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View inflate = View.inflate(getApplicationContext(), R.layout.item_list_view, null);
return new InnerHolder(inflate);
}
@Override
public void onBindViewHolder(MainActivity.ListViewAdapt.InnerHolder holder, int position) {
holder.setData(mData.get(position));
}
@Override
public int getItemCount() {
if (mData != null) {
return mData.size();
}
return 0;
}
public class InnerHolder extends RecyclerView.ViewHolder {
private final ImageView viewById;
private final TextView viewById1;
public InnerHolder(View itemView) {
super(itemView);
viewById = (ImageView) itemView.findViewById(R.id.image_id);
viewById1 = (TextView) itemView.findViewById(R.id.test_id);
}
public void setData(ItemBean itemBean) {
viewById.setImageResource(itemBean.icon);
viewById1.setText(itemBean.title);
}
}
}
private void initData() {
list = new ArrayList<>();
for (int i = 0; i < 10; i++) {
ItemBean itemBean = new ItemBean();
itemBean.icon = R.mipmap.ic_launcher;
itemBean.title = "我是第" + i + "条";
list.add(itemBean);
}
}
private void initView(List<ItemBean> list, boolean vertical, boolean reverse) {
LinearLayoutManager layoutManager = new LinearLayoutManager(this);
layoutManager.setOrientation(vertical ? LinearLayoutManager.VERTICAL : LinearLayoutManager.HORIZONTAL);
layoutManager.setReverseLayout(reverse);
recycler.setLayoutManager(layoutManager);
ListViewAdapt listViewAdapt = new ListViewAdapt(list);
recycler.setAdapter(listViewAdapt);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.menu, menu);
return super.onCreateOptionsMenu(menu);
}
@Override
public boolean onOptionsItemSelected(@NonNull MenuItem item) {
int itemId = item.getItemId();
switch (itemId) {
case R.id.listview_vertical_stander:
initView(list, true, false);
break;
case R.id.listview_vertical_reverse:
initView(list, true, true);
break;
case R.id.listview_horizontal_reverse_stander:
initView(list, false, true);
break;
case R.id.listview_horizontal_stander:
initView(list, false, false);
break;
case R.id.gridview_vertical_stander:
break;
case R.id.gridview_vertical_reverse:
break;
case R.id.gridview_horizontal_reverse_stander:
break;
case R.id.staggervertical_stander:
break;
case R.id.staggervertical_reverse:
break;
case R.id.stagger_horizontal_reverse_stander:
break;
default:
break;
}
return super.onOptionsItemSelected(item);
}
}
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/recycler"
android:layout_width="match_parent"
android:layout_height="match_parent"></androidx.recyclerview.widget.RecyclerView>
</RelativeLayout>
<?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="match_parent">
<ImageView
android:id="@+id/image_id"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:scaleType="fitXY"
android:src="@mipmap/ic_launcher_round"></ImageView>
<TextView
android:id="@+id/test_id"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_toRightOf="@+id/image_id"
android:text="我是标题"
android:textSize="30sp"></TextView>
</RelativeLayout>