linearRecyclerView
-
添加包
其中依赖中加入implementation 'androidx.recyclerview:recyclerview:1.2.0-alpha01'
-
LinearRecyclerViewActivity 该activity中测试linearRecycler
<?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=".recyclerView.LinearRecyclerViewActivity"> <androidx.recyclerview.widget.RecyclerView android:layout_width="match_parent" android:layout_height="wrap_content" android:id="@+id/rv_main" android:background="#808080" /> <!-- 这里的背景颜色后面分隔线会用到--> </RelativeLayout>
package com.example.androidstudy.recyclerView; import androidx.annotation.NonNull; import androidx.appcompat.app.AppCompatActivity; import androidx.recyclerview.widget.LinearLayoutManager; import androidx.recyclerview.widget.RecyclerView; import android.graphics.Rect; import android.os.Bundle; import android.view.View; import android.widget.Toast; import com.example.androidstudy.MainActivity; import com.example.androidstudy.R; public class LinearRecyclerViewActivity extends AppCompatActivity { private RecyclerView mRvMain; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_linear_recycler_view); mRvMain = findViewById(R.id.rv_main); mRvMain.setLayoutManager(new LinearLayoutManager(LinearRecyclerViewActivity.this)); //设置一个线性布局管理器,有好多种布局管理器 mRvMain.addItemDecoration(new MyDecoration()); //这个方法可以实现分割线,但不是专门用来实现分割线的。 //设置适配器,自定义的,详见LinearAdapter.java文件 //这里传入连个参数,一个是context,一个是监听器(这个监听器也是在adapter里自己写的监听器) mRvMain.setAdapter(new LinearAdapter(LinearRecyclerViewActivity.this, new LinearAdapter.OnItemClickListener() { @Override //这里通过实现自己在LinearAdapter里写的方法监听点击事件 public void onClick(int pos) { Toast.makeText(LinearRecyclerViewActivity.this,"click "+pos,Toast.LENGTH_SHORT).show(); } })); } class MyDecoration extends RecyclerView.ItemDecoration{ //设置间隔 @Override public void getItemOffsets(@NonNull Rect outRect, @NonNull View view, @NonNull RecyclerView parent, @NonNull RecyclerView.State state) { super.getItemOffsets(outRect, view, parent, state); outRect.set(0,0,0,getResources().getDimensionPixelOffset(R.dimen.dividerHeight)); //dividerHeight是dimen.xml文件里设置的间隔线高度 //在元素的下方添加了线,实现分隔线,这里的原理是把每个布局中间留一个间隙,把背景露出来,分隔线的颜色其实是背景的颜色 //可以在原来的布局中设置marginTop,更简单,也能达到同样的效果 } } }
-
需要的自定义适配器
package com.example.androidstudy.recyclerView; import android.content.Context; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.TextView; import androidx.annotation.NonNull; import androidx.recyclerview.widget.RecyclerView; import com.example.androidstudy.R; public class LinearAdapter extends RecyclerView.Adapter<LinearAdapter.LinearViewHolder> { private Context mContext; private OnItemClickListener mListener; public LinearAdapter(Context context, OnItemClickListener listener){ this.mContext = context; this.mListener = listener; } @NonNull @Override //创建ViewHolder public LinearAdapter.LinearViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) { return new LinearViewHolder(LayoutInflater.from(mContext).inflate(R.layout.layout_linear_item,parent,false)); //LayoutInflater布局填充器 } @Override //绑定ViewHolder,设置内容 public void onBindViewHolder(@NonNull LinearAdapter.LinearViewHolder holder, final int position) { holder.textView.setText("Hello World!"); holder.itemView.setOnClickListener(new View.OnClickListener() { //监听点击事件 @Override public void onClick(View v) { //实现接口的方法 mListener.onClick(position); } }); } @Override //获取列表数目 public int getItemCount() { return 30; } class LinearViewHolder extends RecyclerView.ViewHolder{ private TextView textView; public LinearViewHolder(@NonNull View itemView) { super(itemView); textView = itemView.findViewById(R.id.tv_title); } } //创建点击事件监听接口 public interface OnItemClickListener{ void onClick(int pos); } }
-
适配器中需要的item的结构
<?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"> <TextView android:id="@+id/tv_title" android:layout_width="match_parent" android:layout_height="50dp" android:gravity="center" android:background="#EAD308" android:textColor="#000" android:textSize="20sp" /> <!-- 这里颜色和背景的颜色不一样,才能看出后面的分隔线效果--> </RelativeLayout>
-
设置间隔的长度value
<?xml version="1.0" encoding="utf-8"?>
<resources>
<dimen name="dividerHeight">1sp</dimen>
</resources>
水平滚动 (LinearLayoutManager.HORIZONTAL)
- HorLinearRecyclerViewActivity 该activity中测试linearRecycler
package com.example.androidstudy.recyclerView;
import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import android.graphics.Rect;
import android.os.Bundle;
import android.view.View;
import android.widget.Toast;
import com.example.androidstudy.R;
public class HorLinearRecyclerViewActivity extends AppCompatActivity {
private RecyclerView mRvHor;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_hor_linear_recycler_view);
mRvHor = findViewById(R.id.rv_hor);
LinearLayoutManager linearLayoutManager = new LinearLayoutManager(HorLinearRecyclerViewActivity.this);
linearLayoutManager.setOrientation(LinearLayoutManager.HORIZONTAL);
mRvHor.setLayoutManager(linearLayoutManager);
mRvHor.addItemDecoration(new MyDecoration());
mRvHor.setAdapter(new LinearAdapter(HorLinearRecyclerViewActivity.this, new LinearAdapter.OnItemClickListener() {
@Override
public void onClick(int pos) {
Toast.makeText(HorLinearRecyclerViewActivity.this,"click: "+pos,Toast.LENGTH_SHORT).show();
}
}));
}
class MyDecoration extends RecyclerView.ItemDecoration{ //设置间隔
@Override
public void getItemOffsets(@NonNull Rect outRect, @NonNull View view, @NonNull RecyclerView parent, @NonNull RecyclerView.State state) {
super.getItemOffsets(outRect, view, parent, state);
outRect.set(0,0,getResources().getDimensionPixelOffset(R.dimen.dividerHeight),0);
//在元素的下方添加了线,实现分隔线,这里的原理是把每个布局中间留一个间隙,把背景露出来,分隔线的颜色其实是背景的颜色
//可以在原来的布局中设置marginTop,更简单,也能达到同样的效果
//左上右下
}
}
}
<?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=".recyclerView.HorLinearRecyclerViewActivity">
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/rv_hor"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#F80B0B"/>
</RelativeLayout>
- 适配器与上面一样
网格布局
- GridLinearRecyclerViewActivity该activity中测试网格布局
package com.example.androidstudy.recyclerView;
import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import android.os.Bundle;
import android.widget.Toast;
import com.example.androidstudy.R;
public class GridLinearRecyclerViewActivity extends AppCompatActivity {
private RecyclerView mRvGrid;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_grid_linear_recycler_view);
mRvGrid = findViewById(R.id.rv_grid);
//设置网格布局(上下文,每行展示的个数)
mRvGrid.setLayoutManager(new GridLayoutManager(GridLinearRecyclerViewActivity.this,3));
mRvGrid.setAdapter(new GridLinearAdapter(GridLinearRecyclerViewActivity.this, new GridLinearAdapter.OnItemClickListener() {
@Override
public void onClick(int pos) {
Toast.makeText(GridLinearRecyclerViewActivity.this,"click:"+pos, Toast.LENGTH_SHORT).show();
}
}));
}
}
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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=".recyclerView.GridLinearRecyclerViewActivity">
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/rv_grid"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#EC0909"/>
</LinearLayout>
- Adapt为
package com.example.androidstudy.recyclerView;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;
import com.example.androidstudy.R;
public class GridLinearAdapter extends RecyclerView.Adapter<GridLinearAdapter.LinearViewHolder> {
private Context mContext;
private OnItemClickListener mListener;
public GridLinearAdapter(Context context, OnItemClickListener listener){
this.mContext = context;
this.mListener = listener;
}
@NonNull
@Override //创建ViewHolder
public GridLinearAdapter.LinearViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
return new LinearViewHolder(LayoutInflater.from(mContext).inflate(R.layout.layout_grid_recycler_item,parent,false)); //LayoutInflater布局填充器
}
@Override //绑定ViewHolder,设置内容
public void onBindViewHolder(@NonNull GridLinearAdapter.LinearViewHolder holder, final int position) {
holder.textView.setText("Hello World!");
holder.itemView.setOnClickListener(new View.OnClickListener() { //监听点击事件
@Override
public void onClick(View v) {
//实现接口的方法
mListener.onClick(position);
}
});
}
@Override //获取列表数目
public int getItemCount() {
return 30;
}
class LinearViewHolder extends RecyclerView.ViewHolder{
private TextView textView;
public LinearViewHolder(@NonNull View itemView) {
super(itemView);
textView = itemView.findViewById(R.id.tv_title);
}
}
//创建点击事件监听接口
public interface OnItemClickListener{
void onClick(int pos);
}
}
- 定制的网格item布局
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
android:padding="10dp"
android:background="#03A9F4">
<TextView
android:id="@+id/tv_title"
android:layout_width="60dp"
android:layout_height="50dp"
android:gravity="center"
android:background="#EAD308"
android:textColor="#000"
android:textSize="20sp" />
ed
</LinearLayout >
瀑布布局(Staggere dGrid)
- StaggeredGridLinearRecyclerViewActivity该activity中测试瀑布布局
package com.example.androidstudy.recyclerView;
import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.RecyclerView;
import androidx.recyclerview.widget.StaggeredGridLayoutManager;
import android.graphics.Rect;
import android.os.Bundle;
import android.view.View;
import android.widget.Toast;
import com.example.androidstudy.R;
public class StaggeredGridLinearRecyclerViewActivity extends AppCompatActivity {
private RecyclerView mRvPu;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_staggered_grid_linear_recycler_view);
mRvPu = findViewById(R.id.rv_pu);
//不规则的网格布局(列数或行数,竖直排列或水平排列)
mRvPu.setLayoutManager(new StaggeredGridLayoutManager(2,StaggeredGridLayoutManager.VERTICAL));
mRvPu.addItemDecoration(new MyDecoration());
mRvPu.setAdapter(new StaggeredGridAdapter(StaggeredGridLinearRecyclerViewActivity.this, new StaggeredGridAdapter.OnItemClickListener() {
@Override
public void onClick(int pos) {
Toast.makeText(StaggeredGridLinearRecyclerViewActivity.this,"click:"+pos,Toast.LENGTH_SHORT).show();
}
}));
}
class MyDecoration extends RecyclerView.ItemDecoration{
@Override
public void getItemOffsets(@NonNull Rect outRect, @NonNull View view, @NonNull RecyclerView parent, @NonNull RecyclerView.State state) {
super.getItemOffsets(outRect, view, parent, state);
int gap = getResources().getDimensionPixelOffset(R.dimen.dividerHeight);
outRect.set(gap,gap,gap,gap);
}
}
}
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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">
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/rv_pu"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#F44336" />
</LinearLayout>
- 自定义适配器
package com.example.androidstudy.recyclerView;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;
import com.bumptech.glide.Glide;
import com.example.androidstudy.R;
public class StaggeredGridAdapter extends RecyclerView.Adapter<StaggeredGridAdapter.LinearViewHolder> {
private Context mContext;
private OnItemClickListener mListener;
public StaggeredGridAdapter(Context context, OnItemClickListener listener){
this.mContext = context;
this.mListener = listener;
}
@NonNull
@Override //创建ViewHolder
public StaggeredGridAdapter.LinearViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
return new LinearViewHolder(LayoutInflater.from(mContext).inflate(R.layout.stagger_grid_item,parent,false)); //LayoutInflater布局填充器
}
@Override //绑定ViewHolder,设置内容
public void onBindViewHolder(@NonNull StaggeredGridAdapter.LinearViewHolder holder, final int position) {
if (position%2 != 0){
holder.imageView.setImageResource(R.drawable.d);
}else{
Glide.with(mContext).load("https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1582820076289&di=1d260f4974919a0730edaedfcfed2f90&imgtype=0&src=http%3A%2F%2Finews.gtimg.com%2Fnewsapp_ls%2F0%2F11234825969_240180%2F0.jpg").into(holder.imageView);
}
holder.itemView.setOnClickListener(new View.OnClickListener() { //监听点击事件
@Override
public void onClick(View v) {
//实现接口的方法
mListener.onClick(position);
}
});
}
@Override //获取列表数目
public int getItemCount() {
return 30;
}
class LinearViewHolder extends RecyclerView.ViewHolder{
private ImageView imageView;
public LinearViewHolder(@NonNull View itemView) {
super(itemView);
imageView = itemView.findViewById(R.id.iv);
}
}
//创建点击事件监听接口
public interface OnItemClickListener{
void onClick(int pos);
}
}
- 自定义瀑布流没item布局
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
android:background="#03A9F4">
<ImageView
android:id="@+id/iv"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:scaleType="centerCrop"/>
</LinearLayout>
加载网页 WebView
开启网络权限
<uses-permission android:name="android.permission.INTERNET" />
package com.example.androidstudy;
import androidx.appcompat.app.AppCompatActivity;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.util.Log;
import android.view.KeyEvent;
import android.webkit.WebChromeClient;
import android.webkit.WebResourceRequest;
import android.webkit.WebView;
import android.webkit.WebViewClient;
public class WebViewActivity extends AppCompatActivity {
private WebView mWvMain;
@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if (keyCode == event.KEYCODE_BACK && mWvMain.canGoBack()) {
mWvMain.goBack();
return true;
}
return super.onKeyDown(keyCode, event);
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_web_view);
mWvMain = findViewById(R.id.mv);
//mWvMain.loadUrl("file:///android_asset/test.html");//加载本地的html,需要新建文件夹assets中
mWvMain.getSettings().setJavaScriptEnabled(true);//有的页面需要JS支持,此时需要加上这句代码
mWvMain.setWebViewClient(new MyWebViewClient());
mWvMain.setWebChromeClient(new MyWebChromeClient());
mWvMain.loadUrl("https://m.baidu.com");//m表示移动
}
class MyWebViewClient extends WebViewClient {
@Override
public boolean shouldOverrideUrlLoading(WebView view, WebResourceRequest request) {
return super.shouldOverrideUrlLoading(view, request);
}
@Override
public void onPageStarted(WebView view, String url, Bitmap favicon) {
super.onPageStarted(view, url, favicon);
Log.d("WebView","正在加载中,请等待。。。。");
}
@Override
public void onPageFinished(WebView view, String url) {
super.onPageFinished(view, url);
Log.d("WebView","加载完成!");
mWvMain.loadUrl("javascript:alert('complete')");//可以支持Javascript代码
}
}
class MyWebChromeClient extends WebChromeClient{
@Override
public void onProgressChanged(WebView view, int newProgress) {
super.onProgressChanged(view, newProgress);
}
@Override
public void onReceivedTitle(WebView view, String title) {
super.onReceivedTitle(view, title);
setTitle(title);
}
}
}
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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=".WebViewActivity">
<WebView
android:id="@+id/mv"
android:layout_width="match_parent"
android:layout_height="match_parent">
</WebView>
</LinearLayout>