1.RecyclerView的使用方法(使用的Android Studio开发)
首先是recyclerview的配置问题,recyclerview是要导入android.support.v7.widget.RecyclerView的包。还要在Gradle Scripts中添加 compile ‘com.android.support:recyclerview-v7:23.4.0’,然后同步。
2.使用RecyclerView实现的主要是内容是Item的添加和删除以及ListView,GridView和瀑布流的切换,还有就是Item的长按和短按的点击事件。
ListView和GridView的切换还是很好用,代码也很简单。主要是实现了以下代码。
/**
*这是ListView的效果
*/
recyclerView.setLayoutManager(new LinearLayoutManager(this));
/**
*这是GridView的效果
*/
recyclerView.setLayoutManager(new GridLayoutManager(this, 2));
看到了吧,只需要改一行代码就能实现GridView和ListView的切换!!
还有就是瀑布流的这个功能,相比于GridView和ListView还是有点复杂的。主要是通过以下代码实现:
首先获取Item的随机高度:
/**
* 得到随机的Item的高度
*/
private void getRandomHeight(List list) {
height = new ArrayList<>();
for (int i = 0; i < list.size(); ++i) {
height.add((int) (200 + Math.random() * 400));
}
}
然后在重写的onBindViewHolder()方法下面获取item的LayoutParams的布局参数,把随机的高度赋予item。
@Override
public void onBindViewHolder(final MyViewHolder holder, final int position) {
/**
* 得到item的LayoutParams布局参数
*/
ViewGroup.LayoutParams params = holder.itemView.getLayoutParams();
params.height = height.get(position);//把随机的高度赋予item布局
holder.itemView.setLayoutParams(params);//把params设置item布局
还有就是使用StaggeredGridLayoutManager():
/**
*2,表示两列;VERTICAL表示垂直方向
*/
recyclerView.setLayoutManager(new StaggeredGridLayoutManager(2, StaggeredGridLayoutManager.VERTICAL));
这样就实现了瀑布流的简单使用。
然后就是Item的增加和删除,主要是在适配器中自定义增加和删除方法。
/**
*增加Item
*/
public void addItem(int position) {
list.add(position,"A");
notifyItemInserted(position);
}
/**
*删除Item
*/
public void removeItem(int position) {
list.remove(position);
notifyItemRemoved(position);
}
然后通过自己设置的Button对Item进行相应的操作。
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.addItem:
homeAdapter.addItem(1);
break;
case R.id.removeItem:
homeAdapter.removeItem(1);
break;
最后就是RecyclerView的长按和短按的监听事件,主要是用的回调,首先定义一个接口。
public interface ItemClickListener {
/**
* Item的普通点击
*/
public void onItemClick(View view, int position);
/**
* Item长按
*/
public void onItemLongClick(View view, int position);
然后为TextView添加监听回调
//为TextView添加监听回调
holder.textView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (itemClickListener != null) {
itemClickListener.onItemSubViewClick(holder.textView, position);
}
}
});
长按和短按
//为item添加普通点击回调
itemView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (itemClickListener != null) {
itemClickListener.onItemClick(itemView, getPosition());
}
}
});
//为item添加长按回调
itemView.setOnLongClickListener(new View.OnLongClickListener() {
@Override
public boolean onLongClick(View v) {
if (itemClickListener != null) {
itemClickListener.onItemLongClick(itemView, getPosition());
}
return true;
}
});
homeAdapter.setItemClickListener(new ItemClickListener() {
@Override
public void onItemClick(View view, int position) {
Toast.makeText(MainActivity.this, "点击了Item" + position, Toast.LENGTH_SHORT).show();
}
@Override
public void onItemLongClick(View view, int position) {
Toast.makeText(MainActivity.this, "长按了Item" + position, Toast.LENGTH_SHORT).show();
}
这就是RecyclerView的长按和短按的实现代码。
这样RecyclerView的一些简单的操作就实现了,以上就是RecyclerView的详细解释。
下面贴出实现这些功能的全部代码:
这是activity_main 的XML文件
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<Button
android:id="@+id/addItem"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="增加Item" />
<Button
android:id="@+id/removeItem"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="删除Item" />
LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<Button
android:id="@+id/change_listView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="切换ListView" />
<Button
android:id="@+id/change_gridView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="切换GridView" />
<Button
android:id="@+id/change_waterfall"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="切换瀑布流" />
LinearLayout>
<android.support.v7.widget.RecyclerView
android:id="@+id/recyclerView_demo"
android:layout_width="match_parent"
android:layout_height="match_parent">
android.support.v7.widget.RecyclerView>
LinearLayout>
这是item的XML文件
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#44ff0000"
android:orientation="vertical">
<TextView
android:id="@+id/id_num"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:text="1" />
LinearLayout>
这是RecyclerView的适配器
import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import com.example.recycleview.Interfaces.ItemClickListener;
import com.example.recycleview.R;
import java.util.ArrayList;
import java.util.List;
/**
* Created by w_x on 2016/6/17.
* 设置RecyclerView的适配器
*/
public class HomeAdapter extends RecyclerView.Adapter<HomeAdapter.MyViewHolder> {
private List list;
private Context context;
private List height;
private ItemClickListener itemClickListener;
public HomeAdapter(Context context, List list) {
this.context = context;
this.list = list;
getRandomHeight(this.list);
}
public void setItemClickListener(ItemClickListener itemClickListener) {
this.itemClickListener = itemClickListener;
}
/**
* 得到随机的Item的高度
*/
private void getRandomHeight(List list) {
height = new ArrayList<>();
for (int i = 0; i < list.size(); ++i) {
height.add((int) (200 + Math.random() * 400));
}
}
@Override
public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
MyViewHolder holder = new MyViewHolder(LayoutInflater.from(context).inflate(R.layout.recyclerview_item, parent, false));
return holder;
}
@Override
public void onBindViewHolder(final MyViewHolder holder, final int position) {
/**
* 得到item的LayoutParams布局参数
*/
ViewGroup.LayoutParams params = holder.itemView.getLayoutParams();
params.height = height.get(position);//把随机的高度赋予item布局
holder.itemView.setLayoutParams(params);//把params设置item布局
holder.textView.setText(list.get(position));//为控件绑定数据
//为TextView添加监听回调
holder.textView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (itemClickListener != null) {
itemClickListener.onItemSubViewClick(holder.textView, position);
}
}
});
}
@Override
public int getItemCount() {
return list.size();
}
class MyViewHolder extends RecyclerView.ViewHolder {
TextView textView;
public MyViewHolder(final View itemView) {
super(itemView);
textView = (TextView) itemView.findViewById(R.id.id_num);
//为item添加普通点击回调
itemView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (itemClickListener != null) {
itemClickListener.onItemClick(itemView, getPosition());
}
}
});
//为item添加长按回调
itemView.setOnLongClickListener(new View.OnLongClickListener() {
@Override
public boolean onLongClick(View v) {
if (itemClickListener != null) {
itemClickListener.onItemLongClick(itemView, getPosition());
}
return true;
}
});
}
}
public void addItem(int position) {
list.add(position,"A");
notifyItemInserted(position);
}
public void removeItem(int position) {
list.remove(position);
notifyItemRemoved(position);
}
}
这是MainActivity
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.DefaultItemAnimator;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.os.Bundle;
import android.support.v7.widget.StaggeredGridLayoutManager;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;
import com.example.recycleview.Adapters.HomeAdapter;
import com.example.recycleview.Interfaces.ItemClickListener;
import com.example.recycleview.R;
import java.util.ArrayList;
import java.util.List;
public class MainActivity extends AppCompatActivity implements View.OnClickListener {
private RecyclerView recyclerView;
private List list;
private HomeAdapter homeAdapter;
private Button addItem;
private Button removeItem;
private Button change_listView;
private Button change_gridView;
private Button change_waterfall;
private boolean isFirstView = true;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
findViewById();
initData();
recyclerView = (RecyclerView) findViewById(R.id.recyclerView_demo);
recyclerView.setItemAnimator(new DefaultItemAnimator());
homeAdapter = new HomeAdapter(this, list);
recyclerView.setAdapter(homeAdapter);
homeAdapter.setItemClickListener(new ItemClickListener() {
@Override
public void onItemClick(View view, int position) {
Toast.makeText(MainActivity.this, "点击了Item" + position, Toast.LENGTH_SHORT).show();
}
@Override
public void onItemLongClick(View view, int position) {
Toast.makeText(MainActivity.this, "长按了Item" + position, Toast.LENGTH_SHORT).show();
}
@Override
public void onItemSubViewClick(View view, int position) {
}
});
}
public void findViewById() {
addItem = (Button) findViewById(R.id.addItem);
removeItem = (Button) findViewById(R.id.removeItem);
change_listView = (Button) findViewById(R.id.change_listView);
change_gridView = (Button) findViewById(R.id.change_gridView);
change_waterfall = (Button) findViewById(R.id.change_waterfall);
addItem.setOnClickListener(this);
removeItem.setOnClickListener(this);
change_listView.setOnClickListener(this);
change_gridView.setOnClickListener(this);
change_waterfall.setOnClickListener(this);
}
private void initData() {
list = new ArrayList<>();
for (int i = 'A'; i <= 'Z'; ++i) {
list.add("" + (char) i);
}
}
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.addItem:
homeAdapter.addItem(1);
break;
case R.id.removeItem:
homeAdapter.removeItem(1);
break;
case R.id.change_listView:
/**
* ListView的效果
*/
recyclerView.setLayoutManager(new LinearLayoutManager(this));
if (isFirstView) {
isFirstView = false;
onClick(findViewById(R.id.recyclerView_demo));
}
break;
case R.id.change_gridView:
/**
* GridView的效果
*/
recyclerView.setLayoutManager(new GridLayoutManager(this, 2));
break;
case R.id.change_waterfall:
/**
* 瀑布流的效果
*/
recyclerView.setLayoutManager(new StaggeredGridLayoutManager(2, StaggeredGridLayoutManager.VERTICAL));
break;
default:
break;
}
}
}