2.
3.
(1)横向布局:(LinearLayoutManager)
//item_view (新建):(运行虚拟机后,发现布局不对时,需要调整下面布局的宽度和高度)
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="60dp"
android:layout_height="60dp">
<TextView
android:id="@+id/text_view"
android:layout_width="match_parent"
android:layout_height="match_parent">
</TextView>
</LinearLayout>
//主活动布局:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:android="http://schemas.android.com/apk/res/android" >
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/recycle_view"
android:layout_width="match_parent"
android:layout_height="match_parent">
</androidx.recyclerview.widget.RecyclerView>
</LinearLayout>
package com.example.test_1;
import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentActivity;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentTransaction;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import androidx.swiperefreshlayout.widget.SwipeRefreshLayout;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.os.Handler;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;
import java.util.ArrayList;
import java.util.List;
public class MainActivity extends FragmentActivity {
RecyclerView recyclerView;
String[] dataArr = {"第一项", "第二项", "第三项", "第四项", "第五项", "第六项"};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
recyclerView = (RecyclerView) findViewById(R.id.recycle_view);
LinearLayoutManager layoutManager = new LinearLayoutManager(this, LinearLayoutManager.HORIZONTAL, true);
recyclerView.setLayoutManager(layoutManager);
/**recyclerView.setLayoutManager(new GridLayoutManager(this, 3));--3代表的是列数**/
/**GridLayoutManager--设置Grid布局**/
/**LinearLayoutManager--设置List布局**/
MyAdapter adapter = new MyAdapter();
recyclerView.setAdapter(adapter);
}
private class MyAdapter extends RecyclerView.Adapter {
@NonNull
@Override
public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
View itemView = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_view, parent, false);
MyViewHolder viewHolder = new MyViewHolder(itemView);
return viewHolder;
}
@Override
public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, int position) {
((MyViewHolder) holder).textView.setText("编号:" + position);
}
@Override
public int getItemCount() {
return dataArr.length;
}
private class MyViewHolder extends RecyclerView.ViewHolder {
private TextView textView;
public MyViewHolder(View view) {
super(view);
textView = (TextView) view.findViewById(R.id.text_view);
}
}
}
}
图-1:(List)
图-2:(List)
*注意:将Java代码对应位置的代码改为下面的代码,就可以得到图2的效果。
LinearLayoutManager layoutManager = new LinearLayoutManager(this,LinearLayoutManager.VERTICAL, true)*
图-3:(Grid)