需要在build里添加依赖
compile 'com.android.support:recyclerview-v7:25.3.1'
布局文件activity_main
<android.support.v7.widget.RecyclerView android:layout_weight="1" android:id="@+id/recyclerView_up" android:layout_width="match_parent" android:layout_height="wrap_content"> </android.support.v7.widget.RecyclerView> <!--用来划分上下的频道--> <View android:layout_width="match_parent" android:layout_height="1dp" android:background="#999999"> </View> <android.support.v7.widget.RecyclerView android:layout_weight="1" android:id="@+id/recyclerView_down" android:layout_width="match_parent" android:layout_height="wrap_content"> </android.support.v7.widget.RecyclerView>
在layout下定义一个message.xml布局文件,用来显示RecyclerView里的文字,跟listview的意义差不多
<TextView android:id="@+id/textView" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_margin="5dp" android:padding="3dp" android:text="123123123" />
写一个 MyItemOnClickListener 接口用来监听点击事件,在里边传入两个参数,第一个是试图,第二是点击的下标
public interface MyItemOnClickListener { void onClick(View view,int position); }
RecyclerView的适配器类
//RecyclerView.Adapter<?>里写的是优化继承ViewHolder的帮助类 public class ViewAdapter extends RecyclerView.Adapter<ViewAdapter.MyViewHolder> { private Context mContext; /** *特别注意一下mList后面一定要默认赋值,不然运行的时候会报错,因为我们默认上面的RecyclerView是没有添加数据的 */ private List<String> mList=new ArrayList<>(); private MyItemOnClickListener myItemOnClickListener; public ViewAdapter(Context context) { this.mContext = context; } //传数组的方法 public void setMessage(List<String> list) { this.mList = list; } //写个方法把接口传过来 public void setMyItemOnClickListener(MyItemOnClickListener myItemOnClickListener) { this.myItemOnClickListener = myItemOnClickListener; } @Override public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) { //打气筒填充我们自定义的message.xml的试图 View view = View.inflate(mContext, R.layout.messgae, null); //优化 final MyViewHolder holder = new MyViewHolder(view); //监听 view.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { //调用我们自定义接口的方法 myItemOnClickListener.onClick(v, holder.getLayoutPosition()); } }); return holder; } @Override public void onBindViewHolder(MyViewHolder holder, int position) { MyViewHolder viewHolder = holder; viewHolder.textView.setText(mList.get(position)); } @Override public int getItemCount() { return mList.size(); } //ViewHolder优化 static class MyViewHolder extends RecyclerView.ViewHolder { private TextView textView; public MyViewHolder(View itemView) { super(itemView); textView = (TextView) itemView.findViewById(R.id.textView); } } }
在MainActivity的实现
public class MainActivity extends AppCompatActivity { private RecyclerView recyclerView_up; private RecyclerView recyclerView_down; private List<String> listUp = new ArrayList<>(); private List<String> listDown = new ArrayList<>(); private ViewAdapter adapterUp; private ViewAdapter adapterDown; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); //添加数据 initData(); initView(); } //添加数据 for (int i = 0; i < 15; i++) { //默认的我们只给下面的RecyclerView添加了数据 listDown.add("频道管理" + i); } } private void initView() { recyclerView_up = (RecyclerView) findViewById(R.id.recyclerView_up); recyclerView_down = (RecyclerView) findViewById(R.id.recyclerView_down); initUp(); initDown(); } private void initUp() { //创建适配器 adapterUp = new ViewAdapter(MainActivity.this); //添加数据,我们默认创建的时候是空的 adapterUp.setMessage(listUp); recyclerView_up.setAdapter(adapterUp); //RecyclerView显示样式 GridLayoutManager gridLayoutManagerUp = new GridLayoutManager(this, 3); recyclerView_up.setLayoutManager(gridLayoutManagerUp); //点击监听 adapterUp.setMyItemOnClickListener(new MyItemOnClickListener() { @Override public void onClick(View view, int position) { Log.e("Up", "上" + position); //下面的RecyclerView添加当前点击的条目信息并刷新数组 listDown.add(listUp.get(position)); adapterDown.setMessage(listDown); adapterDown.notifyDataSetChanged(); //从当前数组移除数据并刷新数组 listUp.remove(position); adapterUp.setMessage(listUp); adapterUp.notifyDataSetChanged(); } }); } private void initDown() { adapterDown = new ViewAdapter(MainActivity.this); adapterDown.setMessage(listDown); recyclerView_down.setAdapter(adapterDown); GridLayoutManager gridLayoutManagerDown = new GridLayoutManager(this, 3); recyclerView_down.setLayoutManager(gridLayoutManagerDown); adapterDown.setMyItemOnClickListener(new MyItemOnClickListener() { @Override public void onClick(View view, int position) { Log.e("Down", "下" + position); //下面的RecyclerView添加当前点击的条目信息并刷新数组 listUp.add(listDown.get(position)); adapterUp.setMessage(listUp); adapterUp.notifyDataSetChanged(); //从当前数组移除数据并刷新数组 listDown.remove(position); adapterDown.setMessage(listDown); adapterDown.notifyDataSetChanged(); } }); } }
以上所述是小编给大家介绍的Android RecyclerView仿新闻头条的频道管理功能,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对呐喊教程网站的支持!
声明:本文内容来源于网络,版权归原作者所有,内容由互联网用户自发贡献自行上传,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任。如果您发现有涉嫌版权的内容,欢迎发送邮件至:notice#nhooo.com(发邮件时,请将#更换为@)进行举报,并提供相关证据,一经查实,本站将立刻删除涉嫌侵权内容。