标签:
要实现ListView列表的循环滚动效果,其实可以用RecyclerView实现。
Runnable run = new Runnable() { @Override public void run() { // String str = "abc"; // datas.add(str); // mRecyclerAdapter.notifyItemInserted(1); datas.remove(curPos); mRecyclerAdapter.notifyItemRemoved(curPos); curPos --; handler.sendEmptyMessage(0); } };
布局文件中使用 RecyclerView,横向和纵向两个:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" tools:context=".MyActivity"> <TextView android:layout_width="match_parent" android:layout_height="wrap_content" android:padding="10dp" android:text="Horizontal" android:textSize="18sp" /> <android.support.v7.widget.RecyclerView android:id="@+id/recyclerview_horizontal" android:layout_width="match_parent" android:layout_height="50dp" android:scrollbars="horizontal" /> <TextView android:layout_width="match_parent" android:layout_height="wrap_content" android:padding="10dp" android:text="Vertical" android:textSize="18sp" /> <android.support.v7.widget.RecyclerView android:id="@+id/recyclerview_vertical" android:layout_width="match_parent" android:layout_height="0dp" android:layout_weight="1" android:scrollbars="vertical" /> </LinearLayout>
package com.baoyz.recyclerviewdemo; import android.app.Activity; import android.os.Bundle; import android.support.v7.widget.LinearLayoutManager; import android.support.v7.widget.RecyclerView; import android.view.Menu; import android.view.MenuItem; public class MyActivity extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_my); initHorizaontal(); initVertical(); } private void initHorizaontal() { RecyclerView recyclerView = (RecyclerView) findViewById(R.id.recyclerview_horizontal); // 创建一个线性布局管理器 LinearLayoutManager layoutManager = new LinearLayoutManager(this); layoutManager.setOrientation(LinearLayoutManager.HORIZONTAL); // 设置布局管理器 recyclerView.setLayoutManager(layoutManager); // 创建数据集 String[] dataset = new String[100]; for (int i = 0; i < dataset.length; i++){ dataset[i] = "item" + i; } // 创建Adapter,并指定数据集 MyAdapter adapter = new MyAdapter(dataset); // 设置Adapter recyclerView.setAdapter(adapter); } public void initVertical(){ RecyclerView recyclerView = (RecyclerView) findViewById(R.id.recyclerview_vertical); // 创建一个线性布局管理器 LinearLayoutManager layoutManager = new LinearLayoutManager(this); // 默认是Vertical,可以不写 layoutManager.setOrientation(LinearLayoutManager.VERTICAL); // 设置布局管理器 recyclerView.setLayoutManager(layoutManager); // 创建数据集 String[] dataset = new String[100]; for (int i = 0; i < dataset.length; i++){ dataset[i] = "item" + i; } // 创建Adapter,并指定数据集 MyAdapter adapter = new MyAdapter(dataset); // 设置Adapter recyclerView.setAdapter(adapter); } }
package com.baoyz.recyclerviewdemo; import android.support.v7.widget.RecyclerView; import android.view.View; import android.view.ViewGroup; import android.widget.TextView; /** * Created by baoyz on 2014/6/29. */ public class MyAdapter extends RecyclerView.Adapter<MyAdapter.ViewHolder>{ // 数据集 private String[] mDataset; public MyAdapter(String[] dataset) { super(); mDataset = dataset; } @Override public ViewHolder onCreateViewHolder(ViewGroup viewGroup, int i) { // 创建一个View,简单起见直接使用系统提供的布局,就是一个TextView View view = View.inflate(viewGroup.getContext(), android.R.layout.simple_list_item_1, null); // 创建一个ViewHolder ViewHolder holder = new ViewHolder(view); return holder; } @Override public void onBindViewHolder(ViewHolder viewHolder, int i) { // 绑定数据到ViewHolder上 viewHolder.mTextView.setText(mDataset[i]); } @Override public int getItemCount() { return mDataset.length; } public static class ViewHolder extends RecyclerView.ViewHolder{ public TextView mTextView; public ViewHolder(View itemView) { super(itemView); mTextView = (TextView) itemView; } } }
惭愧,使用Arrays.as(String[])方法不当,调试半天不知道哪错了:
试运行如下代码,你猜会出现什么情况。
public static void main(String[] args){ String[] strs={"1","2","3","4"}; System.out.println(Arrays.asList(strs).remove(0)); }
如果没有没错,你一般会说是:1。但是实际上返回的是:
Exception in thread "main" java.lang.UnsupportedOperationException at java.util.AbstractList.remove(AbstractList.java:144) at com.lmax.disruptor.YieldingWaitStrategy.main(YieldingWaitStrategy.java:126)
为什么会出现这种情况?因为我们认为JDK会返回一个ArrayList或者LinkedList,所直接调用remove(int index)方法。实际上asList返回的时什么呢?asList返回的是Arrays类内部自定义的AbstractList的子类,它没有实现remove方法。
如何避免这种情况发生呢?
public static void main(String[] args){ String[] strs={"1","2","3","4"}; System.out.println(new LinkedList<String>(Arrays.asList(strs)).remove(0)); }
学会使用RecyclerView 及Arrays.asList函数
标签:
原文地址:http://blog.csdn.net/fancylovejava/article/details/44062859